Method mapping.

This commit is contained in:
Brianna 2021-12-06 11:12:59 -06:00
parent 776ead9870
commit d7010d7483
3 changed files with 94 additions and 13 deletions

View File

@ -0,0 +1,71 @@
package com.songoda.core.compatibility;
import org.bukkit.inventory.ItemStack;
import java.lang.reflect.Method;
public enum MethodMapping {
MC_ITEM_STACK__GET_TAG("getTag", "s"),
MC_ITEM_STACK__SET_TAG("setTag", "c", ClassMapping.NBT_TAG_COMPOUND.getClazz()),
MC_NBT_TAG_COMPOUND__SET("set", "a", String.class, ClassMapping.NBT_BASE.getClazz()),
MC_NBT_TAG_COMPOUND__SET_SHORT("setShort", "a", String.class),
MC_NBT_TAG_COMPOUND__SET_STRING("setString", "a", String.class, String.class),
MC_NBT_TAG_COMPOUND__REMOVE("remove", "r", String.class, short.class),
CB_ITEM_STACK__AS_NMS_COPY("asNMSCopy", ItemStack.class),
CB_ITEM_STACK__AS_CRAFT_MIRROR("asCraftMirror", ClassMapping.ITEM_STACK.getClazz()),
MC_NBT_TAG_LIST__ADD("a", "add", "add", ClassMapping.NBT_BASE.getClazz());
private final String _1_14;
private final String _1_17;
private final String _1_18;
private final Class<?>[] paramaters;
MethodMapping(String _1_14, String _1_17, String _1_18, Class<?>... paramaters) {
this._1_14 = _1_14;
this._1_17 = _1_17;
this._1_18 = _1_18;
this.paramaters = paramaters;
}
MethodMapping(String _1_17, String _1_18, Class<?>... paramaters) {
this._1_14 = null;
this._1_17 = _1_17;
this._1_18 = _1_18;
this.paramaters = paramaters;
}
MethodMapping(String _1_18, Class<?>... paramaters) {
this._1_14 = null;
this._1_17 = null;
this._1_18 = _1_18;
this.paramaters = paramaters;
}
public Method getMethod(Class<?> clazz) {
try {
String methodName = _1_18;
switch (ServerVersion.getServerVersion()) {
case V1_14:
if (_1_14 != null)
methodName = _1_14;
break;
case V1_17:
if (_1_17 != null)
methodName = _1_17;
break;
}
Method method = clazz.getDeclaredMethod(methodName, paramaters);
method.setAccessible(true);
return method;
} catch (Exception e) {
return null;
}
}
}

View File

@ -5,6 +5,7 @@ import com.mojang.authlib.properties.Property;
import com.songoda.core.compatibility.ClassMapping;
import com.songoda.core.compatibility.CompatibleHand;
import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.core.compatibility.MethodMapping;
import com.songoda.core.compatibility.ServerVersion;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
@ -197,7 +198,6 @@ public class ItemUtils {
static Class mc_ItemStack = ClassMapping.ITEM_STACK.getClazz();
static Class mc_NBTTagCompound = ClassMapping.NBT_TAG_COMPOUND.getClazz();
static Class mc_NBTTagList = ClassMapping.NBT_TAG_LIST.getClazz();
static Class mc_NBTBase = ClassMapping.NBT_BASE.getClazz();
static Method mc_ItemStack_getTag;
static Method mc_ItemStack_setTag;
static Method mc_NBTTagCompound_set;
@ -211,17 +211,15 @@ public class ItemUtils {
static {
if (cb_ItemStack != null) {
try {
mc_ItemStack_getTag = mc_ItemStack.getDeclaredMethod("getTag");
mc_ItemStack_setTag = mc_ItemStack.getDeclaredMethod("setTag", mc_NBTTagCompound);
mc_NBTTagCompound_set = mc_NBTTagCompound.getDeclaredMethod("set", String.class, mc_NBTBase);
mc_NBTTagCompound_remove = mc_NBTTagCompound.getDeclaredMethod("remove", String.class);
mc_NBTTagCompound_setShort = mc_NBTTagCompound.getDeclaredMethod("setShort", String.class, short.class);
mc_NBTTagCompound_setString = mc_NBTTagCompound.getDeclaredMethod("setString", String.class, String.class);
cb_CraftItemStack_asNMSCopy = cb_ItemStack.getDeclaredMethod("asNMSCopy", ItemStack.class);
cb_CraftItemStack_asCraftMirror = cb_ItemStack.getDeclaredMethod("asCraftMirror", mc_ItemStack);
mc_NBTTagList_add = ServerVersion.isServerVersionAtLeast(ServerVersion.V1_14)
? NMSUtils.getPrivateMethod(mc_NBTTagList, "a", mc_NBTBase)
: mc_NBTTagList.getDeclaredMethod("add", mc_NBTBase);
mc_ItemStack_getTag = MethodMapping.MC_ITEM_STACK__GET_TAG.getMethod(mc_ItemStack);
mc_ItemStack_setTag = MethodMapping.MC_ITEM_STACK__SET_TAG.getMethod(mc_ItemStack);
mc_NBTTagCompound_set = MethodMapping.MC_NBT_TAG_COMPOUND__SET.getMethod(mc_NBTTagCompound);
mc_NBTTagCompound_remove = MethodMapping.MC_NBT_TAG_COMPOUND__REMOVE.getMethod(mc_NBTTagCompound);
mc_NBTTagCompound_setShort = MethodMapping.MC_NBT_TAG_COMPOUND__SET_SHORT.getMethod(mc_NBTTagCompound);
mc_NBTTagCompound_setString = MethodMapping.MC_NBT_TAG_COMPOUND__SET_STRING.getMethod(mc_NBTTagCompound);
cb_CraftItemStack_asNMSCopy = MethodMapping.CB_ITEM_STACK__AS_NMS_COPY.getMethod(mc_ItemStack);
cb_CraftItemStack_asCraftMirror = MethodMapping.CB_ITEM_STACK__AS_CRAFT_MIRROR.getMethod(mc_ItemStack);
mc_NBTTagList_add = MethodMapping.MC_NBT_TAG_LIST__ADD.getMethod(mc_NBTTagList);
} catch (Exception ex) {
Logger.getLogger(ItemUtils.class.getName()).log(Level.SEVERE, null, ex);
}
@ -229,7 +227,7 @@ public class ItemUtils {
}
/**
* Make an item glow as if it contained an enchantment. <br>
* Make an item glow as if it contained an enchantment. <br>
* Tested working 1.8-1.14
*
* @param item itemstack to create a glowing copy of

View File

@ -3,6 +3,18 @@
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">
<modelVersion>4.0.0</modelVersion>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>
<parent>
<groupId>com.songoda</groupId>