diff --git a/src/main/java/li/cil/oc2/client/gui/AbstractMachineInventoryScreen.java b/src/main/java/li/cil/oc2/client/gui/AbstractMachineInventoryScreen.java index e629f202..c80c8c6c 100644 --- a/src/main/java/li/cil/oc2/client/gui/AbstractMachineInventoryScreen.java +++ b/src/main/java/li/cil/oc2/client/gui/AbstractMachineInventoryScreen.java @@ -19,7 +19,7 @@ import net.minecraftforge.api.distmarker.OnlyIn; import java.util.List; import static java.util.Arrays.asList; -import static li.cil.oc2.common.util.TooltipUtils.withColor; +import static li.cil.oc2.common.util.TextFormatUtils.withFormat; @OnlyIn(Dist.CLIENT) public abstract class AbstractMachineInventoryScreen extends AbstractModContainerScreen { @@ -88,8 +88,8 @@ public abstract class AbstractMachineInventoryScreen tooltip = asList( - new TranslatableComponent(Constants.TOOLTIP_ENERGY, withColor(energyStored + "/" + energyCapacity, ChatFormatting.GREEN)), - new TranslatableComponent(Constants.TOOLTIP_ENERGY_CONSUMPTION, withColor(String.valueOf(energyConsumption), ChatFormatting.GREEN)) + new TranslatableComponent(Constants.TOOLTIP_ENERGY, withFormat(energyStored + "/" + energyCapacity, ChatFormatting.GREEN)), + new TranslatableComponent(Constants.TOOLTIP_ENERGY_CONSUMPTION, withFormat(String.valueOf(energyConsumption), ChatFormatting.GREEN)) ); TooltipUtils.drawTooltip(stack, tooltip, mouseX, mouseY, 200); } diff --git a/src/main/java/li/cil/oc2/client/gui/AbstractMachineTerminalScreen.java b/src/main/java/li/cil/oc2/client/gui/AbstractMachineTerminalScreen.java index e2d1448e..83836484 100644 --- a/src/main/java/li/cil/oc2/client/gui/AbstractMachineTerminalScreen.java +++ b/src/main/java/li/cil/oc2/client/gui/AbstractMachineTerminalScreen.java @@ -22,7 +22,7 @@ import net.minecraftforge.api.distmarker.OnlyIn; import java.util.List; import static java.util.Arrays.asList; -import static li.cil.oc2.common.util.TooltipUtils.withColor; +import static li.cil.oc2.common.util.TextFormatUtils.withFormat; @OnlyIn(Dist.CLIENT) public abstract class AbstractMachineTerminalScreen extends AbstractModContainerScreen { @@ -63,8 +63,8 @@ public abstract class AbstractMachineTerminalScreen tooltip = asList( - new TranslatableComponent(Constants.TOOLTIP_ENERGY, withColor(energyStored + "/" + energyCapacity, ChatFormatting.GREEN)), - new TranslatableComponent(Constants.TOOLTIP_ENERGY_CONSUMPTION, withColor(String.valueOf(energyConsumption), ChatFormatting.GREEN)) + new TranslatableComponent(Constants.TOOLTIP_ENERGY, withFormat(energyStored + "/" + energyCapacity, ChatFormatting.GREEN)), + new TranslatableComponent(Constants.TOOLTIP_ENERGY_CONSUMPTION, withFormat(String.valueOf(energyConsumption), ChatFormatting.GREEN)) ); TooltipUtils.drawTooltip(stack, tooltip, mouseX, mouseY, 200); } diff --git a/src/main/java/li/cil/oc2/common/item/NetworkInterfaceCardItem.java b/src/main/java/li/cil/oc2/common/item/NetworkInterfaceCardItem.java index edbcbd56..33fe0f69 100644 --- a/src/main/java/li/cil/oc2/common/item/NetworkInterfaceCardItem.java +++ b/src/main/java/li/cil/oc2/common/item/NetworkInterfaceCardItem.java @@ -22,12 +22,12 @@ import javax.annotation.Nullable; import java.util.Arrays; import java.util.List; -import static li.cil.oc2.common.util.TooltipUtils.withColor; +import static li.cil.oc2.common.util.TextFormatUtils.withFormat; import static li.cil.oc2.common.util.TranslationUtils.text; public final class NetworkInterfaceCardItem extends ModItem { private static final String SIDE_CONFIGURATION_TAG_NAME = "sides"; - private static final Component IS_CONFIGURED_TEXT = withColor(text("item.{mod}.network_interface_card.is_configured"), ChatFormatting.GREEN); + private static final Component IS_CONFIGURED_TEXT = withFormat(text("item.{mod}.network_interface_card.is_configured"), ChatFormatting.GREEN); /////////////////////////////////////////////////////////////////// diff --git a/src/main/java/li/cil/oc2/common/util/TextFormatUtils.java b/src/main/java/li/cil/oc2/common/util/TextFormatUtils.java index c42bbd39..31a6a6e2 100644 --- a/src/main/java/li/cil/oc2/common/util/TextFormatUtils.java +++ b/src/main/java/li/cil/oc2/common/util/TextFormatUtils.java @@ -1,5 +1,11 @@ package li.cil.oc2.common.util; +import net.minecraft.ChatFormatting; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.MutableComponent; +import net.minecraft.network.chat.TextColor; +import net.minecraft.network.chat.TextComponent; + public final class TextFormatUtils { private static final int SIZE_STEP = 1024; private static final String[] SIZE_FORMAT = {"%dB", "%dKB", "%dMB", "%dGB", "%dTB"}; @@ -12,4 +18,16 @@ public final class TextFormatUtils { } return String.format(SIZE_FORMAT[index], size); } + + public static MutableComponent withFormat(final String value, final ChatFormatting formatting) { + return withFormat(new TextComponent(value), formatting); + } + + public static MutableComponent withFormat(final MutableComponent text, final ChatFormatting formatting) { + return text.withStyle(s -> s.withColor(TextColor.fromLegacyFormat(formatting))); + } + + public static Component withFormat(final Component text, final ChatFormatting formatting) { + return new TextComponent("").withStyle(formatting).append(text); + } } diff --git a/src/main/java/li/cil/oc2/common/util/TooltipUtils.java b/src/main/java/li/cil/oc2/common/util/TooltipUtils.java index 0db75299..b1689d32 100644 --- a/src/main/java/li/cil/oc2/common/util/TooltipUtils.java +++ b/src/main/java/li/cil/oc2/common/util/TooltipUtils.java @@ -30,6 +30,8 @@ import java.util.ArrayList; import java.util.List; import static li.cil.oc2.common.Constants.*; +import static li.cil.oc2.common.util.TextFormatUtils.withFormat; +import static net.minecraft.tags.ItemTags.getAllTags; public final class TooltipUtils { private static final MutableComponent DEVICE_NEEDS_REBOOT = @@ -80,13 +82,13 @@ public final class TooltipUtils { final Language languagemap = Language.getInstance(); if (languagemap.has(translationKey)) { final TranslatableComponent description = new TranslatableComponent(translationKey); - tooltip.add(withColor(description, ChatFormatting.GRAY)); + tooltip.add(withFormat(description, ChatFormatting.GRAY)); } // Tooltips get queried very early in Minecraft initialization, meaning tags may not // have been initialized. Trying to directly use our tag would lead to an exception // in that case, so we do the detour through the collection instead. - final Tag tag = net.minecraft.tags.ItemTags.getAllTags().getTag(ItemTags.DEVICE_NEEDS_REBOOT.getName()); + final Tag tag = getAllTags().getTag(ItemTags.DEVICE_NEEDS_REBOOT.getName()); if (tag != null && tag.contains(stack.getItem())) { tooltip.add(DEVICE_NEEDS_REBOOT); } @@ -94,8 +96,8 @@ public final class TooltipUtils { final ItemDeviceQuery query = Devices.makeQuery(stack); final int energyConsumption = Devices.getEnergyConsumption(query); if (energyConsumption > 0) { - final MutableComponent energy = withColor(String.valueOf(energyConsumption), ChatFormatting.GREEN); - tooltip.add(withColor(new TranslatableComponent(Constants.TOOLTIP_ENERGY_CONSUMPTION, energy), ChatFormatting.GRAY)); + final MutableComponent energy = withFormat(String.valueOf(energyConsumption), ChatFormatting.GREEN); + tooltip.add(withFormat(new TranslatableComponent(Constants.TOOLTIP_ENERGY_CONSUMPTION, energy), ChatFormatting.GRAY)); } } @@ -143,25 +145,17 @@ public final class TooltipUtils { return; } - final MutableComponent value = withColor(energy.getEnergyStored() + "/" + energy.getMaxEnergyStored(), ChatFormatting.GREEN); - tooltip.add(withColor(new TranslatableComponent(Constants.TOOLTIP_ENERGY, value), ChatFormatting.GRAY)); + final MutableComponent value = withFormat(energy.getEnergyStored() + "/" + energy.getMaxEnergyStored(), ChatFormatting.GREEN); + tooltip.add(withFormat(new TranslatableComponent(Constants.TOOLTIP_ENERGY, value), ChatFormatting.GRAY)); }); } public static void addEnergyConsumption(final double value, final List tooltip) { if (value > 0) { - tooltip.add(withColor(new TranslatableComponent(Constants.TOOLTIP_ENERGY_CONSUMPTION, withColor(new DecimalFormat("#.##").format(value), ChatFormatting.GREEN)), ChatFormatting.GRAY)); + tooltip.add(withFormat(new TranslatableComponent(Constants.TOOLTIP_ENERGY_CONSUMPTION, withFormat(new DecimalFormat("#.##").format(value), ChatFormatting.GREEN)), ChatFormatting.GRAY)); } } - public static MutableComponent withColor(final String value, final ChatFormatting formatting) { - return withColor(new TextComponent(value), formatting); - } - - public static MutableComponent withColor(final MutableComponent text, final ChatFormatting formatting) { - return text.withStyle(s -> s.withColor(TextColor.fromLegacyFormat(formatting))); - } - /////////////////////////////////////////////////////////////////// private static String[] getDeviceTypeNames() {