Updates BlockUtils to use MethodMapping and ClassMapping

This commit is contained in:
Christian Koop 2021-12-13 18:00:36 +01:00
parent 71fb43e6ff
commit 9b5eda60b3
No known key found for this signature in database
GPG Key ID: 89A8181384E010A3
3 changed files with 48 additions and 21 deletions

View File

@ -44,7 +44,9 @@ public enum ClassMapping {
CRAFT_ITEM_STACK("inventory", "CraftItemStack"),
CRAFT_MAGIC_NUMBERS("util", "CraftMagicNumbers"),
CRAFT_PLAYER("entity", "CraftPlayer"),
CRAFT_WORLD("CraftWorld");
CRAFT_WORLD("CraftWorld"),
MOJANGSON_PARSER("nbt", "MojangsonParser");
private final String packageName;
private final String className;

View File

@ -5,7 +5,6 @@ import org.bukkit.inventory.ItemStack;
import java.lang.reflect.Method;
public enum MethodMapping {
MC_ITEM_STACK__GET_TAG("getTag", "getTag", "s"),
MC_ITEM_STACK__SET_TAG("setTag", "setTag", "c", ClassMapping.NBT_TAG_COMPOUND.getClazz()),
@ -14,17 +13,32 @@ public enum MethodMapping {
MC_NBT_TAG_COMPOUND__SET_STRING("setString", "setString", "a", String.class, String.class),
MC_NBT_TAG_COMPOUND__REMOVE("remove", "remove", "r", String.class),
CB_ITEM_STACK__AS_NMS_COPY("asNMSCopy", "asNMSCopy", ItemStack.class),
CB_ITEM_STACK__AS_CRAFT_MIRROR("asCraftMirror", "asCraftMirror", ClassMapping.ITEM_STACK.getClazz()),
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("add", "a", "add", "add", ClassMapping.NBT_BASE.getClazz()),
MC_CHUNK__GET_WORLD("getWorld", "D"),
MC_NBT_TAG_LIST__ADD("add", "a", "add", "c", ClassMapping.NBT_BASE.getClazz()),
BLOCK__GET_BLOCK_DATA("getBlockData", "n"),
CHUNK__SET_BLOCK_STATE("setType", "setBlockState", ClassMapping.BLOCK_POSITION.getClazz(), ClassMapping.I_BLOCK_DATA.getClazz(), boolean.class, boolean.class),
ITEM_STACK__SAVE("save", "b", ClassMapping.NBT_TAG_COMPOUND.getClazz()),
ITEM_STACK__GET_ITEM("getItem","c"),
ITEM_STACK__GET_MAX_STACK_SIZE("getMaxStackSize","l"),
WORLD__UPDATE_ADJACENT_COMPARATORS("updateAdjacentComparators", "c", ClassMapping.BLOCK_POSITION.getClazz(), ClassMapping.BLOCK.getClazz()),
WORLD__GET_CHUNK_AT("getChunkAt", "d", int.class, int.class),
WORLD_BOARDER__SET_CENTER("setCenter", "setCenter", "setCenter", "c", double.class, double.class),
WORLD_BOARDER__SET_SIZE("setSize", "setSize", "setSize", "a", double.class),
WORLD_BOARDER__SET_WARNING_TIME("setWarningTime", "setWarningTime", "setWarningTime", "b", int.class),
WORLD_BOARDER__SET_WARNING_DISTANCE("setWarningDistance", "setWarningDistance", "setWarningDistance", "c", int.class),
WORLD_BOARDER__TRANSITION_SIZE_BETWEEN("transitionSizeBetween", "transitionSizeBetween", "transitionSizeBetween", "a", double.class, double.class, long.class);
WORLD_BOARDER__TRANSITION_SIZE_BETWEEN("transitionSizeBetween", "transitionSizeBetween", "transitionSizeBetween", "a", double.class, double.class, long.class),
MOJANGSON_PARSER__PARSE("parse", "a", String.class);
private final String saneFallback;
private final String _1_14;
@ -59,17 +73,30 @@ public enum MethodMapping {
this.paramaters = paramaters;
}
MethodMapping(String saneFallback, Class<?>... paramaters) {
this.saneFallback = saneFallback;
this._1_14 = null;
this._1_17 = null;
this._1_18 = null;
this.paramaters = paramaters;
}
public Method getMethod(Class<?> clazz) {
try {
String methodName = _1_18;
switch (ServerVersion.getServerVersion()) {
case V1_14:
if (_1_14 != null)
if (_1_14 != null) {
methodName = _1_14;
}
break;
case V1_17:
if (_1_17 != null)
if (_1_17 != null) {
methodName = _1_17;
}
break;
}
@ -78,8 +105,8 @@ public enum MethodMapping {
method.setAccessible(true);
return method;
} catch (NoSuchMethodException ex) {
if (saneFallback != null) {
} catch (NullPointerException | NoSuchMethodException ex) {
if (saneFallback != null && !saneFallback.equals(methodName)) {
try {
Method method = clazz.getDeclaredMethod(saneFallback, paramaters);
method.setAccessible(true);
@ -89,6 +116,8 @@ public enum MethodMapping {
ex.printStackTrace();
innerEx.printStackTrace();
}
} else {
ex.printStackTrace();
}
}
} catch (Exception ex) {

View File

@ -2,6 +2,7 @@ package com.songoda.core.utils;
import com.songoda.core.compatibility.ClassMapping;
import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.core.compatibility.MethodMapping;
import com.songoda.core.compatibility.ServerVersion;
import org.bukkit.Effect;
import org.bukkit.Location;
@ -347,20 +348,15 @@ public class BlockUtils {
try {
if (chunkToNmsChunk == null) {
chunkToNmsChunk = loc.getChunk().getClass().getMethod("getHandle");
nmsChunkGetWorld = chunkToNmsChunk.getReturnType().getMethod("getWorld");
nmsChunkGetWorld = MethodMapping.MC_CHUNK__GET_WORLD.getMethod(chunkToNmsChunk.getReturnType());
Class<?> blockPositionClass;
try {
craftBlockGetPosition = craftBlock.getClass().getMethod("getPosition");
blockPositionClass = craftBlockGetPosition.getReturnType();
} catch (NoSuchMethodException ignore) {
blockPositionClass = ClassMapping.BLOCK_POSITION.getClazz();
blockPositionConstructor = blockPositionClass.getConstructor(double.class, double.class, double.class);
blockPositionConstructor = ClassMapping.BLOCK_POSITION.getClazz().getConstructor(double.class, double.class, double.class);
}
nmsWorldUpdateAdjacentComparators = nmsChunkGetWorld.getReturnType().getMethod("updateAdjacentComparators", blockPositionClass, ClassMapping.BLOCK.getClazz());
nmsWorldUpdateAdjacentComparators = MethodMapping.WORLD__UPDATE_ADJACENT_COMPARATORS.getMethod(ClassMapping.WORLD.getClazz());
try {
craftBlockBlockDataGetter = craftBlock.getClass().getMethod("getNMS");
@ -419,11 +415,11 @@ public class BlockUtils {
Class<?> clazzChunk = ClassMapping.CHUNK.getClazz();
getHandle = clazzCraftWorld.getMethod("getHandle");
getChunkAt = clazzWorld.getMethod("getChunkAt", int.class, int.class);
getChunkAt = MethodMapping.WORLD__GET_CHUNK_AT.getMethod(clazzWorld);
if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13)) {
getBlockData = clazzBlock.getMethod("getBlockData");
setType = clazzChunk.getMethod("setType", clazzBlockPosition, clazzIBlockData, boolean.class);
getBlockData = MethodMapping.BLOCK__GET_BLOCK_DATA.getMethod(ClassMapping.BLOCK.getClazz());
setType = MethodMapping.CHUNK__SET_BLOCK_STATE.getMethod(ClassMapping.CHUNK.getClazz());
} else {
getByCombinedId = clazzBlock.getMethod("getByCombinedId", int.class);
setType = clazzChunk.getMethod("a", clazzBlockPosition, clazzIBlockData);