91 lines
3.3 KiB
Java
91 lines
3.3 KiB
Java
package net.minecraft.advancements.critereon;
|
|
|
|
import com.google.gson.JsonElement;
|
|
import net.minecraft.advancements.CriterionTriggerInstance;
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
import net.minecraft.server.level.ServerPlayer;
|
|
import javax.annotation.Nullable;
|
|
import net.minecraft.core.Registry;
|
|
import net.minecraft.util.GsonHelper;
|
|
import net.minecraft.world.level.block.Block;
|
|
import net.minecraft.world.level.block.state.StateDefinition;
|
|
import com.google.gson.JsonSyntaxException;
|
|
import com.google.gson.JsonDeserializationContext;
|
|
import com.google.gson.JsonObject;
|
|
import net.minecraft.resources.ResourceLocation;
|
|
|
|
public class EnterBlockTrigger extends SimpleCriterionTrigger<TriggerInstance> {
|
|
private static final ResourceLocation ID;
|
|
|
|
@Override
|
|
public ResourceLocation getId() {
|
|
return EnterBlockTrigger.ID;
|
|
}
|
|
|
|
@Override
|
|
public TriggerInstance createInstance(final JsonObject jsonObject, final JsonDeserializationContext jsonDeserializationContext) {
|
|
final Block bph4 = deserializeBlock(jsonObject);
|
|
final StatePropertiesPredicate cc5 = StatePropertiesPredicate.fromJson(jsonObject.get("state"));
|
|
if (bph4 != null) {
|
|
final JsonSyntaxException ex;
|
|
final Object obj;
|
|
cc5.checkState(bph4.getStateDefinition(), string -> {
|
|
new JsonSyntaxException("Block " + obj + " has no property " + string);
|
|
throw ex;
|
|
});
|
|
}
|
|
return new TriggerInstance(bph4, cc5);
|
|
}
|
|
|
|
@Nullable
|
|
private static Block deserializeBlock(final JsonObject jsonObject) {
|
|
if (jsonObject.has("block")) {
|
|
final ResourceLocation sm2 = new ResourceLocation(GsonHelper.getAsString(jsonObject, "block"));
|
|
final Object o;
|
|
final Object obj;
|
|
return Registry.BLOCK.getOptional(sm2).<Throwable>orElseThrow(() -> {
|
|
new JsonSyntaxException("Unknown block type '" + obj + "'");
|
|
return o;
|
|
});
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public void trigger(final ServerPlayer xe, final BlockState byj) {
|
|
this.trigger(xe.getAdvancements(), a -> a.matches(byj));
|
|
}
|
|
|
|
static {
|
|
ID = new ResourceLocation("enter_block");
|
|
}
|
|
|
|
public static class TriggerInstance extends AbstractCriterionTriggerInstance {
|
|
private final Block block;
|
|
private final StatePropertiesPredicate state;
|
|
|
|
public TriggerInstance(@Nullable final Block bph, final StatePropertiesPredicate cc) {
|
|
super(EnterBlockTrigger.ID);
|
|
this.block = bph;
|
|
this.state = cc;
|
|
}
|
|
|
|
public static TriggerInstance entersBlock(final Block bph) {
|
|
return new TriggerInstance(bph, StatePropertiesPredicate.ANY);
|
|
}
|
|
|
|
@Override
|
|
public JsonElement serializeToJson() {
|
|
final JsonObject jsonObject2 = new JsonObject();
|
|
if (this.block != null) {
|
|
jsonObject2.addProperty("block", Registry.BLOCK.getKey(this.block).toString());
|
|
}
|
|
jsonObject2.add("state", this.state.serializeToJson());
|
|
return jsonObject2;
|
|
}
|
|
|
|
public boolean matches(final BlockState byj) {
|
|
return (this.block == null || byj.getBlock() == this.block) && this.state.matches(byj);
|
|
}
|
|
}
|
|
}
|