Fix kittycannon on old versions

This commit is contained in:
JRoy 2024-09-14 15:08:37 -04:00 committed by Josh Roy
parent 9dfa8cf3cb
commit 3e29e04722
2 changed files with 49 additions and 4 deletions

View File

@ -2,6 +2,7 @@ package com.earth2me.essentials.commands;
import com.earth2me.essentials.Mob; import com.earth2me.essentials.Mob;
import com.earth2me.essentials.User; import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.RegistryUtil;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.entity.Cat; 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 { 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 Ocelot ocelot = (Ocelot) Mob.OCELOT.spawn(user.getWorld(), server, user.getBase().getEyeLocation());
final int i = random.nextInt(Ocelot.Type.values().length); //noinspection deprecation
ocelot.setCatType(Ocelot.Type.values()[i]); 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); ((Tameable) ocelot).setTamed(true);
ocelot.setBaby(); ocelot.setBaby();
ocelot.setVelocity(user.getBase().getEyeLocation().getDirection().multiply(2)); 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 { 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 Cat cat = (Cat) Mob.CAT.spawn(user.getWorld(), server, user.getBase().getEyeLocation());
final int i = random.nextInt(Cat.Type.values().length); final Object[] values = RegistryUtil.values(Cat.Type.class);
cat.setCatType(Cat.Type.values()[i]);
final int i = random.nextInt(values.length);
cat.setCatType((Cat.Type) values[i]);
cat.setTamed(true); cat.setTamed(true);
cat.setBaby(); cat.setBaby();
cat.setVelocity(user.getBase().getEyeLocation().getDirection().multiply(2)); cat.setVelocity(user.getBase().getEyeLocation().getDirection().multiply(2));

View File

@ -3,12 +3,50 @@ package com.earth2me.essentials.utils;
import com.google.common.collect.HashBasedTable; import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table; import com.google.common.collect.Table;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
public final class RegistryUtil { public final class RegistryUtil {
private static final Table<Class<?>, String, Object> registryCache = HashBasedTable.create(); private static final Table<Class<?>, String, Object> registryCache = HashBasedTable.create();
private RegistryUtil() { private RegistryUtil() {
} }
public static <T> Object[] values(Class<T> 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<T> 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> T valueOf(Class<T> registry, String... names) { public static <T> T valueOf(Class<T> registry, String... names) {
for (final String name : names) { for (final String name : names) {
//noinspection unchecked //noinspection unchecked