From 78474aad66fe7134eb6e89ef76b547d9d7b49fad Mon Sep 17 00:00:00 2001 From: deirn Date: Tue, 23 Nov 2021 13:46:25 +0700 Subject: [PATCH] allow using custom name for energy bar --- .../api/provider/EnergyInfoProvider.java | 18 +++++++++++++++--- .../api/registry/MeganeClientRegistrar.java | 7 ++++++- .../component/block/EnergyComponent.java | 4 ++-- .../megane/runtime/registry/Registrar.java | 4 ++-- .../megane/runtime/renderer/BarRenderer.java | 2 +- 5 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/base/java/badasintended/megane/api/provider/EnergyInfoProvider.java b/src/base/java/badasintended/megane/api/provider/EnergyInfoProvider.java index 370d6e1..f594509 100644 --- a/src/base/java/badasintended/megane/api/provider/EnergyInfoProvider.java +++ b/src/base/java/badasintended/megane/api/provider/EnergyInfoProvider.java @@ -1,9 +1,14 @@ package badasintended.megane.api.provider; -public interface EnergyInfoProvider { +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.text.Text; - static EnergyInfoProvider of(int color, String unit) { - return new EnergyInfoProvider<>() { +@Environment(EnvType.CLIENT) +public interface EnergyInfoProvider { + + static EnergyInfoProvider of(int color, String unit, Text name) { + return new EnergyInfoProvider() { @Override public int getColor() { return color; @@ -13,6 +18,11 @@ public int getColor() { public String getUnit() { return unit; } + + @Override + public Text getName() { + return name; + } }; } @@ -20,4 +30,6 @@ public String getUnit() { String getUnit(); + Text getName(); + } diff --git a/src/base/java/badasintended/megane/api/registry/MeganeClientRegistrar.java b/src/base/java/badasintended/megane/api/registry/MeganeClientRegistrar.java index ee33305..d60120e 100644 --- a/src/base/java/badasintended/megane/api/registry/MeganeClientRegistrar.java +++ b/src/base/java/badasintended/megane/api/registry/MeganeClientRegistrar.java @@ -3,6 +3,7 @@ import badasintended.megane.api.provider.FluidInfoProvider; import net.minecraft.fluid.Fluid; import net.minecraft.text.Text; +import net.minecraft.text.TranslatableText; public interface MeganeClientRegistrar { @@ -10,6 +11,10 @@ public interface MeganeClientRegistrar { MeganeClientRegistrar fluid(Class fluid, FluidInfoProvider provider); - MeganeClientRegistrar energy(String namespace, int color, String unit); + MeganeClientRegistrar energy(String namespace, int color, String unit, Text name); + + default MeganeClientRegistrar energy(String namespace, int color, String unit) { + return energy(namespace, color, unit, new TranslatableText("megane.energy")); + } } diff --git a/src/runtime/java/badasintended/megane/runtime/component/block/EnergyComponent.java b/src/runtime/java/badasintended/megane/runtime/component/block/EnergyComponent.java index 661c372..404859a 100644 --- a/src/runtime/java/badasintended/megane/runtime/component/block/EnergyComponent.java +++ b/src/runtime/java/badasintended/megane/runtime/component/block/EnergyComponent.java @@ -48,7 +48,7 @@ protected void append(ITooltip tooltip, IBlockAccessor accessor) { String namespace = Registry.BLOCK.getId(accessor.getBlock()).getNamespace(); boolean expand = accessor.getPlayer().isSneaking() && energy.isExpandWhenSneak(); List providers = Registrar.ENERGY_INFO.get(namespace); - EnergyInfoProvider provider = providers.isEmpty() ? null : providers.get(0); + EnergyInfoProvider provider = providers.isEmpty() ? null : providers.get(0); int color; if (colors.containsKey(namespace)) { @@ -72,7 +72,7 @@ protected void append(ITooltip tooltip, IBlockAccessor accessor) { CONFIG.save(); } - TAG.putString(B_PREFIX, I18n.translate("megane.energy")); + TAG.putString(B_PREFIX, provider != null ? provider.getName().getString() : I18n.translate("megane.energy")); TAG.putInt(B_COLOR, color); TAG.putDouble(B_STORED, stored); TAG.putDouble(B_MAX, max); diff --git a/src/runtime/java/badasintended/megane/runtime/registry/Registrar.java b/src/runtime/java/badasintended/megane/runtime/registry/Registrar.java index d662365..6ffd602 100644 --- a/src/runtime/java/badasintended/megane/runtime/registry/Registrar.java +++ b/src/runtime/java/badasintended/megane/runtime/registry/Registrar.java @@ -75,8 +75,8 @@ public MeganeClientRegistrar fluid(Class fluid, FluidInfoProvider prov } @Override - public MeganeClientRegistrar energy(String namespace, int color, String unit) { - ENERGY_INFO.add(namespace, EnergyInfoProvider.of(color, unit)); + public MeganeClientRegistrar energy(String namespace, int color, String unit, Text name) { + ENERGY_INFO.add(namespace, EnergyInfoProvider.of(color, unit, name)); return this; } } diff --git a/src/runtime/java/badasintended/megane/runtime/renderer/BarRenderer.java b/src/runtime/java/badasintended/megane/runtime/renderer/BarRenderer.java index 3682ec8..a0fe2c4 100644 --- a/src/runtime/java/badasintended/megane/runtime/renderer/BarRenderer.java +++ b/src/runtime/java/badasintended/megane/runtime/renderer/BarRenderer.java @@ -56,7 +56,7 @@ private String getValString(NbtCompound data) { maxString = verbose ? String.valueOf(max) : suffix((long) max); } - return storedString + "/" + maxString + " " + unit; + return storedString + "/" + maxString + (unit.isEmpty() ? "" : " " + unit); } @Override