From 83ba534386ad6538cd6434f136e18c90352778e3 Mon Sep 17 00:00:00 2001 From: Integer Limit <103940576+IntegerLimit@users.noreply.github.com> Date: Sat, 28 Dec 2024 15:41:08 +1100 Subject: [PATCH] Improve Hover Info --- .../betterp2p/AccessibleInfoWrapper.java | 2 + .../mixin/betterp2p/InfoListMixin.java | 11 ++- .../mixin/betterp2p/InfoWrapperMixin.java | 86 +++++++++++++++++-- .../resources/assets/nomilabs/lang/en_us.lang | 11 +++ 4 files changed, 100 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/nomiceu/nomilabs/integration/betterp2p/AccessibleInfoWrapper.java b/src/main/java/com/nomiceu/nomilabs/integration/betterp2p/AccessibleInfoWrapper.java index cb23868e..1dd5548c 100644 --- a/src/main/java/com/nomiceu/nomilabs/integration/betterp2p/AccessibleInfoWrapper.java +++ b/src/main/java/com/nomiceu/nomilabs/integration/betterp2p/AccessibleInfoWrapper.java @@ -7,4 +7,6 @@ public interface AccessibleInfoWrapper { void labs$calculateDistance(Vec3d playerPos); double labs$getDistance(); + + void labs$setConnectionAmt(int amt); } diff --git a/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/InfoListMixin.java b/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/InfoListMixin.java index a879e19c..5d42ce2a 100644 --- a/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/InfoListMixin.java +++ b/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/InfoListMixin.java @@ -90,17 +90,20 @@ public abstract class InfoListMixin implements AccessibleInfoList { } // Search for same freq, and opposite output state (output search for input, etc.) - info.setError(!labs$has(info.getFrequency(), !info.getOutput())); + int connections = labs$amount(info.getFrequency(), !info.getOutput()); + info.setError(connections == 0); + ((AccessibleInfoWrapper) (Object) info).labs$setConnectionAmt(connections); } } @Unique - private boolean labs$has(int freq, boolean isOutput) { + private int labs$amount(int freq, boolean isOutput) { + int amount = 0; for (InfoWrapper info : labs$getThis().getSorted()) { if (info.getFrequency() == freq && info.getOutput() == isOutput) - return true; + amount++; } - return false; + return amount; } @Inject(method = "refresh", diff --git a/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/InfoWrapperMixin.java b/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/InfoWrapperMixin.java index c0c6997c..94b3b95e 100644 --- a/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/InfoWrapperMixin.java +++ b/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/InfoWrapperMixin.java @@ -3,10 +3,7 @@ import net.minecraft.util.math.Vec3d; import net.minecraft.util.text.TextFormatting; -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.*; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @@ -14,9 +11,11 @@ import com.nomiceu.nomilabs.integration.betterp2p.AccessibleInfoWrapper; import com.nomiceu.nomilabs.util.LabsTranslate; +import com.projecturanus.betterp2p.BetterP2P; import com.projecturanus.betterp2p.client.gui.InfoWrapper; import com.projecturanus.betterp2p.network.data.P2PInfo; import com.projecturanus.betterp2p.network.data.P2PLocation; +import com.projecturanus.betterp2p.util.p2p.TunnelInfo; /** * Allows saving of each P2P's distance to the player, and improves display of frequencies. @@ -31,6 +30,14 @@ public class InfoWrapperMixin implements AccessibleInfoWrapper { @Shadow private short frequency; + @Shadow + @Final + @Mutable + private String description; + + @Unique + private int labs$connectionInfoIndex = 0; + @Unique private double labs$distanceToPlayer = 0.0; @@ -69,12 +76,79 @@ public class InfoWrapperMixin implements AccessibleInfoWrapper { labs$distanceToPlayer = Math.round(distance * 10) / 10.0; } + @Unique + @Override + public void labs$setConnectionAmt(int amt) { + var hover = labs$getThis().getHoverInfo(); + if (hover.size() > labs$connectionInfoIndex) + hover.remove(labs$connectionInfoIndex); + + StringBuilder builder = new StringBuilder("nomilabs.gui.advanced_memory_card.hover_info.connections."); + if (labs$getThis().getOutput()) + builder.append("output."); + else + builder.append("input."); + + switch (amt) { + case 0 -> builder.append("none"); + case 1 -> builder.append("one"); + default -> builder.append("multi"); + } + + hover.add(LabsTranslate.translate(builder.toString(), amt)); + } + + @Inject(method = "setFrequency", at = @At("HEAD"), cancellable = true) + private void cancelChangeTooltip(short value, CallbackInfo ci) { + // In the actual method, hoverInfo's bound/unbound state is updated + // But this shouldn't happen in the first place + frequency = value; + ci.cancel(); + } + @Inject(method = "", at = @At(value = "TAIL")) private void provideChannelInfo(P2PInfo info, CallbackInfo ci) { + // Change existing description (allow localization) + TunnelInfo p2pType = BetterP2P.proxy.getP2PFromIndex(info.getType()); + if (p2pType == null) return; // This should never happen + + description = LabsTranslate.translate("nomilabs.gui.advanced_memory_card.info.type", p2pType.getDispName(), + LabsTranslate.translate(info.getOutput() ? "gui.advanced_memory_card.p2p_status.output" : + "gui.advanced_memory_card.p2p_status.input")); + + // Custom Hover Info + var hover = labs$getThis().getHoverInfo(); + hover.clear(); + + // Title + hover.add(LabsTranslate.translate("nomilabs.gui.advanced_memory_card.hover_info.title", + p2pType.getDispName())); + + // General Information + hover.add(TextFormatting.YELLOW + LabsTranslate.translate("gui.advanced_memory_card.pos", + info.getPos().getX(), info.getPos().getY(), info.getPos().getZ())); + hover.add(TextFormatting.YELLOW + LabsTranslate.translate("gui.advanced_memory_card.side", + info.getFacing().getName())); + hover.add(TextFormatting.YELLOW + LabsTranslate.translate("gui.advanced_memory_card.dim", info.getDim())); + + // ME Tunnel Specific var channels = labs$getThis().getChannels(); if (channels != null) - // Index 0-3: Default Info, 4+, Bound/Unbound, Offline/Online - labs$getThis().getHoverInfo().add(4, TextFormatting.LIGHT_PURPLE + channels); + labs$getThis().getHoverInfo().add(TextFormatting.LIGHT_PURPLE + channels); + + // State + if (frequency == 0) { + hover.add(TextFormatting.RED + LabsTranslate.translate("gui.advanced_memory_card.p2p_status.unbound")); + } else { + hover.add(TextFormatting.GREEN + LabsTranslate.translate("gui.advanced_memory_card.p2p_status.bound")); + } + + if (!info.getHasChannel()) { + hover.add("§c" + LabsTranslate.translate("gui.advanced_memory_card.p2p_status.offline")); + } + + // Init connection index, so we can update it when check applied + labs$connectionInfoIndex = hover.size(); } @Inject(method = "getFreqDisplay", at = @At("HEAD"), cancellable = true) diff --git a/src/main/resources/assets/nomilabs/lang/en_us.lang b/src/main/resources/assets/nomilabs/lang/en_us.lang index 46b2bbc6..cf8a1c91 100644 --- a/src/main/resources/assets/nomilabs/lang/en_us.lang +++ b/src/main/resources/assets/nomilabs/lang/en_us.lang @@ -161,6 +161,17 @@ nomilabs.gui.better_p2p.output=Output nomilabs.gui.better_p2p.error.same_output=P2P is already an {}. nomilabs.gui.advanced_memory_card.info.dist=Distance: %sm +nomilabs.gui.advanced_memory_card.info.type=Type: %s - %s + +nomilabs.gui.advanced_memory_card.hover_info.title=§bP2P - %s + +nomilabs.gui.advanced_memory_card.hover_info.connections.input.none=§cNo Inputs +nomilabs.gui.advanced_memory_card.hover_info.connections.input.one=§a1 Input +nomilabs.gui.advanced_memory_card.hover_info.connections.input.multi=§b%s Inputs + +nomilabs.gui.advanced_memory_card.hover_info.connections.output.none=§cNo Outputs +nomilabs.gui.advanced_memory_card.hover_info.connections.output.one=§a%s Output +nomilabs.gui.advanced_memory_card.hover_info.connections.output.multi=§b%s Outputs gui.advanced_memory_card.mode.add_as_input=Current Mode: Add as Input nomilabs.gui.advanced_memory_card.mode.add_input.desc.1=Before binding, the §aselected P2P§7 has its connections removed.