Skip to content

Commit

Permalink
Rework EmiData json Sorting with the Emily Idea :D
Browse files Browse the repository at this point in the history
Created custom implementation of Supplier Interface, what should keep everything compatible 😅
PrioritySupplier holds two values, integer priority, and supplier itself, what get method is being executed in the implantation of get method of Supplier interface.
This implementation is now used to store suppliers of IndexStackData, additionally achieving the original goal of sorting the list with use of the priority.
  • Loading branch information
Kanzaji committed Nov 6, 2023
1 parent e87ae4e commit 03300c0
Showing 1 changed file with 65 additions and 43 deletions.
108 changes: 65 additions & 43 deletions xplat/src/main/java/dev/emi/emi/data/EmiData.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
public class EmiData {
public static Map<String, EmiRecipeCategoryProperties> categoryPriorities = Map.of();
public static List<Predicate<EmiRecipe>> recipeFilters = List.of();
public static List<Supplier<IndexStackData>> stackData = List.of();
public static List<PrioritySupplier<IndexStackData>> stackData = List.of();
public static List<Supplier<EmiAlias>> aliases = List.of();
public static List<Supplier<EmiRecipe>> recipes = List.of();

Expand Down Expand Up @@ -134,56 +134,51 @@ public static void init(Consumer<EmiResourceReloadListener> register) {
}
}, list -> recipeFilters = list));
register.accept(
new EmiDataLoader<List<Map<Integer, Supplier<IndexStackData>>>>(
new EmiDataLoader<List<PrioritySupplier<IndexStackData>>>(
new Identifier("emi:index_stacks"), "index/stacks", Lists::newArrayList,
(list, json, oid) -> {
Map<Integer, Supplier<IndexStackData>> map = Maps.newHashMap();
map.put(JsonHelper.getInt(json, "priority", 0), () -> {
List<IndexStackData.Added> added = Lists.newArrayList();
List<EmiIngredient> removed = Lists.newArrayList();
List<IndexStackData.Filter> filters = Lists.newArrayList();
if (JsonHelper.hasArray(json, "added")) {
for (JsonElement el : json.getAsJsonArray("added")) {
if (el.isJsonObject()) {
JsonObject obj = el.getAsJsonObject();
EmiIngredient stack = EmiIngredientSerializer.getDeserialized(obj.get("stack"));
EmiIngredient after = EmiStack.EMPTY;
if (obj.has("after")) {
after = EmiIngredientSerializer.getDeserialized(obj.get("after"));
}
added.add(new IndexStackData.Added(stack, after));
(list, json, oid) -> list.add(new PrioritySupplier<>(JsonHelper.getInt(json, "priority", 0), () -> {
List<IndexStackData.Added> added = Lists.newArrayList();
List<EmiIngredient> removed = Lists.newArrayList();
List<IndexStackData.Filter> filters = Lists.newArrayList();
if (JsonHelper.hasArray(json, "added")) {
for (JsonElement el : json.getAsJsonArray("added")) {
if (el.isJsonObject()) {
JsonObject obj = el.getAsJsonObject();
EmiIngredient stack = EmiIngredientSerializer.getDeserialized(obj.get("stack"));
EmiIngredient after = EmiStack.EMPTY;
if (obj.has("after")) {
after = EmiIngredientSerializer.getDeserialized(obj.get("after"));
}
added.add(new IndexStackData.Added(stack, after));
}
}
if (JsonHelper.hasArray(json, "removed")) {
for (JsonElement el : json.getAsJsonArray("removed")) {
removed.add(EmiIngredientSerializer.getDeserialized(el));
}
}
if (JsonHelper.hasArray(json, "removed")) {
for (JsonElement el : json.getAsJsonArray("removed")) {
removed.add(EmiIngredientSerializer.getDeserialized(el));
}
if (JsonHelper.hasArray(json, "filters")) {
for (JsonElement el : json.getAsJsonArray("filters")) {
if (JsonHelper.isString(el)) {
String id = el.getAsString();
if (id.startsWith("/") && id.endsWith("/")) {
Pattern pat = Pattern.compile(id.substring(1, id.length() - 1));
filters.add(new IndexStackData.Filter(s -> pat.matcher(s).find()));
} else {
filters.add(new IndexStackData.Filter(s -> s.equals(id)));
}
}
if (JsonHelper.hasArray(json, "filters")) {
for (JsonElement el : json.getAsJsonArray("filters")) {
if (JsonHelper.isString(el)) {
String id = el.getAsString();
if (id.startsWith("/") && id.endsWith("/")) {
Pattern pat = Pattern.compile(id.substring(1, id.length() - 1));
filters.add(new IndexStackData.Filter(s -> pat.matcher(s).find()));
} else {
filters.add(new IndexStackData.Filter(s -> s.equals(id)));
}
}
}
boolean disable = JsonHelper.getBoolean(json, "disable", false);
return new IndexStackData(disable, added, removed, filters);
});
list.add(map);
},
supplierList -> {
supplierList.sort(Comparator.comparingInt((Map<Integer, Supplier<IndexStackData>> map) -> map.keySet().iterator().next()));
List<Supplier<IndexStackData>> result = Lists.newArrayList();
supplierList.forEach(map -> result.addAll(map.values()));
stackData = result;
}));
}
boolean disable = JsonHelper.getBoolean(json, "disable", false);
return new IndexStackData(disable, added, removed, filters);
})),
list -> {
list.sort(Comparator.comparingInt(PrioritySupplier::getPriority));
stackData = list;
}
));
register.accept(
new EmiDataLoader<List<Supplier<EmiAlias>>>(
new Identifier("emi:aliases"), "aliases", Lists::newArrayList,
Expand Down Expand Up @@ -232,4 +227,31 @@ private static Stream<JsonElement> getArrayOrSingleton(JsonObject json, String k
}
return Stream.of(json.get(key));
}

public static class PrioritySupplier<T> implements Supplier<T> {
private final int priority;
private final Supplier<T> supplier;

public PrioritySupplier(int priority, Supplier<T> supplier) {
this.priority = priority;
this.supplier = supplier;
}

/**
* Gets a result.
* @return a result
*/
@Override
public T get() {
return this.supplier.get();
}

/**
* Gets a priority if this Supplier.
* @return integer priority of this Supplier.
*/
public int getPriority() {
return this.priority;
}
};
}

0 comments on commit 03300c0

Please sign in to comment.