diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandkittycannon.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandkittycannon.java index e40234f52..18397f82f 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandkittycannon.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandkittycannon.java @@ -2,6 +2,7 @@ package com.earth2me.essentials.commands; import com.earth2me.essentials.Mob; import com.earth2me.essentials.User; +import com.earth2me.essentials.utils.RegistryUtil; import org.bukkit.Location; import org.bukkit.Server; import org.bukkit.entity.Cat; @@ -21,8 +22,12 @@ public class Commandkittycannon extends EssentialsCommand { private static Ocelot spawnOcelot(final Server server, final User user) throws Mob.MobException { final Ocelot ocelot = (Ocelot) Mob.OCELOT.spawn(user.getWorld(), server, user.getBase().getEyeLocation()); - final int i = random.nextInt(Ocelot.Type.values().length); - ocelot.setCatType(Ocelot.Type.values()[i]); + //noinspection deprecation + final Object[] values = RegistryUtil.values(Ocelot.Type.class); + + final int i = random.nextInt(values.length); + //noinspection deprecation + ocelot.setCatType((Ocelot.Type) values[i]); ((Tameable) ocelot).setTamed(true); ocelot.setBaby(); ocelot.setVelocity(user.getBase().getEyeLocation().getDirection().multiply(2)); @@ -31,8 +36,10 @@ public class Commandkittycannon extends EssentialsCommand { private static Entity spawnCat(final Server server, final User user) throws Mob.MobException { final Cat cat = (Cat) Mob.CAT.spawn(user.getWorld(), server, user.getBase().getEyeLocation()); - final int i = random.nextInt(Cat.Type.values().length); - cat.setCatType(Cat.Type.values()[i]); + final Object[] values = RegistryUtil.values(Cat.Type.class); + + final int i = random.nextInt(values.length); + cat.setCatType((Cat.Type) values[i]); cat.setTamed(true); cat.setBaby(); cat.setVelocity(user.getBase().getEyeLocation().getDirection().multiply(2)); diff --git a/Essentials/src/main/java/com/earth2me/essentials/utils/RegistryUtil.java b/Essentials/src/main/java/com/earth2me/essentials/utils/RegistryUtil.java index dcf35dd0a..a1efbdf12 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/utils/RegistryUtil.java +++ b/Essentials/src/main/java/com/earth2me/essentials/utils/RegistryUtil.java @@ -3,12 +3,50 @@ package com.earth2me.essentials.utils; import com.google.common.collect.HashBasedTable; import com.google.common.collect.Table; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.List; + public final class RegistryUtil { private static final Table, String, Object> registryCache = HashBasedTable.create(); private RegistryUtil() { } + public static Object[] values(Class registry) { + if (registry.getEnumConstants() != null) { + return registry.getEnumConstants(); + } + + //noinspection unchecked + final T[] values = (T[]) registryCache.get(registry, "$values"); + if (values != null) { + return values; + } + + final List set = new ArrayList<>(); + + for (final Field field : registry.getDeclaredFields()) { + try { + final Object value = field.get(null); + if (value != null && registry.isAssignableFrom(value.getClass())) { + //noinspection unchecked + set.add((T) value); + } + } catch (NullPointerException | IllegalAccessException ignored) { + } + } + + //noinspection unchecked + final T[] array = (T[]) new Object[set.size()]; + for (int i = 0; i < set.size(); i++) { + array[i] = set.get(i); + } + registryCache.put(registry, "$values", array); + + return array; + } + public static T valueOf(Class registry, String... names) { for (final String name : names) { //noinspection unchecked