From 18594d1f9a4fc2ea164d1b4c6319d9c40237a319 Mon Sep 17 00:00:00 2001 From: Sollace Date: Tue, 19 Feb 2019 11:48:00 +0200 Subject: [PATCH] Strip translation calls and put them somewhere safe --- .../unicopia/UnicopiaClient.java | 4 +-- .../unicopia/edibles/Toxicity.java | 8 +++--- .../unicopia/item/ItemAlicornAmulet.java | 4 +-- .../unicopia/item/ItemSpell.java | 6 ++--- .../unicopia/item/ItemStaff.java | 4 +-- .../util/MagicalDamageSource.java | 7 ++--- .../util/lang/ClientLocale.java | 26 ++++++++++++++++++ .../util/lang/ServerLocale.java | 27 +++++++++++++++++++ 8 files changed, 69 insertions(+), 17 deletions(-) create mode 100644 src/main/java/com/minelittlepony/util/lang/ClientLocale.java create mode 100644 src/main/java/com/minelittlepony/util/lang/ServerLocale.java diff --git a/src/main/java/com/minelittlepony/unicopia/UnicopiaClient.java b/src/main/java/com/minelittlepony/unicopia/UnicopiaClient.java index e46fd989..c3aef22c 100644 --- a/src/main/java/com/minelittlepony/unicopia/UnicopiaClient.java +++ b/src/main/java/com/minelittlepony/unicopia/UnicopiaClient.java @@ -19,13 +19,13 @@ import com.minelittlepony.unicopia.player.PlayerSpeciesList; import com.minelittlepony.unicopia.render.DisguiseRenderer; import com.minelittlepony.util.gui.ButtonGridLayout; import com.minelittlepony.util.gui.UButton; +import com.minelittlepony.util.lang.ClientLocale; import com.mojang.authlib.GameProfile; import net.minecraft.client.Minecraft; import net.minecraft.client.audio.PositionedSoundRecord; import net.minecraft.client.entity.EntityPlayerSP; import net.minecraft.client.gui.GuiButton; -import net.minecraft.client.resources.I18n; import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.SoundEvents; @@ -62,7 +62,7 @@ public class UnicopiaClient extends UClient { static void addUniButton(List buttons) { ButtonGridLayout layout = new ButtonGridLayout(buttons); - GuiButton uni = new UButton(layout.getNextButtonId(), 0, 0, 150, 20, I18n.format("gui.unicopia"), b -> { + GuiButton uni = new UButton(layout.getNextButtonId(), 0, 0, 150, 20, ClientLocale.format("gui.unicopia"), b -> { Minecraft.getMinecraft().getSoundHandler().playSound(PositionedSoundRecord.getMasterRecord(SoundEvents.BLOCK_ANVIL_USE, 1)); b.displayString = "<< WIP >>"; diff --git a/src/main/java/com/minelittlepony/unicopia/edibles/Toxicity.java b/src/main/java/com/minelittlepony/unicopia/edibles/Toxicity.java index 7928ec17..c78b15fa 100644 --- a/src/main/java/com/minelittlepony/unicopia/edibles/Toxicity.java +++ b/src/main/java/com/minelittlepony/unicopia/edibles/Toxicity.java @@ -1,11 +1,10 @@ package com.minelittlepony.unicopia.edibles; -import net.minecraft.client.resources.I18n; +import com.minelittlepony.util.lang.ServerLocale; + import net.minecraft.init.MobEffects; import net.minecraft.potion.PotionEffect; import net.minecraft.util.text.TextFormatting; -import net.minecraftforge.fml.relauncher.Side; -import net.minecraftforge.fml.relauncher.SideOnly; public enum Toxicity { SAFE(0, 0), @@ -48,11 +47,10 @@ public enum Toxicity { return String.format("toxicity.%s.name", name().toLowerCase()); } - @SideOnly(Side.CLIENT) public String getTooltip() { TextFormatting color = toxicWhenCooked() ? TextFormatting.RED : toxicWhenRaw() ? TextFormatting.DARK_PURPLE : TextFormatting.GRAY; - return color + I18n.format(getTranslationKey()); + return color + ServerLocale.format(getTranslationKey()); } public static Toxicity byMetadata(int metadata) { diff --git a/src/main/java/com/minelittlepony/unicopia/item/ItemAlicornAmulet.java b/src/main/java/com/minelittlepony/unicopia/item/ItemAlicornAmulet.java index fc6beb35..4a01e202 100644 --- a/src/main/java/com/minelittlepony/unicopia/item/ItemAlicornAmulet.java +++ b/src/main/java/com/minelittlepony/unicopia/item/ItemAlicornAmulet.java @@ -12,9 +12,9 @@ import com.minelittlepony.unicopia.player.IPlayer; import com.minelittlepony.unicopia.player.PlayerSpeciesList; import com.minelittlepony.unicopia.spell.SpellAffinity; import com.minelittlepony.util.MagicalDamageSource; +import com.minelittlepony.util.lang.ClientLocale; import com.minelittlepony.util.vector.VecHelper; -import net.minecraft.client.resources.I18n; import net.minecraft.client.util.ITooltipFlag; import net.minecraft.entity.Entity; import net.minecraft.entity.MoverType; @@ -121,7 +121,7 @@ public class ItemAlicornAmulet extends ItemArmor implements IDependable { if (iplayer != null) { int attachedTime = iplayer.getInventory().getTicksAttached(this); if (attachedTime > 0) { - tooltip.add(I18n.format(this.getTranslationKey() + ".tagline", StringUtils.ticksToElapsedTime(attachedTime))); + tooltip.add(ClientLocale.format(getTranslationKey() + ".tagline", StringUtils.ticksToElapsedTime(attachedTime))); } } } diff --git a/src/main/java/com/minelittlepony/unicopia/item/ItemSpell.java b/src/main/java/com/minelittlepony/unicopia/item/ItemSpell.java index ba66923e..e4e7b005 100644 --- a/src/main/java/com/minelittlepony/unicopia/item/ItemSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/item/ItemSpell.java @@ -12,10 +12,10 @@ import com.minelittlepony.unicopia.spell.SpellAffinity; import com.minelittlepony.unicopia.spell.SpellCastResult; import com.minelittlepony.unicopia.spell.IDispenceable; import com.minelittlepony.unicopia.spell.SpellRegistry; +import com.minelittlepony.util.lang.ClientLocale; import com.minelittlepony.util.vector.VecHelper; import net.minecraft.block.BlockDispenser; -import net.minecraft.client.resources.I18n; import net.minecraft.client.util.ITooltipFlag; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.dispenser.BehaviorDefaultDispenseItem; @@ -184,7 +184,7 @@ public class ItemSpell extends Item implements ICastable { if (SpellRegistry.stackHasEnchantment(stack)) { SpellAffinity affinity = getAffinity(stack); - tooltip.add(affinity.getColourCode() + I18n.format(String.format("%s.%s.tagline", + tooltip.add(affinity.getColourCode() + ClientLocale.format(String.format("%s.%s.tagline", affinity.getTranslationKey(), SpellRegistry.getKeyFromStack(stack) ))); @@ -199,7 +199,7 @@ public class ItemSpell extends Item implements ICastable { @Override public String getItemStackDisplayName(ItemStack stack) { if (SpellRegistry.stackHasEnchantment(stack)) { - return I18n.format(getTranslationKey(stack) + ".enchanted.name", I18n.format(String.format("%s.%s.name", + return ClientLocale.format(getTranslationKey(stack) + ".enchanted.name", ClientLocale.format(String.format("%s.%s.name", getAffinity(stack).getTranslationKey(), SpellRegistry.getKeyFromStack(stack) ))); diff --git a/src/main/java/com/minelittlepony/unicopia/item/ItemStaff.java b/src/main/java/com/minelittlepony/unicopia/item/ItemStaff.java index 1cc81ca3..80ff197b 100644 --- a/src/main/java/com/minelittlepony/unicopia/item/ItemStaff.java +++ b/src/main/java/com/minelittlepony/unicopia/item/ItemStaff.java @@ -8,10 +8,10 @@ import javax.annotation.Nullable; import com.google.common.collect.HashMultimap; import com.google.common.collect.Multimap; import com.minelittlepony.unicopia.Predicates; +import com.minelittlepony.util.lang.ClientLocale; import net.minecraft.block.Block; import net.minecraft.block.state.IBlockState; -import net.minecraft.client.resources.I18n; import net.minecraft.client.util.ITooltipFlag; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityLivingBase; @@ -77,7 +77,7 @@ public class ItemStaff extends ItemSword implements ITossable { @Override public void addInformation(ItemStack stack, @Nullable World worldIn, List tooltip, ITooltipFlag flagIn) { - tooltip.add(I18n.format(getTranslationKey(stack) + ".tagline")); + tooltip.add(ClientLocale.format(getTranslationKey(stack) + ".tagline")); } @Override diff --git a/src/main/java/com/minelittlepony/util/MagicalDamageSource.java b/src/main/java/com/minelittlepony/util/MagicalDamageSource.java index 29fc2113..c04de301 100644 --- a/src/main/java/com/minelittlepony/util/MagicalDamageSource.java +++ b/src/main/java/com/minelittlepony/util/MagicalDamageSource.java @@ -1,6 +1,7 @@ package com.minelittlepony.util; -import net.minecraft.client.resources.I18n; +import com.minelittlepony.util.lang.ServerLocale; + import net.minecraft.entity.Entity; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; @@ -42,11 +43,11 @@ public class MagicalDamageSource extends EntityDamageSource { ItemStack held = attacker instanceof EntityLivingBase ? ((EntityLivingBase)attacker).getHeldItemMainhand() : ItemStack.EMPTY; String withItem = withAttecker + ".item"; - if (held != null && held.hasDisplayName() && I18n.hasKey(withItem)) { + if (held != null && held.hasDisplayName() && ServerLocale.hasKey(withItem)) { return new TextComponentTranslation(withItem, target.getDisplayName(), attacker.getDisplayName(), held.getTextComponent()); } - if (I18n.hasKey(withAttecker)) { + if (ServerLocale.hasKey(withAttecker)) { return new TextComponentTranslation(withAttecker, target.getDisplayName(), attacker.getDisplayName()); } } diff --git a/src/main/java/com/minelittlepony/util/lang/ClientLocale.java b/src/main/java/com/minelittlepony/util/lang/ClientLocale.java new file mode 100644 index 00000000..99d2e3d3 --- /dev/null +++ b/src/main/java/com/minelittlepony/util/lang/ClientLocale.java @@ -0,0 +1,26 @@ +package com.minelittlepony.util.lang; + +import net.minecraft.client.resources.I18n; +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +/** + * Localisation class for client-side translations. + * Only use this in client code. Servers should use ServerLocale. + */ +@SideOnly(Side.CLIENT) +public class ClientLocale { + /** + * Translates the given string and then formats it. Equivalent to String.format(translate(key), parameters). + */ + public static String format(String translateKey, Object... parameters) { + return I18n.format(translateKey, parameters); + } + + /** + * Determines if a language mapping exists in the current locale for the requested key. + */ + public static boolean hasKey(String key) { + return I18n.hasKey(key); + } +} diff --git a/src/main/java/com/minelittlepony/util/lang/ServerLocale.java b/src/main/java/com/minelittlepony/util/lang/ServerLocale.java new file mode 100644 index 00000000..cebde015 --- /dev/null +++ b/src/main/java/com/minelittlepony/util/lang/ServerLocale.java @@ -0,0 +1,27 @@ +package com.minelittlepony.util.lang; + +import com.minelittlepony.unicopia.forgebullshit.FUF; + +import net.minecraft.util.text.translation.I18n; + +/** + * Localisation class for server-side translations. + * Use this in a server context, otherwise use ClientLocale. + */ +@FUF(reason = "Don't deprecate classes we actually need to use") +@SuppressWarnings("deprecation") +public class ServerLocale { + /** + * Translates a Stat name with format args + */ + public static String format(String key, Object... format) { + return I18n.translateToLocalFormatted(key, format); + } + + /** + * Determines whether or not translateToLocal will find a translation for the given key. + */ + public static boolean hasKey(String key) { + return I18n.canTranslate(key); + } +} \ No newline at end of file