Add bit fields to entity extractor output

This commit is contained in:
Ryan 2022-07-24 22:08:57 -07:00
parent 1a66f6f04d
commit 500a905314

View file

@ -23,12 +23,119 @@ import net.minecraft.util.registry.RegistryEntry;
import net.minecraft.village.VillagerData; import net.minecraft.village.VillagerData;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.ParameterizedType;
import java.util.HashSet; import java.util.*;
import java.util.Locale;
import java.util.Optional;
import java.util.OptionalInt;
public class Entities implements Main.Extractor { public class Entities implements Main.Extractor {
private final static Map<String, Bit[]> BIT_FIELDS = Map.ofEntries(
// @formatter:off
bits(
"flags",
bit("on_fire", 0),
bit("crouching", 1),
bit("sprinting", 3),
bit("swimming", 4),
bit("invisible", 5),
bit("glowing", 6),
bit("elytra_flying", 7)
),
bits(
"projectile_flags",
bit("critical", 0),
bit("no_clip", 1)
),
bits(
"living_flags",
bit("using_item", 0),
bit("off_hand_active", 1),
bit("using_riptide", 2)
),
bits(
"player_model_parts",
bit("cape", 0),
bit("jacket", 1),
bit("left_sleeve", 2),
bit("right_sleeve", 3),
bit("left_pants_leg", 4),
bit("right_pants_leg", 5),
bit("hat", 6)
),
bits(
"armor_stand_flags",
bit("small", 0),
bit("show_arms", 1),
bit("hide_base_plate", 2),
bit("marker", 3)
),
bits(
"mob_flags",
bit("ai_disabled", 0),
bit("left_handed", 1),
bit("attacking", 2)
),
bits(
"bat_flags",
bit("hanging", 0)
),
bits(
"horse_flags",
bit("tamed", 1),
bit("saddled", 2),
bit("bred", 3),
bit("eating_grass", 4),
bit("angry", 5),
bit("eating", 6)
),
bits(
"bee_flags",
bit("near_target", 1),
bit("has_stung", 2),
bit("has_nectar", 3)
),
bits(
"fox_flags",
bit("sitting", 0),
bit("crouching", 2),
bit("rolling_head", 3),
bit("chasing", 4),
bit("sleeping", 5),
bit("walking", 6),
bit("aggressive", 7)
),
bits(
"panda_flags",
bit("sneezing", 1),
bit("playing", 2),
bit("sitting", 3),
bit("lying_on_back", 4)
),
bits(
"tameable_flags",
bit("sitting_pose", 0),
bit("tamed", 2)
),
bits(
"iron_golem_flags",
bit("player_created", 0)
),
bits(
"snow_golem_flags",
bit("has_pumpkin", 4)
),
bits(
"blaze_flags",
bit("fire_active", 0)
),
bits(
"vex_flags",
bit("charging", 0)
),
bits(
"spider_flags",
bit("climbing_wall", 0)
)
// @formatter:on
);
public Entities() { public Entities() {
} }
@ -130,6 +237,14 @@ public class Entities implements Main.Extractor {
} }
} }
private static Bit bit(String name, int index) {
return new Bit(name, index);
}
private static Map.Entry<String, Bit[]> bits(String fieldName, Bit... bits) {
return Map.entry(fieldName, bits);
}
@Override @Override
public String fileName() { public String fileName() {
return "entities.json"; return "entities.json";
@ -138,7 +253,7 @@ public class Entities implements Main.Extractor {
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public JsonElement extract() throws IllegalAccessException, NoSuchFieldException { public JsonElement extract() throws IllegalAccessException, NoSuchFieldException {
final var entitiesJson = new JsonArray(); final var entitiesJson = new JsonObject();
final var entityClasses = new HashSet<Class<? extends Entity>>(); final var entityClasses = new HashSet<Class<? extends Entity>>();
final var dummyWorld = DummyWorld.INSTANCE; final var dummyWorld = DummyWorld.INSTANCE;
@ -158,7 +273,7 @@ public class Entities implements Main.Extractor {
while (entityClasses.add(entityClass)) { while (entityClasses.add(entityClass)) {
var entityJson = new JsonObject(); var entityJson = new JsonObject();
entityJson.addProperty("class", entityClass.getSimpleName());
entityJson.add("translation_key", entityType != null ? new JsonPrimitive(entityType.getTranslationKey()) : null); entityJson.add("translation_key", entityType != null ? new JsonPrimitive(entityType.getTranslationKey()) : null);
var fieldsJson = new JsonArray(); var fieldsJson = new JsonArray();
@ -169,7 +284,8 @@ public class Entities implements Main.Extractor {
var data = (TrackedData<?>) entityField.get(null); var data = (TrackedData<?>) entityField.get(null);
var fieldJson = new JsonObject(); var fieldJson = new JsonObject();
fieldJson.addProperty("name", entityField.getName().toLowerCase(Locale.ROOT)); var fieldName = entityField.getName().toLowerCase(Locale.ROOT);
fieldJson.addProperty("name", fieldName);
fieldJson.addProperty("index", data.getId()); fieldJson.addProperty("index", data.getId());
var dataTracker = (DataTracker) dataTrackerField.get(entityInstance); var dataTracker = (DataTracker) dataTrackerField.get(entityInstance);
@ -177,6 +293,15 @@ public class Entities implements Main.Extractor {
fieldJson.addProperty("type", res.left()); fieldJson.addProperty("type", res.left());
fieldJson.add("default_value", res.right()); fieldJson.add("default_value", res.right());
var bitsJson = new JsonArray();
for (var bit : BIT_FIELDS.getOrDefault(fieldName, new Bit[]{})) {
var bitJson = new JsonObject();
bitJson.addProperty("name", bit.name);
bitJson.addProperty("index", bit.index);
bitsJson.add(bitJson);
}
fieldJson.add("bits", bitsJson);
fieldsJson.add(fieldJson); fieldsJson.add(fieldJson);
} }
} }
@ -185,18 +310,22 @@ public class Entities implements Main.Extractor {
var parent = entityClass.getSuperclass(); var parent = entityClass.getSuperclass();
if (parent == null || !Entity.class.isAssignableFrom(parent)) { if (parent == null || !Entity.class.isAssignableFrom(parent)) {
entityJson.add("parent", null); entityJson.add("parent", null);
entitiesJson.add(entityClass.getSimpleName(), entityJson);
break; break;
} }
entityJson.addProperty("parent", parent.getSimpleName()); entityJson.addProperty("parent", parent.getSimpleName());
entitiesJson.add(entityClass.getSimpleName(), entityJson);
entityClass = (Class<? extends Entity>) parent; entityClass = (Class<? extends Entity>) parent;
entityType = null; entityType = null;
entitiesJson.add(entityJson);
} }
} }
} }
return entitiesJson; return entitiesJson;
} }
private record Bit(String name, int index) {
}
} }