Skip to content

Commit

Permalink
[Forge-1.20.1] p2p frequency tooltip fix (#8125)
Browse files Browse the repository at this point in the history
This Pull Request contains two changes:
- Fix memory card tooltip not showing P2P frequency.
- Backport of the colorful frequency format. Show the P2P frequency in
tooltips using the colors used on the P2P tunnels back.
  • Loading branch information
LittleCircleOO authored Dec 7, 2024
1 parent 292600b commit a60077f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package appeng.integration.modules.igtooltip.parts;

import net.minecraft.ChatFormatting;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.Component;
Expand Down Expand Up @@ -45,10 +46,10 @@ public void buildTooltip(P2PTunnelPart object, TooltipContext context, TooltipBu
var freq = serverData.getShort(TAG_P2P_FREQUENCY);

// Show the frequency and name of the frequency if it exists
var freqTooltip = Platform.p2p().toHexString(freq);
var freqTooltip = Platform.p2p().toColoredHexString(freq).withStyle(ChatFormatting.BOLD);
if (serverData.contains(TAG_P2P_FREQUENCY_NAME, Tag.TAG_STRING)) {
var freqName = serverData.getString(TAG_P2P_FREQUENCY_NAME);
freqTooltip = freqName + " (" + freqTooltip + ")";
freqTooltip = Component.literal(freqName).append(" (").append(freqTooltip).append(")");
}

tooltip.addLine(InGameTooltip.P2PFrequency.text(freqTooltip));
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/appeng/items/tools/MemoryCardItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import appeng.api.util.IConfigurableObject;
import appeng.core.AELog;
import appeng.core.localization.GuiText;
import appeng.core.localization.InGameTooltip;
import appeng.core.localization.PlayerMessages;
import appeng.core.localization.Tooltips;
import appeng.helpers.IConfigInvHost;
Expand Down Expand Up @@ -288,11 +289,11 @@ public void appendHoverText(ItemStack stack, Level level, List<Component> lines,
lines.add(Tooltips.of(Component.translatable(tooltipKey)));
}

if (data.contains("freq")) {
final short freq = data.getShort("freq");
final String freqTooltip = ChatFormatting.BOLD + Platform.p2p().toHexString(freq);
if (data.contains("p2pFreq")) {
final short freq = data.getShort("p2pFreq");
var freqTooltip = Platform.p2p().toColoredHexString(freq).withStyle(ChatFormatting.BOLD);

lines.add(Tooltips.of(Component.translatable("gui.tooltips.ae2.P2PFrequency", freqTooltip)));
lines.add(Tooltips.of(Component.translatable(InGameTooltip.P2PFrequency.getTranslationKey(), freqTooltip)));
}
}

Expand Down
25 changes: 24 additions & 1 deletion src/main/java/appeng/util/helpers/P2PHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

import com.google.common.base.Preconditions;

import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;

import appeng.api.util.AEColor;

public class P2PHelper {
Expand All @@ -28,14 +31,18 @@ public AEColor[] toColors(short frequency) {
final AEColor[] colors = new AEColor[4];

for (int i = 0; i < 4; i++) {
int nibble = frequency >> 4 * (3 - i) & 0xF;
int nibble = getFrequencyNibble(frequency, i);

colors[i] = AEColor.values()[nibble];
}

return colors;
}

private static int getFrequencyNibble(short frequency, int i) {
return frequency >> 4 * (3 - i) & 0xF;
}

public short fromColors(AEColor[] colors) {
Preconditions.checkArgument(colors.length == 4);

Expand All @@ -58,4 +65,20 @@ public String toHexString(short frequency) {
return String.format("%04X", frequency);
}

private static final String[] HEX_DIGITS = {
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"
};

public MutableComponent toColoredHexString(short frequency) {
var parent = Component.empty();

for (var i = 0; i < 4; i++) {
var nibble = getFrequencyNibble(frequency, i);
var hex = Component.literal(HEX_DIGITS[nibble]);
parent.append(hex.setStyle(hex.getStyle().withColor(AEColor.values()[nibble].mediumVariant)));
}

return parent;
}

}

0 comments on commit a60077f

Please sign in to comment.