Add ItemStack#getTranslationKey() (#5616)

This commit is contained in:
Jake Potrebic 2021-05-12 04:12:28 -07:00
parent c0a1161311
commit ff1d29c40b
7 changed files with 61 additions and 19 deletions

View file

@ -37,25 +37,32 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+
+ /**
+ * Return the translation key for the Material, so the client can translate it into the active
+ * locale when using a TranslatableComponent.
+ * locale when using a {@link net.kyori.adventure.text.TranslatableComponent}.
+ * @return the translation key
+ */
+ String getTranslationKey(Material mat);
+
+ /**
+ * Return the translation key for the Block, so the client can translate it into the active
+ * locale when using a TranslatableComponent.
+ * locale when using a {@link net.kyori.adventure.text.TranslatableComponent}.
+ * @return the translation key
+ */
+ String getTranslationKey(org.bukkit.block.Block block);
+
+ /**
+ * Return the translation key for the EntityType, so the client can translate it into the active
+ * locale when using a TranslatableComponent.<br>
+ * locale when using a {@link net.kyori.adventure.text.TranslatableComponent}.<br>
+ * This is <code>null</code>, when the EntityType isn't known to NMS (custom entities)
+ * @return the translation key
+ */
+ String getTranslationKey(org.bukkit.entity.EntityType type);
+
+ /**
+ * Return the translation key for the ItemStack, so the client can translate it into the active
+ * locale when using a {@link net.kyori.adventure.text.TranslatableComponent}.<br>
+ * @return the translation key
+ */
+ String getTranslationKey(ItemStack itemStack);
// Paper end
}
diff --git a/src/main/java/org/bukkit/block/Block.java b/src/main/java/org/bukkit/block/Block.java
@ -96,3 +103,25 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ return org.bukkit.Bukkit.getUnsafe().getTranslationKey(this);
+ }
}
diff --git a/src/main/java/org/bukkit/inventory/ItemStack.java b/src/main/java/org/bukkit/inventory/ItemStack.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/org/bukkit/inventory/ItemStack.java
+++ b/src/main/java/org/bukkit/inventory/ItemStack.java
@@ -0,0 +0,0 @@ public class ItemStack implements Cloneable, ConfigurationSerializable, net.kyor
ItemMeta itemMeta = getItemMeta();
return itemMeta != null && itemMeta.hasItemFlag(flag);
}
+
+ /**
+ * Gets the translation key for this itemstack.
+ * This is not the same as getting the translation key
+ * for the material of this itemstack.
+ *
+ * @return the translation key
+ */
+ @NotNull
+ public String getTranslationKey() {
+ return Bukkit.getUnsafe().getTranslationKey(this);
+ }
// Paper end
}

View file

@ -12,7 +12,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
@@ -0,0 +0,0 @@ public interface UnsafeValues {
* @return the translation key
*/
String getTranslationKey(org.bukkit.entity.EntityType type);
String getTranslationKey(ItemStack itemStack);
+
+ /**
+ * Creates and returns the next EntityId available.
@ -20,6 +20,5 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ * Use this when sending custom packets, so that there are no collisions on the client or server.
+ */
+ public int nextEntityId();
+
// Paper end
}

View file

@ -65,9 +65,10 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
--- a/src/main/java/org/bukkit/UnsafeValues.java
+++ b/src/main/java/org/bukkit/UnsafeValues.java
@@ -0,0 +0,0 @@ public interface UnsafeValues {
* Use this when sending custom packets, so that there are no collisions on the client or server.
*/
public int nextEntityId();
+
+ /**
+ * Gets the item rarity of a material. The material <b>MUST</b> be an item.
+ * Use {@link Material#isItem()} before this.
@ -91,8 +92,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
--- a/src/main/java/org/bukkit/inventory/ItemStack.java
+++ b/src/main/java/org/bukkit/inventory/ItemStack.java
@@ -0,0 +0,0 @@ public class ItemStack implements Cloneable, ConfigurationSerializable, net.kyor
ItemMeta itemMeta = getItemMeta();
return itemMeta != null && itemMeta.hasItemFlag(flag);
public String getTranslationKey() {
return Bukkit.getUnsafe().getTranslationKey(this);
}
+
+ /**

View file

@ -46,6 +46,14 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
protected String m() {
if (this.name == null) {
this.name = SystemUtils.a("item", IRegistry.ITEM.getKey(this));
@@ -0,0 +0,0 @@ public class Item implements IMaterial {
return this.m();
}
+ public String getDescriptionId(ItemStack itemStack) { return f(itemStack); } // Paper - OBFHELPER
public String f(ItemStack itemstack) {
return this.getName();
}
diff --git a/src/main/java/net/minecraft/world/level/block/Block.java b/src/main/java/net/minecraft/world/level/block/Block.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/world/level/block/Block.java
@ -54,7 +62,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
return !this.material.isBuildable() && !this.material.isLiquid();
}
+ public String getDescriptionId() { return i(); } // Paper - OBFHELPER
+ public String getOrCreateDescriptionId() { return i(); } // Paper - OBFHELPER
public String i() {
if (this.name == null) {
this.name = SystemUtils.a("block", IRegistry.BLOCK.getKey(this));
@ -93,14 +101,14 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ @Override
+ public String getTranslationKey(Material mat) {
+ if (mat.isBlock()) {
+ return getBlock(mat).getDescriptionId();
+ return getBlock(mat).getOrCreateDescriptionId();
+ }
+ return getItem(mat).getOrCreateDescriptionId();
+ return getItem(mat).getName();
+ }
+
+ @Override
+ public String getTranslationKey(org.bukkit.block.Block block) {
+ return ((org.bukkit.craftbukkit.block.CraftBlock)block).getNMS().getBlock().getDescriptionId();
+ return ((org.bukkit.craftbukkit.block.CraftBlock)block).getNMS().getBlock().getOrCreateDescriptionId();
+ }
+
+ @Override
@ -108,6 +116,11 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ return net.minecraft.world.entity.EntityTypes.getByName(type.getName()).map(net.minecraft.world.entity.EntityTypes::getDescriptionId).orElse(null);
+ }
+
+ @Override
+ public String getTranslationKey(org.bukkit.inventory.ItemStack itemStack) {
+ net.minecraft.world.item.ItemStack nmsItemStack = CraftItemStack.asNMSCopy(itemStack);
+ return nmsItemStack.getItem().getDescriptionId(nmsItemStack);
+ }
// Paper end
/**

View file

@ -9,14 +9,14 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
+++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
@@ -0,0 +0,0 @@ public final class CraftMagicNumbers implements UnsafeValues {
public io.papermc.paper.inventory.ItemRarity getItemStackRarity(org.bukkit.inventory.ItemStack itemStack) {
return io.papermc.paper.inventory.ItemRarity.values()[getItem(itemStack.getType()).getItemStackRarity(CraftItemStack.asNMSCopy(itemStack)).ordinal()];
}
+
+ @Override
+ public int getProtocolVersion() {
+ return net.minecraft.SharedConstants.getGameVersion().getProtocolVersion();
+ }
+
// Paper end
/**

View file

@ -25,13 +25,13 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
+++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
@@ -0,0 +0,0 @@ public final class CraftMagicNumbers implements UnsafeValues {
return net.minecraft.world.entity.EntityTypes.getByName(type.getName()).map(net.minecraft.world.entity.EntityTypes::getDescriptionId).orElse(null);
net.minecraft.world.item.ItemStack nmsItemStack = CraftItemStack.asNMSCopy(itemStack);
return nmsItemStack.getItem().getDescriptionId(nmsItemStack);
}
+
+ public int nextEntityId() {
+ return net.minecraft.world.entity.Entity.nextEntityId();
+ }
+
// Paper end
/**

View file

@ -30,9 +30,10 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
+++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
@@ -0,0 +0,0 @@ public final class CraftMagicNumbers implements UnsafeValues {
public int nextEntityId() {
return net.minecraft.world.entity.Entity.nextEntityId();
}
+
+ @Override
+ public io.papermc.paper.inventory.ItemRarity getItemRarity(org.bukkit.Material material) {
+ Item item = getItem(material);
@ -46,7 +47,6 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ public io.papermc.paper.inventory.ItemRarity getItemStackRarity(org.bukkit.inventory.ItemStack itemStack) {
+ return io.papermc.paper.inventory.ItemRarity.values()[getItem(itemStack.getType()).getItemStackRarity(CraftItemStack.asNMSCopy(itemStack)).ordinal()];
+ }
+
// Paper end
/**