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

91 lines
3.3 KiB
Java
Raw Normal View History

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