#1424: Trial changing a small number of inner enums to classes/interfaces to better support custom values

This PR is a subset of the enum PR #931 and is designed as a low impact
trial run of the design and backwards compatibility to inform
subsequent development.

Additional plugin compatibility features may be available by setting
`settings.compatibility.enum-compatibility-mode` to `true` in
`bukkit.yml`.

By: DerFrZocker <derrieple@gmail.com>
This commit is contained in:
CraftBukkit/Spigot
2024-07-06 17:14:22 +10:00
parent f59f0d1c9b
commit 41b8d833db
27 changed files with 1424 additions and 117 deletions

View File

@@ -1,17 +1,18 @@
package org.bukkit.craftbukkit.entity;
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.animal.CatVariant;
import net.minecraft.world.entity.animal.EntityCat;
import net.minecraft.world.item.EnumColor;
import org.bukkit.DyeColor;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.craftbukkit.CraftRegistry;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
import org.bukkit.craftbukkit.util.Handleable;
import org.bukkit.entity.Cat;
public class CraftCat extends CraftTameableAnimal implements Cat {
@@ -52,14 +53,11 @@ public class CraftCat extends CraftTameableAnimal implements Cat {
getHandle().setCollarColor(EnumColor.byId(color.getWoolData()));
}
public static class CraftType {
public static class CraftType implements Type, Handleable<CatVariant> {
private static int count = 0;
public static Type minecraftToBukkit(CatVariant minecraft) {
Preconditions.checkArgument(minecraft != null);
IRegistry<CatVariant> registry = CraftRegistry.getMinecraftRegistry(Registries.CAT_VARIANT);
return Registry.CAT_VARIANT.get(CraftNamespacedKey.fromMinecraft(registry.getKey(minecraft)));
return CraftRegistry.minecraftToBukkit(minecraft, Registries.CAT_VARIANT, Registry.CAT_VARIANT);
}
public static Type minecraftHolderToBukkit(Holder<CatVariant> minecraft) {
@@ -67,24 +65,80 @@ public class CraftCat extends CraftTameableAnimal implements Cat {
}
public static CatVariant bukkitToMinecraft(Type bukkit) {
Preconditions.checkArgument(bukkit != null);
IRegistry<CatVariant> registry = CraftRegistry.getMinecraftRegistry(Registries.CAT_VARIANT);
return registry.get(CraftNamespacedKey.toMinecraft(bukkit.getKey()));
return CraftRegistry.bukkitToMinecraft(bukkit);
}
public static Holder<CatVariant> bukkitToMinecraftHolder(Type bukkit) {
Preconditions.checkArgument(bukkit != null);
return CraftRegistry.bukkitToMinecraftHolder(bukkit, Registries.CAT_VARIANT);
}
IRegistry<CatVariant> registry = CraftRegistry.getMinecraftRegistry(Registries.CAT_VARIANT);
private final NamespacedKey key;
private final CatVariant catVariant;
private final String name;
private final int ordinal;
if (registry.wrapAsHolder(bukkitToMinecraft(bukkit)) instanceof Holder.c<CatVariant> holder) {
return holder;
public CraftType(NamespacedKey key, CatVariant catVariant) {
this.key = key;
this.catVariant = catVariant;
// For backwards compatibility, minecraft values will still return the uppercase name without the namespace,
// in case plugins use for example the name as key in a config file to receive type specific values.
// Custom types will return the key with namespace. For a plugin this should look than like a new type
// (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 CatVariant getHandle() {
return catVariant;
}
@Override
public NamespacedKey getKey() {
return key;
}
@Override
public int compareTo(Type variant) {
return ordinal - variant.ordinal();
}
@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;
}
throw new IllegalArgumentException("No Reference holder found for " + bukkit
+ ", this can happen if a plugin creates its own cat variant with out properly registering it.");
if (!(other instanceof CraftType)) {
return false;
}
return getKey().equals(((CraftType) other).getKey());
}
@Override
public int hashCode() {
return getKey().hashCode();
}
}
}

View File

@@ -1,15 +1,16 @@
package org.bukkit.craftbukkit.entity;
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.animal.FrogVariant;
import net.minecraft.world.entity.animal.frog.Frog;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.craftbukkit.CraftRegistry;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
import org.bukkit.craftbukkit.util.Handleable;
import org.bukkit.entity.Entity;
public class CraftFrog extends CraftAnimals implements org.bukkit.entity.Frog {
@@ -54,17 +55,11 @@ public class CraftFrog extends CraftAnimals implements org.bukkit.entity.Frog {
getHandle().setVariant(CraftVariant.bukkitToMinecraftHolder(variant));
}
public static class CraftVariant {
public static class CraftVariant implements Variant, Handleable<FrogVariant> {
private static int count = 0;
public static Variant minecraftToBukkit(FrogVariant minecraft) {
Preconditions.checkArgument(minecraft != null);
IRegistry<FrogVariant> registry = CraftRegistry.getMinecraftRegistry(Registries.FROG_VARIANT);
Variant bukkit = Registry.FROG_VARIANT.get(CraftNamespacedKey.fromMinecraft(registry.getResourceKey(minecraft).orElseThrow().location()));
Preconditions.checkArgument(bukkit != null);
return bukkit;
return CraftRegistry.minecraftToBukkit(minecraft, Registries.FROG_VARIANT, Registry.FROG_VARIANT);
}
public static Variant minecraftHolderToBukkit(Holder<FrogVariant> minecraft) {
@@ -72,23 +67,80 @@ public class CraftFrog extends CraftAnimals implements org.bukkit.entity.Frog {
}
public static FrogVariant bukkitToMinecraft(Variant bukkit) {
Preconditions.checkArgument(bukkit != null);
return CraftRegistry.getMinecraftRegistry(Registries.FROG_VARIANT)
.getOptional(CraftNamespacedKey.toMinecraft(bukkit.getKey())).orElseThrow();
return CraftRegistry.bukkitToMinecraft(bukkit);
}
public static Holder<FrogVariant> bukkitToMinecraftHolder(Variant bukkit) {
Preconditions.checkArgument(bukkit != null);
return CraftRegistry.bukkitToMinecraftHolder(bukkit, Registries.FROG_VARIANT);
}
IRegistry<FrogVariant> registry = CraftRegistry.getMinecraftRegistry(Registries.FROG_VARIANT);
private final NamespacedKey key;
private final FrogVariant frogVariant;
private final String name;
private final int ordinal;
if (registry.wrapAsHolder(bukkitToMinecraft(bukkit)) instanceof Holder.c<FrogVariant> holder) {
return holder;
public CraftVariant(NamespacedKey key, FrogVariant frogVariant) {
this.key = key;
this.frogVariant = frogVariant;
// For backwards compatibility, minecraft values will still return the uppercase name without the namespace,
// in case plugins use for example the name as key in a config file to receive variant specific values.
// Custom variants will return the key with namespace. For a plugin this should look than like a new variant
// (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 FrogVariant getHandle() {
return frogVariant;
}
@Override
public NamespacedKey getKey() {
return key;
}
@Override
public int compareTo(Variant variant) {
return ordinal - variant.ordinal();
}
@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;
}
throw new IllegalArgumentException("No Reference holder found for " + bukkit
+ ", this can happen if a plugin creates its own frog variant with out properly registering it.");
if (!(other instanceof CraftVariant)) {
return false;
}
return getKey().equals(((Variant) other).getKey());
}
@Override
public int hashCode() {
return getKey().hashCode();
}
}
}

View File

@@ -1,8 +1,8 @@
package org.bukkit.craftbukkit.entity;
import com.google.common.base.Preconditions;
import java.util.Locale;
import net.minecraft.core.BlockPosition;
import net.minecraft.core.IRegistry;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.entity.monster.EntityZombie;
import net.minecraft.world.entity.monster.EntityZombieVillager;
@@ -12,11 +12,12 @@ import net.minecraft.world.entity.npc.VillagerType;
import net.minecraft.world.level.block.BlockBed;
import net.minecraft.world.level.block.state.IBlockData;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.craftbukkit.CraftRegistry;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.util.CraftLocation;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
import org.bukkit.craftbukkit.util.Handleable;
import org.bukkit.entity.Villager;
import org.bukkit.entity.ZombieVillager;
import org.bukkit.event.entity.CreatureSpawnEvent;
@@ -126,45 +127,165 @@ public class CraftVillager extends CraftAbstractVillager implements Villager {
return (entityzombievillager != null) ? (ZombieVillager) entityzombievillager.getBukkitEntity() : null;
}
public static class CraftType {
public static class CraftType implements Type, Handleable<VillagerType> {
private static int count = 0;
public static Type minecraftToBukkit(VillagerType minecraft) {
Preconditions.checkArgument(minecraft != null);
IRegistry<VillagerType> registry = CraftRegistry.getMinecraftRegistry(Registries.VILLAGER_TYPE);
Type bukkit = Registry.VILLAGER_TYPE.get(CraftNamespacedKey.fromMinecraft(registry.getResourceKey(minecraft).orElseThrow().location()));
Preconditions.checkArgument(bukkit != null);
return bukkit;
return CraftRegistry.minecraftToBukkit(minecraft, Registries.VILLAGER_TYPE, Registry.VILLAGER_TYPE);
}
public static VillagerType bukkitToMinecraft(Type bukkit) {
Preconditions.checkArgument(bukkit != null);
return CraftRegistry.bukkitToMinecraft(bukkit);
}
return CraftRegistry.getMinecraftRegistry(Registries.VILLAGER_TYPE)
.getOptional(CraftNamespacedKey.toMinecraft(bukkit.getKey())).orElseThrow();
private final NamespacedKey key;
private final VillagerType villagerType;
private final String name;
private final int ordinal;
public CraftType(NamespacedKey key, VillagerType villagerType) {
this.key = key;
this.villagerType = villagerType;
// For backwards compatibility, minecraft values will still return the uppercase name without the namespace,
// in case plugins use for example the name as key in a config file to receive type specific values.
// Custom types will return the key with namespace. For a plugin this should look than like a new type
// (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 VillagerType getHandle() {
return villagerType;
}
@Override
public NamespacedKey getKey() {
return key;
}
@Override
public int compareTo(Type type) {
return ordinal - type.ordinal();
}
@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 CraftType)) {
return false;
}
return getKey().equals(((Type) other).getKey());
}
@Override
public int hashCode() {
return getKey().hashCode();
}
}
public static class CraftProfession {
public static class CraftProfession implements Profession, Handleable<VillagerProfession> {
private static int count = 0;
public static Profession minecraftToBukkit(VillagerProfession minecraft) {
Preconditions.checkArgument(minecraft != null);
IRegistry<VillagerProfession> registry = CraftRegistry.getMinecraftRegistry(Registries.VILLAGER_PROFESSION);
Profession bukkit = Registry.VILLAGER_PROFESSION.get(CraftNamespacedKey.fromMinecraft(registry.getResourceKey(minecraft).orElseThrow().location()));
Preconditions.checkArgument(bukkit != null);
return bukkit;
return CraftRegistry.minecraftToBukkit(minecraft, Registries.VILLAGER_PROFESSION, Registry.VILLAGER_PROFESSION);
}
public static VillagerProfession bukkitToMinecraft(Profession bukkit) {
Preconditions.checkArgument(bukkit != null);
return CraftRegistry.bukkitToMinecraft(bukkit);
}
return CraftRegistry.getMinecraftRegistry(Registries.VILLAGER_PROFESSION)
.getOptional(CraftNamespacedKey.toMinecraft(bukkit.getKey())).orElseThrow();
private final NamespacedKey key;
private final VillagerProfession villagerProfession;
private final String name;
private final int ordinal;
public CraftProfession(NamespacedKey key, VillagerProfession villagerProfession) {
this.key = key;
this.villagerProfession = villagerProfession;
// For backwards compatibility, minecraft values will still return the uppercase name without the namespace,
// in case plugins use for example the name as key in a config file to receive profession specific values.
// Custom professions will return the key with namespace. For a plugin this should look than like a new profession
// (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 VillagerProfession getHandle() {
return villagerProfession;
}
@Override
public NamespacedKey getKey() {
return key;
}
@Override
public int compareTo(Profession profession) {
return ordinal - profession.ordinal();
}
@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 CraftProfession)) {
return false;
}
return getKey().equals(((Profession) other).getKey());
}
@Override
public int hashCode() {
return getKey().hashCode();
}
}
}