2021-08-14 06:11:12 +02:00
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
2022-11-20 00:53:20 +01:00
== AT ==
public org.bukkit.craftbukkit.inventory.CraftMetaFirework
2024-04-26 20:39:20 +02:00
public org.bukkit.craftbukkit.inventory.CraftMetaFirework power
public org.bukkit.craftbukkit.inventory.CraftMetaFirework getNBT(Lorg/bukkit/FireworkEffect$Type;)Lnet/minecraft/world/item/component/FireworkExplosion$Shape;
2022-11-20 00:53:20 +01:00
2021-08-14 06:11:12 +02:00
Co-authored-by: MeFisto94 <MeFisto94@users.noreply.github.com>
2024-11-09 23:26:01 +01:00
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftMusicInstrument.java b/src/main/java/org/bukkit/craftbukkit/CraftMusicInstrument.java
index fa0a3f895f12a33938222d354caf13857bdd6955..fb8f68f1aedfb26e4d95fe5bad87f0f2cc91c287 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftMusicInstrument.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftMusicInstrument.java
@@ -68,6 +68,16 @@ public class CraftMusicInstrument extends MusicInstrument implements Handleable<
return this.key;
}
+ // Paper start - add translationKey methods
+ @Override
+ public @NotNull String translationKey() {
+ if (!(this.getHandle().description().getContents() instanceof final net.minecraft.network.chat.contents.TranslatableContents translatableContents)) {
+ throw new UnsupportedOperationException("Description isn't translatable!"); // Paper
+ }
+ return translatableContents.getKey();
+ }
+ // Paper end - add translationKey methods
+
@Override
public boolean equals(Object other) {
if (this == other) {
2024-10-31 23:44:34 +01:00
diff --git a/src/main/java/org/bukkit/craftbukkit/attribute/CraftAttribute.java b/src/main/java/org/bukkit/craftbukkit/attribute/CraftAttribute.java
index 8797a47be16b85dc69a28a56bb355bee7d6b1a78..9ab615347e241ac264fb70f43306075907420885 100644
--- a/src/main/java/org/bukkit/craftbukkit/attribute/CraftAttribute.java
+++ b/src/main/java/org/bukkit/craftbukkit/attribute/CraftAttribute.java
@@ -98,6 +98,11 @@ public class CraftAttribute implements Attribute, Handleable<net.minecraft.world
return this.attributeBase.getDescriptionId();
}
+ @Override
+ public @NotNull String translationKey() {
+ return this.attributeBase.getDescriptionId();
+ }
+
@Override
public int compareTo(@NotNull Attribute attribute) {
return this.ordinal - attribute.ordinal();
2021-08-14 06:11:12 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java b/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java
2024-10-23 16:55:24 +02:00
index aae00320ab8003420bae5de7df47f553b62c5aab..3fa3de9a89550ec2fcb8ca663742826c0c3136b6 100644
2021-08-14 06:11:12 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java
+++ b/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java
2023-12-06 04:00:14 +01:00
@@ -669,5 +669,10 @@ public class CraftBlock implements Block {
2022-04-30 22:44:22 +02:00
public org.bukkit.SoundGroup getBlockSoundGroup() {
return org.bukkit.craftbukkit.CraftSoundGroup.getSoundGroup(this.getNMS().getSoundType());
2021-08-14 06:11:12 +02:00
}
+
+ @Override
+ public String translationKey() {
2023-02-15 23:10:14 +01:00
+ return this.getNMS().getBlock().getDescriptionId();
2021-08-14 06:11:12 +02:00
+ }
// Paper end
}
2024-05-23 19:32:02 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftBlockType.java b/src/main/java/org/bukkit/craftbukkit/block/CraftBlockType.java
2024-07-18 10:13:20 +02:00
index 338a8f4acf413ef24fedab60c19c7a51a0ea19a6..2d8a509446c0ed0d7358f10f67ef29c4df683696 100644
2024-05-23 19:32:02 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/block/CraftBlockType.java
+++ b/src/main/java/org/bukkit/craftbukkit/block/CraftBlockType.java
2024-07-18 10:13:20 +02:00
@@ -234,4 +234,11 @@ public class CraftBlockType<B extends BlockData> implements BlockType.Typed<B>,
2024-05-23 19:32:02 +02:00
public Material asMaterial() {
return Registry.MATERIAL.get(this.key);
}
+
+ // Paper start - add Translatable
+ @Override
+ public String translationKey() {
+ return this.block.getDescriptionId();
+ }
+ // Paper end - add Translatable
}
2021-08-14 06:11:12 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/enchantments/CraftEnchantment.java b/src/main/java/org/bukkit/craftbukkit/enchantments/CraftEnchantment.java
2024-08-17 22:58:36 +02:00
index f73017bff613bd62b86c974b29576e241c24c927..59c9c970b83f62245d860994c4ac0c21dcc15398 100644
2021-08-14 06:11:12 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/enchantments/CraftEnchantment.java
+++ b/src/main/java/org/bukkit/craftbukkit/enchantments/CraftEnchantment.java
2024-08-17 22:58:36 +02:00
@@ -152,6 +152,17 @@ public class CraftEnchantment extends Enchantment implements Handleable<net.mine
2021-08-14 06:11:12 +02:00
}
2024-08-17 22:58:36 +02:00
// Paper end
+ // Paper start - add translationKey methods
2021-08-14 06:11:12 +02:00
+ @Override
+ public String translationKey() {
2024-08-17 22:58:36 +02:00
+ if (!(this.getHandle().description().getContents() instanceof final net.minecraft.network.chat.contents.TranslatableContents translatableContents)) {
+ throw new UnsupportedOperationException("Description isn't translatable!"); // Paper
+ }
+ return translatableContents.getKey();
+
2021-08-14 06:11:12 +02:00
+ }
2024-08-17 22:58:36 +02:00
+ // Paper end - add translationKey methods
+
2023-12-06 04:00:14 +01:00
@Override
2024-08-17 22:58:36 +02:00
public String getTranslationKey() {
return Util.makeDescriptionId("enchantment", this.handle.unwrapKey().get().location());
2024-05-23 19:32:02 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemType.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemType.java
2024-10-23 16:55:24 +02:00
index 0e9d3d823608a694ef1de5c4fec593951d678c1a..68756419ac6ee292db9569eab380a5c14d748002 100644
2024-05-23 19:32:02 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemType.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemType.java
2024-10-23 16:55:24 +02:00
@@ -237,4 +237,11 @@ public class CraftItemType<M extends ItemMeta> implements ItemType.Typed<M>, Han
2024-05-23 19:32:02 +02:00
public Material asMaterial() {
return Registry.MATERIAL.get(this.key);
}
+
+ // Paper start - add Translatable
+ @Override
+ public String translationKey() {
+ return this.item.getDescriptionId();
+ }
+ // Paper end - add Translatable
}
2024-04-26 20:39:20 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaFirework.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaFirework.java
2024-09-19 16:36:07 +02:00
index 4921fc085c9d60c74028ef390325e26c598e8df1..4941e0afff8df5f10f06c715b54bf58eb86051c5 100644
2024-04-26 20:39:20 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaFirework.java
+++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaFirework.java
2024-08-17 21:39:11 +02:00
@@ -123,7 +123,7 @@ class CraftMetaFirework extends CraftMetaItem implements FireworkMeta {
2024-04-26 20:39:20 +02:00
return new FireworkExplosion(CraftMetaFirework.getNBT(effect.getType()), colors, fadeColors, effect.hasTrail(), effect.hasFlicker());
}
- static FireworkExplosion.Shape getNBT(Type type) {
+ public static FireworkExplosion.Shape getNBT(Type type) { // Paper - package-private -> public
switch (type) {
case BALL:
return FireworkExplosion.Shape.SMALL_BALL;
2021-08-14 06:11:12 +02:00
diff --git a/src/test/java/io/papermc/paper/world/TranslationKeyTest.java b/src/test/java/io/papermc/paper/world/TranslationKeyTest.java
2024-11-09 23:26:01 +01:00
index 7f8b6462d2a1bbd39a870d2543bebc135f7eb45b..c843de24f06dcdee474e9a21d0a3dc54b7b3557a 100644
2021-08-14 06:11:12 +02:00
--- a/src/test/java/io/papermc/paper/world/TranslationKeyTest.java
+++ b/src/test/java/io/papermc/paper/world/TranslationKeyTest.java
2024-10-25 19:15:40 +02:00
@@ -1,11 +1,32 @@
2022-06-08 17:31:27 +02:00
package io.papermc.paper.world;
2022-06-02 02:07:10 +02:00
import com.destroystokyo.paper.ClientOption;
2024-06-13 16:45:27 +02:00
+import java.util.Locale;
2022-12-08 19:23:12 +01:00
+import java.util.Map;
2024-10-25 19:15:40 +02:00
+import net.minecraft.core.registries.Registries;
2022-06-08 17:31:27 +02:00
+import net.minecraft.network.chat.contents.TranslatableContents;
2022-07-20 18:23:26 +02:00
+import net.minecraft.resources.ResourceKey;
2023-07-29 00:06:27 +02:00
+import net.minecraft.resources.ResourceLocation;
2022-06-02 02:07:10 +02:00
import net.minecraft.world.entity.player.ChatVisiblity;
2024-10-25 19:15:40 +02:00
+import net.minecraft.world.flag.FeatureFlags;
+import net.minecraft.world.level.GameRules;
2022-06-02 03:30:29 +02:00
+import net.minecraft.world.level.GameType;
2022-07-20 18:23:26 +02:00
+import net.minecraft.world.level.biome.Biome;
2022-06-02 02:07:10 +02:00
import org.bukkit.Difficulty;
+import org.bukkit.FireworkEffect;
2022-06-02 03:30:29 +02:00
+import org.bukkit.GameMode;
2022-06-02 02:07:10 +02:00
+import org.bukkit.GameRule;
2023-07-29 00:06:27 +02:00
+import org.bukkit.MusicInstrument;
2022-06-02 02:07:10 +02:00
+import org.bukkit.attribute.Attribute;
2024-10-25 19:15:40 +02:00
+import org.bukkit.craftbukkit.CraftWorld;
2023-07-29 00:06:27 +02:00
+import org.bukkit.craftbukkit.util.CraftNamespacedKey;
2024-10-21 00:06:54 +02:00
+import org.bukkit.support.RegistryHelper;
+import org.bukkit.support.environment.AllFeatures;
2023-09-24 09:16:58 +02:00
import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
2021-08-14 06:11:12 +02:00
2024-10-21 00:06:54 +02:00
+@AllFeatures
public class TranslationKeyTest {
2021-08-14 06:11:12 +02:00
@Test
2024-11-09 23:26:01 +01:00
@@ -15,4 +36,61 @@ public class TranslationKeyTest {
2023-09-25 01:05:05 +02:00
Assertions.assertEquals(ChatVisiblity.valueOf(chatVisibility.name()).getKey(), chatVisibility.translationKey(), chatVisibility + "'s translation key doesn't match");
2021-08-14 06:11:12 +02:00
}
}
+
+ @Test
+ public void testDifficultyKeys() {
+ for (Difficulty bukkitDifficulty : Difficulty.values()) {
2023-09-25 01:05:05 +02:00
+ Assertions.assertEquals(((TranslatableContents) net.minecraft.world.Difficulty.byId(bukkitDifficulty.ordinal()).getDisplayName().getContents()).getKey(), bukkitDifficulty.translationKey(), bukkitDifficulty + "'s translation key doesn't match");
2021-08-14 06:11:12 +02:00
+ }
+ }
+
+ @Test
+ public void testGameruleKeys() {
2024-10-25 19:15:40 +02:00
+ final Map<String, GameRules.Key<?>> gameRules = CraftWorld.getGameRulesNMS(new GameRules(FeatureFlags.REGISTRY.allFlags()));
2022-06-02 02:07:10 +02:00
+ for (GameRule<?> rule : GameRule.values()) {
2024-10-25 19:15:40 +02:00
+ Assertions.assertEquals(gameRules.get(rule.getName()).getDescriptionId(), rule.translationKey(), rule.getName() + "'s translation doesn't match");
2021-08-14 06:11:12 +02:00
+ }
+ }
+
+ @Test
+ public void testAttributeKeys() {
2022-06-02 02:07:10 +02:00
+ for (Attribute attribute : Attribute.values()) {
2023-09-25 01:05:05 +02:00
+ Assertions.assertEquals(org.bukkit.craftbukkit.attribute.CraftAttribute.bukkitToMinecraft(attribute).getDescriptionId(), attribute.translationKey(), "translation key mismatch for " + attribute);
2021-08-14 06:11:12 +02:00
+ }
+ }
+
+ @Test
+ public void testFireworkEffectType() {
2024-04-26 20:39:20 +02:00
+ for (final FireworkEffect.Type type : FireworkEffect.Type.values()) {
+ final net.minecraft.world.item.component.FireworkExplosion.Shape nmsType = org.bukkit.craftbukkit.inventory.CraftMetaFirework.getNBT(type);
+ Assertions.assertTrue(nmsType.getName().getContents() instanceof TranslatableContents, "contents aren't translatable");
+ Assertions.assertEquals(((TranslatableContents) nmsType.getName().getContents()).getKey(), type.translationKey(), "translation key mismatch for " + type);
2021-08-14 06:11:12 +02:00
+ }
2022-06-02 02:07:10 +02:00
+ }
+
+ @Test
2023-09-24 09:16:58 +02:00
+ @Disabled // TODO fix
2022-06-02 02:07:10 +02:00
+ public void testCreativeCategory() {
2022-12-08 19:23:12 +01:00
+ // for (CreativeModeTab tab : CreativeModeTabs.tabs()) {
+ // CreativeCategory category = Objects.requireNonNull(CraftCreativeCategory.fromNMS(tab));
2023-09-24 09:16:58 +02:00
+ // Assertions.assertEquals("translation key mismatch for " + category, ((TranslatableContents) tab.getDisplayName().getContents()).getKey(), category.translationKey());
2022-12-08 19:23:12 +01:00
+ // }
2022-06-02 03:30:29 +02:00
+ }
+
+ @Test
+ public void testGameMode() {
+ for (GameType nms : GameType.values()) {
+ GameMode bukkit = GameMode.getByValue(nms.getId());
2023-09-24 09:16:58 +02:00
+ Assertions.assertNotNull(bukkit);
2023-09-25 01:05:05 +02:00
+ Assertions.assertEquals(((TranslatableContents) nms.getLongDisplayName().getContents()).getKey(), bukkit.translationKey(), "translation key mismatch for " + bukkit);
2022-06-02 03:30:29 +02:00
+ }
2022-07-20 18:23:26 +02:00
+ }
+
+ @Test
+ public void testBiome() {
2024-10-21 00:06:54 +02:00
+ for (Map.Entry<ResourceKey<Biome>, Biome> nms : RegistryHelper.getBiomes().entrySet()) {
2024-06-13 16:45:27 +02:00
+ org.bukkit.block.Biome bukkit = org.bukkit.block.Biome.valueOf(nms.getKey().location().getPath().toUpperCase(Locale.ROOT));
2023-09-25 01:05:05 +02:00
+ Assertions.assertEquals(nms.getKey().location().toLanguageKey("biome"), bukkit.translationKey(), "translation key mismatch for " + bukkit);
2022-07-20 18:23:26 +02:00
+ }
2021-08-14 06:11:12 +02:00
+ }
}