Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,15 @@ public static Optional<TotalDebugClient> 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();
Comment on lines +198 to 200

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Republish translated pack titles after language reloads

When the edited language file overrides a translation used by a built-in or mod resource pack's translatable title, the quick reload changes pack.getTitle().getString(), which PackStacks.of publishes to Companion. This path only recaptures the catalog, so Companion keeps the old pack title until an unrelated full reload or reconnect; republish the lightweight pack stack here while continuing to skip the expensive resource snapshot.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 1e1fa19: languageReloaded republishes the pack stacks too, which is cheap; only the resource snapshot is skipped.

this.packStacks.republish();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,10 @@ private static CompletableFuture<Void> reloadResources(String managedPack, Set<R
// As Options.updateResourcePacks saves them, which would also start a reload of its own.
if (enabled) ResourcePackEdits.save(minecraft);
if (!kinds.contains(ReloadPayload.Kind.RESOURCES) && !enabled && quickly(managedPack, kinds, watched)) {
// A full reload tells the catalog through its reload listener; these quick ones do not.
TotalDebugClient.current().ifPresent(TotalDebugClient::resourcesReloaded);
// A full reload tells the catalog through its reload listener; these quick ones do not. Textures put in place
// change icons; the language alone changes names only.
TotalDebugClient.current().ifPresent(kinds.contains(ReloadPayload.Kind.TEXTURES)
? TotalDebugClient::resourcesReloaded : TotalDebugClient::languageReloaded);
return CompletableFuture.completedFuture(null);
}
return reloadAll(minecraft);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public static void read(ScriptTarget target, ScriptFacts facts) {
facts.guarded("Entity state", () -> 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));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -34,6 +36,32 @@ public final class SideReader {
record Group<V>(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<Tank> 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) {
Expand All @@ -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<Group<Exposed>> 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<Group<Exposed>> 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. */
Expand All @@ -71,9 +100,29 @@ static void writeItems(Group<List<Slot>> group, ScriptFacts.Section section, int
}
}

private static void report(List<Group<String>> groups, String section, ScriptFacts facts) {
if (groups.size() == 1 && groups.getFirst().view().equals(NOT_EXPOSED)) return;
for (Group<String> 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<Group<Exposed>> 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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Handle failed side reads before dividing by group count

When any fluid or energy capability lookup throws, StorageReader.sided catches that failure and leaves groups empty before calling this method. The resulting division by zero adds a second, misleading ArithmeticException to the inspection after the original capability error, contrary to the intended behavior of preserving unsided contents with one reported side-read problem. Return immediately when the group list is empty, or skip write after the guarded read fails.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 1e1fa19: write returns at once for no groups; tested, and the test fails without it.

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<Exposed> 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 <T, V> List<Group<V>> groups(ScriptTarget.PlacedBlock block, BlockCapability<T, Direction> capability,
Expand Down Expand Up @@ -138,13 +187,15 @@ private static boolean sameSlots(List<Slot> first, List<Slot> 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<String> 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<Tank> tanks = new ArrayList<>();
List<String> access = new ArrayList<>();
int empty = 0;
for (int tank = 0; tank < Math.min(handler.getTanks(), StorageReader.MAX_TANKS); tank++) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve the actual tank count before capping side views

For a side handler exposing more than MAX_TANKS, this loop discards every tank after the first 32 before grouping and truncation reporting. In the common side-only case there are two groups, so perGroup is 62 and the later warning sees a 32-element list as complete: the omitted tanks are silently hidden, and faces differing only after tank 32 can be merged. Retain the handler's total tank count so the output reports the cap and grouping can distinguish truncated views.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not changing this one: a side exposing more than 32 tanks is the same cap StorageReader has always applied to tanks, and the inspection already shows at most 32 per handler. Faces that differ only after tank 32 are not a case worth more code.

FluidStack fluid = handler.getFluidInTank(tank);
tanks.add(new Tank(fluid.copy(), handler.getTankCapacity(tank)));
if (fluid.isEmpty()) {
empty++;
continue;
Expand All @@ -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. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,18 @@
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.
*/
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>(T handler, Direction face) {
}

private StorageReader() {
}

Expand All @@ -62,10 +57,9 @@ private static void readBlock(ScriptTarget.PlacedBlock block, ScriptFacts facts)
facts.section("Items").text("Contents", pending);
return;
}
Found<IItemHandler> found = contents(block, Capabilities.ItemHandler.BLOCK, IItemHandler::getSlots);
List<SideReader.Group<List<SideReader.Slot>>> 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.
Expand All @@ -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<List<SideReader.Group<SideReader.Exposed>>> read, Runnable contents,
ScriptFacts facts) {
List<SideReader.Group<SideReader.Exposed>> sides = read.get();
if (sides.size() == 1) contents.run();
SideReader.write(sides, section, facts);
}

private static void readEntity(Entity entity, ScriptFacts facts) {
Expand All @@ -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 <T> Found<T> contents(ScriptTarget.PlacedBlock block, BlockCapability<T, Direction> capability,
ToIntFunction<T> size) {
T unsided = handler(block, capability, null);
if (unsided != null) return new Found<>(unsided, null);
Found<T> 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> T handler(ScriptTarget.PlacedBlock block, BlockCapability<T, Direction> capability, Direction face) {
if (capability == Capabilities.ItemHandler.BLOCK && otherChestHalfUnloaded(block.level(), block.pos(), block.state())) {
Expand All @@ -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> T through(ScriptTarget.PlacedBlock block, String section, Found<T> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Direction, SideReader.Exposed> 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));
Expand Down