From 5b88c3181b9cd5d9b176c31e2a38abd313f87081 Mon Sep 17 00:00:00 2001 From: Maddy Miller Date: Sun, 18 Jun 2023 17:33:15 +1000 Subject: [PATCH] Use registry lookups for Bukkit adapters (#2330) * Use registry lookups for Bukkit adapters * Remove a redundant todo now that this targets master --- .../sk89q/worldedit/bukkit/BukkitAdapter.java | 37 ++++++++----------- .../bukkit/BukkitServerInterface.java | 10 ++--- .../worldedit/bukkit/WorldEditPlugin.java | 34 ++++++++--------- 3 files changed, 35 insertions(+), 46 deletions(-) diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitAdapter.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitAdapter.java index ab3ef88a86..f4133b8b48 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitAdapter.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitAdapter.java @@ -51,6 +51,8 @@ import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import org.bukkit.Bukkit; import org.bukkit.Material; +import org.bukkit.NamespacedKey; +import org.bukkit.Registry; import org.bukkit.block.Biome; import org.bukkit.block.BlockFace; import org.bukkit.block.data.BlockData; @@ -332,10 +334,7 @@ public static Entity adapt(org.bukkit.entity.Entity entity) { */ public static Material adapt(ItemType itemType) { checkNotNull(itemType); - if (!itemType.getId().startsWith("minecraft:")) { - throw new IllegalArgumentException("Bukkit only supports Minecraft items"); - } - return Material.getMaterial(itemType.getId().substring(10).toUpperCase(Locale.ROOT)); + return Material.matchMaterial(itemType.getId()); } /** @@ -346,10 +345,7 @@ public static Material adapt(ItemType itemType) { */ public static Material adapt(BlockType blockType) { checkNotNull(blockType); - if (!blockType.getId().startsWith("minecraft:")) { - throw new IllegalArgumentException("Bukkit only supports Minecraft blocks"); - } - return Material.getMaterial(blockType.getId().substring(10).toUpperCase(Locale.ROOT)); + return Material.matchMaterial(blockType.getId()); } /** @@ -375,15 +371,16 @@ public static GameMode adapt(org.bukkit.GameMode gameMode) { * @return WorldEdit BiomeType */ public static BiomeType adapt(Biome biome) { - return biomeBiomeTypeCache.computeIfAbsent(biome, b -> BiomeTypes.get(b.name().toLowerCase(Locale.ROOT))); + return biomeBiomeTypeCache.computeIfAbsent(biome, b -> BiomeTypes.get(b.getKey().toString())); } public static Biome adapt(BiomeType biomeType) { - if (!biomeType.getId().startsWith("minecraft:")) { - throw new IllegalArgumentException("Bukkit only supports vanilla biomes"); + NamespacedKey biomeKey = NamespacedKey.fromString(biomeType.getId()); + if (biomeKey == null) { + throw new IllegalArgumentException("Biome key '" + biomeType.getId() + "' does not map to Bukkit"); } try { - return biomeTypeBiomeCache.computeIfAbsent(biomeType, type -> Biome.valueOf(type.getId().substring(10).toUpperCase(Locale.ROOT))); + return biomeTypeBiomeCache.computeIfAbsent(biomeType, type -> Registry.BIOME.get(biomeKey)); } catch (IllegalArgumentException e) { return null; } @@ -396,20 +393,16 @@ public static Biome adapt(BiomeType biomeType) { * @return WorldEdit EntityType */ public static EntityType adapt(org.bukkit.entity.EntityType entityType) { - @SuppressWarnings("deprecation") - final String name = entityType.getName(); - if (name == null) { - return null; - } - return EntityTypes.get(name.toLowerCase(Locale.ROOT)); + return EntityTypes.get(entityType.getKey().toString()); } - @SuppressWarnings("deprecation") public static org.bukkit.entity.EntityType adapt(EntityType entityType) { - if (!entityType.getId().startsWith("minecraft:")) { - throw new IllegalArgumentException("Bukkit only supports vanilla entities"); + NamespacedKey entityKey = NamespacedKey.fromString(entityType.getId()); + if (entityKey == null) { + throw new IllegalArgumentException("Entity key '" + entityType.getId() + "' does not map to Bukkit"); } - return org.bukkit.entity.EntityType.fromName(entityType.getId().substring(10)); + + return Registry.ENTITY_TYPE.get(entityKey); } private static final Map materialBlockTypeCache = Collections.synchronizedMap( diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitServerInterface.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitServerInterface.java index 8803b446c9..1a9b9c8ff2 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitServerInterface.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitServerInterface.java @@ -38,9 +38,10 @@ import com.sk89q.worldedit.world.DataFixer; import com.sk89q.worldedit.world.registry.Registries; import org.bukkit.Bukkit; +import org.bukkit.NamespacedKey; +import org.bukkit.Registry; import org.bukkit.Server; import org.bukkit.World; -import org.bukkit.entity.EntityType; import org.enginehub.piston.CommandManager; import java.util.ArrayList; @@ -104,12 +105,11 @@ public DataFixer getDataFixer() { @Override public boolean isValidMobType(String type) { - if (!type.startsWith("minecraft:")) { + NamespacedKey entityKey = NamespacedKey.fromString(type); + if (entityKey == null) { return false; } - @SuppressWarnings("deprecation") - final EntityType entityType = EntityType.fromName(type.substring(10)); - return entityType != null && entityType.isAlive(); + return Registry.ENTITY_TYPE.get(entityKey) != null; } @Override diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java index f98bc7dc54..b338b2498f 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java @@ -87,7 +87,6 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; -import java.util.Locale; import java.util.Map; import java.util.Optional; import java.util.logging.Level; @@ -206,18 +205,18 @@ private void setupWorldData() { WorldEdit.getInstance().getEventBus().post(new PlatformReadyEvent(platform)); } - @SuppressWarnings({ "deprecation", "unchecked" }) + @SuppressWarnings({ "unchecked" }) private void initializeRegistries() { // Biome for (Biome biome : Biome.values()) { if (!biome.name().equals("CUSTOM")) { - String lowerCaseBiomeName = biome.name().toLowerCase(Locale.ROOT); - BiomeType.REGISTRY.register("minecraft:" + lowerCaseBiomeName, new BiomeType("minecraft:" + lowerCaseBiomeName)); + String key = biome.getKey().toString(); + BiomeType.REGISTRY.register(key, new BiomeType(key)); } } // Block & Item for (Material material : Material.values()) { - if (material.isBlock() && !material.isLegacy()) { + if (material.isBlock()) { BlockType.REGISTRY.register(material.getKey().toString(), new BlockType(material.getKey().toString(), blockState -> { // TODO Use something way less hacky than this. ParserContext context = new ParserContext(); @@ -240,17 +239,18 @@ private void initializeRegistries() { } })); } - if (material.isItem() && !material.isLegacy()) { + if (material.isItem()) { ItemType.REGISTRY.register(material.getKey().toString(), new ItemType(material.getKey().toString())); } } // Entity for (org.bukkit.entity.EntityType entityType : org.bukkit.entity.EntityType.values()) { - String mcid = entityType.getName(); - if (mcid != null) { - String lowerCaseMcId = mcid.toLowerCase(Locale.ROOT); - EntityType.REGISTRY.register("minecraft:" + lowerCaseMcId, new EntityType("minecraft:" + lowerCaseMcId)); + if (entityType == org.bukkit.entity.EntityType.UNKNOWN) { + // This doesn't have a key - skip it + continue; } + String key = entityType.getKey().toString(); + EntityType.REGISTRY.register(key, new EntityType(key)); } // ... :| GameModes.get(""); @@ -264,15 +264,11 @@ private void initializeRegistries() { private void setupTags() { // Tags - try { - for (Tag blockTag : Bukkit.getTags(Tag.REGISTRY_BLOCKS, Material.class)) { - BlockCategory.REGISTRY.register(blockTag.getKey().toString(), new BlockCategory(blockTag.getKey().toString())); - } - for (Tag itemTag : Bukkit.getTags(Tag.REGISTRY_ITEMS, Material.class)) { - ItemCategory.REGISTRY.register(itemTag.getKey().toString(), new ItemCategory(itemTag.getKey().toString())); - } - } catch (NoSuchMethodError ignored) { - getLogger().warning("The version of Spigot/Paper you are using doesn't support Tags. The usage of tags with WorldEdit will not work until you update."); + for (Tag blockTag : Bukkit.getTags(Tag.REGISTRY_BLOCKS, Material.class)) { + BlockCategory.REGISTRY.register(blockTag.getKey().toString(), new BlockCategory(blockTag.getKey().toString())); + } + for (Tag itemTag : Bukkit.getTags(Tag.REGISTRY_ITEMS, Material.class)) { + ItemCategory.REGISTRY.register(itemTag.getKey().toString(), new ItemCategory(itemTag.getKey().toString())); } }