From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: MeFisto94 Date: Tue, 11 Aug 2020 19:16:09 +0200 Subject: [PATCH] Add a way to get translation keys for blocks, entities and materials diff --git a/src/main/java/net/minecraft/world/entity/EntityTypes.java b/src/main/java/net/minecraft/world/entity/EntityTypes.java index 9d2955f05aadd4bbc6dcfec068a55d7fe6950ba0..f2cf33d42839710a3bbdf0c8ea0be28af6fcb19d 100644 --- a/src/main/java/net/minecraft/world/entity/EntityTypes.java +++ b/src/main/java/net/minecraft/world/entity/EntityTypes.java @@ -278,6 +278,7 @@ public class EntityTypes { return IRegistry.ENTITY_TYPE.getKey(entitytypes); } + public static Optional> getByName(String name) { return a(name); } // Paper - OBFHELPER public static Optional> a(String s) { return IRegistry.ENTITY_TYPE.getOptional(MinecraftKey.a(s)); } @@ -431,6 +432,7 @@ public class EntityTypes { return this.bg; } + public String getDescriptionId() { return f(); } // Paper - OBFHELPER public String f() { if (this.bo == null) { this.bo = SystemUtils.a("entity", IRegistry.ENTITY_TYPE.getKey(this)); diff --git a/src/main/java/net/minecraft/world/item/Item.java b/src/main/java/net/minecraft/world/item/Item.java index ca513e7b0a416860aba89e41de6a7c5ff42baa69..467121839b94e09f59c64300de9f26d3c6caf1e5 100644 --- a/src/main/java/net/minecraft/world/item/Item.java +++ b/src/main/java/net/minecraft/world/item/Item.java @@ -56,7 +56,7 @@ public class Item implements IMaterial { private final FoodInfo foodInfo; public static int getId(Item item) { - return item == null ? 0 : IRegistry.ITEM.a((Object) item); + return item == null ? 0 : IRegistry.ITEM.a(item); // Paper - Fix Decompiler Issue } public static Item getById(int i) { @@ -152,6 +152,7 @@ public class Item implements IMaterial { return IRegistry.ITEM.getKey(this).getKey(); } + public String getOrCreateDescriptionId() { return m(); } // Paper - OBFHELPER protected String m() { if (this.name == null) { this.name = SystemUtils.a("item", IRegistry.ITEM.getKey(this)); @@ -164,6 +165,7 @@ 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 cc512bd2e89382e7fdbc59b41640e95ccafbbfe9..d4903842aebeff3e32dbf7d3f4b670af4ebad174 100644 --- a/src/main/java/net/minecraft/world/level/block/Block.java +++ b/src/main/java/net/minecraft/world/level/block/Block.java @@ -318,6 +318,7 @@ public class Block extends BlockBase implements IMaterial { return !this.material.isBuildable() && !this.material.isLiquid(); } + public String getOrCreateDescriptionId() { return i(); } // Paper - OBFHELPER public String i() { if (this.name == null) { this.name = SystemUtils.a("block", IRegistry.BLOCK.getKey(this)); diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java b/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java index e3ab0b76e5003553b29215a43bc5a762f2663648..ee8977a1e4a83598ba7873c4c482fea828c6b26c 100644 --- a/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java +++ b/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java @@ -764,5 +764,10 @@ public class CraftBlock implements Block { public com.destroystokyo.paper.block.BlockSoundGroup getSoundGroup() { return new com.destroystokyo.paper.block.CraftBlockSoundGroup(getNMSBlock().getBlockData().getStepSound()); } + + @Override + public String getTranslationKey() { + return org.bukkit.Bukkit.getUnsafe().getTranslationKey(this); + } // Paper end } diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java index 2519dbce9717ff647d50c31aed6aca68b9f4e3af..8049063ea37f3e0eac3f3a26703adc80fc99341a 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java +++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java @@ -45,6 +45,7 @@ import org.bukkit.Registry; import org.bukkit.UnsafeValues; import org.bukkit.advancement.Advancement; import org.bukkit.block.data.BlockData; +import org.bukkit.craftbukkit.block.CraftBlock; import org.bukkit.craftbukkit.block.data.CraftBlockData; import org.bukkit.craftbukkit.inventory.CraftItemStack; import org.bukkit.craftbukkit.legacy.CraftLegacy; @@ -420,6 +421,30 @@ public final class CraftMagicNumbers implements UnsafeValues { throw new RuntimeException(); } } + + @Override + public String getTranslationKey(Material mat) { + if (mat.isBlock()) { + return getBlock(mat).getOrCreateDescriptionId(); + } + return getItem(mat).getName(); + } + + @Override + public String getTranslationKey(org.bukkit.block.Block block) { + return ((org.bukkit.craftbukkit.block.CraftBlock)block).getNMS().getBlock().getOrCreateDescriptionId(); + } + + @Override + public String getTranslationKey(org.bukkit.entity.EntityType type) { + 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 /**