aboutsummaryrefslogtreecommitdiffhomepage
path: root/patches/server/0495-Add-methods-to-get-translation-keys.patch
blob: 8463983f5379bae93125e03fb57a4bb91e6f5537 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Tue, 11 Aug 2020 19:16:09 +0200
Subject: [PATCH] Add methods to get translation keys

Co-authored-by: MeFisto94 <MeFisto94@users.noreply.github.com>

diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java b/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java
index ea3358905dcd39a1a855d98570457c65dcd7513d..396fb96abed1e77893d8ee8a15cff18cad804475 100644
--- a/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java
+++ b/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java
@@ -668,5 +668,15 @@ public class CraftBlock implements Block {
     public org.bukkit.SoundGroup getBlockSoundGroup() {
         return org.bukkit.craftbukkit.CraftSoundGroup.getSoundGroup(this.getNMS().getSoundType());
     }
+
+    @Override
+    public String getTranslationKey() {
+        return this.translationKey();
+    }
+
+    @Override
+    public String translationKey() {
+        return org.bukkit.Bukkit.getUnsafe().getTranslationKey(this);
+    }
     // Paper end
 }
diff --git a/src/main/java/org/bukkit/craftbukkit/enchantments/CraftEnchantment.java b/src/main/java/org/bukkit/craftbukkit/enchantments/CraftEnchantment.java
index eb99e0c2462a2d1ab4508a5c3f1580b6e31d7465..c536eceef3365a7b726cd970df345ba1d055207d 100644
--- a/src/main/java/org/bukkit/craftbukkit/enchantments/CraftEnchantment.java
+++ b/src/main/java/org/bukkit/craftbukkit/enchantments/CraftEnchantment.java
@@ -192,6 +192,11 @@ public class CraftEnchantment extends Enchantment {
     public net.kyori.adventure.text.Component displayName(int level) {
         return io.papermc.paper.adventure.PaperAdventure.asAdventure(getHandle().getFullname(level));
     }
+
+    @Override
+    public String translationKey() {
+        return this.target.getDescriptionId();
+    }
     // Paper end
 
     public net.minecraft.world.item.enchantment.Enchantment getHandle() {
diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
index 3b4764986302194882e009fe20a9d6406cf2be8e..008c15164a7affb785964f604f8fea93d28344ac 100644
--- a/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
+++ b/src/main/java/org/bukkit/craftbukkit/util/CraftMagicNumbers.java
@@ -478,6 +478,30 @@ public final class CraftMagicNumbers implements UnsafeValues {
         Preconditions.checkArgument(dataVersion <= getDataVersion(), "Newer version! Server downgrades are not supported!");
         return compound;
     }
+
+    @Override
+    public String getTranslationKey(Material mat) {
+        if (mat.isBlock()) {
+            return getBlock(mat).getDescriptionId();
+        }
+        return getItem(mat).getDescriptionId();
+    }
+
+    @Override
+    public String getTranslationKey(org.bukkit.block.Block block) {
+        return ((org.bukkit.craftbukkit.block.CraftBlock)block).getNMS().getBlock().getDescriptionId();
+    }
+
+    @Override
+    public String getTranslationKey(org.bukkit.entity.EntityType type) {
+        return net.minecraft.world.entity.EntityType.byString(type.getName()).map(net.minecraft.world.entity.EntityType::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/src/test/java/io/papermc/paper/world/TranslationKeyTest.java b/src/test/java/io/papermc/paper/world/TranslationKeyTest.java
index 6cd015dc5a2e012ac827c2b2d9aa5542b0591afb..a5a4026a09b45d7af70a56ce65b8382ac4b22efc 100644
--- a/src/test/java/io/papermc/paper/world/TranslationKeyTest.java
+++ b/src/test/java/io/papermc/paper/world/TranslationKeyTest.java
@@ -3,11 +3,20 @@ package io.papermc.paper.world;
 import com.destroystokyo.paper.ClientOption;
 import net.minecraft.network.chat.TranslatableComponent;
 import net.minecraft.world.entity.player.ChatVisiblity;
+import net.minecraft.world.item.CreativeModeTab;
 import org.bukkit.Difficulty;
+import org.bukkit.FireworkEffect;
+import org.bukkit.GameRule;
+import org.bukkit.attribute.Attribute;
+import org.bukkit.craftbukkit.inventory.CraftCreativeCategory;
+import org.bukkit.inventory.CreativeCategory;
+import org.bukkit.support.AbstractTestingBase;
 import org.junit.Assert;
 import org.junit.Test;
 
-public class TranslationKeyTest {
+import java.util.Objects;
+
+public class TranslationKeyTest extends AbstractTestingBase {
 
     @Test
     public void testChatVisibilityKeys() {
@@ -16,4 +25,43 @@ public class TranslationKeyTest {
             Assert.assertEquals(chatVisibility + "'s translation key doesn't match", ChatVisiblity.valueOf(chatVisibility.name()).getKey(), chatVisibility.translationKey());
         }
     }
+
+    @Test
+    public void testDifficultyKeys() {
+        for (Difficulty bukkitDifficulty : Difficulty.values()) {
+            Assert.assertEquals(bukkitDifficulty + "'s translation key doesn't match", ((TranslatableComponent) net.minecraft.world.Difficulty.byId(bukkitDifficulty.ordinal()).getDisplayName()).getKey(), bukkitDifficulty.translationKey());
+        }
+    }
+
+    @Test
+    public void testGameruleKeys() {
+        for (GameRule<?> rule : GameRule.values()) {
+            Assert.assertEquals(rule.getName() + "'s translation doesn't match", org.bukkit.craftbukkit.CraftWorld.getGameRulesNMS().get(rule.getName()).getDescriptionId(), rule.translationKey());
+        }
+    }
+
+    @Test
+    public void testAttributeKeys() {
+        for (Attribute attribute : Attribute.values()) {
+            Assert.assertEquals("translation key mismatch for " + attribute, org.bukkit.craftbukkit.attribute.CraftAttributeMap.toMinecraft(attribute).getDescriptionId(), attribute.translationKey());
+        }
+    }
+
+    @Test
+    public void testFireworkEffectType() {
+        for (FireworkEffect.Type type : FireworkEffect.Type.values()) {
+            Assert.assertEquals("translation key mismatch for " + type, net.minecraft.world.item.FireworkRocketItem.Shape.byId(org.bukkit.craftbukkit.inventory.CraftMetaFirework.getNBT(type)).getName(), org.bukkit.FireworkEffect.Type.NAMES.key(type));
+        }
+    }
+
+    @Test
+    public void testCreativeCategory() {
+        for (CreativeModeTab tab : CreativeModeTab.TABS) {
+            if (tab == CreativeModeTab.TAB_SEARCH || tab == CreativeModeTab.TAB_HOTBAR || tab == CreativeModeTab.TAB_INVENTORY) { // not implemented in the api
+                continue;
+            }
+            CreativeCategory category = Objects.requireNonNull(CraftCreativeCategory.fromNMS(tab));
+            Assert.assertEquals("translation key mismatch for " + category, ((TranslatableComponent) tab.getDisplayName()).getKey(), category.translationKey());
+        }
+    }
 }