#804: Added methods to get translation keys for materials, itemstacks and more

By: mfnalex <mfnalex@gmail.com>
This commit is contained in:
Bukkit/Spigot 2023-02-10 20:51:15 +11:00
parent cd28159b45
commit e17edb7785
6 changed files with 92 additions and 4 deletions

View file

@ -112,7 +112,7 @@ import org.jetbrains.annotations.Nullable;
/**
* An enum of all material IDs accepted by the official server and client
*/
public enum Material implements Keyed {
public enum Material implements Keyed, Translatable {
//<editor-fold desc="Materials" defaultstate="collapsed">
AIR(9648, 0),
STONE(22948),
@ -10566,4 +10566,49 @@ public enum Material implements Keyed {
return Bukkit.getUnsafe().getCreativeCategory(this);
}
/**
* Get the translation key of the item or block associated with this
* material.
*
* If this material has both an item and a block form, the item form is
* used.
*
* @return the translation key of the item or block associated with this
* material
* @see #getBlockTranslationKey()
* @see #getItemTranslationKey()
*/
@Override
@NotNull
public String getTranslationKey() {
if (this.isItem()) {
return Bukkit.getUnsafe().getItemTranslationKey(this);
} else {
return Bukkit.getUnsafe().getBlockTranslationKey(this);
}
}
/**
* Get the translation key of the block associated with this material, or
* null if this material does not have an associated block.
*
* @return the translation key of the block associated with this material,
* or null if this material does not have an associated block
*/
@Nullable
public String getBlockTranslationKey() {
return Bukkit.getUnsafe().getBlockTranslationKey(this);
}
/**
* Get the translation key of the item associated with this material, or
* null if this material does not have an associated item.
*
* @return the translation key of the item associated with this material, or
* null if this material does not have an associated item.
*/
@Nullable
public String getItemTranslationKey() {
return Bukkit.getUnsafe().getItemTranslationKey(this);
}
}

View file

@ -0,0 +1,18 @@
package org.bukkit;
import org.jetbrains.annotations.NotNull;
/**
* Represents an object with a text representation that can be translated by the
* Minecraft client.
*/
public interface Translatable {
/**
* Get the translation key, suitable for use in a translation component.
*
* @return the translation key
*/
@NotNull
String getTranslationKey();
}

View file

@ -5,6 +5,7 @@ import org.bukkit.advancement.Advancement;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.CreativeCategory;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
@ -78,4 +79,12 @@ public interface UnsafeValues {
Multimap<Attribute, AttributeModifier> getDefaultAttributeModifiers(Material material, EquipmentSlot slot);
CreativeCategory getCreativeCategory(Material material);
String getBlockTranslationKey(Material material);
String getItemTranslationKey(Material material);
String getTranslationKey(EntityType entityType);
String getTranslationKey(ItemStack itemStack);
}

View file

@ -5,6 +5,7 @@ import org.bukkit.Chunk;
import org.bukkit.FluidCollisionMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Translatable;
import org.bukkit.World;
import org.bukkit.block.data.Bisected;
import org.bukkit.block.data.BlockData;
@ -31,7 +32,7 @@ import org.jetbrains.annotations.Nullable;
* (i.e. lighting and power) may not be able to be safely accessed during world
* generation when used in cases like BlockPhysicsEvent!!!!
*/
public interface Block extends Metadatable {
public interface Block extends Metadatable, Translatable {
/**
* Gets the metadata for this block

View file

@ -3,9 +3,11 @@ package org.bukkit.entity;
import com.google.common.base.Preconditions;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.Keyed;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.Translatable;
import org.bukkit.World;
import org.bukkit.entity.minecart.CommandMinecart;
import org.bukkit.entity.minecart.ExplosiveMinecart;
@ -20,7 +22,7 @@ import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public enum EntityType implements Keyed {
public enum EntityType implements Keyed, Translatable {
// These strings MUST match the strings in nms.EntityTypes and are case sensitive.
/**
@ -425,4 +427,10 @@ public enum EntityType implements Keyed {
public boolean isAlive() {
return living;
}
@Override
@NotNull
public String getTranslationKey() {
return Bukkit.getUnsafe().getTranslationKey(this);
}
}

View file

@ -6,6 +6,7 @@ import java.util.LinkedHashMap;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Translatable;
import org.bukkit.Utility;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.enchantments.Enchantment;
@ -22,7 +23,7 @@ import org.jetbrains.annotations.Nullable;
* use this class to encapsulate Materials for which {@link Material#isItem()}
* returns false.</b>
*/
public class ItemStack implements Cloneable, ConfigurationSerializable {
public class ItemStack implements Cloneable, ConfigurationSerializable, Translatable {
private Material type = Material.AIR;
private int amount = 0;
private MaterialData data = null;
@ -595,4 +596,10 @@ public class ItemStack implements Cloneable, ConfigurationSerializable {
return true;
}
@Override
@NotNull
public String getTranslationKey() {
return Bukkit.getUnsafe().getTranslationKey(this);
}
}