Skip to content

Commit

Permalink
Show the P2P frequency in tooltips using the colors used on the P2P t…
Browse files Browse the repository at this point in the history
…unnels back (#7922)
  • Loading branch information
shartte authored Jun 16, 2024
1 parent 6ecddfb commit 952a66c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 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
2 changes: 1 addition & 1 deletion src/main/java/appeng/items/tools/MemoryCardItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public void appendHoverText(ItemStack stack, TooltipContext context, List<Compon

var p2pFreq = stack.get(AEComponents.EXPORTED_P2P_FREQUENCY);
if (p2pFreq != null) {
final String freqTooltip = ChatFormatting.BOLD + Platform.p2p().toHexString(p2pFreq);
var freqTooltip = Platform.p2p().toColoredHexString(p2pFreq).withStyle(ChatFormatting.BOLD);
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);
parent.append(Component.literal(HEX_DIGITS[nibble])
.withColor(AEColor.values()[nibble].whiteVariant));
}

return parent;
}

}

0 comments on commit 952a66c

Please sign in to comment.