diff --git a/extractor/src/main/java/dev/_00a/valence_extractor/ClassComparator.java b/extractor/src/main/java/dev/_00a/valence_extractor/ClassComparator.java new file mode 100644 index 0000000..d15c0ba --- /dev/null +++ b/extractor/src/main/java/dev/_00a/valence_extractor/ClassComparator.java @@ -0,0 +1,21 @@ +package dev._00a.valence_extractor; + +import java.util.Comparator; + +/** + * Compare Class objects by their simple names lexicographically. + */ +public class ClassComparator implements Comparator> { + public ClassComparator() { + } + + @Override + public int compare(Class c1, Class c2) { + return c1.getSimpleName().compareTo(c2.getSimpleName()); + } + + @Override + public boolean equals(Object comp) { + return comp instanceof ClassComparator; + } +} 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 4793b19..90f521b 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 @@ -1,6 +1,7 @@ package dev._00a.valence_extractor.extractors; import com.google.gson.*; +import dev._00a.valence_extractor.ClassComparator; import dev._00a.valence_extractor.DummyPlayerEntity; import dev._00a.valence_extractor.DummyWorld; import dev._00a.valence_extractor.Main; @@ -272,7 +273,7 @@ public class Entities implements Main.Extractor { final var dataTrackerField = Entity.class.getDeclaredField("dataTracker"); dataTrackerField.setAccessible(true); - var entitiesJson = new JsonObject(); + var entitiesMap = new TreeMap, JsonElement>(new ClassComparator()); for (var entry : entityClassToType.entrySet()) { var entityClass = entry.getKey(); @@ -285,7 +286,7 @@ public class Entities implements Main.Extractor { final var entityInstance = entityType.equals(EntityType.PLAYER) ? DummyPlayerEntity.INSTANCE : entityType.create(DummyWorld.INSTANCE); final var dataTracker = (DataTracker) dataTrackerField.get(entityInstance); - while (entitiesJson.get(entityClass.getSimpleName()) == null) { + while (entitiesMap.get(entityClass) == null) { var entityJson = new JsonObject(); var parent = entityClass.getSuperclass(); @@ -331,7 +332,7 @@ public class Entities implements Main.Extractor { } entityJson.add("fields", fieldsJson); - entitiesJson.add(entityClass.getSimpleName(), entityJson); + entitiesMap.put(entityClass, entityJson); if (!hasParent) { break; @@ -342,6 +343,11 @@ public class Entities implements Main.Extractor { } } + var entitiesJson = new JsonObject(); + for (var entry : entitiesMap.entrySet()) { + entitiesJson.add(entry.getKey().getSimpleName(), entry.getValue()); + } + return entitiesJson; }