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

Make translation key of MetaItems modifiable #2259

Merged
merged 2 commits into from
Jan 11, 2024
Merged
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
56 changes: 39 additions & 17 deletions src/main/java/gregtech/api/items/metaitem/MetaItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,21 @@
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 @@ -132,8 +132,9 @@ public static List<MetaItem<?>> getMetaItems() {
private CreativeTabs[] defaultCreativeTabs = new CreativeTabs[] { GTCreativeTabs.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 @@ -376,6 +377,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 @@ -549,10 +551,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 @@ -563,7 +583,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 @@ -586,7 +606,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 @@ -682,25 +702,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 @@ -709,7 +731,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