Moved text formatting methods to TextFormatUtils.

This commit is contained in:
Florian Nücke
2022-01-13 10:44:30 +01:00
parent 547d28aa45
commit a4549ab212
5 changed files with 35 additions and 23 deletions

View File

@@ -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<T extends AbstractMachineTerminalContainer> extends AbstractModContainerScreen<T> {
@@ -88,8 +88,8 @@ public abstract class AbstractMachineInventoryScreen<T extends AbstractMachineTe
if (isMouseOver(mouseX, mouseY, -Sprites.SIDEBAR_2.width + 4, ENERGY_TOP + 4, Sprites.ENERGY_BAR.width, Sprites.ENERGY_BAR.height)) {
final List<? extends FormattedText> 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);
}

View File

@@ -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<T extends AbstractMachineTerminalContainer> extends AbstractModContainerScreen<T> {
@@ -63,8 +63,8 @@ public abstract class AbstractMachineTerminalScreen<T extends AbstractMachineTer
if (isMouseOver(mouseX, mouseY, -Sprites.SIDEBAR_2.width + 4, ENERGY_TOP + 4, Sprites.ENERGY_BAR.width, Sprites.ENERGY_BAR.height)) {
final List<? extends FormattedText> 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);
}

View File

@@ -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);
///////////////////////////////////////////////////////////////////

View File

@@ -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);
}
}

View File

@@ -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<Item> tag = net.minecraft.tags.ItemTags.getAllTags().getTag(ItemTags.DEVICE_NEEDS_REBOOT.getName());
final Tag<Item> 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<Component> 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() {