From ff1d29c40bdfa76ceeff4424362e432379adb6f2 Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Wed, 12 May 2021 04:12:28 -0700 Subject: [PATCH] Add ItemStack#getTranslationKey() (#5616) --- ...-translation-keys-for-blocks-entitie.patch | 35 +++++++++++++++++-- ...y-Counter-to-allow-plugins-to-use-va.patch | 3 +- Spigot-API-Patches/Item-Rarity-API.patch | 7 ++-- ...-translation-keys-for-blocks-entitie.patch | 21 ++++++++--- .../Expose-protocol-version.patch | 4 +-- ...y-Counter-to-allow-plugins-to-use-va.patch | 6 ++-- Spigot-Server-Patches/Item-Rarity-API.patch | 4 +-- 7 files changed, 61 insertions(+), 19 deletions(-) diff --git a/Spigot-API-Patches/Add-a-way-to-get-translation-keys-for-blocks-entitie.patch b/Spigot-API-Patches/Add-a-way-to-get-translation-keys-for-blocks-entitie.patch index 6a1c52dfee..b2800e9f32 100644 --- a/Spigot-API-Patches/Add-a-way-to-get-translation-keys-for-blocks-entitie.patch +++ b/Spigot-API-Patches/Add-a-way-to-get-translation-keys-for-blocks-entitie.patch @@ -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.
++ * locale when using a {@link net.kyori.adventure.text.TranslatableComponent}.
+ * This is null, 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}.
++ * @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 + } diff --git a/Spigot-API-Patches/Expose-the-Entity-Counter-to-allow-plugins-to-use-va.patch b/Spigot-API-Patches/Expose-the-Entity-Counter-to-allow-plugins-to-use-va.patch index 879008e81a..de5800f633 100644 --- a/Spigot-API-Patches/Expose-the-Entity-Counter-to-allow-plugins-to-use-va.patch +++ b/Spigot-API-Patches/Expose-the-Entity-Counter-to-allow-plugins-to-use-va.patch @@ -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 } diff --git a/Spigot-API-Patches/Item-Rarity-API.patch b/Spigot-API-Patches/Item-Rarity-API.patch index a13b021173..dec20c0839 100644 --- a/Spigot-API-Patches/Item-Rarity-API.patch +++ b/Spigot-API-Patches/Item-Rarity-API.patch @@ -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 MUST 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); } + + /** diff --git a/Spigot-Server-Patches/Add-a-way-to-get-translation-keys-for-blocks-entitie.patch b/Spigot-Server-Patches/Add-a-way-to-get-translation-keys-for-blocks-entitie.patch index 9e92e01b34..b798100830 100644 --- a/Spigot-Server-Patches/Add-a-way-to-get-translation-keys-for-blocks-entitie.patch +++ b/Spigot-Server-Patches/Add-a-way-to-get-translation-keys-for-blocks-entitie.patch @@ -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 /** diff --git a/Spigot-Server-Patches/Expose-protocol-version.patch b/Spigot-Server-Patches/Expose-protocol-version.patch index 3433e03113..dafc87e28d 100644 --- a/Spigot-Server-Patches/Expose-protocol-version.patch +++ b/Spigot-Server-Patches/Expose-protocol-version.patch @@ -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 /** diff --git a/Spigot-Server-Patches/Expose-the-Entity-Counter-to-allow-plugins-to-use-va.patch b/Spigot-Server-Patches/Expose-the-Entity-Counter-to-allow-plugins-to-use-va.patch index 76a689e3fd..04af79fdae 100644 --- a/Spigot-Server-Patches/Expose-the-Entity-Counter-to-allow-plugins-to-use-va.patch +++ b/Spigot-Server-Patches/Expose-the-Entity-Counter-to-allow-plugins-to-use-va.patch @@ -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 /** diff --git a/Spigot-Server-Patches/Item-Rarity-API.patch b/Spigot-Server-Patches/Item-Rarity-API.patch index 84e941ddbc..4c3e4b9412 100644 --- a/Spigot-Server-Patches/Item-Rarity-API.patch +++ b/Spigot-Server-Patches/Item-Rarity-API.patch @@ -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 /**