package net.minecraft.util.context; import com.google.common.collect.Sets; import java.util.IdentityHashMap; import java.util.Map; import java.util.NoSuchElementException; import java.util.Set; import javax.annotation.Nullable; import org.jetbrains.annotations.Contract; public class ContextMap { private final Map, Object> params; ContextMap(Map, Object> p_362494_) { this.params = p_362494_; } public boolean has(ContextKey p_365248_) { return this.params.containsKey(p_365248_); } public T getOrThrow(ContextKey p_366932_) { T t = (T)this.params.get(p_366932_); if (t == null) { throw new NoSuchElementException(p_366932_.name().toString()); } else { return t; } } @Nullable public T getOptional(ContextKey p_362151_) { return (T)this.params.get(p_362151_); } @Nullable @Contract("_,!null->!null; _,_->_") public T getOrDefault(ContextKey p_368195_, @Nullable T p_368521_) { return (T)this.params.getOrDefault(p_368195_, p_368521_); } public static class Builder { private final Map, Object> params = new IdentityHashMap<>(); public ContextMap.Builder withParameter(ContextKey p_369289_, T p_362485_) { this.params.put(p_369289_, p_362485_); return this; } public ContextMap.Builder withOptionalParameter(ContextKey p_366888_, @Nullable T p_366368_) { if (p_366368_ == null) { this.params.remove(p_366888_); } else { this.params.put(p_366888_, p_366368_); } return this; } public T getParameter(ContextKey p_363188_) { T t = (T)this.params.get(p_363188_); if (t == null) { throw new NoSuchElementException(p_363188_.name().toString()); } else { return t; } } @Nullable public T getOptionalParameter(ContextKey p_362877_) { return (T)this.params.get(p_362877_); } public ContextMap create(ContextKeySet p_369803_) { Set> set = Sets.difference(this.params.keySet(), p_369803_.allowed()); if (!set.isEmpty()) { throw new IllegalArgumentException("Parameters not allowed in this parameter set: " + set); } else { Set> set1 = Sets.difference(p_369803_.required(), this.params.keySet()); if (!set1.isEmpty()) { throw new IllegalArgumentException("Missing required parameters: " + set1); } else { return new ContextMap(this.params); } } } } }