diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftArt.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftArt.java index e67ec2ea8c..d9da8e49b4 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftArt.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftArt.java @@ -1,25 +1,23 @@ package org.bukkit.craftbukkit; import com.google.common.base.Preconditions; +import java.util.Locale; import net.minecraft.core.Holder; import net.minecraft.core.IRegistry; import net.minecraft.core.registries.Registries; import net.minecraft.world.entity.decoration.PaintingVariant; import org.bukkit.Art; +import org.bukkit.NamespacedKey; import org.bukkit.Registry; -import org.bukkit.craftbukkit.util.CraftNamespacedKey; +import org.bukkit.craftbukkit.util.Handleable; +import org.jetbrains.annotations.NotNull; -public class CraftArt { +public class CraftArt implements Art, Handleable { + + private static int count = 0; public static Art minecraftToBukkit(PaintingVariant minecraft) { - Preconditions.checkArgument(minecraft != null); - - IRegistry registry = CraftRegistry.getMinecraftRegistry(Registries.PAINTING_VARIANT); - Art bukkit = Registry.ART.get(CraftNamespacedKey.fromMinecraft(registry.getResourceKey(minecraft).orElseThrow().location())); - - Preconditions.checkArgument(bukkit != null); - - return bukkit; + return CraftRegistry.minecraftToBukkit(minecraft, Registries.PAINTING_VARIANT, Registry.ART); } public static Art minecraftHolderToBukkit(Holder minecraft) { @@ -27,10 +25,7 @@ public class CraftArt { } public static PaintingVariant bukkitToMinecraft(Art bukkit) { - Preconditions.checkArgument(bukkit != null); - - return CraftRegistry.getMinecraftRegistry(Registries.PAINTING_VARIANT) - .getOptional(CraftNamespacedKey.toMinecraft(bukkit.getKey())).orElseThrow(); + return CraftRegistry.bukkitToMinecraft(bukkit); } public static Holder bukkitToMinecraftHolder(Art bukkit) { @@ -45,4 +40,90 @@ public class CraftArt { throw new IllegalArgumentException("No Reference holder found for " + bukkit + ", this can happen if a plugin creates its own painting variant with out properly registering it."); } + + private final NamespacedKey key; + private final PaintingVariant paintingVariant; + private final String name; + private final int ordinal; + + public CraftArt(NamespacedKey key, PaintingVariant paintingVariant) { + this.key = key; + this.paintingVariant = paintingVariant; + // For backwards compatibility, minecraft values will stile return the uppercase name without the namespace, + // in case plugins use for example the name as key in a config file to receive art specific values. + // Custom arts will return the key with namespace. For a plugin this should look than like a new art + // (which can always be added in new minecraft versions and the plugin should therefore handle it accordingly). + if (NamespacedKey.MINECRAFT.equals(key.getNamespace())) { + this.name = key.getKey().toUpperCase(Locale.ROOT); + } else { + this.name = key.toString(); + } + this.ordinal = count++; + } + + @Override + public PaintingVariant getHandle() { + return paintingVariant; + } + + @Override + public int getBlockWidth() { + return paintingVariant.width(); + } + + @Override + public int getBlockHeight() { + return paintingVariant.height(); + } + + @Override + public int getId() { + return CraftRegistry.getMinecraftRegistry(Registries.PAINTING_VARIANT).getId(paintingVariant); + } + + @NotNull + @Override + public NamespacedKey getKey() { + return key; + } + + @Override + public int compareTo(@NotNull Art art) { + return ordinal - art.ordinal(); + } + + @NotNull + @Override + public String name() { + return name; + } + + @Override + public int ordinal() { + return ordinal; + } + + @Override + public String toString() { + // For backwards compatibility + return name(); + } + + @Override + public boolean equals(Object other) { + if (this == other) { + return true; + } + + if (!(other instanceof CraftArt otherArt)) { + return false; + } + + return getKey().equals(otherArt.getKey()); + } + + @Override + public int hashCode() { + return getKey().hashCode(); + } } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java index e23daadeca..cea16c3b2f 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/CraftRegistry.java @@ -11,6 +11,7 @@ import net.minecraft.core.IRegistry; import net.minecraft.core.IRegistryCustom; import net.minecraft.core.registries.Registries; import net.minecraft.resources.ResourceKey; +import org.bukkit.Art; import org.bukkit.Fluid; import org.bukkit.GameEvent; import org.bukkit.JukeboxSong; @@ -135,6 +136,9 @@ public class CraftRegistry implements Registry { * @return the bukkit registry of the provided class */ public static Registry createRegistry(Class bukkitClass, IRegistryCustom registryHolder) { + if (bukkitClass == Art.class) { + return new CraftRegistry<>(Art.class, registryHolder.lookupOrThrow(Registries.PAINTING_VARIANT), CraftArt::new, FieldRename.NONE); + } if (bukkitClass == Attribute.class) { return new CraftRegistry<>(Attribute.class, registryHolder.lookupOrThrow(Registries.ATTRIBUTE), CraftAttribute::new, FieldRename.ATTRIBUTE_RENAME); } diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/legacy/enums/EnumEvil.java b/paper-server/src/main/java/org/bukkit/craftbukkit/legacy/enums/EnumEvil.java index 32ba6cc36d..4c7b199d4b 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/legacy/enums/EnumEvil.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/legacy/enums/EnumEvil.java @@ -15,6 +15,7 @@ import java.util.Optional; import java.util.function.Function; import java.util.stream.Collector; import java.util.stream.Collectors; +import org.bukkit.Art; import org.bukkit.Fluid; import org.bukkit.NamespacedKey; import org.bukkit.Registry; @@ -48,6 +49,7 @@ public class EnumEvil { static { // Add Classes which got changed here + REGISTRIES.put(Art.class, new LegacyRegistryData(Registry.ART, Art::valueOf)); REGISTRIES.put(Attribute.class, new LegacyRegistryData(Registry.ATTRIBUTE, Attribute::valueOf)); REGISTRIES.put(Biome.class, new LegacyRegistryData(Registry.BIOME, Biome::valueOf)); REGISTRIES.put(Fluid.class, new LegacyRegistryData(Registry.FLUID, Fluid::valueOf)); diff --git a/paper-server/src/main/java/org/bukkit/craftbukkit/util/Commodore.java b/paper-server/src/main/java/org/bukkit/craftbukkit/util/Commodore.java index 3f6a25d77a..79e734f569 100644 --- a/paper-server/src/main/java/org/bukkit/craftbukkit/util/Commodore.java +++ b/paper-server/src/main/java/org/bukkit/craftbukkit/util/Commodore.java @@ -88,6 +88,7 @@ public class Commodore { Map.entry("org/bukkit/entity/Cat$Type", "NOP"), Map.entry("org/bukkit/map/MapCursor$Type", "NOP"), Map.entry("org/bukkit/block/banner/PatternType", "NOP"), + Map.entry("org/bukkit/Art", "NOP"), Map.entry("org/bukkit/attribute/Attribute", "NOP"), Map.entry("org/bukkit/block/Biome", "NOP"), Map.entry("org/bukkit/Fluid", "NOP"), diff --git a/paper-server/src/test/java/org/bukkit/ArtTest.java b/paper-server/src/test/java/org/bukkit/ArtTest.java index 1c4469f2d1..750b8f8103 100644 --- a/paper-server/src/test/java/org/bukkit/ArtTest.java +++ b/paper-server/src/test/java/org/bukkit/ArtTest.java @@ -5,7 +5,6 @@ import static org.hamcrest.Matchers.*; import static org.junit.jupiter.api.Assertions.*; import com.google.common.collect.Lists; import java.util.Collections; -import java.util.EnumMap; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -58,7 +57,7 @@ public class ArtTest { @Test public void testCraftArtToBukkit() { - Map> cache = new EnumMap(Art.class); + Map> cache = new HashMap<>(); for (Holder enumArt : CraftRegistry.getMinecraftRegistry(Registries.PAINTING_VARIANT).asHolderIdMap()) { Art art = CraftArt.minecraftHolderToBukkit(enumArt); assertNotNull(art, "Could not CraftArt.NotchToBukkit " + enumArt); diff --git a/paper-server/src/test/java/org/bukkit/support/provider/RegistriesArgumentProvider.java b/paper-server/src/test/java/org/bukkit/support/provider/RegistriesArgumentProvider.java index 905d401a7f..9ed690ad3e 100644 --- a/paper-server/src/test/java/org/bukkit/support/provider/RegistriesArgumentProvider.java +++ b/paper-server/src/test/java/org/bukkit/support/provider/RegistriesArgumentProvider.java @@ -11,6 +11,7 @@ import net.minecraft.world.entity.ai.attributes.AttributeBase; import net.minecraft.world.entity.animal.CatVariant; import net.minecraft.world.entity.animal.FrogVariant; import net.minecraft.world.entity.animal.WolfVariant; +import net.minecraft.world.entity.decoration.PaintingVariant; import net.minecraft.world.entity.npc.VillagerProfession; import net.minecraft.world.entity.npc.VillagerType; import net.minecraft.world.inventory.Containers; @@ -19,6 +20,7 @@ import net.minecraft.world.level.biome.BiomeBase; import net.minecraft.world.level.block.entity.EnumBannerPatternType; import net.minecraft.world.level.material.FluidType; import net.minecraft.world.level.saveddata.maps.MapDecorationType; +import org.bukkit.Art; import org.bukkit.Fluid; import org.bukkit.GameEvent; import org.bukkit.JukeboxSong; @@ -28,6 +30,7 @@ import org.bukkit.attribute.Attribute; import org.bukkit.block.Biome; import org.bukkit.block.BlockType; import org.bukkit.block.banner.PatternType; +import org.bukkit.craftbukkit.CraftArt; import org.bukkit.craftbukkit.CraftFluid; import org.bukkit.craftbukkit.CraftGameEvent; import org.bukkit.craftbukkit.CraftJukeboxSong; @@ -75,6 +78,7 @@ public class RegistriesArgumentProvider implements ArgumentsProvider { static { // Order: Bukkit class, Minecraft Registry key, CraftBukkit class, Minecraft class + register(Art.class, Registries.PAINTING_VARIANT, CraftArt.class, PaintingVariant.class); register(Attribute.class, Registries.ATTRIBUTE, CraftAttribute.class, AttributeBase.class); register(Biome.class, Registries.BIOME, CraftBiome.class, BiomeBase.class); register(Enchantment.class, Registries.ENCHANTMENT, CraftEnchantment.class, net.minecraft.world.item.enchantment.Enchantment.class);