From 03300c0275ee7dec55bc60908a094d50caeb5fb1 Mon Sep 17 00:00:00 2001 From: Kanzaji <60540476+Kanzaji@users.noreply.github.com> Date: Mon, 6 Nov 2023 21:08:19 +0100 Subject: [PATCH] Rework EmiData json Sorting with the Emily Idea :D Created custom implementation of Supplier Interface, what should keep everything compatible :sweat_smile: 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. --- .../main/java/dev/emi/emi/data/EmiData.java | 108 +++++++++++------- 1 file changed, 65 insertions(+), 43 deletions(-) diff --git a/xplat/src/main/java/dev/emi/emi/data/EmiData.java b/xplat/src/main/java/dev/emi/emi/data/EmiData.java index b7686d97..1daeaac8 100644 --- a/xplat/src/main/java/dev/emi/emi/data/EmiData.java +++ b/xplat/src/main/java/dev/emi/emi/data/EmiData.java @@ -31,7 +31,7 @@ public class EmiData { public static Map categoryPriorities = Map.of(); public static List> recipeFilters = List.of(); - public static List> stackData = List.of(); + public static List> stackData = List.of(); public static List> aliases = List.of(); public static List> recipes = List.of(); @@ -134,56 +134,51 @@ public static void init(Consumer register) { } }, list -> recipeFilters = list)); register.accept( - new EmiDataLoader>>>( + new EmiDataLoader>>( new Identifier("emi:index_stacks"), "index/stacks", Lists::newArrayList, - (list, json, oid) -> { - Map> map = Maps.newHashMap(); - map.put(JsonHelper.getInt(json, "priority", 0), () -> { - List added = Lists.newArrayList(); - List removed = Lists.newArrayList(); - List 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 added = Lists.newArrayList(); + List removed = Lists.newArrayList(); + List 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> map) -> map.keySet().iterator().next())); - List> 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>>( new Identifier("emi:aliases"), "aliases", Lists::newArrayList, @@ -232,4 +227,31 @@ private static Stream getArrayOrSingleton(JsonObject json, String k } return Stream.of(json.get(key)); } + + public static class PrioritySupplier implements Supplier { + private final int priority; + private final Supplier supplier; + + public PrioritySupplier(int priority, Supplier 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; + } + }; }