From 933d45fabaf0bb05c9cdf8dcdd897ebfc8a2adea Mon Sep 17 00:00:00 2001 From: Boblet Date: Fri, 23 Feb 2024 09:07:55 +0100 Subject: [PATCH] localization for custom machines --- changelog | 2 ++ .../java/com/hbm/config/CustomMachineConfigJSON.java | 12 ++++++++++++ .../java/com/hbm/inventory/gui/GUIMachineCustom.java | 9 ++++++--- .../java/com/hbm/items/block/ItemCustomMachine.java | 4 +++- src/main/java/com/hbm/main/ClientProxy.java | 7 +++++++ src/main/java/com/hbm/main/ServerProxy.java | 2 ++ 6 files changed, 32 insertions(+), 4 deletions(-) diff --git a/changelog b/changelog index 4d6932f67..249f85271 100644 --- a/changelog +++ b/changelog @@ -11,7 +11,9 @@ * Electrolyzing fluids now only takes 20 ticks instead of 60 * Batch sizes for water and heavy water have been doubled, effectively increasing throughout 6x * The throughput for electrolysis on chemical plants has been halved (but heavy water still has the output buff, effectively remaining unchanged) +* Custom machines now have an optional localization field which allows translations to be added within the config ## Fixed * Fixed the structure toggle on the world creation screen not working correctly on most world types * Fixed antiknock having a broken sprite and localization +* Fixed crash caused by fallout affecting spotlight blocks, crashing the game diff --git a/src/main/java/com/hbm/config/CustomMachineConfigJSON.java b/src/main/java/com/hbm/config/CustomMachineConfigJSON.java index dc746f7d8..aab315abd 100644 --- a/src/main/java/com/hbm/config/CustomMachineConfigJSON.java +++ b/src/main/java/com/hbm/config/CustomMachineConfigJSON.java @@ -8,10 +8,12 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map.Entry; import java.util.Set; import com.google.gson.Gson; import com.google.gson.JsonArray; +import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.stream.JsonWriter; import com.hbm.blocks.ModBlocks; @@ -58,6 +60,9 @@ public class CustomMachineConfigJSON { writer.beginObject(); writer.name("recipeKey").value("paperPress"); writer.name("unlocalizedName").value("paperPress"); + writer.name("localization").beginObject(); + writer.name("de_DE").value("Papierpresse"); + writer.endObject(); writer.name("localizedName").value("Paper Press"); writer.name("fluidInCount").value(1); writer.name("fluidInCap").value(1_000); @@ -152,6 +157,12 @@ public class CustomMachineConfigJSON { configuration.recipeKey = machineObject.get("recipeKey").getAsString(); configuration.unlocalizedName = machineObject.get("unlocalizedName").getAsString(); configuration.localizedName = machineObject.get("localizedName").getAsString(); + if(machineObject.has("localization")) { + JsonObject localization = machineObject.get("localization").getAsJsonObject(); + for(Entry entry : localization.entrySet()) { + configuration.localization.put(entry.getKey(), entry.getValue().getAsString()); + } + } configuration.fluidInCount = machineObject.get("fluidInCount").getAsInt(); configuration.fluidInCap = machineObject.get("fluidInCap").getAsInt(); configuration.itemInCount = machineObject.get("itemInCount").getAsInt(); @@ -240,6 +251,7 @@ public class CustomMachineConfigJSON { public String unlocalizedName; /** The display name of this machine */ public String localizedName; + public HashMap localization = new HashMap();; public int fluidInCount; public int fluidInCap; diff --git a/src/main/java/com/hbm/inventory/gui/GUIMachineCustom.java b/src/main/java/com/hbm/inventory/gui/GUIMachineCustom.java index dcfb6effd..2a02cd763 100644 --- a/src/main/java/com/hbm/inventory/gui/GUIMachineCustom.java +++ b/src/main/java/com/hbm/inventory/gui/GUIMachineCustom.java @@ -9,6 +9,7 @@ import org.lwjgl.opengl.GL11; import com.hbm.inventory.SlotPattern; import com.hbm.inventory.container.ContainerMachineCustom; import com.hbm.lib.RefStrings; +import com.hbm.main.MainRegistry; import com.hbm.tileentity.machine.TileEntityCustomMachine; import net.minecraft.client.Minecraft; @@ -69,6 +70,8 @@ public class GUIMachineCustom extends GuiInfoContainer { @Override protected void drawGuiContainerForegroundLayer(int i, int j) { String name = this.custom.getInventoryName(); + String localizedName = this.custom.config.localization.get(MainRegistry.proxy.getLanguageCode()); + if(localizedName != null) name = localizedName; this.fontRendererObj.drawString(name, 68 - this.fontRendererObj.getStringWidth(name) / 2, 6, 4210752); this.fontRendererObj.drawString(I18n.format("container.inventory"), 8, this.ySize - 96 + 2, 4210752); if(custom.config.fluxMode) this.fontRendererObj.drawString("Flux:" + custom.flux,83, 57,0x08FF00); @@ -82,14 +85,14 @@ public class GUIMachineCustom extends GuiInfoContainer { if(custom.config.fluxMode){ drawTexturedModalRect(guiLeft + 78, guiTop + 54, 192, 122,51 , 15); } - if(custom.maxHeat>0) { - drawTexturedModalRect(guiLeft + 61, guiTop + 53, 236,0 , 18, 18); + if(custom.maxHeat > 0) { + drawTexturedModalRect(guiLeft + 61, guiTop + 53, 236, 0, 18, 18); GaugeUtil.drawSmoothGauge(guiLeft + 70, guiTop + 62, this.zLevel, (double) custom.heat / (double) custom.config.maxHeat, 5, 2, 1, 0x7F0000); } int p = custom.progress * 90 / custom.maxProgress; drawTexturedModalRect(guiLeft + 78, guiTop + 119, 192, 0, Math.min(p, 44), 16); if(p > 44) { - p-= 44; + p -= 44; drawTexturedModalRect(guiLeft + 78 + 44, guiTop + 119, 192, 16, p, 16); } diff --git a/src/main/java/com/hbm/items/block/ItemCustomMachine.java b/src/main/java/com/hbm/items/block/ItemCustomMachine.java index 30723223d..ce6559b82 100644 --- a/src/main/java/com/hbm/items/block/ItemCustomMachine.java +++ b/src/main/java/com/hbm/items/block/ItemCustomMachine.java @@ -4,6 +4,7 @@ import java.util.List; import com.hbm.config.CustomMachineConfigJSON; import com.hbm.config.CustomMachineConfigJSON.MachineConfiguration; +import com.hbm.main.MainRegistry; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; @@ -39,7 +40,8 @@ public class ItemCustomMachine extends ItemBlock { MachineConfiguration conf = CustomMachineConfigJSON.niceList.get(id); if(conf != null) { - return conf.localizedName; + String localized = conf.localization.get(MainRegistry.proxy.getLanguageCode()); + return localized != null ? localized : conf.localizedName; } } diff --git a/src/main/java/com/hbm/main/ClientProxy.java b/src/main/java/com/hbm/main/ClientProxy.java index 9661ef3b6..a28dc0c35 100644 --- a/src/main/java/com/hbm/main/ClientProxy.java +++ b/src/main/java/com/hbm/main/ClientProxy.java @@ -15,6 +15,7 @@ import net.minecraft.client.renderer.entity.RenderMinecart; import net.minecraft.client.renderer.entity.RenderSnowball; import net.minecraft.client.renderer.texture.TextureManager; import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher; +import net.minecraft.client.resources.Language; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; @@ -2104,4 +2105,10 @@ public class ClientProxy extends ServerProxy { public void playSoundClient(double x, double y, double z, String sound, float volume, float pitch) { Minecraft.getMinecraft().getSoundHandler().playSound(new PositionedSoundRecord(new ResourceLocation(sound), volume, pitch, (float) x, (float) y, (float) z)); } + + @Override + public String getLanguageCode() { + Language lang = Minecraft.getMinecraft().getLanguageManager().getCurrentLanguage(); + return lang.getLanguageCode(); + } } diff --git a/src/main/java/com/hbm/main/ServerProxy.java b/src/main/java/com/hbm/main/ServerProxy.java index 554783cee..5878d4d40 100644 --- a/src/main/java/com/hbm/main/ServerProxy.java +++ b/src/main/java/com/hbm/main/ServerProxy.java @@ -86,4 +86,6 @@ public class ServerProxy { } public void playSoundClient(double x, double y, double z, String sound, float volume, float pitch) { } + + public String getLanguageCode() { return "en_US"; } } \ No newline at end of file