From 42cd6c82fff109e068b424cb99e81cd4fc130488 Mon Sep 17 00:00:00 2001 From: Bukkit/Spigot Date: Fri, 27 Sep 2024 08:15:05 +1000 Subject: [PATCH] #1058: Add tests for Minecraft registry <-> Bukkit fields By: DerFrZocker --- .../src/main/java/org/bukkit/GameEvent.java | 8 +-- .../src/main/java/org/bukkit/JukeboxSong.java | 5 +- .../main/java/org/bukkit/MusicInstrument.java | 8 +-- .../src/main/java/org/bukkit/Registry.java | 60 +++++++++++++++++-- .../main/java/org/bukkit/block/BlockType.java | 16 +++-- .../org/bukkit/block/banner/PatternType.java | 6 +- .../java/org/bukkit/damage/DamageType.java | 5 +- .../org/bukkit/enchantments/Enchantment.java | 8 +-- .../src/main/java/org/bukkit/entity/Cat.java | 6 +- .../src/main/java/org/bukkit/entity/Frog.java | 6 +- .../main/java/org/bukkit/entity/Villager.java | 12 +--- .../src/main/java/org/bukkit/entity/Wolf.java | 6 +- .../bukkit/generator/structure/Structure.java | 5 +- .../generator/structure/StructureType.java | 6 +- .../java/org/bukkit/inventory/ItemType.java | 15 ++--- .../java/org/bukkit/inventory/MenuType.java | 6 +- .../inventory/meta/trim/TrimMaterial.java | 26 ++++---- .../inventory/meta/trim/TrimPattern.java | 42 +++++++------ .../main/java/org/bukkit/map/MapCursor.java | 6 +- .../org/bukkit/potion/PotionEffectType.java | 5 +- .../java/org/bukkit/support/TestServer.java | 11 ++++ 21 files changed, 147 insertions(+), 121 deletions(-) diff --git a/paper-api/src/main/java/org/bukkit/GameEvent.java b/paper-api/src/main/java/org/bukkit/GameEvent.java index 6c9689baca..cb5f7dfcdb 100644 --- a/paper-api/src/main/java/org/bukkit/GameEvent.java +++ b/paper-api/src/main/java/org/bukkit/GameEvent.java @@ -1,6 +1,5 @@ package org.bukkit; -import com.google.common.base.Preconditions; import com.google.common.collect.Lists; import java.util.Collection; import java.util.Collections; @@ -140,11 +139,6 @@ public abstract class GameEvent implements Keyed { @NotNull private static GameEvent getEvent(@NotNull String key) { - NamespacedKey namespacedKey = NamespacedKey.minecraft(key); - GameEvent gameEvent = Registry.GAME_EVENT.get(namespacedKey); - - Preconditions.checkNotNull(gameEvent, "No GameEvent found for %s. This is a bug.", namespacedKey); - - return gameEvent; + return Registry.GAME_EVENT.getOrThrow(NamespacedKey.minecraft(key)); } } diff --git a/paper-api/src/main/java/org/bukkit/JukeboxSong.java b/paper-api/src/main/java/org/bukkit/JukeboxSong.java index 3bf2743fbf..5872188c2f 100644 --- a/paper-api/src/main/java/org/bukkit/JukeboxSong.java +++ b/paper-api/src/main/java/org/bukkit/JukeboxSong.java @@ -1,6 +1,5 @@ package org.bukkit; -import java.util.Objects; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; @@ -31,7 +30,7 @@ public interface JukeboxSong extends Keyed, Translatable { public static final JukeboxSong CREATOR_MUSIC_BOX = get("creator_music_box"); @NotNull - private static JukeboxSong get(@NotNull String s) { - return Objects.requireNonNull(Registry.JUKEBOX_SONG.get(NamespacedKey.minecraft(s)), "Missing song " + s); + private static JukeboxSong get(@NotNull String key) { + return Registry.JUKEBOX_SONG.getOrThrow(NamespacedKey.minecraft(key)); } } diff --git a/paper-api/src/main/java/org/bukkit/MusicInstrument.java b/paper-api/src/main/java/org/bukkit/MusicInstrument.java index eae90e72b1..807e89d026 100644 --- a/paper-api/src/main/java/org/bukkit/MusicInstrument.java +++ b/paper-api/src/main/java/org/bukkit/MusicInstrument.java @@ -1,6 +1,5 @@ package org.bukkit; -import com.google.common.base.Preconditions; import com.google.common.collect.Lists; import java.util.Collection; import java.util.Collections; @@ -45,11 +44,6 @@ public abstract class MusicInstrument implements Keyed { @NotNull private static MusicInstrument getInstrument(@NotNull String key) { - NamespacedKey namespacedKey = NamespacedKey.minecraft(key); - MusicInstrument instrument = Registry.INSTRUMENT.get(namespacedKey); - - Preconditions.checkNotNull(instrument, "No MusicInstrument found for %s. This is a bug.", namespacedKey); - - return instrument; + return Registry.INSTRUMENT.getOrThrow(NamespacedKey.minecraft(key)); } } diff --git a/paper-api/src/main/java/org/bukkit/Registry.java b/paper-api/src/main/java/org/bukkit/Registry.java index 883338632b..feb22f7ea3 100644 --- a/paper-api/src/main/java/org/bukkit/Registry.java +++ b/paper-api/src/main/java/org/bukkit/Registry.java @@ -60,6 +60,16 @@ public interface Registry extends Iterable { return Bukkit.getAdvancement(key); } + @NotNull + @Override + public Advancement getOrThrow(@NotNull NamespacedKey key) { + Advancement advancement = get(key); + + Preconditions.checkArgument(advancement != null, "No registry entry found for key " + key); + + return advancement; + } + @NotNull @Override public Stream stream() { @@ -118,6 +128,16 @@ public interface Registry extends Iterable { return Bukkit.getBossBar(key); } + @NotNull + @Override + public KeyedBossBar getOrThrow(@NotNull NamespacedKey key) { + KeyedBossBar keyedBossBar = get(key); + + Preconditions.checkArgument(keyedBossBar != null, "No registry entry found for key " + key); + + return keyedBossBar; + } + @NotNull @Override public Stream stream() { @@ -210,13 +230,13 @@ public interface Registry extends Iterable { * * @see Structure */ - Registry STRUCTURE = Bukkit.getRegistry(Structure.class); + Registry STRUCTURE = Objects.requireNonNull(Bukkit.getRegistry(Structure.class), "No registry present for Structure. This is a bug."); /** * Server structure types. * * @see StructureType */ - Registry STRUCTURE_TYPE = Bukkit.getRegistry(StructureType.class); + Registry STRUCTURE_TYPE = Objects.requireNonNull(Bukkit.getRegistry(StructureType.class), "No registry present for StructureType. This is a bug."); /** * Sound keys. * @@ -229,14 +249,14 @@ public interface Registry extends Iterable { * @see TrimMaterial */ @ApiStatus.Experimental - Registry TRIM_MATERIAL = Bukkit.getRegistry(TrimMaterial.class); + Registry TRIM_MATERIAL = Objects.requireNonNull(Bukkit.getRegistry(TrimMaterial.class), "No registry present for TrimMaterial. This is a bug."); /** * Trim patterns. * * @see TrimPattern */ @ApiStatus.Experimental - Registry TRIM_PATTERN = Bukkit.getRegistry(TrimPattern.class); + Registry TRIM_PATTERN = Objects.requireNonNull(Bukkit.getRegistry(TrimPattern.class), "No registry present for TrimPattern. This is a bug."); /** * Damage types. * @@ -282,6 +302,16 @@ public interface Registry extends Iterable { return MemoryKey.getByKey(key); } + @NotNull + @Override + public MemoryKey getOrThrow(@NotNull NamespacedKey key) { + MemoryKey memoryKey = get(key); + + Preconditions.checkArgument(memoryKey != null, "No registry entry found for key " + key); + + return memoryKey; + } + @NotNull @Override public Stream stream() { @@ -327,6 +357,18 @@ public interface Registry extends Iterable { @Nullable T get(@NotNull NamespacedKey key); + /** + * Get the object by its key. + * + * If there is no object with the given key, an exception will be thrown. + * + * @param key to get the object from + * @return object with the given key + * @throws IllegalArgumentException if there is no object with the given key + */ + @NotNull + T getOrThrow(@NotNull NamespacedKey key); + /** * Returns a new stream, which contains all registry items, which are registered to the registry. * @@ -381,6 +423,16 @@ public interface Registry extends Iterable { return map.get(key); } + @NotNull + @Override + public T getOrThrow(@NotNull NamespacedKey key) { + T object = get(key); + + Preconditions.checkArgument(object != null, "No registry entry found for key " + key); + + return object; + } + @NotNull @Override public Stream stream() { diff --git a/paper-api/src/main/java/org/bukkit/block/BlockType.java b/paper-api/src/main/java/org/bukkit/block/BlockType.java index dfd8187ef9..6c745f4db6 100644 --- a/paper-api/src/main/java/org/bukkit/block/BlockType.java +++ b/paper-api/src/main/java/org/bukkit/block/BlockType.java @@ -1,6 +1,5 @@ package org.bukkit.block; -import com.google.common.base.Preconditions; import java.util.function.Consumer; import org.bukkit.Keyed; import org.bukkit.Material; @@ -108,6 +107,7 @@ import org.bukkit.block.data.type.TrialSpawner; import org.bukkit.block.data.type.Tripwire; import org.bukkit.block.data.type.TripwireHook; import org.bukkit.block.data.type.TurtleEgg; +import org.bukkit.block.data.type.Vault; import org.bukkit.block.data.type.Wall; import org.bukkit.block.data.type.WallHangingSign; import org.bukkit.block.data.type.WallSign; @@ -3295,16 +3295,20 @@ public interface BlockType extends Keyed, Translatable { * BlockData: {@link TrialSpawner} */ BlockType.Typed TRIAL_SPAWNER = getBlockType("trial_spawner"); - + /** + * BlockData: {@link Vault} + */ + BlockType.Typed VAULT = getBlockType("vault"); + /** + * BlockData: {@link Waterlogged} + */ + BlockType.Typed HEAVY_CORE = getBlockType("heavy_core"); // @NotNull private static B getBlockType(@NotNull String key) { - NamespacedKey namespacedKey = NamespacedKey.minecraft(key); - BlockType blockType = Registry.BLOCK.get(namespacedKey); - Preconditions.checkNotNull(blockType, "No BlockType found for %s. This is a bug.", namespacedKey); // Cast instead of using BlockType#typed, since block type can be a mock during testing and would return null - return (B) blockType; + return (B) Registry.BLOCK.getOrThrow(NamespacedKey.minecraft(key)); } /** diff --git a/paper-api/src/main/java/org/bukkit/block/banner/PatternType.java b/paper-api/src/main/java/org/bukkit/block/banner/PatternType.java index 9e90572745..eaf6cd7583 100644 --- a/paper-api/src/main/java/org/bukkit/block/banner/PatternType.java +++ b/paper-api/src/main/java/org/bukkit/block/banner/PatternType.java @@ -100,11 +100,7 @@ public interface PatternType extends OldEnum, Keyed { @NotNull private static PatternType getType(@NotNull String key) { - NamespacedKey namespacedKey = NamespacedKey.minecraft(key); - PatternType type = Registry.BANNER_PATTERN.get(namespacedKey); - - Preconditions.checkNotNull(type, "No Banner Pattern found for %s. This is a bug.", namespacedKey); - return type; + return Registry.BANNER_PATTERN.getOrThrow(NamespacedKey.minecraft(key)); } /** diff --git a/paper-api/src/main/java/org/bukkit/damage/DamageType.java b/paper-api/src/main/java/org/bukkit/damage/DamageType.java index 69abda41ef..f36ff676be 100644 --- a/paper-api/src/main/java/org/bukkit/damage/DamageType.java +++ b/paper-api/src/main/java/org/bukkit/damage/DamageType.java @@ -1,6 +1,5 @@ package org.bukkit.damage; -import com.google.common.base.Preconditions; import org.bukkit.Keyed; import org.bukkit.NamespacedKey; import org.bukkit.Registry; @@ -66,11 +65,11 @@ public interface DamageType extends Keyed, Translatable { public static final DamageType BAD_RESPAWN_POINT = getDamageType("bad_respawn_point"); public static final DamageType OUTSIDE_BORDER = getDamageType("outside_border"); public static final DamageType GENERIC_KILL = getDamageType("generic_kill"); + public static final DamageType WIND_CHARGE = getDamageType("wind_charge"); @NotNull private static DamageType getDamageType(@NotNull String key) { - NamespacedKey namespacedKey = NamespacedKey.minecraft(key); - return Preconditions.checkNotNull(Registry.DAMAGE_TYPE.get(namespacedKey), "No DamageType found for %s. This is a bug.", namespacedKey); + return Registry.DAMAGE_TYPE.getOrThrow(NamespacedKey.minecraft(key)); } /** diff --git a/paper-api/src/main/java/org/bukkit/enchantments/Enchantment.java b/paper-api/src/main/java/org/bukkit/enchantments/Enchantment.java index 80fb27d6c5..241bc2ea61 100644 --- a/paper-api/src/main/java/org/bukkit/enchantments/Enchantment.java +++ b/paper-api/src/main/java/org/bukkit/enchantments/Enchantment.java @@ -1,6 +1,5 @@ package org.bukkit.enchantments; -import com.google.common.base.Preconditions; import com.google.common.collect.Lists; import java.util.Locale; import org.bukkit.Keyed; @@ -230,12 +229,7 @@ public abstract class Enchantment implements Keyed, Translatable { @NotNull private static Enchantment getEnchantment(@NotNull String key) { - NamespacedKey namespacedKey = NamespacedKey.minecraft(key); - Enchantment enchantment = Registry.ENCHANTMENT.get(namespacedKey); - - Preconditions.checkNotNull(enchantment, "No Enchantment found for %s. This is a bug.", namespacedKey); - - return enchantment; + return Registry.ENCHANTMENT.getOrThrow(NamespacedKey.minecraft(key)); } /** diff --git a/paper-api/src/main/java/org/bukkit/entity/Cat.java b/paper-api/src/main/java/org/bukkit/entity/Cat.java index 117e3e8c63..104b9c8a28 100644 --- a/paper-api/src/main/java/org/bukkit/entity/Cat.java +++ b/paper-api/src/main/java/org/bukkit/entity/Cat.java @@ -64,11 +64,7 @@ public interface Cat extends Tameable, Sittable { @NotNull private static Type getType(@NotNull String key) { - NamespacedKey namespacedKey = NamespacedKey.minecraft(key); - Type type = Registry.CAT_VARIANT.get(namespacedKey); - - Preconditions.checkNotNull(type, "No cat type found for %s. This is a bug.", namespacedKey); - return type; + return Registry.CAT_VARIANT.getOrThrow(NamespacedKey.minecraft(key)); } /** diff --git a/paper-api/src/main/java/org/bukkit/entity/Frog.java b/paper-api/src/main/java/org/bukkit/entity/Frog.java index 7cf8ae63eb..db34a6241d 100644 --- a/paper-api/src/main/java/org/bukkit/entity/Frog.java +++ b/paper-api/src/main/java/org/bukkit/entity/Frog.java @@ -65,11 +65,7 @@ public interface Frog extends Animals { @NotNull private static Variant getVariant(@NotNull String key) { - NamespacedKey namespacedKey = NamespacedKey.minecraft(key); - Variant variant = Registry.FROG_VARIANT.get(namespacedKey); - - Preconditions.checkNotNull(variant, "No frog variant found for %s. This is a bug.", namespacedKey); - return variant; + return Registry.FROG_VARIANT.getOrThrow(NamespacedKey.minecraft(key)); } /** diff --git a/paper-api/src/main/java/org/bukkit/entity/Villager.java b/paper-api/src/main/java/org/bukkit/entity/Villager.java index 45dd54afa6..c48f13cc8e 100644 --- a/paper-api/src/main/java/org/bukkit/entity/Villager.java +++ b/paper-api/src/main/java/org/bukkit/entity/Villager.java @@ -134,11 +134,7 @@ public interface Villager extends AbstractVillager { @NotNull private static Type getType(@NotNull String key) { - NamespacedKey namespacedKey = NamespacedKey.minecraft(key); - Type type = Registry.VILLAGER_TYPE.get(namespacedKey); - - Preconditions.checkNotNull(type, "No villager type found for %s. This is a bug.", namespacedKey); - return type; + return Registry.VILLAGER_TYPE.getOrThrow(NamespacedKey.minecraft(key)); } /** @@ -245,11 +241,7 @@ public interface Villager extends AbstractVillager { @NotNull private static Profession getProfession(@NotNull String key) { - NamespacedKey namespacedKey = NamespacedKey.minecraft(key); - Profession profession = Registry.VILLAGER_PROFESSION.get(namespacedKey); - - Preconditions.checkNotNull(profession, "No villager profession found for %s. This is a bug.", namespacedKey); - return profession; + return Registry.VILLAGER_PROFESSION.getOrThrow(NamespacedKey.minecraft(key)); } /** diff --git a/paper-api/src/main/java/org/bukkit/entity/Wolf.java b/paper-api/src/main/java/org/bukkit/entity/Wolf.java index 03c4a3dedf..91e96ee536 100644 --- a/paper-api/src/main/java/org/bukkit/entity/Wolf.java +++ b/paper-api/src/main/java/org/bukkit/entity/Wolf.java @@ -1,6 +1,5 @@ package org.bukkit.entity; -import com.google.common.base.Preconditions; import org.bukkit.DyeColor; import org.bukkit.Keyed; import org.bukkit.NamespacedKey; @@ -104,10 +103,7 @@ public interface Wolf extends Tameable, Sittable { @NotNull private static Variant getVariant(@NotNull String key) { - NamespacedKey namespacedKey = NamespacedKey.minecraft(key); - Variant variant = Registry.WOLF_VARIANT.get(namespacedKey); - Preconditions.checkNotNull(variant, "No Wolf Variant found for %s. This is a bug.", namespacedKey); - return variant; + return Registry.WOLF_VARIANT.getOrThrow(NamespacedKey.minecraft(key)); } } } diff --git a/paper-api/src/main/java/org/bukkit/generator/structure/Structure.java b/paper-api/src/main/java/org/bukkit/generator/structure/Structure.java index 1a766e6871..20a7fd27ba 100644 --- a/paper-api/src/main/java/org/bukkit/generator/structure/Structure.java +++ b/paper-api/src/main/java/org/bukkit/generator/structure/Structure.java @@ -49,8 +49,9 @@ public abstract class Structure implements Keyed { public static final Structure TRAIL_RUINS = getStructure("trail_ruins"); public static final Structure TRIAL_CHAMBERS = getStructure("trial_chambers"); - private static Structure getStructure(String name) { - return Registry.STRUCTURE.get(NamespacedKey.minecraft(name)); + @NotNull + private static Structure getStructure(@NotNull String name) { + return Registry.STRUCTURE.getOrThrow(NamespacedKey.minecraft(name)); } /** diff --git a/paper-api/src/main/java/org/bukkit/generator/structure/StructureType.java b/paper-api/src/main/java/org/bukkit/generator/structure/StructureType.java index 2f908d5545..ffa0fc250f 100644 --- a/paper-api/src/main/java/org/bukkit/generator/structure/StructureType.java +++ b/paper-api/src/main/java/org/bukkit/generator/structure/StructureType.java @@ -3,6 +3,7 @@ package org.bukkit.generator.structure; import org.bukkit.Keyed; import org.bukkit.NamespacedKey; import org.bukkit.Registry; +import org.jetbrains.annotations.NotNull; /** * Represent a StructureType of a {@link Structure}. @@ -31,7 +32,8 @@ public abstract class StructureType implements Keyed { public static final StructureType SWAMP_HUT = getStructureType("swamp_hut"); public static final StructureType WOODLAND_MANSION = getStructureType("woodland_mansion"); - private static StructureType getStructureType(String name) { - return Registry.STRUCTURE_TYPE.get(NamespacedKey.minecraft(name)); + @NotNull + private static StructureType getStructureType(@NotNull String name) { + return Registry.STRUCTURE_TYPE.getOrThrow(NamespacedKey.minecraft(name)); } } diff --git a/paper-api/src/main/java/org/bukkit/inventory/ItemType.java b/paper-api/src/main/java/org/bukkit/inventory/ItemType.java index d4c29562aa..ae72b587bf 100644 --- a/paper-api/src/main/java/org/bukkit/inventory/ItemType.java +++ b/paper-api/src/main/java/org/bukkit/inventory/ItemType.java @@ -1,6 +1,5 @@ package org.bukkit.inventory; -import com.google.common.base.Preconditions; import com.google.common.collect.Multimap; import java.util.function.Consumer; import org.bukkit.Keyed; @@ -31,6 +30,7 @@ import org.bukkit.inventory.meta.MapMeta; import org.bukkit.inventory.meta.MusicInstrumentMeta; import org.bukkit.inventory.meta.OminousBottleMeta; import org.bukkit.inventory.meta.PotionMeta; +import org.bukkit.inventory.meta.ShieldMeta; import org.bukkit.inventory.meta.SkullMeta; import org.bukkit.inventory.meta.SpawnEggMeta; import org.bukkit.inventory.meta.SuspiciousStewMeta; @@ -1028,8 +1028,8 @@ public interface ItemType extends Keyed, Translatable { * ItemMeta: {@link ColorableArmorMeta} */ ItemType.Typed WOLF_ARMOR = getItemType("wolf_armor"); - ItemType.Typed BOWL = getItemType("bowl"); ItemType.Typed FLINT_AND_STEEL = getItemType("flint_and_steel"); + ItemType.Typed BOWL = getItemType("bowl"); ItemType.Typed APPLE = getItemType("apple"); ItemType.Typed BOW = getItemType("bow"); ItemType.Typed ARROW = getItemType("arrow"); @@ -1891,9 +1891,9 @@ public interface ItemType extends Keyed, Translatable { */ ItemType.Typed LINGERING_POTION = getItemType("lingering_potion"); /** - * ItemMeta: {@link BlockStateMeta} + * ItemMeta: {@link ShieldMeta} */ - ItemType.Typed SHIELD = getItemType("shield"); + ItemType.Typed SHIELD = getItemType("shield"); ItemType.Typed TOTEM_OF_UNDYING = getItemType("totem_of_undying"); ItemType.Typed SHULKER_SHELL = getItemType("shulker_shell"); ItemType.Typed IRON_NUGGET = getItemType("iron_nugget"); @@ -2109,17 +2109,12 @@ public interface ItemType extends Keyed, Translatable { */ ItemType.Typed OMINOUS_BOTTLE = getItemType("ominous_bottle"); ItemType.Typed BREEZE_ROD = getItemType("breeze_rod"); - - // @NotNull private static M getItemType(@NotNull String key) { - NamespacedKey namespacedKey = NamespacedKey.minecraft(key); - ItemType itemType = Registry.ITEM.get(namespacedKey); - Preconditions.checkNotNull(itemType, "No ItemType found for %s. This is a bug.", namespacedKey); // Cast instead of using ItemType#typed, since item type can be a mock during testing and would return null - return (M) itemType; + return (M) Registry.ITEM.getOrThrow(NamespacedKey.minecraft(key)); } /** diff --git a/paper-api/src/main/java/org/bukkit/inventory/MenuType.java b/paper-api/src/main/java/org/bukkit/inventory/MenuType.java index ee39bf9019..45d3889206 100644 --- a/paper-api/src/main/java/org/bukkit/inventory/MenuType.java +++ b/paper-api/src/main/java/org/bukkit/inventory/MenuType.java @@ -1,6 +1,5 @@ package org.bukkit.inventory; -import com.google.common.base.Preconditions; import org.bukkit.Keyed; import org.bukkit.NamespacedKey; import org.bukkit.Registry; @@ -183,9 +182,8 @@ public interface MenuType extends Keyed { @NotNull Class getInventoryViewClass(); + @NotNull private static T get(@NotNull final String key) { - final MenuType type = Registry.MENU.get(NamespacedKey.minecraft(key)); - Preconditions.checkArgument(type != null, "The given string key must be an existing menu type"); - return (T) type; + return (T) Registry.MENU.getOrThrow(NamespacedKey.minecraft(key)); } } diff --git a/paper-api/src/main/java/org/bukkit/inventory/meta/trim/TrimMaterial.java b/paper-api/src/main/java/org/bukkit/inventory/meta/trim/TrimMaterial.java index eb80f24da6..5fc9aaac8b 100644 --- a/paper-api/src/main/java/org/bukkit/inventory/meta/trim/TrimMaterial.java +++ b/paper-api/src/main/java/org/bukkit/inventory/meta/trim/TrimMaterial.java @@ -5,6 +5,7 @@ import org.bukkit.Material; import org.bukkit.NamespacedKey; import org.bukkit.Registry; import org.bukkit.Translatable; +import org.jetbrains.annotations.NotNull; /** * Represents a material that may be used in an {@link ArmorTrim}. @@ -14,41 +15,46 @@ public interface TrimMaterial extends Keyed, Translatable { /** * {@link Material#QUARTZ}. */ - public static final TrimMaterial QUARTZ = Registry.TRIM_MATERIAL.get(NamespacedKey.minecraft("quartz")); + public static final TrimMaterial QUARTZ = getTrimMaterial("quartz"); /** * {@link Material#IRON_INGOT}. */ - public static final TrimMaterial IRON = Registry.TRIM_MATERIAL.get(NamespacedKey.minecraft("iron")); + public static final TrimMaterial IRON = getTrimMaterial("iron"); /** * {@link Material#NETHERITE_INGOT}. */ - public static final TrimMaterial NETHERITE = Registry.TRIM_MATERIAL.get(NamespacedKey.minecraft("netherite")); + public static final TrimMaterial NETHERITE = getTrimMaterial("netherite"); /** * {@link Material#REDSTONE}. */ - public static final TrimMaterial REDSTONE = Registry.TRIM_MATERIAL.get(NamespacedKey.minecraft("redstone")); + public static final TrimMaterial REDSTONE = getTrimMaterial("redstone"); /** * {@link Material#COPPER_INGOT}. */ - public static final TrimMaterial COPPER = Registry.TRIM_MATERIAL.get(NamespacedKey.minecraft("copper")); + public static final TrimMaterial COPPER = getTrimMaterial("copper"); /** * {@link Material#GOLD_INGOT}. */ - public static final TrimMaterial GOLD = Registry.TRIM_MATERIAL.get(NamespacedKey.minecraft("gold")); + public static final TrimMaterial GOLD = getTrimMaterial("gold"); /** * {@link Material#EMERALD}. */ - public static final TrimMaterial EMERALD = Registry.TRIM_MATERIAL.get(NamespacedKey.minecraft("emerald")); + public static final TrimMaterial EMERALD = getTrimMaterial("emerald"); /** * {@link Material#DIAMOND}. */ - public static final TrimMaterial DIAMOND = Registry.TRIM_MATERIAL.get(NamespacedKey.minecraft("diamond")); + public static final TrimMaterial DIAMOND = getTrimMaterial("diamond"); /** * {@link Material#LAPIS_LAZULI}. */ - public static final TrimMaterial LAPIS = Registry.TRIM_MATERIAL.get(NamespacedKey.minecraft("lapis")); + public static final TrimMaterial LAPIS = getTrimMaterial("lapis"); /** * {@link Material#AMETHYST_SHARD}. */ - public static final TrimMaterial AMETHYST = Registry.TRIM_MATERIAL.get(NamespacedKey.minecraft("amethyst")); + public static final TrimMaterial AMETHYST = getTrimMaterial("amethyst"); + + @NotNull + private static TrimMaterial getTrimMaterial(@NotNull String key) { + return Registry.TRIM_MATERIAL.getOrThrow(NamespacedKey.minecraft(key)); + } } diff --git a/paper-api/src/main/java/org/bukkit/inventory/meta/trim/TrimPattern.java b/paper-api/src/main/java/org/bukkit/inventory/meta/trim/TrimPattern.java index bd512a7840..e8e0786467 100644 --- a/paper-api/src/main/java/org/bukkit/inventory/meta/trim/TrimPattern.java +++ b/paper-api/src/main/java/org/bukkit/inventory/meta/trim/TrimPattern.java @@ -5,6 +5,7 @@ import org.bukkit.Material; import org.bukkit.NamespacedKey; import org.bukkit.Registry; import org.bukkit.Translatable; +import org.jetbrains.annotations.NotNull; /** * Represents a pattern that may be used in an {@link ArmorTrim}. @@ -14,73 +15,78 @@ public interface TrimPattern extends Keyed, Translatable { /** * {@link Material#SENTRY_ARMOR_TRIM_SMITHING_TEMPLATE}. */ - public static final TrimPattern SENTRY = Registry.TRIM_PATTERN.get(NamespacedKey.minecraft("sentry")); + public static final TrimPattern SENTRY = getTrimPattern("sentry"); /** * {@link Material#DUNE_ARMOR_TRIM_SMITHING_TEMPLATE}. */ - public static final TrimPattern DUNE = Registry.TRIM_PATTERN.get(NamespacedKey.minecraft("dune")); + public static final TrimPattern DUNE = getTrimPattern("dune"); /** * {@link Material#COAST_ARMOR_TRIM_SMITHING_TEMPLATE}. */ - public static final TrimPattern COAST = Registry.TRIM_PATTERN.get(NamespacedKey.minecraft("coast")); + public static final TrimPattern COAST = getTrimPattern("coast"); /** * {@link Material#WILD_ARMOR_TRIM_SMITHING_TEMPLATE}. */ - public static final TrimPattern WILD = Registry.TRIM_PATTERN.get(NamespacedKey.minecraft("wild")); + public static final TrimPattern WILD = getTrimPattern("wild"); /** * {@link Material#WARD_ARMOR_TRIM_SMITHING_TEMPLATE}. */ - public static final TrimPattern WARD = Registry.TRIM_PATTERN.get(NamespacedKey.minecraft("ward")); + public static final TrimPattern WARD = getTrimPattern("ward"); /** * {@link Material#EYE_ARMOR_TRIM_SMITHING_TEMPLATE}. */ - public static final TrimPattern EYE = Registry.TRIM_PATTERN.get(NamespacedKey.minecraft("eye")); + public static final TrimPattern EYE = getTrimPattern("eye"); /** * {@link Material#VEX_ARMOR_TRIM_SMITHING_TEMPLATE}. */ - public static final TrimPattern VEX = Registry.TRIM_PATTERN.get(NamespacedKey.minecraft("vex")); + public static final TrimPattern VEX = getTrimPattern("vex"); /** * {@link Material#TIDE_ARMOR_TRIM_SMITHING_TEMPLATE}. */ - public static final TrimPattern TIDE = Registry.TRIM_PATTERN.get(NamespacedKey.minecraft("tide")); + public static final TrimPattern TIDE = getTrimPattern("tide"); /** * {@link Material#SNOUT_ARMOR_TRIM_SMITHING_TEMPLATE}. */ - public static final TrimPattern SNOUT = Registry.TRIM_PATTERN.get(NamespacedKey.minecraft("snout")); + public static final TrimPattern SNOUT = getTrimPattern("snout"); /** * {@link Material#RIB_ARMOR_TRIM_SMITHING_TEMPLATE}. */ - public static final TrimPattern RIB = Registry.TRIM_PATTERN.get(NamespacedKey.minecraft("rib")); + public static final TrimPattern RIB = getTrimPattern("rib"); /** * {@link Material#SPIRE_ARMOR_TRIM_SMITHING_TEMPLATE}. */ - public static final TrimPattern SPIRE = Registry.TRIM_PATTERN.get(NamespacedKey.minecraft("spire")); + public static final TrimPattern SPIRE = getTrimPattern("spire"); /** * {@link Material#WAYFINDER_ARMOR_TRIM_SMITHING_TEMPLATE}. */ - public static final TrimPattern WAYFINDER = Registry.TRIM_PATTERN.get(NamespacedKey.minecraft("wayfinder")); + public static final TrimPattern WAYFINDER = getTrimPattern("wayfinder"); /** * {@link Material#SHAPER_ARMOR_TRIM_SMITHING_TEMPLATE}. */ - public static final TrimPattern SHAPER = Registry.TRIM_PATTERN.get(NamespacedKey.minecraft("shaper")); + public static final TrimPattern SHAPER = getTrimPattern("shaper"); /** * {@link Material#SILENCE_ARMOR_TRIM_SMITHING_TEMPLATE}. */ - public static final TrimPattern SILENCE = Registry.TRIM_PATTERN.get(NamespacedKey.minecraft("silence")); + public static final TrimPattern SILENCE = getTrimPattern("silence"); /** * {@link Material#RAISER_ARMOR_TRIM_SMITHING_TEMPLATE}. */ - public static final TrimPattern RAISER = Registry.TRIM_PATTERN.get(NamespacedKey.minecraft("raiser")); + public static final TrimPattern RAISER = getTrimPattern("raiser"); /** * {@link Material#HOST_ARMOR_TRIM_SMITHING_TEMPLATE}. */ - public static final TrimPattern HOST = Registry.TRIM_PATTERN.get(NamespacedKey.minecraft("host")); + public static final TrimPattern HOST = getTrimPattern("host"); /** * {@link Material#FLOW_ARMOR_TRIM_SMITHING_TEMPLATE}. */ - public static final TrimPattern FLOW = Registry.TRIM_PATTERN.get(NamespacedKey.minecraft("flow")); + public static final TrimPattern FLOW = getTrimPattern("flow"); /** * {@link Material#BOLT_ARMOR_TRIM_SMITHING_TEMPLATE}. */ - public static final TrimPattern BOLT = Registry.TRIM_PATTERN.get(NamespacedKey.minecraft("bolt")); + public static final TrimPattern BOLT = getTrimPattern("bolt"); + + @NotNull + private static TrimPattern getTrimPattern(@NotNull String key) { + return Registry.TRIM_PATTERN.getOrThrow(NamespacedKey.minecraft(key)); + } } diff --git a/paper-api/src/main/java/org/bukkit/map/MapCursor.java b/paper-api/src/main/java/org/bukkit/map/MapCursor.java index 8250821fe7..a37e6419fc 100644 --- a/paper-api/src/main/java/org/bukkit/map/MapCursor.java +++ b/paper-api/src/main/java/org/bukkit/map/MapCursor.java @@ -269,11 +269,7 @@ public final class MapCursor { @NotNull private static Type getType(@NotNull String key) { - NamespacedKey namespacedKey = NamespacedKey.minecraft(key); - Type type = Registry.MAP_DECORATION_TYPE.get(namespacedKey); - - Preconditions.checkNotNull(type, "No type found for %s. This is a bug.", namespacedKey); - return type; + return Registry.MAP_DECORATION_TYPE.getOrThrow(NamespacedKey.minecraft(key)); } /** diff --git a/paper-api/src/main/java/org/bukkit/potion/PotionEffectType.java b/paper-api/src/main/java/org/bukkit/potion/PotionEffectType.java index 6bbfebab2e..bd9ea2fe16 100644 --- a/paper-api/src/main/java/org/bukkit/potion/PotionEffectType.java +++ b/paper-api/src/main/java/org/bukkit/potion/PotionEffectType.java @@ -222,9 +222,8 @@ public abstract class PotionEffectType implements Keyed, Translatable { @NotNull private static PotionEffectType getPotionEffectType(int typeId, @NotNull String key) { - NamespacedKey namespacedKey = NamespacedKey.minecraft(key); - PotionEffectType potionEffectType = Registry.EFFECT.get(namespacedKey); - Preconditions.checkNotNull(potionEffectType, "No PotionEffectType found for %s. This is a bug.", namespacedKey); + PotionEffectType potionEffectType = Registry.EFFECT.getOrThrow(NamespacedKey.minecraft(key)); + if (typeId > 0) { ID_MAP.put(typeId, potionEffectType); } diff --git a/paper-api/src/test/java/org/bukkit/support/TestServer.java b/paper-api/src/test/java/org/bukkit/support/TestServer.java index 5709d52ed4..4040ecb2e0 100644 --- a/paper-api/src/test/java/org/bukkit/support/TestServer.java +++ b/paper-api/src/test/java/org/bukkit/support/TestServer.java @@ -1,6 +1,7 @@ package org.bukkit.support; import static org.mockito.Mockito.*; +import com.google.common.base.Preconditions; import java.util.HashMap; import java.util.Iterator; import java.util.Locale; @@ -57,6 +58,16 @@ public final class TestServer { return cache.computeIfAbsent(key, key2 -> mock(theClass, withSettings().stubOnly())); } + @NotNull + @Override + public Keyed getOrThrow(@NotNull NamespacedKey key) { + Keyed keyed = get(key); + + Preconditions.checkArgument(keyed != null, "No registry entry found for key " + key); + + return keyed; + } + @NotNull @Override public Stream stream() {