From 500a9053143e09005f43e32de1f65442e15fa77e Mon Sep 17 00:00:00 2001 From: Ryan Date: Sun, 24 Jul 2022 22:08:57 -0700 Subject: [PATCH] Add bit fields to entity extractor output --- .../extractors/Entities.java | 145 +++++++++++++++++- 1 file changed, 137 insertions(+), 8 deletions(-) diff --git a/extractor/src/main/java/dev/_00a/valence_extractor/extractors/Entities.java b/extractor/src/main/java/dev/_00a/valence_extractor/extractors/Entities.java index 0b83b83..aa3206b 100644 --- a/extractor/src/main/java/dev/_00a/valence_extractor/extractors/Entities.java +++ b/extractor/src/main/java/dev/_00a/valence_extractor/extractors/Entities.java @@ -23,12 +23,119 @@ import net.minecraft.util.registry.RegistryEntry; import net.minecraft.village.VillagerData; import java.lang.reflect.ParameterizedType; -import java.util.HashSet; -import java.util.Locale; -import java.util.Optional; -import java.util.OptionalInt; +import java.util.*; public class Entities implements Main.Extractor { + private final static Map 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() { } @@ -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 bits(String fieldName, Bit... bits) { + return Map.entry(fieldName, bits); + } + @Override public String fileName() { return "entities.json"; @@ -138,7 +253,7 @@ public class Entities implements Main.Extractor { @Override @SuppressWarnings("unchecked") public JsonElement extract() throws IllegalAccessException, NoSuchFieldException { - final var entitiesJson = new JsonArray(); + final var entitiesJson = new JsonObject(); final var entityClasses = new HashSet>(); final var dummyWorld = DummyWorld.INSTANCE; @@ -158,7 +273,7 @@ public class Entities implements Main.Extractor { while (entityClasses.add(entityClass)) { var entityJson = new JsonObject(); - entityJson.addProperty("class", entityClass.getSimpleName()); + entityJson.add("translation_key", entityType != null ? new JsonPrimitive(entityType.getTranslationKey()) : null); var fieldsJson = new JsonArray(); @@ -169,7 +284,8 @@ public class Entities implements Main.Extractor { var data = (TrackedData) entityField.get(null); 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()); var dataTracker = (DataTracker) dataTrackerField.get(entityInstance); @@ -177,6 +293,15 @@ public class Entities implements Main.Extractor { fieldJson.addProperty("type", res.left()); 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); } } @@ -185,18 +310,22 @@ public class Entities implements Main.Extractor { var parent = entityClass.getSuperclass(); if (parent == null || !Entity.class.isAssignableFrom(parent)) { entityJson.add("parent", null); + entitiesJson.add(entityClass.getSimpleName(), entityJson); break; } entityJson.addProperty("parent", parent.getSimpleName()); + entitiesJson.add(entityClass.getSimpleName(), entityJson); entityClass = (Class) parent; entityType = null; - entitiesJson.add(entityJson); } } } return entitiesJson; } + + private record Bit(String name, int index) { + } }