From c6b51f7c66ceabf0c2ef136c4803a3a2f30b5997 Mon Sep 17 00:00:00 2001 From: DerFrZocker Date: Wed, 24 May 2023 18:20:06 +0200 Subject: [PATCH] Convert Cat type --- src/main/java/org/bukkit/Registry.java | 7 +++ src/main/java/org/bukkit/entity/Cat.java | 61 +++++++++++++++++++----- 2 files changed, 56 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/bukkit/Registry.java b/src/main/java/org/bukkit/Registry.java index e9fe44b0..e86865a7 100644 --- a/src/main/java/org/bukkit/Registry.java +++ b/src/main/java/org/bukkit/Registry.java @@ -13,6 +13,7 @@ import org.bukkit.block.Biome; import org.bukkit.block.BlockType; import org.bukkit.boss.KeyedBossBar; import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.Cat; import org.bukkit.entity.EntityType; import org.bukkit.entity.Frog; import org.bukkit.entity.Villager; @@ -100,6 +101,12 @@ public interface Registry extends Iterable { return Bukkit.getBossBars(); } }; + /** + * Server cat types. + * + * @see Cat.Type + */ + Registry CAT_TYPE = Objects.requireNonNull(Bukkit.getRegistry(Cat.Type.class), "No registry present for Cat Type. This is a bug."); /** * Server enchantments. * diff --git a/src/main/java/org/bukkit/entity/Cat.java b/src/main/java/org/bukkit/entity/Cat.java index c2a566b8..a541d961 100644 --- a/src/main/java/org/bukkit/entity/Cat.java +++ b/src/main/java/org/bukkit/entity/Cat.java @@ -1,6 +1,12 @@ package org.bukkit.entity; +import com.google.common.base.Preconditions; +import com.google.common.collect.Lists; 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; /** @@ -41,17 +47,48 @@ public interface Cat extends Tameable, Sittable { /** * Represents the various different cat types there are. */ - public enum Type { - TABBY, - BLACK, - RED, - SIAMESE, - BRITISH_SHORTHAIR, - CALICO, - PERSIAN, - RAGDOLL, - WHITE, - JELLIE, - ALL_BLACK; + abstract class Type extends OldEnum implements Keyed { + public static final Type TABBY = getType("tabby"); + public static final Type BLACK = getType("black"); + public static final Type RED = getType("red"); + public static final Type SIAMESE = getType("siamese"); + public static final Type BRITISH_SHORTHAIR = getType("british_shorthair"); + public static final Type CALICO = getType("calico"); + public static final Type PERSIAN = getType("persian"); + public static final Type RAGDOLL = getType("ragdoll"); + public static final Type WHITE = getType("white"); + public static final Type JELLIE = getType("jellie"); + public static final Type ALL_BLACK = getType("all_black"); + + @NotNull + private static Type getType(@NotNull String key) { + NamespacedKey namespacedKey = NamespacedKey.minecraft(key); + Type type = Registry.CAT_TYPE.get(namespacedKey); + Preconditions.checkNotNull(type, "No cat type found for %s. This is a bug.", namespacedKey); + return type; + } + + /** + * @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 + @Deprecated + public static Type valueOf(@NotNull String name) { + Type type = Registry.CAT_TYPE.get(NamespacedKey.fromString(name.toLowerCase())); + 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 + public static Type[] values() { + return Lists.newArrayList(Registry.CAT_TYPE).toArray(new Type[0]); + } } }