From fa1b388a4e15f85357db84e236cab00804376de4 Mon Sep 17 00:00:00 2001 From: DStrand1 Date: Sat, 14 Aug 2021 19:59:18 -0500 Subject: [PATCH] pull changes from upstream #1701 --- .../items/materialitem/MetaPrefixItem.java | 1 - .../gregtech/api/items/metaitem/MetaItem.java | 9 ++-- .../api/items/toolitem/ToolMetaItem.java | 4 +- .../block/material/ItemBlockMaterialPipe.java | 3 -- .../java/gregtech/api/recipes/RecipeMap.java | 11 +---- .../api/recipes/machines/FuelRecipeMap.java | 7 +-- .../api/unification/material/Material.java | 7 +-- .../api/unification/ore/OrePrefix.java | 9 ++-- .../gregtech/api/util/LocalizationUtils.java | 49 +++++++++++++++++++ .../common/blocks/CompressedItemBlock.java | 4 -- .../common/blocks/FrameItemBlock.java | 3 -- .../gregtech/common/blocks/OreItemBlock.java | 5 -- .../AbstractMaterialPartBehavior.java | 6 +-- .../common/items/behaviors/FacadeItem.java | 4 +- 14 files changed, 70 insertions(+), 52 deletions(-) create mode 100644 src/main/java/gregtech/api/util/LocalizationUtils.java diff --git a/src/main/java/gregtech/api/items/materialitem/MetaPrefixItem.java b/src/main/java/gregtech/api/items/materialitem/MetaPrefixItem.java index 83f9397c32e..400a00475a4 100644 --- a/src/main/java/gregtech/api/items/materialitem/MetaPrefixItem.java +++ b/src/main/java/gregtech/api/items/materialitem/MetaPrefixItem.java @@ -97,7 +97,6 @@ protected boolean canGenerate(OrePrefix orePrefix, Material material) { } @Override - @SideOnly(Side.CLIENT) public String getItemStackDisplayName(ItemStack itemStack) { Material material = MaterialRegistry.MATERIAL_REGISTRY.getObjectById(itemStack.getItemDamage()); if (material == null || prefix == null) return ""; diff --git a/src/main/java/gregtech/api/items/metaitem/MetaItem.java b/src/main/java/gregtech/api/items/metaitem/MetaItem.java index 24f989f3058..1e1c45e25b7 100644 --- a/src/main/java/gregtech/api/items/metaitem/MetaItem.java +++ b/src/main/java/gregtech/api/items/metaitem/MetaItem.java @@ -22,6 +22,7 @@ import gregtech.api.unification.material.Material; import gregtech.api.unification.ore.OrePrefix; import gregtech.api.unification.stack.ItemMaterialInfo; +import gregtech.api.util.LocalizationUtils; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.block.model.ModelBakery; import net.minecraft.client.renderer.block.model.ModelResourceLocation; @@ -71,7 +72,6 @@ * {@code addItem(0, "test_item").addStats(new ElectricStats(10000, 1, false)) } * This will add single-use (not rechargeable) LV battery with initial capacity 10000 EU */ -@SuppressWarnings("deprecation") public abstract class MetaItem.MetaValueItem> extends Item implements ItemUIFactory { private static final List> META_ITEMS = new ArrayList<>(); @@ -486,7 +486,6 @@ public boolean shouldCauseReequipAnimation(@Nonnull ItemStack oldStack, @Nonnull @Nonnull @Override - @SideOnly(Side.CLIENT) public String getItemStackDisplayName(ItemStack stack) { if (stack.getItemDamage() >= metaItemOffset) { T item = getItem(stack); @@ -501,9 +500,11 @@ public String getItemStackDisplayName(ItemStack stack) { .getCapability(CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY, null); if (fluidHandlerItem != null) { FluidStack fluidInside = fluidHandlerItem.drain(Integer.MAX_VALUE, false); - return I18n.format(unlocalizedName, fluidInside == null ? I18n.format("metaitem.fluid_cell.empty") : fluidInside.getLocalizedName()); + return LocalizationUtils.format(unlocalizedName, fluidInside == null ? + LocalizationUtils.format("metaitem.fluid_cell.empty") : + fluidInside.getLocalizedName()); } - return I18n.format(unlocalizedName); + return LocalizationUtils.format(unlocalizedName); } return super.getItemStackDisplayName(stack); } diff --git a/src/main/java/gregtech/api/items/toolitem/ToolMetaItem.java b/src/main/java/gregtech/api/items/toolitem/ToolMetaItem.java index 58ec6528d24..5857bbbf32c 100755 --- a/src/main/java/gregtech/api/items/toolitem/ToolMetaItem.java +++ b/src/main/java/gregtech/api/items/toolitem/ToolMetaItem.java @@ -25,6 +25,7 @@ import gregtech.api.unification.material.properties.ToolProperty; import gregtech.api.util.GTLog; import gregtech.api.util.GTUtility; +import gregtech.api.util.LocalizationUtils; import gregtech.common.ConfigHolder; import gregtech.common.tools.DamageValues; import gregtech.common.tools.ToolWrench; @@ -422,7 +423,6 @@ public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable NBTTagCom } @Override - @SideOnly(Side.CLIENT) public String getItemStackDisplayName(ItemStack stack) { if (stack.getItemDamage() >= metaItemOffset) { T item = getItem(stack); @@ -431,7 +431,7 @@ public String getItemStackDisplayName(ItemStack stack) { } Material primaryMaterial = getToolMaterial(stack); String materialName = primaryMaterial == null ? "" : String.valueOf(primaryMaterial.getLocalizedName()); - return I18n.format("metaitem." + item.unlocalizedName + ".name", materialName); + return LocalizationUtils.format("metaitem." + item.unlocalizedName + ".name", materialName); } return super.getItemStackDisplayName(stack); } diff --git a/src/main/java/gregtech/api/pipenet/block/material/ItemBlockMaterialPipe.java b/src/main/java/gregtech/api/pipenet/block/material/ItemBlockMaterialPipe.java index 3ac1fd27c9f..4aa2d6dd04a 100644 --- a/src/main/java/gregtech/api/pipenet/block/material/ItemBlockMaterialPipe.java +++ b/src/main/java/gregtech/api/pipenet/block/material/ItemBlockMaterialPipe.java @@ -3,8 +3,6 @@ import gregtech.api.pipenet.block.ItemBlockPipe; import gregtech.api.unification.material.Material; import net.minecraft.item.ItemStack; -import net.minecraftforge.fml.relauncher.Side; -import net.minecraftforge.fml.relauncher.SideOnly; import javax.annotation.Nonnull; @@ -17,7 +15,6 @@ public ItemBlockMaterialPipe(BlockMaterialPipe block) @Nonnull @SuppressWarnings("unchecked") @Override - @SideOnly(Side.CLIENT) public String getItemStackDisplayName(@Nonnull ItemStack stack) { PipeType pipeType = blockPipe.getItemPipeType(stack); Material material = ((BlockMaterialPipe) blockPipe).getItemMaterial(stack); diff --git a/src/main/java/gregtech/api/recipes/RecipeMap.java b/src/main/java/gregtech/api/recipes/RecipeMap.java index 8072d0b14e2..1c6e683b03c 100644 --- a/src/main/java/gregtech/api/recipes/RecipeMap.java +++ b/src/main/java/gregtech/api/recipes/RecipeMap.java @@ -22,17 +22,11 @@ import gregtech.api.recipes.crafttweaker.CTRecipeBuilder; import gregtech.api.unification.material.Material; import gregtech.api.unification.ore.OrePrefix; -import gregtech.api.util.EnumValidationResult; -import gregtech.api.util.GTLog; -import gregtech.api.util.GTUtility; -import gregtech.api.util.ValidationResult; -import net.minecraft.client.resources.I18n; +import gregtech.api.util.*; import net.minecraft.item.ItemStack; import net.minecraftforge.fluids.Fluid; import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fml.common.Optional.Method; -import net.minecraftforge.fml.relauncher.Side; -import net.minecraftforge.fml.relauncher.SideOnly; import net.minecraftforge.items.IItemHandlerModifiable; import stanhebben.zenscript.annotations.Optional; import stanhebben.zenscript.annotations.*; @@ -424,10 +418,9 @@ public List ccGetRecipeList() { .collect(Collectors.toList()); } - @SideOnly(Side.CLIENT) @ZenGetter("localizedName") public String getLocalizedName() { - return I18n.format("recipemap." + unlocalizedName + ".name"); + return LocalizationUtils.format("recipemap." + unlocalizedName + ".name"); } @ZenGetter("unlocalizedName") diff --git a/src/main/java/gregtech/api/recipes/machines/FuelRecipeMap.java b/src/main/java/gregtech/api/recipes/machines/FuelRecipeMap.java index 5e95f907627..35cb973c718 100644 --- a/src/main/java/gregtech/api/recipes/machines/FuelRecipeMap.java +++ b/src/main/java/gregtech/api/recipes/machines/FuelRecipeMap.java @@ -6,11 +6,9 @@ import gregtech.api.GTValues; import gregtech.api.recipes.FluidKey; import gregtech.api.recipes.recipes.FuelRecipe; -import net.minecraft.client.resources.I18n; +import gregtech.api.util.LocalizationUtils; import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fml.common.Optional.Method; -import net.minecraftforge.fml.relauncher.Side; -import net.minecraftforge.fml.relauncher.SideOnly; import stanhebben.zenscript.annotations.ZenClass; import stanhebben.zenscript.annotations.ZenGetter; import stanhebben.zenscript.annotations.ZenMethod; @@ -84,10 +82,9 @@ public List getRecipeList() { return Collections.unmodifiableList(recipeList); } - @SideOnly(Side.CLIENT) @ZenGetter("localizedName") public String getLocalizedName() { - return I18n.format("recipemap." + unlocalizedName + ".name"); + return LocalizationUtils.format("recipemap." + unlocalizedName + ".name"); } @ZenGetter("unlocalizedName") diff --git a/src/main/java/gregtech/api/unification/material/Material.java b/src/main/java/gregtech/api/unification/material/Material.java index 658e75e7775..9faf83ab07a 100644 --- a/src/main/java/gregtech/api/unification/material/Material.java +++ b/src/main/java/gregtech/api/unification/material/Material.java @@ -10,13 +10,11 @@ import gregtech.api.unification.material.info.MaterialIconSet; import gregtech.api.unification.material.properties.*; import gregtech.api.unification.stack.MaterialStack; +import gregtech.api.util.LocalizationUtils; import gregtech.api.util.SmallDigits; -import net.minecraft.client.resources.I18n; import net.minecraft.enchantment.Enchantment; import net.minecraftforge.fluids.Fluid; import net.minecraftforge.fluids.FluidStack; -import net.minecraftforge.fml.relauncher.Side; -import net.minecraftforge.fml.relauncher.SideOnly; import stanhebben.zenscript.annotations.OperatorType; import stanhebben.zenscript.annotations.ZenOperator; @@ -301,10 +299,9 @@ public String getUnlocalizedName() { return "material." + materialInfo.name; } - @SideOnly(Side.CLIENT) //@ZenGetter("localizedName") public String getLocalizedName() { - return I18n.format(getUnlocalizedName()); + return LocalizationUtils.format(getUnlocalizedName()); } @Override diff --git a/src/main/java/gregtech/api/unification/ore/OrePrefix.java b/src/main/java/gregtech/api/unification/ore/OrePrefix.java index cbdcdddf63f..50e8766b346 100644 --- a/src/main/java/gregtech/api/unification/ore/OrePrefix.java +++ b/src/main/java/gregtech/api/unification/ore/OrePrefix.java @@ -8,10 +8,9 @@ import gregtech.api.unification.material.properties.IMaterialProperty; import gregtech.api.unification.material.properties.PropertyKey; import gregtech.api.unification.stack.MaterialStack; +import gregtech.api.util.LocalizationUtils; import gregtech.api.util.function.TriConsumer; import net.minecraft.client.resources.I18n; -import net.minecraftforge.fml.relauncher.Side; -import net.minecraftforge.fml.relauncher.SideOnly; import org.apache.commons.lang3.Validate; import javax.annotation.Nullable; @@ -546,13 +545,13 @@ private void runGeneratedMaterialHandlers() { currentProcessingPrefix.set(null); } - @SideOnly(Side.CLIENT) // todo clean this up + // todo clean this up public String getLocalNameForItem(Material material) { String specifiedUnlocalized = "item." + material.toString() + "." + this.name; - if (I18n.hasKey(specifiedUnlocalized)) return I18n.format(specifiedUnlocalized); + if (LocalizationUtils.hasKey(specifiedUnlocalized)) return I18n.format(specifiedUnlocalized); String unlocalized = "item.material.oreprefix." + this.name; String matLocalized = material.getLocalizedName(); - String formatted = I18n.format(unlocalized, matLocalized); + String formatted = LocalizationUtils.format(unlocalized, matLocalized); return formatted.equals(unlocalized) ? matLocalized : formatted; } diff --git a/src/main/java/gregtech/api/util/LocalizationUtils.java b/src/main/java/gregtech/api/util/LocalizationUtils.java new file mode 100644 index 00000000000..2ec1a6bb386 --- /dev/null +++ b/src/main/java/gregtech/api/util/LocalizationUtils.java @@ -0,0 +1,49 @@ +package gregtech.api.util; + +import net.minecraftforge.fml.common.FMLCommonHandler; +import net.minecraftforge.fml.relauncher.Side; + +@SuppressWarnings("deprecation") +public class LocalizationUtils { + + /** + * This function calls `net.minecraft.client.resources.I18n.format` when called on client + * or `net.minecraft.util.text.translation.I18n.translateToLocalFormatted` when called on server. + *
    + *
  • It is intended that translations should be done using `I18n` on the client.
  • + *
  • For setting up translations on the server you should use `TextComponentTranslatable`.
  • + *
  • `LocalisationUtils` is only for cases where some kind of translation is required on the server and there is no client/player in context.
  • + *
  • `LocalisationUtils` is "best effort" and will probably only work properly with en-us.
  • + *
+ * @param localisationKey the localisation key passed to the underlying format function + * @param substitutions the substitutions passed to the underlying format function + * @return the localized string. + */ + public static String format(String localisationKey, Object... substitutions) { + if (FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER) { + return net.minecraft.util.text.translation.I18n.translateToLocalFormatted(localisationKey, substitutions); + } else { + return net.minecraft.client.resources.I18n.format(localisationKey, substitutions); + } + } + + /** + * This function calls `net.minecraft.client.resources.I18n.hasKey` when called on client + * or `net.minecraft.util.text.translation.I18n.canTranslate` when called on server. + *
    + *
  • It is intended that translations should be done using `I18n` on the client.
  • + *
  • For setting up translations on the server you should use `TextComponentTranslatable`.
  • + *
  • `LocalisationUtils` is only for cases where some kind of translation is required on the server and there is no client/player in context.
  • + *
  • `LocalisationUtils` is "best effort" and will probably only work properly with en-us.
  • + *
+ * @param localisationKey the localisation key passed to the underlying hasKey function + * @return a boolean indicating if the given localisation key has localisations + */ + public static boolean hasKey(String localisationKey) { + if (FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER) { + return net.minecraft.util.text.translation.I18n.canTranslate(localisationKey); + } else { + return net.minecraft.client.resources.I18n.hasKey(localisationKey); + } + } +} diff --git a/src/main/java/gregtech/common/blocks/CompressedItemBlock.java b/src/main/java/gregtech/common/blocks/CompressedItemBlock.java index 58e8d697302..881c01ae919 100644 --- a/src/main/java/gregtech/common/blocks/CompressedItemBlock.java +++ b/src/main/java/gregtech/common/blocks/CompressedItemBlock.java @@ -5,8 +5,6 @@ import net.minecraft.block.state.IBlockState; import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemStack; -import net.minecraftforge.fml.relauncher.Side; -import net.minecraftforge.fml.relauncher.SideOnly; import javax.annotation.Nonnull; @@ -25,14 +23,12 @@ public int getMetadata(int damage) { return damage; } - @SuppressWarnings("deprecation") public IBlockState getBlockState(ItemStack stack) { return compressedBlock.getStateFromMeta(getMetadata(stack.getItemDamage())); } @Nonnull @Override - @SideOnly(Side.CLIENT) public String getItemStackDisplayName(@Nonnull ItemStack stack) { Material material = getBlockState(stack).getValue(compressedBlock.variantProperty); return OrePrefix.block.getLocalNameForItem(material); diff --git a/src/main/java/gregtech/common/blocks/FrameItemBlock.java b/src/main/java/gregtech/common/blocks/FrameItemBlock.java index 50d917643fc..959954ad897 100644 --- a/src/main/java/gregtech/common/blocks/FrameItemBlock.java +++ b/src/main/java/gregtech/common/blocks/FrameItemBlock.java @@ -5,8 +5,6 @@ import net.minecraft.block.state.IBlockState; import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemStack; -import net.minecraftforge.fml.relauncher.Side; -import net.minecraftforge.fml.relauncher.SideOnly; import javax.annotation.Nonnull; @@ -31,7 +29,6 @@ public IBlockState getBlockState(ItemStack stack) { @Nonnull @Override - @SideOnly(Side.CLIENT) public String getItemStackDisplayName(@Nonnull ItemStack stack) { Material material = frameBlock.frameMaterial; return OrePrefix.frameGt.getLocalNameForItem(material); diff --git a/src/main/java/gregtech/common/blocks/OreItemBlock.java b/src/main/java/gregtech/common/blocks/OreItemBlock.java index e2662db182f..170d00112dc 100644 --- a/src/main/java/gregtech/common/blocks/OreItemBlock.java +++ b/src/main/java/gregtech/common/blocks/OreItemBlock.java @@ -6,8 +6,6 @@ import net.minecraft.creativetab.CreativeTabs; import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemStack; -import net.minecraftforge.fml.relauncher.Side; -import net.minecraftforge.fml.relauncher.SideOnly; import javax.annotation.Nonnull; @@ -32,18 +30,15 @@ public CreativeTabs[] getCreativeTabs() { return new CreativeTabs[]{CreativeTabs.SEARCH, GregTechAPI.TAB_GREGTECH_ORES}; } - @SuppressWarnings("deprecation") protected IBlockState getBlockState(ItemStack stack) { return oreBlock.getStateFromMeta(getMetadata(stack.getItemDamage())); } @Nonnull @Override - @SideOnly(Side.CLIENT) public String getItemStackDisplayName(@Nonnull ItemStack stack) { IBlockState blockState = getBlockState(stack); StoneType stoneType = blockState.getValue(oreBlock.STONE_TYPE); return stoneType.processingPrefix.getLocalNameForItem(oreBlock.material); } - } diff --git a/src/main/java/gregtech/common/items/behaviors/AbstractMaterialPartBehavior.java b/src/main/java/gregtech/common/items/behaviors/AbstractMaterialPartBehavior.java index 6195604b786..1ae9b244090 100644 --- a/src/main/java/gregtech/common/items/behaviors/AbstractMaterialPartBehavior.java +++ b/src/main/java/gregtech/common/items/behaviors/AbstractMaterialPartBehavior.java @@ -8,13 +8,12 @@ import gregtech.api.unification.material.MaterialRegistry; import gregtech.api.unification.material.Materials; import gregtech.api.unification.material.properties.PropertyKey; +import gregtech.api.util.LocalizationUtils; import net.minecraft.client.resources.I18n; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.math.MathHelper; import net.minecraftforge.common.util.Constants.NBT; -import net.minecraftforge.fml.relauncher.Side; -import net.minecraftforge.fml.relauncher.SideOnly; import java.util.List; @@ -65,10 +64,9 @@ public void setPartDamage(ItemStack itemStack, int rotorDamage) { } @Override - @SideOnly(Side.CLIENT) public String getItemStackDisplayName(ItemStack itemStack, String unlocalizedName) { Material material = getPartMaterial(itemStack); - return I18n.format(unlocalizedName, material.getLocalizedName()); + return LocalizationUtils.format(unlocalizedName, material.getLocalizedName()); } @Override diff --git a/src/main/java/gregtech/common/items/behaviors/FacadeItem.java b/src/main/java/gregtech/common/items/behaviors/FacadeItem.java index d215473d050..46f7f0b6174 100644 --- a/src/main/java/gregtech/common/items/behaviors/FacadeItem.java +++ b/src/main/java/gregtech/common/items/behaviors/FacadeItem.java @@ -3,9 +3,9 @@ import com.google.common.collect.ImmutableList; import gregtech.api.items.metaitem.stats.IItemNameProvider; import gregtech.api.items.metaitem.stats.ISubItemHandler; +import gregtech.api.util.LocalizationUtils; import gregtech.common.ConfigHolder; import gregtech.common.covers.facade.FacadeHelper; -import net.minecraft.client.resources.I18n; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.init.Blocks; import net.minecraft.init.Items; @@ -24,7 +24,7 @@ public class FacadeItem implements IItemNameProvider, ISubItemHandler { public String getItemStackDisplayName(ItemStack itemStack, String unlocalizedName) { ItemStack facadeStack = getFacadeStack(itemStack); String name = facadeStack.getItem().getItemStackDisplayName(facadeStack); - return I18n.format(unlocalizedName, name); + return LocalizationUtils.format(unlocalizedName, name); } @Override