From ad1abd84927c62dfb26822e17e8a266bcdefc405 Mon Sep 17 00:00:00 2001 From: SBDeveloper Date: Fri, 2 Sep 2022 20:39:09 +0200 Subject: [PATCH] Dropped 1.12.x support --- .../themeparkplus/ThemeParkPlus.java | 4 +- .../themeparkplus/commands/TPPCMD.java | 11 +- .../themeparkplus/util/LGUtil.java | 259 +++--------------- 3 files changed, 47 insertions(+), 227 deletions(-) diff --git a/src/main/java/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java b/src/main/java/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java index 989e83b..3eccb15 100644 --- a/src/main/java/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java +++ b/src/main/java/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java @@ -87,10 +87,10 @@ public final class ThemeParkPlus extends JavaPlugin { if (Bukkit.getPluginManager().getPlugin("ThemePark") == null && Bukkit.getPluginManager().getPlugin("ParkPlusMC") == null) { getLogger().severe("ThemePark or ParkPlusMC not found! Some functions will not work without it!"); } else if (Bukkit.getPluginManager().getPlugin("ThemePark") != null) { - getLogger().severe("Enabling ThemePark hook..."); + getLogger().info("Enabling ThemePark hook..."); hasParkHook = true; } else if (Bukkit.getPluginManager().getPlugin("ParkPlusMC") != null) { - getLogger().severe("Enabling ParkPlusMC hook..."); + getLogger().info("Enabling ParkPlusMC hook..."); hasParkHook = true; } diff --git a/src/main/java/nl/sbdeveloper/themeparkplus/commands/TPPCMD.java b/src/main/java/nl/sbdeveloper/themeparkplus/commands/TPPCMD.java index 0667137..0fe3e48 100644 --- a/src/main/java/nl/sbdeveloper/themeparkplus/commands/TPPCMD.java +++ b/src/main/java/nl/sbdeveloper/themeparkplus/commands/TPPCMD.java @@ -169,19 +169,14 @@ public class TPPCMD implements TabExecutor { long onDelayTicks = Long.parseLong(args[5]) * 20L; long offDelayTicks = args.length == 7 ? Long.parseLong(args[6]) * 20L : 20L; - Block block = loc.getBlock(); - Material oldMaterial = block.getType(); - byte oldData = block.getState().getRawData(); - Material redstoneMaterial = XMaterial.REDSTONE_BLOCK.parseMaterial(); - - if (redstoneMaterial == null) throw new IllegalArgumentException("Redstone Material not found."); + final Block block = loc.getBlock(); + final Material oldMaterial = block.getType(); Bukkit.getScheduler().scheduleSyncDelayedTask(ThemeParkPlus.getInstance(), () -> { - block.setType(redstoneMaterial); + block.setType(XMaterial.REDSTONE_BLOCK.parseMaterial()); Bukkit.getScheduler().scheduleSyncDelayedTask(ThemeParkPlus.getInstance(), () -> { BlockState state = block.getState(); state.setType(oldMaterial); - if (!XMaterial.isNewVersion()) state.setRawData(oldData); state.update(true); }, offDelayTicks); }, onDelayTicks); diff --git a/src/main/java/nl/sbdeveloper/themeparkplus/util/LGUtil.java b/src/main/java/nl/sbdeveloper/themeparkplus/util/LGUtil.java index c0f3d8d..cbd4d16 100644 --- a/src/main/java/nl/sbdeveloper/themeparkplus/util/LGUtil.java +++ b/src/main/java/nl/sbdeveloper/themeparkplus/util/LGUtil.java @@ -2,81 +2,30 @@ package nl.sbdeveloper.themeparkplus.util; import com.cryptomorin.xseries.XMaterial; import org.bukkit.Bukkit; -import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; -import org.bukkit.block.BlockState; - -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Objects; +import org.bukkit.block.data.BlockData; +import org.bukkit.block.data.Directional; +import org.bukkit.block.data.Openable; +import org.jetbrains.annotations.NotNull; public class LGUtil { - private static boolean is113Plus = false; + private static final BlockData lampAanData = Bukkit.createBlockData(XMaterial.REDSTONE_LAMP.parseMaterial(), "lit=true"); + private static final BlockData lampUitData = Bukkit.createBlockData(XMaterial.REDSTONE_LAMP.parseMaterial(), "lit=false"); - private static Method getBlockDataMethod; - private static Method setBlockDataMethod; - private static Method getAsStringMethod; - private static Object lampAanData; - private static Object lampUitData; - - static { - try { - Class blockDataClass = Class.forName("org.bukkit.block.data.BlockData"); - Method methodCreateBlockData = Bukkit.class.getMethod("createBlockData", String.class); - getBlockDataMethod = Block.class.getMethod("getBlockData"); - setBlockDataMethod = Block.class.getMethod("setBlockData", blockDataClass, - Boolean.TYPE); - getAsStringMethod = blockDataClass.getMethod("getAsString"); - lampAanData = methodCreateBlockData.invoke(null, "minecraft:redstone_lamp[lit=true]"); - lampUitData = methodCreateBlockData.invoke(null, "minecraft:redstone_lamp[lit=false]"); - is113Plus = true; - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored) {} - } + private LGUtil() {} public static boolean zetLampAan(Block lampBlock) { - if (is113Plus) { - if ((isLamp(lampBlock)) && (!isAan(lampBlock))) { - setBlockData(lampBlock, lampAanData); - - return true; - } - return false; + if ((isLamp(lampBlock)) && (!isAan(lampBlock))) { + lampBlock.setBlockData(lampAanData); + return true; } - - if (!isAan(lampBlock)) { - final Block neighbor = getNeighbor(lampBlock); - if (neighbor != null) { - BlockState neighborState = neighbor.getState(); - try { - neighbor.setType(Material.REDSTONE_BLOCK); - } catch (Exception e) { - e.printStackTrace(); - } finally { - neighborState.update(true, false); - } - return true; - } - } - return false; } public static boolean zetLampUit(Block lampBlock) { - if (is113Plus) { - if ((isLamp(lampBlock)) && (isAan(lampBlock))) { - setBlockData(lampBlock, lampUitData); - - return true; - } - return false; - } - - if (isAan(lampBlock)) { - lampBlock.setType(Objects.requireNonNull(Material.matchMaterial(XMaterial.REDSTONE_LAMP.getLegacy()[1]))); + if ((isLamp(lampBlock)) && (isAan(lampBlock))) { + lampBlock.setBlockData(lampUitData); return true; } return false; @@ -87,186 +36,62 @@ public class LGUtil { } public static boolean openGate(Block b, BlockFace openFace) { - if (is113Plus) { - org.bukkit.block.data.BlockData blockData = b.getBlockData(); - if (isOpenable(b)) { - org.bukkit.block.data.Openable op = (org.bukkit.block.data.Openable) blockData; - if (op.isOpen()) return false; - op.setOpen(true); - b.setBlockData(blockData); + BlockData blockData = b.getBlockData(); + if (isOpenable(b)) { + Openable op = (Openable) blockData; + if (op.isOpen()) return false; + op.setOpen(true); + b.setBlockData(blockData); - org.bukkit.block.data.Directional dir = (org.bukkit.block.data.Directional) blockData; - dir.setFacing(openFace); - b.setBlockData(blockData); - return true; - } - } else { - BlockState state = b.getState(); - if (isOpenable(b)) { - org.bukkit.material.Openable openable = (org.bukkit.material.Openable) state.getData(); - if (openable.isOpen()) return false; - openable.setOpen(true); - state.setData((org.bukkit.material.MaterialData) openable); - state.update(); - - org.bukkit.material.Directional dir = (org.bukkit.material.Directional) state.getData(); - dir.setFacingDirection(openFace); - state.setData((org.bukkit.material.MaterialData) dir); - state.update(); - return true; - } + Directional dir = (Directional) blockData; + dir.setFacing(openFace); + b.setBlockData(blockData); + return true; } return false; } public static boolean closeGate(Block b) { - if (is113Plus) { - org.bukkit.block.data.BlockData blockData = b.getBlockData(); - if (isOpenable(b)) { - org.bukkit.block.data.Openable op = (org.bukkit.block.data.Openable) blockData; - if (!op.isOpen()) { - return false; - } - op.setOpen(false); - b.setBlockData(blockData); - return true; - } - } else { - BlockState state = b.getState(); - if (isOpenable(b)) { - org.bukkit.material.Openable openable = (org.bukkit.material.Openable) state.getData(); - if (!openable.isOpen()) { - return false; - } - openable.setOpen(false); - state.setData((org.bukkit.material.MaterialData) openable); - state.update(); - return true; + BlockData blockData = b.getBlockData(); + if (isOpenable(b)) { + Openable op = (Openable) blockData; + if (!op.isOpen()) { + return false; } + op.setOpen(false); + b.setBlockData(blockData); + return true; } return false; } public static BlockFace getDirection(Block b) { - if (is113Plus) { - org.bukkit.block.data.BlockData blockData = b.getBlockData(); - if (isOpenable(b)) { - org.bukkit.block.data.Directional dir = (org.bukkit.block.data.Directional) blockData; - return dir.getFacing(); - } - } else { - BlockState state = b.getState(); - if (isOpenable(b)) { - org.bukkit.material.Directional dir = (org.bukkit.material.Directional) state.getData(); - return dir.getFacing(); - } + BlockData blockData = b.getBlockData(); + if (isOpenable(b)) { + Directional dir = (Directional) blockData; + return dir.getFacing(); } return null; } public static boolean isOpen(Block b) { - if (is113Plus) { - org.bukkit.block.data.BlockData blockData = b.getBlockData(); - if (isOpenable(b)) { - org.bukkit.block.data.Openable op = (org.bukkit.block.data.Openable) blockData; - return op.isOpen(); - } - } else { - BlockState state = b.getState(); - if (isOpenable(b)) { - org.bukkit.material.Openable openable = (org.bukkit.material.Openable) state.getData(); - return openable.isOpen(); - } + BlockData blockData = b.getBlockData(); + if (isOpenable(b)) { + Openable op = (Openable) blockData; + return op.isOpen(); } return false; } - /* Gate codes sponsored by MrWouter <3 */ - public static boolean isOpenable(Block b) { - if (b == null) { - return false; - } - if (is113Plus) { - return b.getBlockData() instanceof org.bukkit.block.data.Openable; - } else { - return b.getState().getData() instanceof org.bukkit.material.Openable; - } - } - - private static Block getNeighbor(Block lampBlock) { - List possibleNeighbors = new ArrayList<>(); - if (lampBlock.getLocation().getY() > 0.0D) { - possibleNeighbors.add(lampBlock.getLocation().add(0.0D, -1.0D, 0.0D).getBlock()); - } - if (lampBlock.getLocation().getY() < 255.0D) { - possibleNeighbors.add(lampBlock.getLocation().add(0.0D, 1.0D, 0.0D).getBlock()); - } - possibleNeighbors.add(lampBlock.getLocation().add(0.0D, 0.0D, 1.0D).getBlock()); - possibleNeighbors.add(lampBlock.getLocation().add(0.0D, 0.0D, -1.0D).getBlock()); - possibleNeighbors.add(lampBlock.getLocation().add(1.0D, 0.0D, 0.0D).getBlock()); - possibleNeighbors.add(lampBlock.getLocation().add(-1.0D, 0.0D, 0.0D).getBlock()); - - for (Block neighbor: possibleNeighbors) { - if (neighbor.getType() == Material.AIR) { - return neighbor; - } - } - - for (Block neighbor: possibleNeighbors) { - if ((neighbor.getState().getClass().getSimpleName().equals("CraftBlockState")) && (neighbor.getType() != Material.valueOf(XMaterial.PISTON_HEAD.getLegacy()[0]) && neighbor.getType() != Material.valueOf(XMaterial.MOVING_PISTON.getLegacy()[1])) && (neighbor.getType() != Material.valueOf("REDSTONE_LAMP_ON"))) { - return neighbor; - } - } - - for (Block neighbor: possibleNeighbors) { - if ((neighbor.getState().getClass().getSimpleName().equals("CraftBlockState")) && (neighbor.getType() != Material.valueOf(XMaterial.PISTON_HEAD.getLegacy()[0]) && neighbor.getType() != Material.valueOf(XMaterial.MOVING_PISTON.getLegacy()[1]))) { - return neighbor; - } - } - return null; + public static boolean isOpenable(@NotNull Block b) { + return b.getBlockData() instanceof org.bukkit.block.data.Openable; } private static boolean isAan(Block lamp) { - if (is113Plus) { - return getAsString(getBlockData(lamp)).contains("lit=true"); - } else { - return lamp.getType() == Material.matchMaterial(XMaterial.REDSTONE_LAMP.getLegacy()[0]); - } + return lamp.getBlockData().getAsString().contains("lit=true"); } private static boolean isLamp(Block lamp) { - if (is113Plus) { - return lamp.getType() == XMaterial.REDSTONE_LAMP.parseMaterial(); - } else { - return Arrays.asList(XMaterial.REDSTONE_LAMP.getLegacy()).contains(lamp.getType().name()); - } + return lamp.getType() == XMaterial.REDSTONE_LAMP.parseMaterial(); } - - private static void setBlockData(Block lamp, Object blockData) { - try { - setBlockDataMethod.invoke(lamp, blockData, - Boolean.FALSE); - } catch (Exception e) { - e.printStackTrace(); - } - } - - private static Object getBlockData(Block lamp) { - try { - return getBlockDataMethod.invoke(lamp); - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } - - private static String getAsString(Object blockData) { - try { - return (String) getAsStringMethod.invoke(blockData, new Object[0]); - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } - } \ No newline at end of file