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 14bf04e4..fd9586a5 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 @@ -49,6 +49,7 @@ public class CardinalClientModuleLoader extends JsonDataLoader implements Identi private static final String KEY_CONDITION = "condition"; private static final String KEY_ICON = "icon"; private static final String KEY_ICON_PATH = "path"; + private static final String KEY_INVENTORIES = "inventories"; private static final String KEY_VALUES = "values"; private static final NbtPathArgumentType pathAtg = NbtPathArgumentType.nbtPath(); @@ -67,7 +68,7 @@ protected void apply(Map prepared, ResourceManager mana SwitchyCardinalClient.LOGGER.warn("[Switchy Cardinal UI] module '{}' is missing options, skipping...", moduleId); return; } - List valuePaths = new ArrayList<>(); + try { Function iconStackSupplier; JsonObject icon = moduleOptions.get(KEY_ICON).getAsJsonObject(); @@ -91,12 +92,25 @@ protected void apply(Map prepared, ResourceManager mana iconStackSupplier = nbt -> stack; } + List valuePaths = new ArrayList<>(); if (moduleOptions.has(KEY_VALUES)) { - for (JsonElement valueElement : moduleOptions.getAsJsonArray(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; + } + } + } + + List inventoryPaths = new ArrayList<>(); + if (moduleOptions.has(KEY_INVENTORIES)) { + for (JsonElement element : moduleOptions.getAsJsonArray(KEY_INVENTORIES)) { try { - valuePaths.add(pathAtg.parse(new StringReader(valueElement.getAsString()))); + inventoryPaths.add(pathAtg.parse(new StringReader(element.getAsString()))); } catch (CommandSyntaxException e) { - SwitchyCardinalClient.LOGGER.warn("[Switchy Cardinal UI] module '{}' has invalid path '{}', skipping...", moduleId, valueElement.getAsString()); + SwitchyCardinalClient.LOGGER.warn("[Switchy Cardinal UI] module '{}' has invalid path '{}', skipping...", moduleId, element.getAsString()); return; } } @@ -112,7 +126,7 @@ protected void apply(Map prepared, ResourceManager mana } } - CardinalSerializerClientModule.register(moduleId, new CardinalSerializerClientModule.PreviewConfig(iconStackSupplier, valuePaths, condition)); + 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); } diff --git a/cardinal-ui/src/main/java/folk/sisby/switchy/client/api/modules/CardinalSerializerClientModule.java b/cardinal-ui/src/main/java/folk/sisby/switchy/client/api/modules/CardinalSerializerClientModule.java index e30c4d3a..e9202264 100644 --- a/cardinal-ui/src/main/java/folk/sisby/switchy/client/api/modules/CardinalSerializerClientModule.java +++ b/cardinal-ui/src/main/java/folk/sisby/switchy/client/api/modules/CardinalSerializerClientModule.java @@ -57,17 +57,14 @@ public Pair getPreviewComponent(String presetName) ItemComponent component = Components.item(config.icon.apply(moduleNbt)); List tooltips = new ArrayList<>(List.of(TooltipComponent.of(Text.translatable("switchy.modules.%s.%s.preview.tooltip".formatted(id.getNamespace(), id.getPath()), config.getValues(moduleNbt).toArray()).asOrderedText()))); - DefaultedList inv = DefaultedList.ofSize(255); - Inventories.readNbt(moduleNbt, inv); - DefaultedList items = DefaultedList.of(); - inv.stream().filter(i -> !i.isEmpty()).forEach(items::add); - if (!inv.isEmpty()) tooltips.add(TooltipComponent.of(new BundleTooltipData(items, 0))); + DefaultedList items = config.getStacks(moduleNbt); + if (!items.isEmpty()) tooltips.add(TooltipComponent.of(new BundleTooltipData(items, 0))); component.tooltip(tooltips); return Pair.of(component, SwitchyUIPosition.GRID_RIGHT); } - public record PreviewConfig(Function icon, List valuePaths, @Nullable NbtPathArgumentType.NbtPath conditionPath) { + public record PreviewConfig(Function icon, List valuePaths, List inventoryPaths, @Nullable NbtPathArgumentType.NbtPath conditionPath) { public List getValues(NbtCompound nbt) { return valuePaths.stream().map(v -> { try { @@ -81,6 +78,18 @@ public List getValues(NbtCompound nbt) { }).toList(); } + public DefaultedList getStacks(NbtCompound nbt) { + DefaultedList items = DefaultedList.of(); + inventoryPaths.forEach(v -> { + try { + DefaultedList inv = DefaultedList.ofSize(255); + v.get(nbt).forEach(compound -> Inventories.readNbt((NbtCompound) compound, inv)); + inv.stream().filter(i -> !i.isEmpty()).forEach(items::add); + } catch (CommandSyntaxException | ClassCastException ignored) {} + }); + return items; + } + public boolean failsCondition(NbtCompound nbt) { if (conditionPath != null) { try {