From 37f57892027911ffe161c23ea70f46b0cba9f93b Mon Sep 17 00:00:00 2001 From: Ryan Date: Tue, 26 Jul 2022 02:18:32 -0700 Subject: [PATCH] Extract more data from entities --- .../java/dev/_00a/valence_extractor/Main.java | 4 +- .../extractors/Entities.java | 16 ++-- .../extractors/EntityData.java | 89 +++++++++++++++++++ .../extractors/EntityStatuses.java | 32 ------- 4 files changed, 102 insertions(+), 39 deletions(-) create mode 100644 extractor/src/main/java/dev/_00a/valence_extractor/extractors/EntityData.java delete mode 100644 extractor/src/main/java/dev/_00a/valence_extractor/extractors/EntityStatuses.java diff --git a/extractor/src/main/java/dev/_00a/valence_extractor/Main.java b/extractor/src/main/java/dev/_00a/valence_extractor/Main.java index d798af6..01a9fc6 100644 --- a/extractor/src/main/java/dev/_00a/valence_extractor/Main.java +++ b/extractor/src/main/java/dev/_00a/valence_extractor/Main.java @@ -4,7 +4,7 @@ import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import dev._00a.valence_extractor.extractors.Blocks; import dev._00a.valence_extractor.extractors.Entities; -import dev._00a.valence_extractor.extractors.EntityStatuses; +import dev._00a.valence_extractor.extractors.EntityData; import net.fabricmc.api.ModInitializer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -39,7 +39,7 @@ public class Main implements ModInitializer { public void onInitialize() { LOGGER.info("Starting extractors..."); - var extractors = new Extractor[]{new Blocks(), new Entities(), new EntityStatuses(),}; + var extractors = new Extractor[]{new Blocks(), new Entities(), new EntityData(),}; Path outputDirectory; try { 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 9ce9ae5..09de8b1 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 @@ -6,6 +6,7 @@ import dev._00a.valence_extractor.DummyWorld; import dev._00a.valence_extractor.Main; import dev._00a.valence_extractor.Main.Pair; import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityPose; import net.minecraft.entity.EntityType; import net.minecraft.entity.data.DataTracker; import net.minecraft.entity.data.TrackedData; @@ -199,19 +200,22 @@ public class Entities implements Main.Extractor { // TODO: base64 binary representation or SNBT? return new Pair<>("nbt_compound", new JsonPrimitive(val.toString())); } else if (handler == TrackedDataHandlerRegistry.PARTICLE) { - return new Pair<>("particle", new JsonPrimitive(((ParticleEffect) val).asString())); + var id = Registry.PARTICLE_TYPE.getId(((ParticleEffect) val).getType()); + return new Pair<>("particle", new JsonPrimitive(id.getPath())); } else if (handler == TrackedDataHandlerRegistry.VILLAGER_DATA) { var vd = (VillagerData) val; var json = new JsonObject(); - json.addProperty("type", vd.getType().toString()); - json.addProperty("profession", vd.getProfession().toString()); + var type = Registry.VILLAGER_TYPE.getId(vd.getType()).getPath(); + var profession = Registry.VILLAGER_PROFESSION.getId(vd.getProfession()).getPath(); + json.addProperty("type", type); + json.addProperty("profession", profession); json.addProperty("level", vd.getLevel()); return new Pair<>("villager_data", json); } else if (handler == TrackedDataHandlerRegistry.OPTIONAL_INT) { var opt = (OptionalInt) val; return new Pair<>("optional_int", opt.isPresent() ? new JsonPrimitive(opt.getAsInt()) : JsonNull.INSTANCE); } else if (handler == TrackedDataHandlerRegistry.ENTITY_POSE) { - return new Pair<>("entity_pose", new JsonPrimitive(val.toString())); + return new Pair<>("entity_pose", new JsonPrimitive(((EntityPose) val).name().toLowerCase(Locale.ROOT))); } else if (handler == TrackedDataHandlerRegistry.CAT_VARIANT) { return new Pair<>("cat_variant", new JsonPrimitive(Registry.CAT_VARIANT.getId((CatVariant) val).getPath())); } else if (handler == TrackedDataHandlerRegistry.FROG_VARIANT) { @@ -322,7 +326,9 @@ public class Entities implements Main.Extractor { entitiesJson.add(entityClass.getSimpleName(), entityJson); - if (!hasParent) break; + if (!hasParent) { + break; + } entityClass = (Class) parent; entityType = entityClassToType.get(entityClass); diff --git a/extractor/src/main/java/dev/_00a/valence_extractor/extractors/EntityData.java b/extractor/src/main/java/dev/_00a/valence_extractor/extractors/EntityData.java new file mode 100644 index 0000000..14d7bb2 --- /dev/null +++ b/extractor/src/main/java/dev/_00a/valence_extractor/extractors/EntityData.java @@ -0,0 +1,89 @@ +package dev._00a.valence_extractor.extractors; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import dev._00a.valence_extractor.Main; +import net.minecraft.entity.EntityPose; +import net.minecraft.util.math.Direction; +import net.minecraft.util.registry.Registry; + +import java.util.Locale; + +public class EntityData implements Main.Extractor { + @Override + public String fileName() { + return "entity_data.json"; + } + + @Override + public JsonElement extract() throws Exception { + var dataJson = new JsonObject(); + + var statusesJson = new JsonObject(); + for (var field : net.minecraft.entity.EntityStatuses.class.getDeclaredFields()) { + if (field.canAccess(null) && field.get(null) instanceof Byte code) { + if (field.getName().equals("field_30030")) { + statusesJson.addProperty("stop_attack", code); + } else { + statusesJson.addProperty(field.getName().toLowerCase(Locale.ROOT), code); + } + } + } + dataJson.add("statuses", statusesJson); + + var villagerTypesJson = new JsonObject(); + for (var type : Registry.VILLAGER_TYPE) { + villagerTypesJson.addProperty(Registry.VILLAGER_TYPE.getId(type).getPath(), Registry.VILLAGER_TYPE.getRawId(type)); + } + dataJson.add("villager_types", villagerTypesJson); + + var villagerProfessionsJson = new JsonObject(); + for (var profession : Registry.VILLAGER_PROFESSION) { + villagerProfessionsJson.addProperty(profession.id(), Registry.VILLAGER_PROFESSION.getRawId(profession)); + } + dataJson.add("villager_professions", villagerProfessionsJson); + + var catVariantsJson = new JsonObject(); + for (var variant : Registry.CAT_VARIANT) { + catVariantsJson.addProperty(Registry.CAT_VARIANT.getId(variant).getPath(), Registry.CAT_VARIANT.getRawId(variant)); + } + dataJson.add("cat_variants", catVariantsJson); + + var frogVariantsJson = new JsonObject(); + for (var variant : Registry.FROG_VARIANT) { + frogVariantsJson.addProperty(Registry.FROG_VARIANT.getId(variant).getPath(), Registry.FROG_VARIANT.getRawId(variant)); + } + dataJson.add("frog_variants", frogVariantsJson); + + var paintingVariantsJson = new JsonObject(); + for (var variant : Registry.PAINTING_VARIANT) { + var variantJson = new JsonObject(); + variantJson.addProperty("id", Registry.PAINTING_VARIANT.getRawId(variant)); + variantJson.addProperty("width", variant.getWidth()); + variantJson.addProperty("height", variant.getHeight()); + paintingVariantsJson.add(Registry.PAINTING_VARIANT.getId(variant).getPath(), variantJson); + } + dataJson.add("painting_variants", paintingVariantsJson); + + var facingJson = new JsonObject(); + for (var dir : Direction.values()) { + facingJson.addProperty(dir.getName(), dir.getId()); + } + dataJson.add("facing", facingJson); + + var posesJson = new JsonObject(); + var poses = EntityPose.values(); + for (int i = 0; i < poses.length; i++) { + posesJson.addProperty(poses[i].name().toLowerCase(Locale.ROOT), i); + } + dataJson.add("poses", posesJson); + + var particleTypesJson = new JsonObject(); + for (var type : Registry.PARTICLE_TYPE) { + particleTypesJson.addProperty(Registry.PARTICLE_TYPE.getId(type).getPath(), Registry.PARTICLE_TYPE.getRawId(type)); + } + dataJson.add("particle_types", particleTypesJson); + + return dataJson; + } +} diff --git a/extractor/src/main/java/dev/_00a/valence_extractor/extractors/EntityStatuses.java b/extractor/src/main/java/dev/_00a/valence_extractor/extractors/EntityStatuses.java deleted file mode 100644 index 6373dc5..0000000 --- a/extractor/src/main/java/dev/_00a/valence_extractor/extractors/EntityStatuses.java +++ /dev/null @@ -1,32 +0,0 @@ -package dev._00a.valence_extractor.extractors; - -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import dev._00a.valence_extractor.Main; - -import java.util.Locale; - -public class EntityStatuses implements Main.Extractor { - @Override - public String fileName() { - return "entity_statuses.json"; - } - - @Override - public JsonElement extract() throws Exception { - var statusesJson = new JsonObject(); - - for (var field : net.minecraft.entity.EntityStatuses.class.getDeclaredFields()) { - if (field.canAccess(null) && field.get(null) instanceof Byte code) { - if (field.getName().equals("field_30030")) { - // TODO: temp - statusesJson.addProperty("stop_attack", code); - } else { - statusesJson.addProperty(field.getName().toLowerCase(Locale.ROOT), code); - } - } - } - - return statusesJson; - } -}