Remove broken & unnecessary reflections; #1035

This commit is contained in:
Daniel Saukel 2021-07-05 19:06:56 +02:00
parent a139f6647d
commit c09c46a3ca
3 changed files with 26 additions and 115 deletions

View File

@ -22,8 +22,11 @@ import de.erethon.dungeonsxl.api.DungeonsAPI;
import de.erethon.dungeonsxl.api.sign.Rocker;
import de.erethon.dungeonsxl.api.world.InstanceWorld;
import de.erethon.dungeonsxl.player.DPermission;
import de.erethon.dungeonsxl.util.MagicValueUtil;
import de.erethon.dungeonsxl.util.commons.compatibility.CompatibilityHandler;
import de.erethon.dungeonsxl.util.commons.misc.NumberUtil;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
/**
@ -104,7 +107,7 @@ public class BlockSign extends Rocker {
getSign().getBlock().setType(offBlock.getMaterial());
try {
MagicValueUtil.setBlockData(getSign().getBlock(), offBlockData);
setBlockData(getSign().getBlock(), offBlockData);
} catch (IllegalArgumentException exception) {
markAsErroneous("offBlock data value " + offBlockData + " cannot be applied to given type " + offBlock.getId());
}
@ -114,7 +117,7 @@ public class BlockSign extends Rocker {
public void activate() {
getSign().getBlock().setType(onBlock.getMaterial());
try {
MagicValueUtil.setBlockData(getSign().getBlock(), onBlockData);
setBlockData(getSign().getBlock(), onBlockData);
} catch (IllegalArgumentException exception) {
markAsErroneous("onBlock data value " + onBlockData + " cannot be applied to given type " + onBlock.getId());
return;
@ -126,7 +129,7 @@ public class BlockSign extends Rocker {
public void deactivate() {
getSign().getBlock().setType(offBlock.getMaterial());
try {
MagicValueUtil.setBlockData(getSign().getBlock(), offBlockData);
setBlockData(getSign().getBlock(), offBlockData);
} catch (IllegalArgumentException exception) {
markAsErroneous("onBlock data value " + offBlockData + " cannot be applied to given type " + onBlock.getId());
return;
@ -134,4 +137,23 @@ public class BlockSign extends Rocker {
active = false;
}
private static Method craftBlockSetData;
private static void setBlockData(Block block, byte data) {
if (craftBlockSetData == null) {
try {
craftBlockSetData = Class.forName(
"org.bukkit.craftbukkit." + CompatibilityHandler.getInstance().getInternals() + ".block.CraftBlock")
.getDeclaredMethod("setData", byte.class);
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalArgumentException exception) {
exception.printStackTrace();
}
}
try {
craftBlockSetData.invoke(block, data);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException exception) {
exception.printStackTrace();
}
}
}

View File

@ -1,31 +0,0 @@
/*
* Copyright (C) 2012-2021 Frank Baumann
*
* This program 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.
*
* This program 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.erethon.dungeonsxl.util;
import org.bukkit.block.Block;
/**
* @author Daniel Saukel
*/
@Deprecated
public class MagicValueUtil {
public static void setBlockData(Block block, byte data) {
ReflectionUtil.invoke(ReflectionUtil.CRAFT_BLOCK_SET_DATA, block, data);
}
}

View File

@ -1,80 +0,0 @@
/*
* Copyright (C) 2012-2021 Frank Baumann
*
* This program 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.
*
* This program 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.erethon.dungeonsxl.util;
import de.erethon.dungeonsxl.util.commons.compatibility.CompatibilityHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.bukkit.inventory.ItemStack;
/**
* @author Daniel Saukel
*/
public class ReflectionUtil {
static String INTERNALS_VERSION = CompatibilityHandler.getInstance().getInternals().toString();
static String ORG_BUKKIT_CRAFTBUKKIT = "org.bukkit.craftbukkit." + INTERNALS_VERSION;
static String NET_MINECRAFT_SERVER = "net.minecraft.server." + INTERNALS_VERSION;
static Class NBT_TAG_COMPOUND;
static Method NBT_TAG_COMPOUND_HAS_KEY;
static Method NBT_TAG_COMPOUND_REMOVE;
static Method NBT_TAG_COMPOUND_SET_BOOLEAN;
static Class ITEM_STACK;
static Method ITEM_STACK_GET_TAG;
static Method ITEM_STACK_SET_TAG;
static Class CRAFT_ITEM_STACK;
static Method CRAFT_ITEM_STACK_AS_BUKKIT_COPY;
static Method CRAFT_ITEM_STACK_AS_NMS_COPY;
static Method CRAFT_BLOCK_SET_DATA;
static {
try {
NBT_TAG_COMPOUND = Class.forName(NET_MINECRAFT_SERVER + ".NBTTagCompound");
NBT_TAG_COMPOUND_HAS_KEY = NBT_TAG_COMPOUND.getDeclaredMethod("hasKey", String.class);
NBT_TAG_COMPOUND_REMOVE = NBT_TAG_COMPOUND.getDeclaredMethod("remove", String.class);
NBT_TAG_COMPOUND_SET_BOOLEAN = NBT_TAG_COMPOUND.getDeclaredMethod("setBoolean", String.class, boolean.class);
ITEM_STACK = Class.forName(NET_MINECRAFT_SERVER + ".ItemStack");
ITEM_STACK_GET_TAG = ITEM_STACK.getDeclaredMethod("getTag");
ITEM_STACK_SET_TAG = ITEM_STACK.getDeclaredMethod("setTag", NBT_TAG_COMPOUND);
CRAFT_ITEM_STACK = Class.forName(ORG_BUKKIT_CRAFTBUKKIT + ".inventory.CraftItemStack");
CRAFT_ITEM_STACK_AS_BUKKIT_COPY = CRAFT_ITEM_STACK.getDeclaredMethod("asBukkitCopy", ITEM_STACK);
CRAFT_ITEM_STACK_AS_NMS_COPY = CRAFT_ITEM_STACK.getDeclaredMethod("asNMSCopy", ItemStack.class);
CRAFT_BLOCK_SET_DATA = Class.forName(ReflectionUtil.ORG_BUKKIT_CRAFTBUKKIT + ".block.CraftBlock").getDeclaredMethod("setData", byte.class);
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalArgumentException exception) {
exception.printStackTrace();
}
}
static Object newInstance(Class clazz) {
try {
return clazz.newInstance();
} catch (IllegalAccessException | InstantiationException exception) {
exception.printStackTrace();
return null;
}
}
static Object invoke(Method method, Object instance, Object... args) {
try {
return method.invoke(instance, args);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException exception) {
exception.printStackTrace();
return null;
}
}
}