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

133 lines
4.6 KiB
Java

package net.minecraft.advancements.critereon;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import net.minecraft.tags.BlockTags;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;
import com.google.gson.JsonElement;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import javax.annotation.Nullable;
import net.minecraft.world.level.block.Block;
import net.minecraft.tags.Tag;
public class BlockPredicate {
public static final BlockPredicate ANY;
@Nullable
private final Tag<Block> tag;
@Nullable
private final Block block;
private final StatePropertiesPredicate properties;
private final NbtPredicate nbt;
public BlockPredicate(@Nullable final Tag<Block> aaz, @Nullable final Block bph, final StatePropertiesPredicate cc, final NbtPredicate bt) {
this.tag = aaz;
this.block = bph;
this.properties = cc;
this.nbt = bt;
}
public boolean matches(final ServerLevel xd, final BlockPos fk) {
if (this == BlockPredicate.ANY) {
return true;
}
if (!xd.isLoaded(fk)) {
return false;
}
final BlockState byj4 = xd.getBlockState(fk);
final Block bph5 = byj4.getBlock();
if (this.tag != null && !this.tag.contains(bph5)) {
return false;
}
if (this.block != null && bph5 != this.block) {
return false;
}
if (!this.properties.matches(byj4)) {
return false;
}
if (this.nbt != NbtPredicate.ANY) {
final BlockEntity bwl6 = xd.getBlockEntity(fk);
if (bwl6 == null || !this.nbt.matches(bwl6.save(new CompoundTag()))) {
return false;
}
}
return true;
}
public static BlockPredicate fromJson(@Nullable final JsonElement jsonElement) {
if (jsonElement == null || jsonElement.isJsonNull()) {
return BlockPredicate.ANY;
}
final JsonObject jsonObject2 = GsonHelper.convertToJsonObject(jsonElement, "block");
final NbtPredicate bt3 = NbtPredicate.fromJson(jsonObject2.get("nbt"));
Block bph4 = null;
if (jsonObject2.has("block")) {
final ResourceLocation sm5 = new ResourceLocation(GsonHelper.getAsString(jsonObject2, "block"));
bph4 = Registry.BLOCK.get(sm5);
}
Tag<Block> aaz5 = null;
if (jsonObject2.has("tag")) {
final ResourceLocation sm6 = new ResourceLocation(GsonHelper.getAsString(jsonObject2, "tag"));
aaz5 = BlockTags.getAllTags().getTag(sm6);
if (aaz5 == null) {
throw new JsonSyntaxException("Unknown block tag '" + sm6 + "'");
}
}
final StatePropertiesPredicate cc6 = StatePropertiesPredicate.fromJson(jsonObject2.get("state"));
return new BlockPredicate(aaz5, bph4, cc6, bt3);
}
public JsonElement serializeToJson() {
if (this == BlockPredicate.ANY) {
return JsonNull.INSTANCE;
}
final JsonObject jsonObject2 = new JsonObject();
if (this.block != null) {
jsonObject2.addProperty("block", Registry.BLOCK.getKey(this.block).toString());
}
if (this.tag != null) {
jsonObject2.addProperty("tag", this.tag.getId().toString());
}
jsonObject2.add("nbt", this.nbt.serializeToJson());
jsonObject2.add("state", this.properties.serializeToJson());
return jsonObject2;
}
static {
ANY = new BlockPredicate(null, null, StatePropertiesPredicate.ANY, NbtPredicate.ANY);
}
public static class Builder {
@Nullable
private Block block;
@Nullable
private Tag<Block> blocks;
private StatePropertiesPredicate properties;
private NbtPredicate nbt;
private Builder() {
this.properties = StatePropertiesPredicate.ANY;
this.nbt = NbtPredicate.ANY;
}
public static Builder block() {
return new Builder();
}
public Builder of(final Tag<Block> aaz) {
this.blocks = aaz;
return this;
}
public BlockPredicate build() {
return new BlockPredicate(this.blocks, this.block, this.properties, this.nbt);
}
}
}