mirror of
https://github.com/EssentialsX/Essentials.git
synced 2024-12-22 09:08:01 +01:00
Fix kittycannon on old versions
This commit is contained in:
parent
9dfa8cf3cb
commit
3e29e04722
@ -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));
|
||||
|
@ -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<Class<?>, String, Object> registryCache = HashBasedTable.create();
|
||||
|
||||
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) {
|
||||
for (final String name : names) {
|
||||
//noinspection unchecked
|
||||
|
Loading…
Reference in New Issue
Block a user