mirror of
https://github.com/EssentialsX/Essentials.git
synced 2025-01-03 15:08:18 +01:00
Add reflection based spawn egg provider
This commit is contained in:
parent
f4fa24cd7f
commit
827891f10a
@ -157,6 +157,18 @@
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.ess3</groupId>
|
||||
<artifactId>ReflectionProvider</artifactId>
|
||||
<version>2.0.1</version>
|
||||
<scope>compile</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.bukkit</groupId>
|
||||
<artifactId>craftbukkit</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<repositories>
|
||||
<repository>
|
||||
|
@ -38,6 +38,7 @@ import net.ess3.nms.PotionMetaProvider;
|
||||
import net.ess3.nms.SpawnEggProvider;
|
||||
import net.ess3.nms.SpawnerProvider;
|
||||
import net.ess3.nms.legacy.LegacyPotionMetaProvider;
|
||||
import net.ess3.nms.refl.ReflSpawnEggProvider;
|
||||
import net.ess3.nms.updatedmeta.BasePotionDataProvider;
|
||||
import net.ess3.nms.updatedmeta.BlockMetaSpawnerProvider;
|
||||
import net.ess3.nms.legacy.LegacySpawnEggProvider;
|
||||
@ -216,6 +217,7 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials {
|
||||
), "mob spawner").getProvider();
|
||||
spawnEggProvider = new ProviderFactory<>(getLogger(),
|
||||
Arrays.asList(
|
||||
ReflSpawnEggProvider.class,
|
||||
v1_10_R1SpawnEggProvider.class,
|
||||
v1_9_R2SpawnEggProvider.class,
|
||||
v1_9_R1SpawnEggProvider.class,
|
||||
|
29
nms/ReflectionProvider/pom.xml
Normal file
29
nms/ReflectionProvider/pom.xml
Normal file
@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>EssentialsXParent</artifactId>
|
||||
<groupId>net.ess3</groupId>
|
||||
<version>2.0.1</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>ReflectionProvider</artifactId>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spigot-repo</id>
|
||||
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>net.ess3</groupId>
|
||||
<artifactId>NMSProvider</artifactId>
|
||||
<version>2.0.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -0,0 +1,30 @@
|
||||
package net.ess3.nms.refl;
|
||||
|
||||
import net.ess3.nms.SpawnEggProvider;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
public class ReflSpawnEggProvider extends SpawnEggProvider {
|
||||
@Override
|
||||
public ItemStack createEggItem(EntityType type) throws IllegalArgumentException {
|
||||
try {
|
||||
return new SpawnEggRefl(type).toItemStack();
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException(e.getMessage(), e.getCause());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public EntityType getSpawnedType(ItemStack eggItem) throws IllegalArgumentException {
|
||||
try {
|
||||
return SpawnEggRefl.fromItemStack(eggItem).getSpawnedType();
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException(e.getMessage(), e.getCause());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHumanName() {
|
||||
return "Reflection based provider";
|
||||
}
|
||||
}
|
203
nms/ReflectionProvider/src/net/ess3/nms/refl/ReflUtil.java
Normal file
203
nms/ReflectionProvider/src/net/ess3/nms/refl/ReflUtil.java
Normal file
@ -0,0 +1,203 @@
|
||||
package net.ess3.nms.refl;
|
||||
|
||||
import com.google.common.collect.HashBasedTable;
|
||||
import com.google.common.collect.Table;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class ReflUtil {
|
||||
private static String nmsVersion;
|
||||
|
||||
public static String getNMSVersion() {
|
||||
if (nmsVersion == null) {
|
||||
String name = Bukkit.getServer().getClass().getName();
|
||||
String[] parts = name.split("\\.");
|
||||
nmsVersion = parts[3];
|
||||
}
|
||||
return nmsVersion;
|
||||
}
|
||||
|
||||
public static Class<?> getNMSClass(String className) {
|
||||
return getClassCached("net.minecraft.server." + getNMSVersion() + "." + className);
|
||||
}
|
||||
|
||||
public static Class<?> getOBCClass(String className) {
|
||||
return getClassCached("org.bukkit.craftbukkit." + getNMSVersion() + "." + className);
|
||||
}
|
||||
|
||||
private static Map<String, Class<?>> classCache = new HashMap<>();
|
||||
|
||||
public static Class<?> getClassCached(String className) {
|
||||
if (classCache.containsKey(className)) {
|
||||
return classCache.get(className);
|
||||
}
|
||||
try {
|
||||
Class<?> classForName = Class.forName(className);
|
||||
classCache.put(className, classForName);
|
||||
return classForName;
|
||||
} catch (ClassNotFoundException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static Table<Class<?>, String, Method> methodCache = HashBasedTable.create();
|
||||
|
||||
public static Method getMethodCached(Class<?> clazz, String methodName) {
|
||||
if (methodCache.contains(clazz, methodName)) {
|
||||
return methodCache.get(clazz, methodName);
|
||||
}
|
||||
try {
|
||||
Method method = clazz.getDeclaredMethod(methodName);
|
||||
method.setAccessible(true);
|
||||
methodCache.put(clazz, methodName, method);
|
||||
return method;
|
||||
} catch (NoSuchMethodException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static Table<Class<?>, MethodParams, Method> methodParamCache = HashBasedTable.create();
|
||||
|
||||
public static Method getMethodCached(Class<?> clazz, String methodName, Class<?>... params) {
|
||||
MethodParams methodParams = new MethodParams(methodName, params);
|
||||
if (methodParamCache.contains(clazz, methodParams)) {
|
||||
return methodParamCache.get(clazz, methodParams);
|
||||
}
|
||||
try {
|
||||
Method method = clazz.getDeclaredMethod(methodName, params);
|
||||
method.setAccessible(true);
|
||||
methodParamCache.put(clazz, methodParams, method);
|
||||
return method;
|
||||
} catch (NoSuchMethodException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static Table<Class<?>, String, Field> fieldCache = HashBasedTable.create();
|
||||
|
||||
public static Field getFieldCached(Class<?> clazz, String fieldName) {
|
||||
if (fieldCache.contains(clazz, fieldName)) {
|
||||
return fieldCache.get(clazz, fieldName);
|
||||
}
|
||||
try {
|
||||
Field field = clazz.getDeclaredField(fieldName);
|
||||
field.setAccessible(true);
|
||||
fieldCache.put(clazz, fieldName, field);
|
||||
return field;
|
||||
} catch (NoSuchFieldException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static Map<Class<?>, Constructor<?>> constructorCache = new HashMap<>();
|
||||
|
||||
public static Constructor<?> getConstructorCached(Class<?> clazz) {
|
||||
if (constructorCache.containsKey(clazz)) {
|
||||
return constructorCache.get(clazz);
|
||||
}
|
||||
try {
|
||||
Constructor<?> constructor = clazz.getDeclaredConstructor();
|
||||
constructor.setAccessible(true);
|
||||
constructorCache.put(clazz, constructor);
|
||||
return constructor;
|
||||
} catch (NoSuchMethodException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static Table<Class<?>, ConstructorParams, Constructor<?>> constructorParamCache = HashBasedTable.create();
|
||||
|
||||
public static Constructor<?> getConstructorCached(Class<?> clazz, Class<?>... params) {
|
||||
ConstructorParams constructorParams = new ConstructorParams(params);
|
||||
if (constructorParamCache.contains(clazz, constructorParams)) {
|
||||
return constructorParamCache.get(clazz, constructorParams);
|
||||
}
|
||||
try {
|
||||
Constructor<?> constructor = clazz.getDeclaredConstructor(params);
|
||||
constructor.setAccessible(true);
|
||||
constructorParamCache.put(clazz, constructorParams, constructor);
|
||||
return constructor;
|
||||
} catch (NoSuchMethodException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Adapted from @minecrafter
|
||||
private static class MethodParams {
|
||||
private final String name;
|
||||
private final Class<?>[] params;
|
||||
|
||||
public MethodParams(final String name, final Class<?>[] params) {
|
||||
this.name = name;
|
||||
this.params = params;
|
||||
}
|
||||
|
||||
// Ugly autogenned Lombok code
|
||||
@Override
|
||||
public boolean equals(final Object o) {
|
||||
if (o == this) {
|
||||
return true;
|
||||
}
|
||||
if (!(o instanceof MethodParams)) {
|
||||
return false;
|
||||
}
|
||||
final MethodParams that = (MethodParams) o;
|
||||
if (!that.canEqual(this)) {
|
||||
return false;
|
||||
}
|
||||
final Object thisName = this.name;
|
||||
final Object thatName = that.name;
|
||||
if (thisName == null) {
|
||||
if (thatName == null) {
|
||||
return Arrays.deepEquals(this.params, that.params);
|
||||
}
|
||||
} else if (thisName.equals(thatName)) {
|
||||
return Arrays.deepEquals(this.params, that.params);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean canEqual(final Object that) {
|
||||
return that instanceof MethodParams;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = 1;
|
||||
final Object thisName = this.name;
|
||||
result = result * 31 + ((thisName == null) ? 0 : thisName.hashCode());
|
||||
result = result * 31 + Arrays.deepHashCode(this.params);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
// Necessary for deepequals
|
||||
private static class ConstructorParams {
|
||||
private final Class<?>[] params;
|
||||
|
||||
public ConstructorParams(Class<?>[] params) {
|
||||
this.params = params;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
ConstructorParams that = (ConstructorParams) o;
|
||||
|
||||
return Arrays.deepEquals(params, that.params);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Arrays.deepHashCode(params);
|
||||
}
|
||||
}
|
||||
}
|
143
nms/ReflectionProvider/src/net/ess3/nms/refl/SpawnEggRefl.java
Normal file
143
nms/ReflectionProvider/src/net/ess3/nms/refl/SpawnEggRefl.java
Normal file
@ -0,0 +1,143 @@
|
||||
/*******************************************************************************
|
||||
* This file is part of ASkyBlock.
|
||||
* <p>
|
||||
* ASkyBlock is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
* <p>
|
||||
* ASkyBlock is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
* <p>
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with ASkyBlock. If not, see <http://www.gnu.org/licenses/>.
|
||||
*******************************************************************************/
|
||||
package net.ess3.nms.refl;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* Represents a spawn egg that can be used to spawn mobs.
|
||||
*
|
||||
* @author tastybento
|
||||
*/
|
||||
public class SpawnEggRefl {
|
||||
private EntityType type;
|
||||
|
||||
public SpawnEggRefl(EntityType type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the type of entity this egg will spawn.
|
||||
*
|
||||
* @return The entity type.
|
||||
*/
|
||||
public EntityType getSpawnedType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the type of entity this egg will spawn.
|
||||
*
|
||||
* @param type The entity type.
|
||||
*/
|
||||
public void setSpawnedType(EntityType type) {
|
||||
if (type.isAlive()) {
|
||||
this.type = type;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SPAWN EGG{" + getSpawnedType() + "}";
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an ItemStack of one spawn egg
|
||||
*
|
||||
* @return ItemStack
|
||||
*/
|
||||
public ItemStack toItemStack() throws Exception {
|
||||
return toItemStack(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an itemstack of spawn eggs
|
||||
*
|
||||
* @param amount
|
||||
* @return ItemStack of spawn eggs
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public ItemStack toItemStack(int amount) throws Exception {
|
||||
ItemStack item = new ItemStack(Material.MONSTER_EGG, amount);
|
||||
|
||||
Class<?> craftItemStackClass = ReflUtil.getOBCClass("inventory.CraftItemStack");
|
||||
Method asNMSCopyMethod = ReflUtil.getMethodCached(craftItemStackClass, "asNMSCopy", ItemStack.class);
|
||||
|
||||
Class<?> NMSItemStackClass = ReflUtil.getNMSClass("ItemStack");
|
||||
Object stack = asNMSCopyMethod.invoke(null, item);
|
||||
Object tagCompound = ReflUtil.getMethodCached(NMSItemStackClass, "getTag").invoke(stack);
|
||||
|
||||
Class<?> NBTTagCompoundClass = ReflUtil.getNMSClass("NBTTagCompound");
|
||||
Constructor<?> NBTTagCompoundConstructor = ReflUtil.getConstructorCached(NBTTagCompoundClass);
|
||||
if (tagCompound == null) {
|
||||
tagCompound = NBTTagCompoundConstructor.newInstance();
|
||||
}
|
||||
Object id = NBTTagCompoundConstructor.newInstance();
|
||||
Method tagSetString = NBTTagCompoundClass.getDeclaredMethod("setString", String.class, String.class);
|
||||
tagSetString.invoke(id, "id", type.getName());
|
||||
|
||||
Method tagSetTag = NBTTagCompoundClass.getDeclaredMethod("set", String.class, NBTTagCompoundClass.getSuperclass());
|
||||
tagSetTag.invoke(tagCompound, "EntityTag", id);
|
||||
|
||||
Method stackSetTag = ReflUtil.getMethodCached(NMSItemStackClass, "setTag", NBTTagCompoundClass);
|
||||
stackSetTag.invoke(stack, tagCompound);
|
||||
|
||||
Method asBukkitCopyMethod = ReflUtil.getMethodCached(craftItemStackClass, "asBukkitCopy", NMSItemStackClass);
|
||||
return (ItemStack) asBukkitCopyMethod.invoke(null, stack);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts from an item stack to a spawn egg
|
||||
*
|
||||
* @param item - ItemStack, quantity is disregarded
|
||||
* @return SpawnEgg
|
||||
*/
|
||||
public static SpawnEggRefl fromItemStack(ItemStack item) throws Exception {
|
||||
if (item == null)
|
||||
throw new IllegalArgumentException("Item cannot be null");
|
||||
if (item.getType() != Material.MONSTER_EGG)
|
||||
throw new IllegalArgumentException("Item is not a monster egg");
|
||||
|
||||
Class<?> NMSItemStackClass = ReflUtil.getNMSClass("ItemStack");
|
||||
Class<?> craftItemStackClass = ReflUtil.getOBCClass("inventory.CraftItemStack");
|
||||
Method asNMSCopyMethod = ReflUtil.getMethodCached(craftItemStackClass, "asNMSCopy", ItemStack.class);
|
||||
|
||||
Object stack = asNMSCopyMethod.invoke(null, item);
|
||||
Object tagCompound = ReflUtil.getMethodCached(NMSItemStackClass, "getTag").invoke(stack);
|
||||
if (tagCompound != null) {
|
||||
Method tagGetCompound = ReflUtil.getMethodCached(tagCompound.getClass(), "getCompound", String.class);
|
||||
Object entityTag = tagGetCompound.invoke(tagCompound, "EntityTag");
|
||||
|
||||
Method tagGetString = ReflUtil.getMethodCached(entityTag.getClass(), "getString", String.class);
|
||||
@SuppressWarnings("deprecation")
|
||||
EntityType type = EntityType.fromName((String) tagGetString.invoke(entityTag, "id"));
|
||||
if (type != null) {
|
||||
return new SpawnEggRefl(type);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Unable to parse type from item");
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException("Item is lacking tag compound");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user