minecraft-source/src/net/minecraft/advancements/critereon/BrewedPotionTrigger.java

72 lines
2.4 KiB
Java
Raw Normal View History

2020-07-22 06:23:34 +01:00
package net.minecraft.advancements.critereon;
import com.google.gson.JsonElement;
import javax.annotation.Nullable;
import net.minecraft.advancements.CriterionTriggerInstance;
import net.minecraft.server.level.ServerPlayer;
import com.google.gson.JsonSyntaxException;
import net.minecraft.core.Registry;
import net.minecraft.world.item.alchemy.Potion;
import net.minecraft.util.GsonHelper;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;
2020-07-22 06:25:47 +01:00
public class BrewedPotionTrigger extends SimpleCriterionTrigger<TriggerInstance> {
2020-07-22 06:23:34 +01:00
private static final ResourceLocation ID;
@Override
public ResourceLocation getId() {
return BrewedPotionTrigger.ID;
}
@Override
public TriggerInstance createInstance(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
2020-07-22 06:30:03 +01:00
Potion bgd4 = null;
2020-07-22 06:23:34 +01:00
if (jsonObject.has("potion")) {
2020-07-22 06:25:47 +01:00
final ResourceLocation sm5 = new ResourceLocation(GsonHelper.getAsString(jsonObject, "potion"));
2020-07-22 06:23:34 +01:00
final Object o;
final Object obj;
2020-07-22 06:30:03 +01:00
bgd4 = Registry.POTION.getOptional(sm5).<Throwable>orElseThrow(() -> {
2020-07-22 06:23:34 +01:00
new JsonSyntaxException("Unknown potion '" + obj + "'");
return o;
});
}
2020-07-22 06:30:03 +01:00
return new TriggerInstance(bgd4);
2020-07-22 06:23:34 +01:00
}
2020-07-22 06:30:03 +01:00
public void trigger(final ServerPlayer xe, final Potion bgd) {
this.trigger(xe.getAdvancements(), a -> a.matches(bgd));
2020-07-22 06:23:34 +01:00
}
static {
ID = new ResourceLocation("brewed_potion");
}
public static class TriggerInstance extends AbstractCriterionTriggerInstance {
private final Potion potion;
2020-07-22 06:30:03 +01:00
public TriggerInstance(@Nullable final Potion bgd) {
2020-07-22 06:23:34 +01:00
super(BrewedPotionTrigger.ID);
2020-07-22 06:30:03 +01:00
this.potion = bgd;
2020-07-22 06:23:34 +01:00
}
public static TriggerInstance brewedPotion() {
return new TriggerInstance((Potion)null);
}
2020-07-22 06:30:03 +01:00
public boolean matches(final Potion bgd) {
return this.potion == null || this.potion == bgd;
2020-07-22 06:23:34 +01:00
}
@Override
public JsonElement serializeToJson() {
final JsonObject jsonObject2 = new JsonObject();
if (this.potion != null) {
jsonObject2.addProperty("potion", Registry.POTION.getKey(this.potion).toString());
}
return jsonObject2;
}
}
}