Sort entity extractor output for better diffing

This commit is contained in:
Ryan 2022-08-12 10:28:16 -07:00
parent 55cb595740
commit 02fe0bae3b
2 changed files with 30 additions and 3 deletions

View file

@ -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<Class<?>> {
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;
}
}

View file

@ -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<Class<? extends Entity>, 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;
}