#1504: Make Art an interface

By: DerFrZocker <derrieple@gmail.com>
This commit is contained in:
CraftBukkit/Spigot 2024-11-02 11:00:13 +11:00
parent 42ebf1afa4
commit aefa65b696
6 changed files with 107 additions and 16 deletions

View file

@ -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<PaintingVariant> {
private static int count = 0;
public static Art minecraftToBukkit(PaintingVariant minecraft) {
Preconditions.checkArgument(minecraft != null);
IRegistry<PaintingVariant> 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<PaintingVariant> 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<PaintingVariant> 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();
}
}

View file

@ -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<B extends Keyed, M> implements Registry<B> {
* @return the bukkit registry of the provided class
*/
public static <B extends Keyed> Registry<?> createRegistry(Class<? super B> 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);
}

View file

@ -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));

View file

@ -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"),

View file

@ -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<Art, Holder<PaintingVariant>> cache = new EnumMap(Art.class);
Map<Art, Holder<PaintingVariant>> cache = new HashMap<>();
for (Holder<PaintingVariant> enumArt : CraftRegistry.getMinecraftRegistry(Registries.PAINTING_VARIANT).asHolderIdMap()) {
Art art = CraftArt.minecraftHolderToBukkit(enumArt);
assertNotNull(art, "Could not CraftArt.NotchToBukkit " + enumArt);

View file

@ -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);