Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix getItemStackDisplayName on Servers #1701

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ protected boolean canGenerate(OrePrefix orePrefix, Material material) {
}

@Override
@SideOnly(Side.CLIENT)
public String getItemStackDisplayName(ItemStack itemStack) {
if (itemStack.getItemDamage() < metaItemOffset) {
Material material = Material.MATERIAL_REGISTRY.getObjectById(itemStack.getItemDamage() % 1000);
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/gregtech/api/items/metaitem/MetaItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import gregtech.api.unification.material.type.Material;
import gregtech.api.unification.ore.OrePrefix;
import gregtech.api.unification.stack.ItemMaterialInfo;
import gregtech.api.util.LocalisationUtils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.block.model.ModelBakery;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
Expand Down Expand Up @@ -471,7 +472,7 @@ public boolean shouldCauseReequipAnimation(ItemStack oldStack, ItemStack newStac
}

@Override
@SideOnly(Side.CLIENT)
@SuppressWarnings("deprecation")
public String getItemStackDisplayName(ItemStack stack) {
if (stack.getItemDamage() >= metaItemOffset) {
T item = getItem(stack);
Expand All @@ -486,9 +487,9 @@ 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 LocalisationUtils.format(unlocalizedName, fluidInside == null ? LocalisationUtils.format("metaitem.fluid_cell.empty") : fluidInside.getLocalizedName());
}
return I18n.format(unlocalizedName);
return LocalisationUtils.format(unlocalizedName);
}
return super.getItemStackDisplayName(stack);
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/gregtech/api/items/toolitem/ToolMetaItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import gregtech.api.unification.material.type.SolidMaterial;
import gregtech.api.util.GTLog;
import gregtech.api.util.GTUtility;
import gregtech.api.util.LocalisationUtils;
import gregtech.common.ConfigHolder;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.resources.I18n;
Expand Down Expand Up @@ -406,7 +407,7 @@ public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable NBTTagCom
}

@Override
@SideOnly(Side.CLIENT)
@SuppressWarnings("deprecation")
public String getItemStackDisplayName(ItemStack stack) {
if (stack.getItemDamage() >= metaItemOffset) {
T item = getItem(stack);
Expand All @@ -415,7 +416,7 @@ public String getItemStackDisplayName(ItemStack stack) {
}
SolidMaterial primaryMaterial = getToolMaterial(stack);
String materialName = primaryMaterial == null ? "" : String.valueOf(primaryMaterial.getLocalizedName());
return I18n.format("metaitem." + item.unlocalizedName + ".name", materialName);
return LocalisationUtils.format("metaitem." + item.unlocalizedName + ".name", materialName);
}
return super.getItemStackDisplayName(stack);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public ItemBlockMaterialPipe(BlockMaterialPipe<PipeType, NodeDataType, ?> block)

@SuppressWarnings("unchecked")
@Override
@SideOnly(Side.CLIENT)
public String getItemStackDisplayName(ItemStack stack) {
PipeType pipeType = blockPipe.getItemPipeType(stack);
Material material = ((BlockMaterialPipe<PipeType, NodeDataType, ?>) blockPipe).getItemMaterial(stack);
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/gregtech/api/recipes/RecipeMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import gregtech.api.util.EnumValidationResult;
import gregtech.api.util.GTLog;
import gregtech.api.util.GTUtility;
import gregtech.api.util.LocalisationUtils;
import gregtech.api.util.ValidationResult;
import net.minecraft.client.resources.I18n;
import net.minecraft.item.ItemStack;
Expand Down Expand Up @@ -406,10 +407,10 @@ public List<CTRecipe> ccGetRecipeList() {
.collect(Collectors.toList());
}

@SideOnly(Side.CLIENT)
@ZenGetter("localizedName")
@SuppressWarnings("deprecation")
public String getLocalizedName() {
return I18n.format("recipemap." + unlocalizedName + ".name");
return LocalisationUtils.format("recipemap." + unlocalizedName + ".name");
}

@ZenGetter("unlocalizedName")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.LocalisationUtils;
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;
Expand Down Expand Up @@ -84,10 +82,10 @@ public List<FuelRecipe> getRecipeList() {
return Collections.unmodifiableList(recipeList);
}

@SideOnly(Side.CLIENT)
@ZenGetter("localizedName")
@SuppressWarnings("deprecation")
public String getLocalizedName() {
return I18n.format("recipemap." + unlocalizedName + ".name");
return LocalisationUtils.format("recipemap." + unlocalizedName + ".name");
}

@ZenGetter("unlocalizedName")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import gregtech.api.unification.stack.MaterialStack;
import gregtech.api.util.GTControlledRegistry;
import gregtech.api.util.GTLog;
import net.minecraft.client.resources.I18n;
import gregtech.api.util.LocalisationUtils;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import stanhebben.zenscript.annotations.*;
Expand Down Expand Up @@ -381,10 +381,10 @@ public String getUnlocalizedName() {
return "material." + toString();
}

@SideOnly(Side.CLIENT)
@ZenGetter("localizedName")
@SuppressWarnings("deprecation")
public String getLocalizedName() {
return I18n.format(getUnlocalizedName());
return LocalisationUtils.format(getUnlocalizedName());
}

@Override
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/gregtech/api/unification/ore/OrePrefix.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
import gregtech.api.unification.material.type.*;
import gregtech.api.unification.stack.MaterialStack;
import gregtech.api.util.GTUtility;
import net.minecraft.client.resources.I18n;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import gregtech.api.util.LocalisationUtils;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.apache.commons.lang3.Validate;

import javax.annotation.Nullable;
Expand Down Expand Up @@ -442,13 +441,13 @@ private void runGeneratedMaterialHandlers() {
currentProcessingPrefix.set(null);
}

@SideOnly(Side.CLIENT)
@SuppressWarnings("deprecation")
public String getLocalNameForItem(Material material) {
String specfiedUnlocalized = "item." + material.toString() + "." + this.name();
if (I18n.hasKey(specfiedUnlocalized)) return I18n.format(specfiedUnlocalized);
if (LocalisationUtils.hasKey(specfiedUnlocalized)) return LocalisationUtils.format(specfiedUnlocalized);
String unlocalized = "item.material.oreprefix." + this.name();
String matLocalized = material.getLocalizedName();
String formatted = I18n.format(unlocalized, matLocalized);
String formatted = LocalisationUtils.format(unlocalized, matLocalized);
return formatted.equals(unlocalized) ? matLocalized : formatted;
}

Expand Down
53 changes: 53 additions & 0 deletions src/main/java/gregtech/api/util/LocalisationUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package gregtech.api.util;

import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.relauncher.Side;

public class LocalisationUtils {

/**
* @deprecated
* 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.
* <ul>
* <li>It is intended that translations should be done using `I18n` on the client.</li>
* <li>For setting up translations on the server you should use `TextComponentTranslatable`.</li>
* <li>`LocalisationUtils` is only for cases where some kind of translation is required on the server and there is no client/player in context.</li>
* <li>`LocalisationUtils` is "best effort" and will probably only work properly with en-us.</li>
* </ul>
* @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.
*/
@Deprecated
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);
}
}

/**
* @deprecated
* 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.
* <ul>
* <li>It is intended that translations should be done using `I18n` on the client.</li>
* <li>For setting up translations on the server you should use `TextComponentTranslatable`.</li>
* <li>`LocalisationUtils` is only for cases where some kind of translation is required on the server and there is no client/player in context.</li>
* <li>`LocalisationUtils` is "best effort" and will probably only work properly with en-us.</li>
* </ul>
* @param localisationKey the localisation key passed to the underlying hasKey function
* @return a boolean indicating if the given localisation key has localisations
*/
@Deprecated
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);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public IBlockState getBlockState(ItemStack stack) {
}

@Override
@SideOnly(Side.CLIENT)
public String getItemStackDisplayName(ItemStack stack) {
Material material = getBlockState(stack).getValue(compressedBlock.variantProperty);
return OrePrefix.block.getLocalNameForItem(material);
Expand Down
1 change: 0 additions & 1 deletion src/main/java/gregtech/common/blocks/FrameItemBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public IBlockState getBlockState(ItemStack stack) {
}

@Override
@SideOnly(Side.CLIENT)
public String getItemStackDisplayName(ItemStack stack) {
Material material = frameBlock.frameMaterial;
return OrePrefix.frameGt.getLocalNameForItem(material);
Expand Down
1 change: 0 additions & 1 deletion src/main/java/gregtech/common/blocks/OreItemBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ protected IBlockState getBlockState(ItemStack stack) {
}

@Override
@SideOnly(Side.CLIENT)
public String getItemStackDisplayName(ItemStack stack) {
IBlockState blockState = getBlockState(stack);
StoneType stoneType = blockState.getValue(oreBlock.STONE_TYPE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import gregtech.api.unification.material.Materials;
import gregtech.api.unification.material.type.IngotMaterial;
import gregtech.api.unification.material.type.Material;
import gregtech.api.util.LocalisationUtils;
import net.minecraft.client.resources.I18n;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
Expand Down Expand Up @@ -62,10 +63,10 @@ public void setPartDamage(ItemStack itemStack, int rotorDamage) {
}

@Override
@SideOnly(Side.CLIENT)
@SuppressWarnings("deprecation")
public String getItemStackDisplayName(ItemStack itemStack, String unlocalizedName) {
IngotMaterial material = getPartMaterial(itemStack);
return I18n.format(unlocalizedName, material.getLocalizedName());
return LocalisationUtils.format(unlocalizedName, material.getLocalizedName());
}

@Override
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/gregtech/common/items/behaviors/FacadeItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.LocalisationUtils;
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;
Expand All @@ -21,10 +21,11 @@
public class FacadeItem implements IItemNameProvider, ISubItemHandler {

@Override
@SuppressWarnings("deprecation")
public String getItemStackDisplayName(ItemStack itemStack, String unlocalizedName) {
ItemStack facadeStack = getFacadeStack(itemStack);
String name = facadeStack.getItem().getItemStackDisplayName(facadeStack);
return I18n.format(unlocalizedName, name);
return LocalisationUtils.format(unlocalizedName, name);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import mcjty.theoneprobe.api.ElementAlignment;
import mcjty.theoneprobe.api.IProbeInfo;
import mcjty.theoneprobe.api.TextStyleClass;
import net.minecraft.client.resources.I18n;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;

Expand Down