Repository navigation
The mod's small items: fluids and energy per face, a language reload without the icon archive #145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 1.21.1
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
|
|
@@ -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<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) { | ||
|
|
@@ -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. */ | ||
|
|
@@ -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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When any fluid or energy capability lookup throws, Useful? React with 👍 / 👎.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 1e1fa19: |
||
| 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, | ||
|
|
@@ -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++) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For a side handler exposing more than Useful? React with 👍 / 👎.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| 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. */ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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(), whichPackStacks.ofpublishes 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 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 1e1fa19:
languageReloadedrepublishes the pack stacks too, which is cheap; only the resource snapshot is skipped.