#1029: 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 #660 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:
Bukkit/Spigot
2024-07-06 17:14:17 +10:00
parent 250d585147
commit abc756fce8
7 changed files with 293 additions and 128 deletions

View File

@@ -1,8 +1,13 @@
package org.bukkit.entity;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import java.util.Locale;
import org.bukkit.DyeColor;
import org.bukkit.Keyed;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.util.OldEnum;
import org.jetbrains.annotations.NotNull;
/**
@@ -43,29 +48,50 @@ public interface Cat extends Tameable, Sittable {
/**
* Represents the various different cat types there are.
*/
public enum Type implements Keyed {
TABBY("tabby"),
BLACK("black"),
RED("red"),
SIAMESE("siamese"),
BRITISH_SHORTHAIR("british_shorthair"),
CALICO("calico"),
PERSIAN("persian"),
RAGDOLL("ragdoll"),
WHITE("white"),
JELLIE("jellie"),
ALL_BLACK("all_black");
interface Type extends OldEnum<Type>, Keyed {
private final NamespacedKey key;
Type TABBY = getType("tabby");
Type BLACK = getType("black");
Type RED = getType("red");
Type SIAMESE = getType("siamese");
Type BRITISH_SHORTHAIR = getType("british_shorthair");
Type CALICO = getType("calico");
Type PERSIAN = getType("persian");
Type RAGDOLL = getType("ragdoll");
Type WHITE = getType("white");
Type JELLIE = getType("jellie");
Type ALL_BLACK = getType("all_black");
private Type(String key) {
this.key = NamespacedKey.minecraft(key);
@NotNull
private static Type getType(@NotNull String key) {
NamespacedKey namespacedKey = NamespacedKey.minecraft(key);
Type type = Registry.CAT_VARIANT.get(namespacedKey);
Preconditions.checkNotNull(type, "No cat type found for %s. This is a bug.", namespacedKey);
return type;
}
@Override
/**
* @param name of the cat type.
* @return the cat type with the given name.
* @deprecated only for backwards compatibility, use {@link Registry#get(NamespacedKey)} instead.
*/
@NotNull
public NamespacedKey getKey() {
return key;
@Deprecated(since = "1.21")
static Type valueOf(@NotNull String name) {
Type type = Registry.CAT_VARIANT.get(NamespacedKey.fromString(name.toLowerCase(Locale.ROOT)));
Preconditions.checkArgument(type != null, "No cat type found with the name %s", name);
return type;
}
/**
* @return an array of all known cat types.
* @deprecated use {@link Registry#iterator()}.
*/
@NotNull
@Deprecated(since = "1.21")
static Type[] values() {
return Lists.newArrayList(Registry.CAT_VARIANT).toArray(new Type[0]);
}
}
}

View File

@@ -1,8 +1,12 @@
package org.bukkit.entity;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import java.util.Locale;
import org.bukkit.Keyed;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.util.OldEnum;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -44,30 +48,51 @@ public interface Frog extends Animals {
/**
* Represents the variant of a frog - ie its color.
*/
public enum Variant implements Keyed {
interface Variant extends OldEnum<Variant>, Keyed {
/**
* Temperate (brown-orange) frog.
*/
TEMPERATE,
Variant TEMPERATE = getVariant("temperate");
/**
* Warm (gray) frog.
*/
WARM,
Variant WARM = getVariant("warm");
/**
* Cold (green) frog.
*/
COLD;
private final NamespacedKey key;
private Variant() {
this.key = NamespacedKey.minecraft(name().toLowerCase(Locale.ROOT));
}
Variant COLD = getVariant("cold");
@NotNull
@Override
public NamespacedKey getKey() {
return key;
private static Variant getVariant(@NotNull String key) {
NamespacedKey namespacedKey = NamespacedKey.minecraft(key);
Variant variant = Registry.FROG_VARIANT.get(namespacedKey);
Preconditions.checkNotNull(variant, "No frog variant found for %s. This is a bug.", namespacedKey);
return variant;
}
/**
* @param name of the frog variant.
* @return the frog variant with the given name.
* @deprecated only for backwards compatibility, use {@link Registry#get(NamespacedKey)} instead.
*/
@NotNull
@Deprecated(since = "1.21")
static Variant valueOf(@NotNull String name) {
Variant variant = Registry.FROG_VARIANT.get(NamespacedKey.fromString(name.toLowerCase(Locale.ROOT)));
Preconditions.checkArgument(variant != null, "No frog variant found with the name %s", name);
return variant;
}
/**
* @return an array of all known frog variants.
* @deprecated use {@link Registry#iterator()}.
*/
@NotNull
@Deprecated(since = "1.21")
static Variant[] values() {
return Lists.newArrayList(Registry.FROG_VARIANT).toArray(new Variant[0]);
}
}
}

View File

@@ -1,9 +1,13 @@
package org.bukkit.entity;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import java.util.Locale;
import org.bukkit.Keyed;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
import org.bukkit.util.OldEnum;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -118,25 +122,46 @@ public interface Villager extends AbstractVillager {
* Represents Villager type, usually corresponding to what biome they spawn
* in.
*/
public enum Type implements Keyed {
interface Type extends OldEnum<Type>, Keyed {
DESERT,
JUNGLE,
PLAINS,
SAVANNA,
SNOW,
SWAMP,
TAIGA;
private final NamespacedKey key;
private Type() {
this.key = NamespacedKey.minecraft(this.name().toLowerCase(Locale.ROOT));
}
Type DESERT = getType("desert");
Type JUNGLE = getType("jungle");
Type PLAINS = getType("plains");
Type SAVANNA = getType("savanna");
Type SNOW = getType("snow");
Type SWAMP = getType("swamp");
Type TAIGA = getType("taiga");
@NotNull
@Override
public NamespacedKey getKey() {
return key;
private static Type getType(@NotNull String key) {
NamespacedKey namespacedKey = NamespacedKey.minecraft(key);
Type type = Registry.VILLAGER_TYPE.get(namespacedKey);
Preconditions.checkNotNull(type, "No villager type found for %s. This is a bug.", namespacedKey);
return type;
}
/**
* @param name of the villager type.
* @return the villager type with the given name.
* @deprecated only for backwards compatibility, use {@link Registry#get(NamespacedKey)} instead.
*/
@NotNull
@Deprecated(since = "1.21")
static Type valueOf(@NotNull String name) {
Type type = Registry.VILLAGER_TYPE.get(NamespacedKey.fromString(name.toLowerCase(Locale.ROOT)));
Preconditions.checkArgument(type != null, "No villager type found with the name %s", name);
return type;
}
/**
* @return an array of all known villager types.
* @deprecated use {@link Registry#iterator()}.
*/
@NotNull
@Deprecated(since = "1.21")
static Type[] values() {
return Lists.newArrayList(Registry.VILLAGER_TYPE).toArray(new Type[0]);
}
}
@@ -144,88 +169,110 @@ public interface Villager extends AbstractVillager {
* Represents the various different Villager professions there may be.
* Villagers have different trading options depending on their profession,
*/
public enum Profession implements Keyed {
NONE,
interface Profession extends OldEnum<Profession>, Keyed {
Profession NONE = getProfession("none");
/**
* Armorer profession. Wears a black apron. Armorers primarily trade for
* iron armor, chainmail armor, and sometimes diamond armor.
*/
ARMORER,
Profession ARMORER = getProfession("armorer");
/**
* Butcher profession. Wears a white apron. Butchers primarily trade for
* raw and cooked food.
*/
BUTCHER,
Profession BUTCHER = getProfession("butcher");
/**
* Cartographer profession. Wears a white robe. Cartographers primarily
* trade for explorer maps and some paper.
*/
CARTOGRAPHER,
Profession CARTOGRAPHER = getProfession("cartographer");
/**
* Cleric profession. Wears a purple robe. Clerics primarily trade for
* rotten flesh, gold ingot, redstone, lapis, ender pearl, glowstone,
* and bottle o' enchanting.
*/
CLERIC,
Profession CLERIC = getProfession("cleric");
/**
* Farmer profession. Wears a brown robe. Farmers primarily trade for
* food-related items.
*/
FARMER,
Profession FARMER = getProfession("farmer");
/**
* Fisherman profession. Wears a brown robe. Fisherman primarily trade
* for fish, as well as possibly selling string and/or coal.
*/
FISHERMAN,
Profession FISHERMAN = getProfession("fisherman");
/**
* Fletcher profession. Wears a brown robe. Fletchers primarily trade
* for string, bows, and arrows.
*/
FLETCHER,
Profession FLETCHER = getProfession("fletcher");
/**
* Leatherworker profession. Wears a white apron. Leatherworkers
* primarily trade for leather, and leather armor, as well as saddles.
*/
LEATHERWORKER,
Profession LEATHERWORKER = getProfession("leatherworker");
/**
* Librarian profession. Wears a white robe. Librarians primarily trade
* for paper, books, and enchanted books.
*/
LIBRARIAN,
Profession LIBRARIAN = getProfession("librarian");
/**
* Mason profession.
*/
MASON,
Profession MASON = getProfession("mason");
/**
* Nitwit profession. Wears a green apron, cannot trade. Nitwit
* villagers do not do anything. They do not have any trades by default.
*/
NITWIT,
Profession NITWIT = getProfession("nitwit");
/**
* Sheperd profession. Wears a brown robe. Shepherds primarily trade for
* wool items, and shears.
*/
SHEPHERD,
Profession SHEPHERD = getProfession("shepherd");
/**
* Toolsmith profession. Wears a black apron. Tool smiths primarily
* trade for iron and diamond tools.
*/
TOOLSMITH,
Profession TOOLSMITH = getProfession("toolsmith");
/**
* Weaponsmith profession. Wears a black apron. Weapon smiths primarily
* trade for iron and diamond weapons, sometimes enchanted.
*/
WEAPONSMITH;
private final NamespacedKey key;
private Profession() {
this.key = NamespacedKey.minecraft(this.name().toLowerCase(Locale.ROOT));
}
Profession WEAPONSMITH = getProfession("weaponsmith");
@NotNull
@Override
public NamespacedKey getKey() {
return key;
private static Profession getProfession(@NotNull String key) {
NamespacedKey namespacedKey = NamespacedKey.minecraft(key);
Profession profession = Registry.VILLAGER_PROFESSION.get(namespacedKey);
Preconditions.checkNotNull(profession, "No villager profession found for %s. This is a bug.", namespacedKey);
return profession;
}
/**
* @param name of the villager profession.
* @return the villager profession with the given name.
* @deprecated only for backwards compatibility, use {@link Registry#get(NamespacedKey)} instead.
*/
@NotNull
@Deprecated(since = "1.21")
static Profession valueOf(@NotNull String name) {
Profession profession = Registry.VILLAGER_PROFESSION.get(NamespacedKey.fromString(name.toLowerCase(Locale.ROOT)));
Preconditions.checkArgument(profession != null, "No villager profession found with the name %s", name);
return profession;
}
/**
* @return an array of all known villager professions.
* @deprecated use {@link Registry#iterator()}.
*/
@NotNull
@Deprecated(since = "1.21")
static Profession[] values() {
return Lists.newArrayList(Registry.VILLAGER_PROFESSION).toArray(new Profession[0]);
}
}
}