Skip to content

Commit

Permalink
allow using custom name for energy bar
Browse files Browse the repository at this point in the history
  • Loading branch information
deirn committed Nov 23, 2021
1 parent 2c4deb9 commit 78474aa
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package badasintended.megane.api.provider;

public interface EnergyInfoProvider<T> {
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.text.Text;

static <T> EnergyInfoProvider<T> 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;
Expand All @@ -13,11 +18,18 @@ public int getColor() {
public String getUnit() {
return unit;
}

@Override
public Text getName() {
return name;
}
};
}

int getColor();

String getUnit();

Text getName();

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
import badasintended.megane.api.provider.FluidInfoProvider;
import net.minecraft.fluid.Fluid;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;

public interface MeganeClientRegistrar {

MeganeClientRegistrar fluid(Fluid fluid, int color, Text name);

<T> MeganeClientRegistrar fluid(Class<T> fluid, FluidInfoProvider<T> 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"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<EnergyInfoProvider> 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)) {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public <T> MeganeClientRegistrar fluid(Class<T> fluid, FluidInfoProvider<T> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 78474aa

Please sign in to comment.