Strip translation calls and put them somewhere safe

This commit is contained in:
Sollace 2019-02-19 11:48:00 +02:00
parent 918dd852fd
commit 18594d1f9a
8 changed files with 69 additions and 17 deletions

View file

@ -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<GuiButton> 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 >>";

View file

@ -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) {

View file

@ -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)));
}
}
}

View file

@ -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)
)));

View file

@ -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<String> tooltip, ITooltipFlag flagIn) {
tooltip.add(I18n.format(getTranslationKey(stack) + ".tagline"));
tooltip.add(ClientLocale.format(getTranslationKey(stack) + ".tagline"));
}
@Override

View file

@ -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());
}
}

View file

@ -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);
}
}

View file

@ -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);
}
}