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

103 lines
3.4 KiB
Java
Raw Normal View History

2020-07-22 06:23:34 +01:00
package net.minecraft.advancements.critereon;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonNull;
import net.minecraft.tags.Tag;
import com.google.gson.JsonSyntaxException;
import net.minecraft.core.Registry;
import net.minecraft.tags.EntityTypeTags;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;
import javax.annotation.Nullable;
import com.google.gson.JsonElement;
import net.minecraft.world.entity.EntityType;
import com.google.common.base.Joiner;
public abstract class EntityTypePredicate {
public static final EntityTypePredicate ANY;
private static final Joiner COMMA_JOINER;
2020-07-22 06:30:03 +01:00
public abstract boolean matches(final EntityType<?> aku);
2020-07-22 06:23:34 +01:00
public abstract JsonElement serializeToJson();
public static EntityTypePredicate fromJson(@Nullable final JsonElement jsonElement) {
if (jsonElement == null || jsonElement.isJsonNull()) {
return EntityTypePredicate.ANY;
}
final String string2 = GsonHelper.convertToString(jsonElement, "type");
if (string2.startsWith("#")) {
2020-07-22 06:25:47 +01:00
final ResourceLocation sm3 = new ResourceLocation(string2.substring(1));
final Tag<EntityType<?>> aaz4 = EntityTypeTags.getAllTags().getTagOrEmpty(sm3);
return new TagPredicate(aaz4);
2020-07-22 06:23:34 +01:00
}
2020-07-22 06:25:47 +01:00
final ResourceLocation sm3 = new ResourceLocation(string2);
2020-07-22 06:23:34 +01:00
final Object o;
final Object obj;
2020-07-22 06:30:03 +01:00
final EntityType<?> aku4 = Registry.ENTITY_TYPE.getOptional(sm3).<Throwable>orElseThrow(() -> {
2020-07-22 06:23:34 +01:00
new JsonSyntaxException("Unknown entity type '" + obj + "', valid types are: " + EntityTypePredicate.COMMA_JOINER.join(Registry.ENTITY_TYPE.keySet()));
return o;
});
2020-07-22 06:30:03 +01:00
return new TypePredicate(aku4);
2020-07-22 06:23:34 +01:00
}
2020-07-22 06:30:03 +01:00
public static EntityTypePredicate of(final EntityType<?> aku) {
return new TypePredicate(aku);
2020-07-22 06:23:34 +01:00
}
2020-07-22 06:25:47 +01:00
public static EntityTypePredicate of(final Tag<EntityType<?>> aaz) {
return new TagPredicate(aaz);
2020-07-22 06:23:34 +01:00
}
static {
ANY = new EntityTypePredicate() {
@Override
2020-07-22 06:30:03 +01:00
public boolean matches(final EntityType<?> aku) {
2020-07-22 06:23:34 +01:00
return true;
}
@Override
public JsonElement serializeToJson() {
return JsonNull.INSTANCE;
}
};
COMMA_JOINER = Joiner.on(", ");
}
static class TypePredicate extends EntityTypePredicate {
private final EntityType<?> type;
2020-07-22 06:30:03 +01:00
public TypePredicate(final EntityType<?> aku) {
this.type = aku;
2020-07-22 06:23:34 +01:00
}
@Override
2020-07-22 06:30:03 +01:00
public boolean matches(final EntityType<?> aku) {
return this.type == aku;
2020-07-22 06:23:34 +01:00
}
@Override
public JsonElement serializeToJson() {
return new JsonPrimitive(Registry.ENTITY_TYPE.getKey(this.type).toString());
}
}
static class TagPredicate extends EntityTypePredicate {
private final Tag<EntityType<?>> tag;
2020-07-22 06:25:47 +01:00
public TagPredicate(final Tag<EntityType<?>> aaz) {
this.tag = aaz;
2020-07-22 06:23:34 +01:00
}
@Override
2020-07-22 06:30:03 +01:00
public boolean matches(final EntityType<?> aku) {
return this.tag.contains(aku);
2020-07-22 06:23:34 +01:00
}
@Override
public JsonElement serializeToJson() {
return new JsonPrimitive("#" + this.tag.getId().toString());
}
}
}