Skip to content

Commit

Permalink
Make translation key of MetaItems modifiable (#2259)
Browse files Browse the repository at this point in the history
Co-authored-by: ALongStringOfNumbers <[email protected]>
  • Loading branch information
2 people authored and serenibyss committed Jan 21, 2024
1 parent 8c48186 commit 6e1a167
Showing 1 changed file with 46 additions and 18 deletions.
64 changes: 46 additions & 18 deletions src/main/java/gregtech/api/items/metaitem/MetaItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,27 @@

import java.time.Duration;
import java.time.Instant;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
* MetaItem is item that can have up to Short.MAX_VALUE items inside one id.
* These items even can be edible, have custom behaviours, be electric or act like fluid containers!
* They can also have different burn time, plus be handheld, oredicted or invisible!
* They also can be reactor components.
* MetaItem is item that can have up to Short.MAX_VALUE items inside one id. These items even can be edible, have custom
* behaviours, be electric or act like fluid containers! They can also have different burn time, plus be handheld,
* oredicted or invisible! They also can be reactor components.
* <p>
* You can also extend this class and occupy some of it's MetaData, and just pass an meta offset in constructor, and
* everything will work properly.
* <p>
* Items are added in MetaItem via {@link #addItem(int, String)}. You will get {@link MetaValueItem} instance, which you
* can configure in builder-alike pattern:
* {@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
* {@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
*/
@Optional.Interface(modid = GTValues.MODID_ECORE, iface = "com.enderio.core.common.interfaces.IOverlayRenderAware")
public abstract class MetaItem<T extends MetaItem<?>.MetaValueItem> extends Item
Expand All @@ -110,8 +116,9 @@ public static List<MetaItem<?>> getMetaItems() {
private CreativeTabs[] defaultCreativeTabs = new CreativeTabs[] { GregTechAPI.TAB_GREGTECH };
private final Set<CreativeTabs> additionalCreativeTabs = new ObjectArraySet<>();

private String translationKey = "metaitem";

public MetaItem(short metaItemOffset) {
setTranslationKey("meta_item");
setHasSubtypes(true);
this.metaItemOffset = metaItemOffset;
META_ITEMS.add(this);
Expand Down Expand Up @@ -354,6 +361,7 @@ public void onPlayerStoppedUsing(@NotNull ItemStack stack, @NotNull World world,
}
}

@NotNull
@Override
public ItemStack onItemUseFinish(@NotNull ItemStack stack, @NotNull World world, @NotNull EntityLivingBase player) {
if (player instanceof EntityPlayer) {
Expand Down Expand Up @@ -527,10 +535,28 @@ public boolean shouldCauseReequipAnimation(@NotNull ItemStack oldStack, @NotNull
return !ItemStack.areItemStacksEqual(oldStack, newStack);
}

@NotNull
@Override
public String getTranslationKey(ItemStack stack) {
T metaItem = getItem(stack);
return metaItem == null ? getTranslationKey() : getTranslationKey() + "." + metaItem.unlocalizedName;
public MetaItem<T> setTranslationKey(@NotNull String key) {
this.translationKey = Objects.requireNonNull(key, "key == null");
return this;
}

@NotNull
@Override
public String getTranslationKey() {
return getTranslationKey((T) null);
}

@NotNull
@Override
public String getTranslationKey(@NotNull ItemStack stack) {
return getTranslationKey(getItem(stack));
}

@NotNull
protected String getTranslationKey(@Nullable T metaValueItem) {
return metaValueItem == null ? this.translationKey : this.translationKey + "." + metaValueItem.unlocalizedName;
}

@NotNull
Expand All @@ -541,7 +567,7 @@ public String getItemStackDisplayName(ItemStack stack) {
if (item == null) {
return "invalid item";
}
String unlocalizedName = String.format("metaitem.%s.name", item.unlocalizedName);
String unlocalizedName = getTranslationKey(item) + ".name";
if (item.getNameProvider() != null) {
return item.getNameProvider().getItemStackDisplayName(stack, unlocalizedName);
}
Expand All @@ -564,7 +590,7 @@ public void addInformation(@NotNull ItemStack itemStack, @Nullable World worldIn
@NotNull ITooltipFlag tooltipFlag) {
T item = getItem(itemStack);
if (item == null) return;
String unlocalizedTooltip = "metaitem." + item.unlocalizedName + ".tooltip";
String unlocalizedTooltip = getTranslationKey(item) + ".tooltip";
if (I18n.hasKey(unlocalizedTooltip)) {
Collections.addAll(lines, LocalizationUtils.formatLines(unlocalizedTooltip));
}
Expand Down Expand Up @@ -660,25 +686,27 @@ public ItemStack getContainerItem(@NotNull ItemStack itemStack) {

@NotNull
@Override
public CreativeTabs[] getCreativeTabs() {
public CreativeTabs @NotNull [] getCreativeTabs() {
if (additionalCreativeTabs.isEmpty()) return defaultCreativeTabs; // short circuit
Set<CreativeTabs> tabs = new ObjectArraySet<>(additionalCreativeTabs);
tabs.addAll(Arrays.asList(defaultCreativeTabs));
return tabs.toArray(new CreativeTabs[0]);
}

@NotNull
@Override
public MetaItem<T> setCreativeTab(CreativeTabs tab) {
public MetaItem<T> setCreativeTab(@NotNull CreativeTabs tab) {
this.defaultCreativeTabs = new CreativeTabs[] { tab };
return this;
}

public MetaItem<T> setCreativeTabs(CreativeTabs... tabs) {
@NotNull
public MetaItem<T> setCreativeTabs(@NotNull CreativeTabs @NotNull... tabs) {
this.defaultCreativeTabs = tabs;
return this;
}

public void addAdditionalCreativeTabs(CreativeTabs... tabs) {
public void addAdditionalCreativeTabs(@NotNull CreativeTabs @NotNull... tabs) {
for (CreativeTabs tab : tabs) {
if (!ArrayUtils.contains(defaultCreativeTabs, tab) && tab != CreativeTabs.SEARCH) {
additionalCreativeTabs.add(tab);
Expand All @@ -687,7 +715,7 @@ public void addAdditionalCreativeTabs(CreativeTabs... tabs) {
}

@Override
protected boolean isInCreativeTab(CreativeTabs tab) {
protected boolean isInCreativeTab(@NotNull CreativeTabs tab) {
return tab == CreativeTabs.SEARCH ||
ArrayUtils.contains(defaultCreativeTabs, tab) ||
additionalCreativeTabs.contains(tab);
Expand Down

0 comments on commit 6e1a167

Please sign in to comment.