From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Sat, 6 Jan 2024 14:31:00 +0100 Subject: [PATCH] Fixup NamespacedKey handling diff --git a/src/main/java/org/bukkit/craftbukkit/CraftLootTable.java b/src/main/java/org/bukkit/craftbukkit/CraftLootTable.java index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftLootTable.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftLootTable.java @@ -0,0 +0,0 @@ public class CraftLootTable implements org.bukkit.loot.LootTable { } public static org.bukkit.loot.LootTable minecraftToBukkit(ResourceKey minecraft) { - return (minecraft == null) ? null : Bukkit.getLootTable(CraftLootTable.minecraftToBukkitKey(minecraft)); + return (minecraft == null || minecraft.location().getPath().isEmpty()) ? null : Bukkit.getLootTable(CraftLootTable.minecraftToBukkitKey(minecraft)); // Paper - fix some NamespacedKey parsing } public static NamespacedKey minecraftToBukkitKey(ResourceKey minecraft) { diff --git a/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java b/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java @@ -0,0 +0,0 @@ public class CraftRegistry implements Registry { + ", this can happen if a plugin creates its own registry entry with out properly registering it."); } + // Paper start - fixup upstream being dum + public static java.util.Optional unwrapAndConvertHolder(final io.papermc.paper.registry.RegistryKey registryKey, final Holder value) { + return unwrapAndConvertHolder(io.papermc.paper.registry.RegistryAccess.registryAccess().getRegistry(registryKey), value); + } + + public static java.util.Optional unwrapAndConvertHolder(final Registry registry, final Holder value) { + return value.unwrapKey().map(key -> registry.get(CraftNamespacedKey.fromMinecraft(key.location()))); + } + // Paper end - fixup upstream being dum + // Paper - move to PaperRegistries // Paper - NOTE: As long as all uses of the method below relate to *serialization* via ConfigurationSerializable, it's fine diff --git a/src/main/java/org/bukkit/craftbukkit/attribute/CraftAttribute.java b/src/main/java/org/bukkit/craftbukkit/attribute/CraftAttribute.java index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/org/bukkit/craftbukkit/attribute/CraftAttribute.java +++ b/src/main/java/org/bukkit/craftbukkit/attribute/CraftAttribute.java @@ -0,0 +0,0 @@ public class CraftAttribute { string = FieldRename.convertAttributeName(ApiVersion.CURRENT, string); string = string.toLowerCase(Locale.ROOT); NamespacedKey key = NamespacedKey.fromString(string); + if (key == null) return null; // Paper - Fixup NamespacedKey handling // Now also convert from when keys where saved return CraftRegistry.get(Registry.ATTRIBUTE, key, ApiVersion.CURRENT); diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftBanner.java b/src/main/java/org/bukkit/craftbukkit/block/CraftBanner.java index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/org/bukkit/craftbukkit/block/CraftBanner.java +++ b/src/main/java/org/bukkit/craftbukkit/block/CraftBanner.java @@ -0,0 +0,0 @@ public class CraftBanner extends CraftBlockEntityState implem if (banner.getPatterns() != null) { for (int i = 0; i < banner.getPatterns().layers().size(); i++) { BannerPatternLayers.Layer p = banner.getPatterns().layers().get(i); - this.patterns.add(new Pattern(DyeColor.getByWoolData((byte) p.color().getId()), CraftPatternType.minecraftHolderToBukkit(p.pattern()))); + // Paper start - fix upstream not handling inlined banner pattern + java.util.Optional type = org.bukkit.craftbukkit.CraftRegistry.unwrapAndConvertHolder(org.bukkit.Registry.BANNER_PATTERN, p.pattern()); + if (type.isEmpty()) continue; + this.patterns.add(new Pattern(DyeColor.getByWoolData((byte) p.color().getId()), type.get())); + // Paper end - fix upstream not handling inlined banner pattern } } } diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPainting.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPainting.java index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPainting.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPainting.java @@ -0,0 +0,0 @@ public class CraftPainting extends CraftHanging implements Painting { @Override public Art getArt() { - return CraftArt.minecraftHolderToBukkit(this.getHandle().getVariant()); + return org.bukkit.craftbukkit.CraftRegistry.unwrapAndConvertHolder(org.bukkit.Registry.ART, this.getHandle().getVariant()).orElseThrow(() -> new IllegalStateException("Inlined/custom painting variants are not supported yet in the API!")); // Paper } @Override diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaArmor.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaArmor.java index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaArmor.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaArmor.java @@ -0,0 +0,0 @@ public class CraftMetaArmor extends CraftMetaItem implements ArmorMeta { super(tag); getOrEmpty(tag, CraftMetaArmor.TRIM).ifPresent((trimCompound) -> { - TrimMaterial trimMaterial = CraftTrimMaterial.minecraftHolderToBukkit(trimCompound.material()); - TrimPattern trimPattern = CraftTrimPattern.minecraftHolderToBukkit(trimCompound.pattern()); + TrimMaterial trimMaterial = org.bukkit.craftbukkit.CraftRegistry.unwrapAndConvertHolder(io.papermc.paper.registry.RegistryKey.TRIM_MATERIAL, trimCompound.material()).orElse(null); // Paper - fix upstream not being correct + TrimPattern trimPattern = org.bukkit.craftbukkit.CraftRegistry.unwrapAndConvertHolder(io.papermc.paper.registry.RegistryKey.TRIM_PATTERN, trimCompound.pattern()).orElse(null); // Paper - fix upstream not being correct + if (trimMaterial == null || trimPattern == null) return; // Paper - just delete the trim because upstream is not doing this right this.trim = new ArmorTrim(trimMaterial, trimPattern); diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBanner.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBanner.java index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBanner.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaBanner.java @@ -0,0 +0,0 @@ public class CraftMetaBanner extends CraftMetaItem implements BannerMeta { for (int i = 0; i < Math.min(patterns.size(), 20); i++) { BannerPatternLayers.Layer p = patterns.get(i); DyeColor color = DyeColor.getByWoolData((byte) p.color().getId()); - PatternType pattern = CraftPatternType.minecraftHolderToBukkit(p.pattern()); + PatternType pattern = org.bukkit.craftbukkit.CraftRegistry.unwrapAndConvertHolder(org.bukkit.Registry.BANNER_PATTERN, p.pattern()).orElse(null); // Paper - fix upstream not handling inlined banner pattern if (color != null && pattern != null) { this.patterns.add(new Pattern(color, pattern)); diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaMusicInstrument.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaMusicInstrument.java index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaMusicInstrument.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaMusicInstrument.java @@ -0,0 +0,0 @@ public class CraftMetaMusicInstrument extends CraftMetaItem implements MusicInst super(tag); getOrEmpty(tag, CraftMetaMusicInstrument.GOAT_HORN_INSTRUMENT).ifPresent((instrument) -> { - this.instrument = CraftMusicInstrument.minecraftHolderToBukkit(instrument); + this.instrument = org.bukkit.craftbukkit.CraftRegistry.unwrapAndConvertHolder(org.bukkit.Registry.INSTRUMENT, instrument).orElse(null); // Paper - fix upstream not handling inlined instrument }); } diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaShield.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaShield.java index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaShield.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaShield.java @@ -0,0 +0,0 @@ import org.bukkit.block.BlockState; import org.bukkit.block.banner.Pattern; import org.bukkit.block.banner.PatternType; import org.bukkit.configuration.serialization.DelegateDeserialization; +import org.bukkit.craftbukkit.CraftRegistry; import org.bukkit.craftbukkit.block.CraftBlockStates; import org.bukkit.craftbukkit.block.banner.CraftPatternType; import org.bukkit.inventory.meta.BlockStateMeta; @@ -0,0 +0,0 @@ public class CraftMetaShield extends CraftMetaItem implements ShieldMeta, BlockS for (int i = 0; i < Math.min(patterns.size(), 20); i++) { BannerPatternLayers.Layer p = patterns.get(i); DyeColor color = DyeColor.getByWoolData((byte) p.color().getId()); - PatternType pattern = CraftPatternType.minecraftHolderToBukkit(p.pattern()); + PatternType pattern = CraftRegistry.unwrapAndConvertHolder(io.papermc.paper.registry.RegistryKey.BANNER_PATTERN, p.pattern()).orElse(null); // Paper - fix upstream not being correct if (color != null && pattern != null) { this.addPattern(new Pattern(color, pattern)); diff --git a/src/main/java/org/bukkit/craftbukkit/potion/CraftPotionType.java b/src/main/java/org/bukkit/craftbukkit/potion/CraftPotionType.java index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/org/bukkit/craftbukkit/potion/CraftPotionType.java +++ b/src/main/java/org/bukkit/craftbukkit/potion/CraftPotionType.java @@ -0,0 +0,0 @@ public class CraftPotionType implements PotionType.InternalPotionData { string = FieldRename.convertPotionTypeName(ApiVersion.CURRENT, string); string = string.toLowerCase(Locale.ROOT); NamespacedKey key = NamespacedKey.fromString(string); + if (key == null) return null; // Paper - Fixup NamespacedKey handling // Now also convert from when keys where saved return CraftRegistry.get(Registry.POTION, key, ApiVersion.CURRENT); diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftNamespacedKey.java b/src/main/java/org/bukkit/craftbukkit/util/CraftNamespacedKey.java index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/CraftNamespacedKey.java +++ b/src/main/java/org/bukkit/craftbukkit/util/CraftNamespacedKey.java @@ -0,0 +0,0 @@ public final class CraftNamespacedKey { return null; } ResourceLocation minecraft = ResourceLocation.tryParse(string); - return (minecraft == null) ? null : CraftNamespacedKey.fromMinecraft(minecraft); + return (minecraft == null || minecraft.getPath().isEmpty()) ? null : CraftNamespacedKey.fromMinecraft(minecraft); // Paper - Bukkit's parser does not match Vanilla for empty paths } public static NamespacedKey fromString(String string) {