diff --git a/mod/src/main/java/com/github/minecraft_ta/totaldebug/client/TotalDebugClient.java b/mod/src/main/java/com/github/minecraft_ta/totaldebug/client/TotalDebugClient.java index 704da092e..74e1d28e0 100644 --- a/mod/src/main/java/com/github/minecraft_ta/totaldebug/client/TotalDebugClient.java +++ b/mod/src/main/java/com/github/minecraft_ta/totaldebug/client/TotalDebugClient.java @@ -187,6 +187,15 @@ public static Optional current() { */ public void resourcesReloaded() { this.resources.resourcesChanged(); + languageReloaded(); + } + + /** + * The language was reloaded alone, as Companion's quick reload of a language file does. Only translated names + * changed, which the catalog and the packs' titles hold; the snapshot's models and textures did not, and capturing + * them again would take seconds. Client thread only. + */ + public void languageReloaded() { this.catalogs.recapture(); this.companionApp.announceInventory(); this.packStacks.republish(); diff --git a/mod/src/main/java/com/github/minecraft_ta/totaldebug/client/resource/ResourceReloads.java b/mod/src/main/java/com/github/minecraft_ta/totaldebug/client/resource/ResourceReloads.java index a967908a3..c857ced03 100644 --- a/mod/src/main/java/com/github/minecraft_ta/totaldebug/client/resource/ResourceReloads.java +++ b/mod/src/main/java/com/github/minecraft_ta/totaldebug/client/resource/ResourceReloads.java @@ -109,8 +109,10 @@ private static CompletableFuture reloadResources(String managedPack, Set EntityReader.read(target, facts)); facts.guarded("Block state", () -> BlockStateReader.read(target, facts)); StorageReader.read(target, facts); - SideReader.read(target, facts); facts.guarded("Capabilities", () -> CapabilityReader.read(target, facts)); facts.guarded("NBT", () -> NbtReader.read(target, facts)); } diff --git a/mod/src/main/java/com/github/minecraft_ta/totaldebug/inspection/SideReader.java b/mod/src/main/java/com/github/minecraft_ta/totaldebug/inspection/SideReader.java index e3b48f522..cabf64534 100644 --- a/mod/src/main/java/com/github/minecraft_ta/totaldebug/inspection/SideReader.java +++ b/mod/src/main/java/com/github/minecraft_ta/totaldebug/inspection/SideReader.java @@ -1,5 +1,6 @@ package com.github.minecraft_ta.totaldebug.inspection; +import com.github.minecraft_ta.totaldebug.protocol.execution.FactSection; import com.github.minecraft_ta.totaldebug.script.ScriptFacts; import com.github.minecraft_ta.totaldebug.script.ScriptTarget; import net.minecraft.core.Direction; @@ -10,6 +11,7 @@ import net.neoforged.neoforge.fluids.FluidStack; import net.neoforged.neoforge.fluids.capability.IFluidHandler; import net.neoforged.neoforge.items.IItemHandler; +import java.text.NumberFormat; import java.util.ArrayList; import java.util.EnumMap; import java.util.List; @@ -34,6 +36,32 @@ public final class SideReader { record Group(String label, V view) { } + /** + * What a side exposes of fluids or energy: its tanks, each with its fluid and capacity, or the energy's amounts, such + * as {@code 1,200 / 10,000 FE: takes}, and what it does, such as {@code Water: takes, 1 empty tank}. Sides group by + * all of it, so faces of separate storages stay apart. + */ + record Exposed(List tanks, String amounts, String access) { + static final Exposed NONE = new Exposed(List.of(), NOT_EXPOSED, NOT_EXPOSED); + + boolean same(Exposed other) { + if (!this.amounts.equals(other.amounts) || !this.access.equals(other.access) || this.tanks.size() != other.tanks.size()) { + return false; + } + for (int index = 0; index < this.tanks.size(); index++) { + if (!this.tanks.get(index).same(other.tanks.get(index))) return false; + } + return true; + } + } + + /** A tank as a side exposes it: its fluid, which may be empty, and its capacity. */ + record Tank(FluidStack fluid, int capacity) { + boolean same(Tank other) { + return FluidStack.matches(this.fluid, other.fluid) && this.capacity == other.capacity; + } + } + /** A slot as a side exposes it; {@code takes} and {@code gives} are null where its state cannot tell. */ record Slot(ItemStack stack, Boolean takes, Boolean gives) { boolean same(Slot other) { @@ -45,13 +73,14 @@ boolean same(Slot other) { private SideReader() { } - /** The rows for a block's fluids and energy, after their contents. Items are reported with their contents. */ - public static void read(ScriptTarget target, ScriptFacts facts) { - if (!(target instanceof ScriptTarget.PlacedBlock block)) return; - facts.guarded("Fluids", () -> report(groups(block, Capabilities.FluidHandler.BLOCK, SideReader::fluids, - NOT_EXPOSED, String::equals), "Fluids", facts)); - facts.guarded("Energy", () -> report(groups(block, Capabilities.EnergyStorage.BLOCK, SideReader::energy, - NOT_EXPOSED, String::equals), "Energy", facts)); + /** The fluids the block exposes without a side and through each face, grouped; one group when all sides are alike. */ + static List> fluids(ScriptTarget.PlacedBlock block) { + return groups(block, Capabilities.FluidHandler.BLOCK, SideReader::fluids, Exposed.NONE, Exposed::same); + } + + /** The energy the block exposes without a side and through each face, grouped; one group when all sides are alike. */ + static List> energy(ScriptTarget.PlacedBlock block) { + return groups(block, Capabilities.EnergyStorage.BLOCK, SideReader::energy, Exposed.NONE, Exposed::same); } /** The slots the block exposes without a side and through each face, grouped; one group when all sides are alike. */ @@ -71,9 +100,29 @@ static void writeItems(Group> group, ScriptFacts.Section section, int } } - private static void report(List> groups, String section, ScriptFacts facts) { - if (groups.size() == 1 && groups.getFirst().view().equals(NOT_EXPOSED)) return; - for (Group group : groups) facts.section(section).text(group.label(), group.view()); + /** + * Writes the rows of fluids or energy after their contents: when all sides are alike, what they do, the contents + * above being theirs; otherwise each group's own tanks or amounts and what it does, as no one side's contents are the + * block's. The groups share the section's facts, as slots do. + */ + static void write(List> groups, String section, ScriptFacts facts) { + if (groups.size() == 1) { + String access = groups.getFirst().view().access(); + if (!access.equals(NOT_EXPOSED)) facts.section(section).text(groups.getFirst().label(), access); + return; + } + int perGroup = Math.max(1, FactSection.MAX_FACTS / groups.size() - 2); + ScriptFacts.Section rows = facts.section(section); + if (groups.stream().anyMatch(group -> group.view().tanks().size() > perGroup)) { + rows.text("Tanks", "first " + perGroup + " of each side shown"); + } + for (Group group : groups) { + Exposed view = group.view(); + for (Tank tank : view.tanks().subList(0, Math.min(perGroup, view.tanks().size()))) { + rows.fluid(group.label(), tank.fluid(), tank.capacity()); + } + rows.text(group.label(), view.tanks().isEmpty() ? view.amounts() : view.access()); + } } private static List> groups(ScriptTarget.PlacedBlock block, BlockCapability capability, @@ -138,13 +187,15 @@ private static boolean sameSlots(List first, List second) { return true; } - /** Each tank's fluid and what it does, such as {@code Water: takes and gives}, then the empty tanks. */ - private static String fluids(IFluidHandler handler) { - if (handler.getTanks() == 0) return NOT_EXPOSED; - List tanks = new ArrayList<>(); + /** Each tank with its fluid and capacity, and what each does, such as {@code Water: takes}, then the empty tanks. */ + private static Exposed fluids(IFluidHandler handler) { + if (handler.getTanks() == 0) return Exposed.NONE; + List tanks = new ArrayList<>(); + List access = new ArrayList<>(); int empty = 0; for (int tank = 0; tank < Math.min(handler.getTanks(), StorageReader.MAX_TANKS); tank++) { FluidStack fluid = handler.getFluidInTank(tank); + tanks.add(new Tank(fluid.copy(), handler.getTankCapacity(tank))); if (fluid.isEmpty()) { empty++; continue; @@ -153,19 +204,26 @@ private static String fluids(IFluidHandler handler) { Boolean takes = fluid.getAmount() >= handler.getTankCapacity(tank) ? null : handler.fill(fluid.copyWithAmount(Integer.MAX_VALUE), IFluidHandler.FluidAction.SIMULATE) > 0; boolean gives = !handler.drain(fluid.copyWithAmount(Integer.MAX_VALUE), IFluidHandler.FluidAction.SIMULATE).isEmpty(); - tanks.add(fluid.getHoverName().getString() + ": " + access(takes, gives).toLowerCase(Locale.ROOT)); + access.add(fluid.getHoverName().getString() + ": " + access(takes, gives).toLowerCase(Locale.ROOT)); } - if (empty > 0) tanks.add(empty + (empty == 1 ? " empty tank" : " empty tanks")); - return String.join(", ", tanks); + if (empty > 0) access.add(empty + (empty == 1 ? " empty tank" : " empty tanks")); + return new Exposed(tanks, "", String.join(", ", access)); } /** Simulates with the largest amount, since storages converting units round a small amount down to nothing. */ - private static String energy(IEnergyStorage storage) { + private static Exposed energy(IEnergyStorage storage) { int stored = storage.getEnergyStored(); - if (storage.getMaxEnergyStored() <= 0) return "No capacity"; + if (storage.getMaxEnergyStored() <= 0) return new Exposed(List.of(), "No capacity", "No capacity"); Boolean takes = stored >= storage.getMaxEnergyStored() ? null : storage.receiveEnergy(Integer.MAX_VALUE, true) > 0; Boolean gives = stored == 0 ? null : storage.extractEnergy(Integer.MAX_VALUE, true) > 0; - return access(takes, gives); + String access = access(takes, gives); + return new Exposed(List.of(), amounts(stored, storage.getMaxEnergyStored(), "FE") + ": " + access.toLowerCase(Locale.ROOT), access); + } + + /** An amount of a capacity, such as {@code 4,000 / 8,000 mB}. */ + static String amounts(long amount, long capacity, String unit) { + NumberFormat format = NumberFormat.getIntegerInstance(); + return format.format(amount) + " / " + format.format(capacity) + " " + unit; } /** What something takes and gives in words; a part its state cannot tell is left out and the state named. */ diff --git a/mod/src/main/java/com/github/minecraft_ta/totaldebug/inspection/StorageReader.java b/mod/src/main/java/com/github/minecraft_ta/totaldebug/inspection/StorageReader.java index 047fac2f0..07080338c 100644 --- a/mod/src/main/java/com/github/minecraft_ta/totaldebug/inspection/StorageReader.java +++ b/mod/src/main/java/com/github/minecraft_ta/totaldebug/inspection/StorageReader.java @@ -22,12 +22,11 @@ import net.neoforged.neoforge.items.IItemHandler; import java.util.List; import java.util.function.Supplier; -import java.util.function.ToIntFunction; /** * Built-in reader for what a block or entity holds, through NeoForge's item, fluid and energy capabilities, read - * without a side. A block that only exposes a handler through its faces is read through the face exposing the most. - * When a block's faces expose its slots differently, the slots are shown per side as {@link SideReader} groups them. + * without a side, with what the sides do after it. When a block's faces expose its contents differently, each group of + * faces {@link SideReader} finds is shown with its own contents instead. * No chunk is loaded and containers whose loot is not generated yet are not read, because reading their slots would * generate it. Each part is read independently, so one failing handler does not hide the others. */ @@ -35,10 +34,6 @@ public final class StorageReader { public static final int MAX_SLOTS = 96; public static final int MAX_TANKS = 32; - /** A block's handler and the face it was read through, which is null for the handler without a side. */ - record Found(T handler, Direction face) { - } - private StorageReader() { } @@ -62,10 +57,9 @@ private static void readBlock(ScriptTarget.PlacedBlock block, ScriptFacts facts) facts.section("Items").text("Contents", pending); return; } - Found found = contents(block, Capabilities.ItemHandler.BLOCK, IItemHandler::getSlots); List>> sides = SideReader.items(block); if (sides.size() == 1) { - items(block.blockEntity(), () -> through(block, "Items", found, facts), facts); + items(block.blockEntity(), () -> handler(block, Capabilities.ItemHandler.BLOCK, null), facts); return; } // Each group shows its slots, the contents among them, and all groups share the section's facts. @@ -78,10 +72,21 @@ private static void readBlock(ScriptTarget.PlacedBlock block, ScriptFacts facts) SideReader.writeItems(group, section, perGroup); } }); - facts.guarded("Fluids", () -> fluids(through(block, "Fluids", - contents(block, Capabilities.FluidHandler.BLOCK, IFluidHandler::getTanks), facts), facts)); - facts.guarded("Energy", () -> energy(through(block, "Energy", - contents(block, Capabilities.EnergyStorage.BLOCK, IEnergyStorage::getMaxEnergyStored), facts), facts)); + facts.guarded("Fluids", () -> sided("Fluids", () -> SideReader.fluids(block), + () -> fluids(handler(block, Capabilities.FluidHandler.BLOCK, null), facts), facts)); + facts.guarded("Energy", () -> sided("Energy", () -> SideReader.energy(block), + () -> energy(handler(block, Capabilities.EnergyStorage.BLOCK, null), facts), facts)); + } + + /** + * The contents without a side and what the sides do, or where the sides differ, each one's rows with its own amounts, + * the block having no contents of its own to show. + */ + private static void sided(String section, Supplier>> read, Runnable contents, + ScriptFacts facts) { + List> sides = read.get(); + if (sides.size() == 1) contents.run(); + SideReader.write(sides, section, facts); } private static void readEntity(Entity entity, ScriptFacts facts) { @@ -107,24 +112,6 @@ private static void energyWithFlags(IEnergyStorage storage, ScriptFacts facts) { } } - /** - * The block's handler without a side, or when it has none, the handler of the face whose {@code size} is largest. - * The handler is null when no face exposes one either. - */ - static Found contents(ScriptTarget.PlacedBlock block, BlockCapability capability, - ToIntFunction size) { - T unsided = handler(block, capability, null); - if (unsided != null) return new Found<>(unsided, null); - Found best = new Found<>(null, null); - for (Direction face : Direction.values()) { - T handler = handler(block, capability, face); - if (handler != null && (best.handler() == null || size.applyAsInt(handler) > size.applyAsInt(best.handler()))) { - best = new Found<>(handler, face); - } - } - return best; - } - /** The block's handler through {@code face}; a double chest whose other half is not loaded has no item handler. */ static T handler(ScriptTarget.PlacedBlock block, BlockCapability capability, Direction face) { if (capability == Capabilities.ItemHandler.BLOCK && otherChestHalfUnloaded(block.level(), block.pos(), block.state())) { @@ -139,14 +126,6 @@ static String unreadableItems(ScriptTarget.PlacedBlock block) { return pending != null ? pending : otherChestHalfUnreadable(block.level(), block.pos(), block.state()); } - /** The found handler, noting the face it was read through when it is not the handler without a side. */ - private static T through(ScriptTarget.PlacedBlock block, String section, Found found, ScriptFacts facts) { - if (found.handler() != null && found.face() != null) { - facts.section(section).text("Read through", Faces.name(found.face(), Faces.facing(block.state()))); - } - return found.handler(); - } - /** * Reports the slots of {@code owner}'s item handler, unless {@code owner} is a container whose loot is not * generated yet; then the handler is never requested, because reading any slot would generate the loot. diff --git a/mod/src/test/java/com/github/minecraft_ta/totaldebug/inspection/SideReaderTest.java b/mod/src/test/java/com/github/minecraft_ta/totaldebug/inspection/SideReaderTest.java index 8704703da..dd51932e1 100644 --- a/mod/src/test/java/com/github/minecraft_ta/totaldebug/inspection/SideReaderTest.java +++ b/mod/src/test/java/com/github/minecraft_ta/totaldebug/inspection/SideReaderTest.java @@ -5,6 +5,7 @@ import java.util.EnumMap; import java.util.List; +import java.util.Locale; import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -54,6 +55,29 @@ void aBlockWithoutFacingUsesCompassNames() { ), SideReader.group("closed", faces, null, String::equals)); } + @Test + void facesOfSeparateStoragesThatDoTheSameStayApartWithTheirAmounts() { + SideReader.Exposed input = new SideReader.Exposed(List.of(), "1,000 / 8,000 FE: takes and gives", "Takes and gives"); + SideReader.Exposed output = new SideReader.Exposed(List.of(), "6,000 / 8,000 FE: takes and gives", "Takes and gives"); + Map faces = new EnumMap<>(Direction.class); + for (Direction face : Direction.values()) faces.put(face, face == Direction.DOWN ? output : input); + + assertEquals(List.of(SideReader.Exposed.NONE, input, output), + SideReader.group(SideReader.Exposed.NONE, faces, null, SideReader.Exposed::same).stream() + .map(SideReader.Group::view).toList()); + } + + @Test + void amountsAreGroupedNumbersOfTheirCapacity() { + Locale before = Locale.getDefault(); + Locale.setDefault(Locale.ROOT); + try { + assertEquals("4,000 / 8,000 mB", SideReader.amounts(4_000, 8_000, "mB")); + } finally { + Locale.setDefault(before); + } + } + @Test void accessNamesWhatTheStateCannotTell() { assertEquals("Takes and gives", SideReader.access(true, true));