Skip to content

Commit

Permalink
allow specifying inventory keys
Browse files Browse the repository at this point in the history
  • Loading branch information
sisby-folk committed Aug 3, 2023
1 parent 248795b commit e3d3de5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -67,7 +68,7 @@ protected void apply(Map<Identifier, JsonElement> prepared, ResourceManager mana
SwitchyCardinalClient.LOGGER.warn("[Switchy Cardinal UI] module '{}' is missing options, skipping...", moduleId);
return;
}
List<NbtPathArgumentType.NbtPath> valuePaths = new ArrayList<>();

try {
Function<NbtCompound, ItemStack> iconStackSupplier;
JsonObject icon = moduleOptions.get(KEY_ICON).getAsJsonObject();
Expand All @@ -91,12 +92,25 @@ protected void apply(Map<Identifier, JsonElement> prepared, ResourceManager mana
iconStackSupplier = nbt -> stack;
}

List<NbtPathArgumentType.NbtPath> 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<NbtPathArgumentType.NbtPath> 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;
}
}
Expand All @@ -112,7 +126,7 @@ protected void apply(Map<Identifier, JsonElement> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,14 @@ public Pair<Component, SwitchyUIPosition> getPreviewComponent(String presetName)
ItemComponent component = Components.item(config.icon.apply(moduleNbt));
List<TooltipComponent> 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<ItemStack> inv = DefaultedList.ofSize(255);
Inventories.readNbt(moduleNbt, inv);
DefaultedList<ItemStack> items = DefaultedList.of();
inv.stream().filter(i -> !i.isEmpty()).forEach(items::add);
if (!inv.isEmpty()) tooltips.add(TooltipComponent.of(new BundleTooltipData(items, 0)));
DefaultedList<ItemStack> 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<NbtCompound, ItemStack> icon, List<NbtPathArgumentType.NbtPath> valuePaths, @Nullable NbtPathArgumentType.NbtPath conditionPath) {
public record PreviewConfig(Function<NbtCompound, ItemStack> icon, List<NbtPathArgumentType.NbtPath> valuePaths, List<NbtPathArgumentType.NbtPath> inventoryPaths, @Nullable NbtPathArgumentType.NbtPath conditionPath) {
public List<MutableText> getValues(NbtCompound nbt) {
return valuePaths.stream().map(v -> {
try {
Expand All @@ -81,6 +78,18 @@ public List<MutableText> getValues(NbtCompound nbt) {
}).toList();
}

public DefaultedList<ItemStack> getStacks(NbtCompound nbt) {
DefaultedList<ItemStack> items = DefaultedList.of();
inventoryPaths.forEach(v -> {
try {
DefaultedList<ItemStack> 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 {
Expand Down

0 comments on commit e3d3de5

Please sign in to comment.