diff --git a/cardinal-ui/src/main/java/folk/sisby/switchy/client/CardinalClientModuleLoader.java b/cardinal-ui/src/main/java/folk/sisby/switchy/client/CardinalClientModuleLoader.java index 04cb965..b6674cc 100644 --- a/cardinal-ui/src/main/java/folk/sisby/switchy/client/CardinalClientModuleLoader.java +++ b/cardinal-ui/src/main/java/folk/sisby/switchy/client/CardinalClientModuleLoader.java @@ -58,80 +58,82 @@ public class CardinalClientModuleLoader extends JsonDataLoader implements Identi super(gson, "switchy_cardinal"); } - @Override - protected void apply(Map prepared, ResourceManager manager, Profiler profiler) { - prepared.forEach((moduleId, contents) -> { - if (SwitchyClientModuleRegistry.containsModule(moduleId)) { - return; - } - JsonObject moduleOptions = contents.getAsJsonObject(); - if (!moduleOptions.has(KEY_ICON)) { - SwitchyCardinalClient.LOGGER.warn("[Switchy Cardinal UI] module '{}' is missing options, skipping...", moduleId); - return; - } + private static void accept(Identifier moduleId, JsonElement contents) { + if (SwitchyClientModuleRegistry.containsModule(moduleId)) { + return; + } + JsonObject moduleOptions = contents.getAsJsonObject(); + if (!moduleOptions.has(KEY_ICON)) { + SwitchyCardinalClient.LOGGER.warn("[Switchy Cardinal UI] module '{}' is missing options, skipping...", moduleId); + return; + } - try { - Function iconStackSupplier; - JsonObject icon = moduleOptions.get(KEY_ICON).getAsJsonObject(); - if (icon.has(KEY_ICON_PATH)) { - try { - NbtPathArgumentType.NbtPath path = pathAtg.parse(new StringReader(icon.get(KEY_ICON_PATH).getAsString())); - iconStackSupplier = nbt -> { - try { - return ItemStack.CODEC.decode(NbtOps.INSTANCE, path.get(nbt).get(0)).getOrThrow().getFirst(); - } catch (Exception e) { - return Items.DIRT.getDefaultStack(); - } - }; - } catch (CommandSyntaxException e) { - SwitchyCardinalClient.LOGGER.warn("[Switchy Cardinal UI] module '{}' has invalid condition path '{}', skipping...", moduleId, icon.get(KEY_ICON_PATH).getAsString()); - return; - } - } else { - ItemStack stack = ItemStack.CODEC.decode(JsonOps.INSTANCE, icon).getOrThrow().getFirst(); - if (stack.getCount() == 0) stack.setCount(1); - iconStackSupplier = nbt -> stack; - } - - List valuePaths = new ArrayList<>(); - if (moduleOptions.has(KEY_VALUES)) { - for (JsonElement element : moduleOptions.getAsJsonArray(KEY_VALUES)) { + try { + Function iconStackSupplier; + JsonObject icon = moduleOptions.get(KEY_ICON).getAsJsonObject(); + if (icon.has(KEY_ICON_PATH)) { + try { + NbtPathArgumentType.NbtPath path = pathAtg.parse(new StringReader(icon.get(KEY_ICON_PATH).getAsString())); + iconStackSupplier = nbt -> { try { - valuePaths.add(pathAtg.parse(new StringReader(element.getAsString()))); - } catch (CommandSyntaxException e) { - SwitchyCardinalClient.LOGGER.warn("[Switchy Cardinal UI] module '{}' has invalid path '{}', skipping...", moduleId, element.getAsString()); - return; + return ItemStack.CODEC.decode(NbtOps.INSTANCE, path.get(nbt).get(0)).getOrThrow().getFirst(); + } catch (Exception e) { + return Items.DIRT.getDefaultStack(); } - } + }; + } catch (CommandSyntaxException e) { + SwitchyCardinalClient.LOGGER.warn("[Switchy Cardinal UI] module '{}' has invalid condition path '{}', skipping...", moduleId, icon.get(KEY_ICON_PATH).getAsString()); + return; } + } else { + ItemStack stack = ItemStack.CODEC.decode(JsonOps.INSTANCE, icon).getOrThrow().getFirst(); + if (stack.getCount() == 0) stack.setCount(1); + iconStackSupplier = nbt -> stack; + } - List inventoryPaths = new ArrayList<>(); - if (moduleOptions.has(KEY_INVENTORIES)) { - for (JsonElement element : moduleOptions.getAsJsonArray(KEY_INVENTORIES)) { - try { - inventoryPaths.add(pathAtg.parse(new StringReader(element.getAsString()))); - } catch (CommandSyntaxException e) { - SwitchyCardinalClient.LOGGER.warn("[Switchy Cardinal UI] module '{}' has invalid path '{}', skipping...", moduleId, element.getAsString()); - return; - } + List valuePaths = new ArrayList<>(); + if (moduleOptions.has(KEY_VALUES)) { + for (JsonElement element : moduleOptions.getAsJsonArray(KEY_VALUES)) { + try { + valuePaths.add(pathAtg.parse(new StringReader(element.getAsString()))); + } catch (CommandSyntaxException e) { + SwitchyCardinalClient.LOGGER.warn("[Switchy Cardinal UI] module '{}' has invalid path '{}', skipping...", moduleId, element.getAsString()); + return; } } + } - NbtPathArgumentType.NbtPath condition = null; - if (moduleOptions.has(KEY_CONDITION)) { + List inventoryPaths = new ArrayList<>(); + if (moduleOptions.has(KEY_INVENTORIES)) { + for (JsonElement element : moduleOptions.getAsJsonArray(KEY_INVENTORIES)) { try { - condition = pathAtg.parse(new StringReader(moduleOptions.get(KEY_CONDITION).getAsString())); + inventoryPaths.add(pathAtg.parse(new StringReader(element.getAsString()))); } catch (CommandSyntaxException e) { - SwitchyCardinalClient.LOGGER.warn("[Switchy Cardinal UI] module '{}' has invalid condition path '{}', skipping...", moduleId, moduleOptions.get(KEY_CONDITION).getAsString()); + SwitchyCardinalClient.LOGGER.warn("[Switchy Cardinal UI] module '{}' has invalid path '{}', skipping...", moduleId, element.getAsString()); return; } } + } - CardinalSerializerClientModule.register(moduleId, new CardinalSerializerClientModule.PreviewConfig(iconStackSupplier, valuePaths, inventoryPaths, condition)); - } catch (UnsupportedOperationException | JsonSyntaxException ignoredGetFromJsonEx) { - SwitchyCardinalClient.LOGGER.warn("[Switchy Cardinal UI] module '{}' has invalid types, skipping...", moduleId); + NbtPathArgumentType.NbtPath condition = null; + if (moduleOptions.has(KEY_CONDITION)) { + try { + condition = pathAtg.parse(new StringReader(moduleOptions.get(KEY_CONDITION).getAsString())); + } catch (CommandSyntaxException e) { + SwitchyCardinalClient.LOGGER.warn("[Switchy Cardinal UI] module '{}' has invalid condition path '{}', skipping...", moduleId, moduleOptions.get(KEY_CONDITION).getAsString()); + return; + } } - }); + + CardinalSerializerClientModule.register(moduleId, new CardinalSerializerClientModule.PreviewConfig(iconStackSupplier, valuePaths, inventoryPaths, condition)); + } catch (UnsupportedOperationException | JsonSyntaxException ignoredGetFromJsonEx) { + SwitchyCardinalClient.LOGGER.warn("[Switchy Cardinal UI] module '{}' has invalid types, skipping...", moduleId); + } + } + + @Override + protected void apply(Map prepared, ResourceManager manager, Profiler profiler) { + prepared.forEach(CardinalClientModuleLoader::accept); SwitchyCardinalClient.LOGGER.info("[Switchy Cardinal UI] Finished reloading {} modules!", prepared.size()); }