Skip to content

Commit

Permalink
Fix Fluid Localization Issues in TOP
Browse files Browse the repository at this point in the history
  • Loading branch information
IntegerLimit committed Aug 12, 2024
1 parent 2a2f6a1 commit 0a6cd12
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 7 deletions.
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ enable_controlling = false
# Whether to enable Default World Generator Port in runtime. Fixes GUI Rescaling Issues, adds a Cancel Button.
enable_default_world_gen_port = false

# Whether to enable TOP Addons in runtime. Fixes Error Messages with Architecture Craft 3.108.
# Whether to enable TOP Addons in runtime. Fixes Error Messages with Architecture Craft 3.108, and issues with fluid localization.
# Requires Architecture Craft to also be enabled.
enable_top_addons = false

# Whether to enable Actually Additions in runtime. Allows specifying Linear XP Scaling for AA Machines.
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/nomiceu/nomilabs/event/CommonProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import com.nomiceu.nomilabs.groovy.NCActiveCoolerHelper;
import com.nomiceu.nomilabs.integration.architecturecraft.LabsShapes;
import com.nomiceu.nomilabs.integration.jei.JEIPlugin;
import com.nomiceu.nomilabs.integration.top.TOPTooltipManager;
import com.nomiceu.nomilabs.integration.top.LabsTOPManager;
import com.nomiceu.nomilabs.item.ItemExcitationCoil;
import com.nomiceu.nomilabs.item.registry.LabsItems;
import com.nomiceu.nomilabs.network.LabsNetworkHandler;
Expand Down Expand Up @@ -91,7 +91,7 @@ public static void preInit() {
LabsMetaTileEntities.preInit();

if (LabsConfig.modIntegration.enableTOPIntegration && Loader.isModLoaded(LabsValues.TOP_MODID))
TOPTooltipManager.registerProviders();
LabsTOPManager.register();

DataFixerHandler.preInit();
FluidRegistryMixinHelper.preInit();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.nomiceu.nomilabs.integration.top;

import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;

import com.nomiceu.nomilabs.NomiLabs;
import com.nomiceu.nomilabs.util.LabsTranslate;

import io.netty.buffer.ByteBuf;
import mcjty.theoneprobe.api.IElement;
import mcjty.theoneprobe.api.TextStyleClass;
import mcjty.theoneprobe.apiimpl.client.ElementTextRender;
import mcjty.theoneprobe.network.NetworkTools;

public class LabsFluidNameElement implements IElement {

private final String fluidName;
private final int amount;
private final String translatedName;

public LabsFluidNameElement(FluidStack fluid) {
this.fluidName = fluid.getFluid().getName();
this.amount = fluid.amount;

// Temp Translated Name, for usage if needed
this.translatedName = fluid.getUnlocalizedName();
}

public LabsFluidNameElement(ByteBuf byteBuf) {
this.fluidName = NetworkTools.readStringUTF8(byteBuf);
this.amount = byteBuf.readInt();
this.translatedName = translateFluid(fluidName, amount, "LabsFluidNameElement");
}

@Override
public int getWidth() {
return ElementTextRender.getWidth(translatedName);
}

@Override
public int getHeight() {
return 10;
}

@Override
public void toBytes(ByteBuf byteBuf) {
NetworkTools.writeStringUTF8(byteBuf, fluidName);
byteBuf.writeInt(amount);
}

@Override
public void render(int x, int y) {
ElementTextRender.render(
TextStyleClass.NAME + LabsTranslate.translate("nomilabs.gui.top_override.fluid", translatedName), x, y);
}

@Override
public int getID() {
return LabsTOPManager.FLUID_NAME_ELEMENT;
}

public static String translateFluid(String fluidName, int amount, String packet) {
var fluid = FluidRegistry.getFluid(fluidName);

// At least try and translate it if fluid is null
if (fluid == null) {
NomiLabs.LOGGER.error("Received Fluid Info Packet {} with Unknown Fluid {}!", packet, fluidName);
return LabsTranslate.translate(fluidName);
}

return fluid.getLocalizedName(new FluidStack(fluid, amount));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
import mcjty.theoneprobe.TheOneProbe;
import mcjty.theoneprobe.api.*;

public class TOPTooltipManager {
public class LabsTOPManager {

public static void registerProviders() {
public static int FLUID_NAME_ELEMENT;

public static void register() {
ITheOneProbe TOP = TheOneProbe.theOneProbeImp;
TOP.registerProvider(new TOPTooltipMessage());
FLUID_NAME_ELEMENT = TOP.registerElementFactory(LabsFluidNameElement::new);
}

public static class TOPTooltipMessage implements IProbeInfoProvider {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.nomiceu.nomilabs.mixin.theoneprobe;

import net.minecraftforge.fluids.FluidStack;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import com.nomiceu.nomilabs.integration.top.LabsFluidNameElement;

import mcjty.theoneprobe.api.IProbeInfo;
import mcjty.theoneprobe.api.TextStyleClass;
import mcjty.theoneprobe.apiimpl.ProbeConfig;
import mcjty.theoneprobe.apiimpl.elements.ElementProgress;
import mcjty.theoneprobe.apiimpl.providers.DefaultProbeInfoProvider;
import mcjty.theoneprobe.config.Config;

/**
* Fixes Localization of Fluid Names.
*/
@Mixin(value = DefaultProbeInfoProvider.class, remap = false)
public class DefaultProbeInfoProviderMixin {

@Inject(method = "addFluidInfo", at = @At("HEAD"), cancellable = true)
private void showTranslatedFluidInfo(IProbeInfo probeInfo, ProbeConfig config, FluidStack fluidStack,
int maxContents, CallbackInfo ci) {
int contents = fluidStack == null ? 0 : fluidStack.amount;
if (fluidStack != null) {
probeInfo.element(new LabsFluidNameElement(fluidStack));
}
if (config.getTankMode() == 1) {
probeInfo.progress(contents, maxContents,
probeInfo.defaultProgressStyle()
.suffix("mB")
.filledColor(Config.tankbarFilledColor)
.alternateFilledColor(Config.tankbarAlternateFilledColor)
.borderColor(Config.tankbarBorderColor)
.numberFormat(Config.tankFormat));
} else {
probeInfo.text(TextStyleClass.PROGRESS + ElementProgress.format(contents, Config.tankFormat, "mB"));
}

ci.cancel();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.nomiceu.nomilabs.mixin.topaddons;

import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import io.github.drmanganese.topaddons.addons.AddonForge;

/**
* Fixes Localization of Fluid Names.
*/
@Mixin(value = AddonForge.class, remap = false)
public class AddonForgeMixin {

@Redirect(method = "addTankElement(Lmcjty/theoneprobe/api/IProbeInfo;Ljava/lang/Class;Lnet/minecraftforge/fluids/FluidTank;ILmcjty/theoneprobe/api/ProbeMode;Lnet/minecraft/entity/player/EntityPlayer;)Lmcjty/theoneprobe/api/IProbeInfo;",
at = @At(value = "INVOKE",
target = "Lnet/minecraftforge/fluids/FluidStack;getLocalizedName()Ljava/lang/String;"))
private static String useFluidName1(FluidStack instance) {
return FluidRegistry.getFluidName(instance);
}

@Redirect(method = "addTankElement(Lmcjty/theoneprobe/api/IProbeInfo;Ljava/lang/String;Lnet/minecraftforge/fluids/FluidTankInfo;Lmcjty/theoneprobe/api/ProbeMode;Lnet/minecraft/entity/player/EntityPlayer;)Lmcjty/theoneprobe/api/IProbeInfo;",
at = @At(value = "INVOKE",
target = "Lnet/minecraftforge/fluids/FluidStack;getLocalizedName()Ljava/lang/String;"))
private static String useUnlocalizedName2(FluidStack instance) {
return FluidRegistry.getFluidName(instance);
}

@Redirect(method = "addProbeInfo",
at = @At(value = "INVOKE",
target = "Lnet/minecraftforge/fluids/Fluid;getLocalizedName(Lnet/minecraftforge/fluids/FluidStack;)Ljava/lang/String;"))
private String useUnlocalizedName3(Fluid instance, FluidStack stack) {
return FluidRegistry.getFluidName(instance);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.nomiceu.nomilabs.mixin.topaddons;

import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import com.nomiceu.nomilabs.integration.top.LabsFluidNameElement;

import io.github.drmanganese.topaddons.elements.ElementTankGauge;
import io.netty.buffer.ByteBuf;

/**
* Fixes Localization of Fluid Names. (Client Only)
*/
@Mixin(value = ElementTankGauge.class, remap = false)
public class ElementTankGaugeMixin {

@Shadow
@Final
@Mutable
private String fluidName;

@Shadow
@Final
private int amount;

@Inject(method = "<init>(Lio/netty/buffer/ByteBuf;)V", at = @At("RETURN"))
private void translateFluidName(ByteBuf buf, CallbackInfo ci) {
fluidName = LabsFluidNameElement.translateFluid(fluidName, amount, "ElementTankGauge");
}
}
2 changes: 2 additions & 0 deletions src/main/resources/assets/nomilabs/lang/en_us.lang
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ nomilabs.gui.language_jei.title=§lShould JEI be Reloaded?§r
nomilabs.gui.language_jei.desc.1=Reloading §bJEI§r ensures that category titles and descriptions are translated to your chosen language.
nomilabs.gui.language_jei.desc.2=However, Reloading JEI will take a §ccouple seconds§r to perform.

nomilabs.gui.top_override.fluid=Fluid: %s

# Custom Fluids
fluid.uranium233=Uranium 233
fluid.plutonium2=Plutonium
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/mixins.nomilabs.theoneprobe.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"minVersion": "0.8",
"compatibilityLevel": "JAVA_8",
"mixins": [
"DefaultProbeInfoProviderMixin",
"HarvestInfoToolsMixin"
],
"client": [],
Expand Down
7 changes: 5 additions & 2 deletions src/main/resources/mixins.nomilabs.topaddons.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
"minVersion": "0.8",
"compatibilityLevel": "JAVA_8",
"mixins": [
"AddonArchitectureCraftMixin"
"AddonArchitectureCraftMixin",
"AddonForgeMixin"
],
"client": [
"ElementTankGaugeMixin"
],
"client": [],
"server": []
}

0 comments on commit 0a6cd12

Please sign in to comment.