From c61063ebdf085e8ff9eefe8dcd46aec826a8901a Mon Sep 17 00:00:00 2001 From: stijnb1234 Date: Mon, 3 Feb 2020 19:53:29 +0100 Subject: [PATCH] Fixed GSON issues for 1.14+ & fixed dir blocks --- pom.xml | 6 ++ .../V10Lift/API/Objects/LiftBlock.java | 89 ++++++++++++++----- .../V10Lift/API/Runnables/MoveLift.java | 14 ++- .../SBDeveloper/V10Lift/API/V10LiftAPI.java | 79 ++++++++++++---- .../V10Lift/Utils/DirectionUtil.java | 21 ++++- 5 files changed, 163 insertions(+), 46 deletions(-) diff --git a/pom.xml b/pom.xml index d00f140..2fad641 100644 --- a/pom.xml +++ b/pom.xml @@ -90,6 +90,12 @@ 1.18.10 provided + + + com.google.code.gson + gson + 2.8.6 + diff --git a/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/LiftBlock.java b/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/LiftBlock.java index 957af9e..d020b59 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/LiftBlock.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/API/Objects/LiftBlock.java @@ -20,6 +20,7 @@ public class LiftBlock implements Comparable { @Getter private final Material mat; @Getter private final byte data; @Getter private final BlockFace face; + @Getter private final Object bisected; @Getter private final String[] signLines; //Only used for inputs! @@ -29,6 +30,7 @@ public class LiftBlock implements Comparable { //Only used for chests public Map[] serializedItemStacks = null; + /* Floor based liftblock, no material */ public LiftBlock(String world, int x, int y, int z, String floor) { this.world = world; this.x = x; @@ -39,20 +41,12 @@ public class LiftBlock implements Comparable { this.face = null; this.signLines = null; this.floor = floor; + this.bisected = null; } - public LiftBlock(String world, int x, int y, int z, Material mat) { - this.world = world; - this.x = x; - this.y = y; - this.z = z; - this.mat = mat; - this.face = null; - this.data = 0; - this.signLines = null; - this.floor = null; - } + /** 1.12 liftblocks **/ + /* 1.12 liftblock (Directional) */ public LiftBlock(String world, int x, int y, int z, Material mat, byte data) { this.world = world; this.x = x; @@ -63,20 +57,10 @@ public class LiftBlock implements Comparable { this.data = data; this.signLines = null; this.floor = null; + this.bisected = null; } - public LiftBlock(String world, int x, int y, int z, Material mat, BlockFace face, String[] signLines) { - this.world = world; - this.x = x; - this.y = y; - this.z = z; - this.mat = mat; - this.face = face; - this.data = 0; - this.signLines = signLines; - this.floor = null; - } - + /* 1.12 liftblock (sign) */ public LiftBlock(String world, int x, int y, int z, Material mat, byte data, String[] signLines) { this.world = world; this.x = x; @@ -87,6 +71,65 @@ public class LiftBlock implements Comparable { this.data = data; this.signLines = signLines; this.floor = null; + this.bisected = null; + } + + /** 1.13 liftblocks **/ + + /* 1.13 liftblock (no Dir) */ + public LiftBlock(String world, int x, int y, int z, Material mat) { + this.world = world; + this.x = x; + this.y = y; + this.z = z; + this.mat = mat; + this.face = null; + this.data = 0; + this.signLines = null; + this.floor = null; + this.bisected = null; + } + + /* 1.13 liftblock (Directional) */ + public LiftBlock(String world, int x, int y, int z, Material mat, BlockFace face) { + this.world = world; + this.x = x; + this.y = y; + this.z = z; + this.mat = mat; + this.face = face; + this.data = 0; + this.signLines = null; + this.floor = null; + this.bisected = null; + } + + /* 1.13 liftblock (dir & bisec) */ + public LiftBlock(String world, int x, int y, int z, Material mat, BlockFace face, Object bisected) { + this.world = world; + this.x = x; + this.y = y; + this.z = z; + this.mat = mat; + this.face = face; + this.data = 0; + this.signLines = null; + this.floor = null; + this.bisected = bisected; + } + + /* 1.13 liftblock (sign) */ + public LiftBlock(String world, int x, int y, int z, Material mat, BlockFace face, String[] signLines) { + this.world = world; + this.x = x; + this.y = y; + this.z = z; + this.mat = mat; + this.face = face; + this.data = 0; + this.signLines = signLines; + this.floor = null; + this.bisected = null; } @Override diff --git a/src/main/java/nl/SBDeveloper/V10Lift/API/Runnables/MoveLift.java b/src/main/java/nl/SBDeveloper/V10Lift/API/Runnables/MoveLift.java index 6bcc6ba..9c63b2a 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/API/Runnables/MoveLift.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/API/Runnables/MoveLift.java @@ -205,6 +205,7 @@ public class MoveLift implements Runnable { state.update(true); if (XMaterial.isNewVersion()) { DirectionUtil.setDirection(block, lib.getFace()); + DirectionUtil.setBisected(block, lib.getBisected()); } lb = lift.getBlocks().first(); for (Entity ent : Objects.requireNonNull(Bukkit.getWorld(lib.getWorld()), "World is null at MoveLift").getBlockAt(lib.getX(), lib.getY(), lib.getZ()).getChunk().getEntities()) { @@ -248,6 +249,7 @@ public class MoveLift implements Runnable { state.update(true); if (XMaterial.isNewVersion()) { DirectionUtil.setDirection(block, lib.getFace()); + DirectionUtil.setBisected(block, lib.getBisected()); } lift.getBlocks().add(lib); if (lib.getSignLines() != null) { @@ -336,6 +338,7 @@ public class MoveLift implements Runnable { state.update(true); if (XMaterial.isNewVersion()) { DirectionUtil.setDirection(block, lib.getFace()); + DirectionUtil.setBisected(block, lib.getBisected()); } } veiter = lift.getToMove().iterator(); @@ -359,6 +362,7 @@ public class MoveLift implements Runnable { state.update(true); if (XMaterial.isNewVersion()) { DirectionUtil.setDirection(block, lib.getFace()); + DirectionUtil.setBisected(block, lib.getBisected()); } lift.getBlocks().add(lib); if (lib.getSignLines() != null) { @@ -410,8 +414,9 @@ public class MoveLift implements Runnable { block = world.getBlockAt(rope.getX(), rope.getCurrently(), rope.getZ()); block.setType(rope.getType()); if (XMaterial.isNewVersion()) { - org.bukkit.block.data.type.Ladder ladder = (org.bukkit.block.data.type.Ladder) block.getBlockData(); - ladder.setFacing(rope.getFace()); + org.bukkit.block.data.Directional data = (org.bukkit.block.data.Directional) block.getBlockData(); + data.setFacing(rope.getFace()); + block.setBlockData(data); } else { BlockState state = block.getState(); org.bukkit.material.Ladder ladder = new org.bukkit.material.Ladder(rope.getType()); @@ -426,8 +431,9 @@ public class MoveLift implements Runnable { block = world.getBlockAt(rope.getX(), rope.getCurrently(), rope.getZ()); block.setType(rope.getType()); if (XMaterial.isNewVersion()) { - org.bukkit.block.data.type.Ladder ladder = (org.bukkit.block.data.type.Ladder) block.getBlockData(); - ladder.setFacing(rope.getFace()); + org.bukkit.block.data.Directional data = (org.bukkit.block.data.Directional) block.getBlockData(); + data.setFacing(rope.getFace()); + block.setBlockData(data); } else { BlockState state = block.getState(); org.bukkit.material.Ladder ladder = new org.bukkit.material.Ladder(rope.getType()); diff --git a/src/main/java/nl/SBDeveloper/V10Lift/API/V10LiftAPI.java b/src/main/java/nl/SBDeveloper/V10Lift/API/V10LiftAPI.java index 4d02e54..c42b4cd 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/API/V10LiftAPI.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/API/V10LiftAPI.java @@ -173,17 +173,34 @@ public class V10LiftAPI { public int addBlockToLift(Set blocks, @Nonnull Block block) { Material type = block.getType(); LiftBlock lb; - if (type.toString().contains("SIGN")) { - //SIGN - if (XMaterial.isNewVersion()) { + if (XMaterial.isNewVersion()) { + Class biClass; + try { + biClass = Class.forName("org.bukkit.block.data.Bisected"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + return -1; + } + + if (type.toString().contains("SIGN")) { + Bukkit.getLogger().info("Block instanceof Dir 1.13 & is sign"); lb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), type, DirectionUtil.getDirection(block), ((Sign) block.getState()).getLines()); + } else if (block.getBlockData() instanceof org.bukkit.block.data.Directional && block.getBlockData().getClass().isInstance(biClass)) { + Bukkit.getLogger().info("Block instanceof Dir 1.13 & bisected"); + lb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), type, DirectionUtil.getDirection(block), DirectionUtil.getBisected(block)); + } else if (block.getBlockData() instanceof org.bukkit.block.data.Directional) { + Bukkit.getLogger().info("Block instanceof Dir 1.13"); + lb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), type, DirectionUtil.getDirection(block)); } else { - lb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), type, block.getState().getRawData(), ((Sign) block.getState()).getLines()); + Bukkit.getLogger().info("Block not instanceof Dir 1.13"); + lb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), type); } } else { - if (XMaterial.isNewVersion()) { - lb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), type); + if (type.toString().contains("SIGN")) { + Bukkit.getLogger().info("Block instanceof Dir 1.12 & is sign"); + lb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), type, block.getState().getRawData(), ((Sign) block.getState()).getLines()); } else { + Bukkit.getLogger().info("Block no sign 1.12"); lb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), type, block.getState().getRawData()); } } @@ -218,17 +235,34 @@ public class V10LiftAPI { Lift lift = DataManager.getLift(liftName); Material type = block.getType(); LiftBlock lb; - if (type.toString().contains("SIGN")) { - //SIGN - if (XMaterial.isNewVersion()) { + if (XMaterial.isNewVersion()) { + Class biClass; + try { + biClass = Class.forName("org.bukkit.block.data.Bisected"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + return -1; + } + + if (type.toString().contains("SIGN")) { + Bukkit.getLogger().info("Block instanceof Dir 1.13 & is sign"); lb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), type, DirectionUtil.getDirection(block), ((Sign) block.getState()).getLines()); + } else if (block.getBlockData() instanceof org.bukkit.block.data.Directional && block.getBlockData().getClass().isInstance(biClass)) { + Bukkit.getLogger().info("Block instanceof Dir 1.13 & bisected"); + lb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), type, DirectionUtil.getDirection(block), DirectionUtil.getBisected(block)); + } else if (block.getBlockData() instanceof org.bukkit.block.data.Directional) { + Bukkit.getLogger().info("Block instanceof Dir 1.13"); + lb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), type, DirectionUtil.getDirection(block)); } else { - lb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), type, block.getState().getRawData(), ((Sign) block.getState()).getLines()); + Bukkit.getLogger().info("Block not instanceof Dir 1.13"); + lb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), type); } } else { - if (XMaterial.isNewVersion()) { - lb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), type); + if (type.toString().contains("SIGN")) { + Bukkit.getLogger().info("Block instanceof Dir 1.12 & is sign"); + lb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), type, block.getState().getRawData(), ((Sign) block.getState()).getLines()); } else { + Bukkit.getLogger().info("Block no sign 1.12"); lb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), type, block.getState().getRawData()); } } @@ -263,17 +297,26 @@ public class V10LiftAPI { Material type = block.getType(); if (getFBM().isForbidden(type)) return -2; LiftBlock lb; - if (type.toString().contains("SIGN")) { - //SIGN - if (XMaterial.isNewVersion()) { + if (XMaterial.isNewVersion()) { + if (type.toString().contains("SIGN")) { + Bukkit.getLogger().info("Block instanceof Dir 1.13 & is sign"); lb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), type, DirectionUtil.getDirection(block), ((Sign) block.getState()).getLines()); + } else if (block.getBlockData() instanceof org.bukkit.block.data.Directional && block.getBlockData() instanceof org.bukkit.block.data.Bisected) { + Bukkit.getLogger().info("Block instanceof Dir 1.13 & bisected"); + lb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), type, DirectionUtil.getDirection(block), DirectionUtil.getBisected(block)); + } else if (block.getBlockData() instanceof org.bukkit.block.data.Directional) { + Bukkit.getLogger().info("Block instanceof Dir 1.13"); + lb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), type, DirectionUtil.getDirection(block)); } else { - lb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), type, block.getState().getRawData(), ((Sign) block.getState()).getLines()); + Bukkit.getLogger().info("Block not instanceof Dir 1.13"); + lb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), type); } } else { - if (XMaterial.isNewVersion()) { - lb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), type); + if (type.toString().contains("SIGN")) { + Bukkit.getLogger().info("Block instanceof Dir 1.12 & is sign"); + lb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), type, block.getState().getRawData(), ((Sign) block.getState()).getLines()); } else { + Bukkit.getLogger().info("Block no sign 1.12"); lb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), type, block.getState().getRawData()); } } diff --git a/src/main/java/nl/SBDeveloper/V10Lift/Utils/DirectionUtil.java b/src/main/java/nl/SBDeveloper/V10Lift/Utils/DirectionUtil.java index 5c0168c..95e1184 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/Utils/DirectionUtil.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/Utils/DirectionUtil.java @@ -2,6 +2,7 @@ package nl.SBDeveloper.V10Lift.Utils; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; +import org.bukkit.block.data.Bisected; import org.bukkit.block.data.BlockData; import org.bukkit.block.data.Directional; @@ -20,11 +21,29 @@ public class DirectionUtil { } public static void setDirection(@Nonnull Block block, BlockFace blockFace) { - if (block.getBlockData() instanceof Directional) { + if (blockFace != null && block.getBlockData() instanceof Directional) { BlockData bd = block.getBlockData(); Directional dir = (Directional) bd; dir.setFacing(blockFace); block.setBlockData(bd); } } + + @Nullable + public static Object getBisected(@Nonnull Block block) { + if (block.getBlockData() instanceof Bisected) { + Bisected bis = (Bisected) block.getBlockData(); + return bis.getHalf(); + } + return null; + } + + public static void setBisected(@Nonnull Block block, Object bisected) { + if (bisected != null && block.getBlockData() instanceof Bisected && bisected instanceof Bisected.Half) { + BlockData bd = block.getBlockData(); + Bisected bis = (Bisected) bd; + bis.setHalf((Bisected.Half) bisected); + block.setBlockData(bd); + } + } }