diff --git a/src/main/java/tech/sbdevelopment/v10lift/api/V10LiftAPI.java b/src/main/java/tech/sbdevelopment/v10lift/api/V10LiftAPI.java index 4dfa6dd..749386b 100644 --- a/src/main/java/tech/sbdevelopment/v10lift/api/V10LiftAPI.java +++ b/src/main/java/tech/sbdevelopment/v10lift/api/V10LiftAPI.java @@ -17,7 +17,7 @@ import tech.sbdevelopment.v10lift.managers.DataManager; import tech.sbdevelopment.v10lift.managers.ForbiddenBlockManager; import tech.sbdevelopment.v10lift.sbutils.LocationSerializer; import tech.sbdevelopment.v10lift.utils.ConfigUtil; -import tech.sbdevelopment.v10lift.utils.DirectionUtil; +import tech.sbdevelopment.v10lift.utils.BlockStateUtil; import tech.sbdevelopment.v10lift.utils.DoorUtil; import javax.annotation.Nonnull; @@ -180,35 +180,7 @@ public class V10LiftAPI { * @return 0 if added, -1 if null or doesn't exists, -2 if forbidden, -3 if already added */ public int addBlockToLift(Set blocks, @Nonnull Block block) { - Material type = block.getType(); - LiftBlock lb; - if (XMaterial.supports(13)) { - 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 if (block.getBlockData() instanceof org.bukkit.block.data.type.Slab) { - Bukkit.getLogger().info("Block instanceof Slab 1.13"); - lb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), type, DirectionUtil.getSlabType(block)); - } else { - Bukkit.getLogger().info("Block not instanceof Dir 1.13"); - lb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), type); - } - } else { - 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()); - } - } - return addBlockToLift(blocks, lb); + return addBlockToLift(blocks, new LiftBlock(block)); } /** @@ -237,34 +209,7 @@ public class V10LiftAPI { public int removeBlockFromLift(String liftName, Block block) { if (liftName == null || block == null || !DataManager.containsLift(liftName)) return -1; Lift lift = DataManager.getLift(liftName); - Material type = block.getType(); - LiftBlock lb; - if (XMaterial.supports(13)) { - 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 if (block.getBlockData() instanceof org.bukkit.block.data.type.Slab) { - Bukkit.getLogger().info("Block instanceof Slab 1.13"); - lb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), type, DirectionUtil.getSlabType(block)); - } else { - Bukkit.getLogger().info("Block not instanceof Dir 1.13"); - lb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), type); - } - } else { - 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()); - } - } + LiftBlock lb = new LiftBlock(block); if (!lift.getBlocks().contains(lb)) return -2; lift.getBlocks().remove(lb); return 0; @@ -293,35 +238,8 @@ public class V10LiftAPI { */ public int switchBlockAtLift(TreeSet blocks, Block block) { if (blocks == null || block == null) return -1; - Material type = block.getType(); - if (ForbiddenBlockManager.isForbidden(type)) return -2; - LiftBlock lb; - if (XMaterial.supports(13)) { - 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 if (block.getBlockData() instanceof org.bukkit.block.data.type.Slab) { - Bukkit.getLogger().info("Block instanceof Slab 1.13"); - lb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), type, DirectionUtil.getSlabType(block)); - } else { - Bukkit.getLogger().info("Block not instanceof Dir 1.13"); - lb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), type); - } - } else { - 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()); - } - } + if (ForbiddenBlockManager.isForbidden(block.getType())) return -2; + LiftBlock lb = new LiftBlock(block); if (blocks.contains(lb)) { blocks.remove(lb); return 1; @@ -469,9 +387,6 @@ public class V10LiftAPI { Block block = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at closeDoor").getBlockAt(lb.getX(), lb.getY(), lb.getZ()); BlockState state = block.getState(); state.setType(lb.getMat()); - if (!XMaterial.supports(13)) { - state.setRawData(lb.getData()); - } state.update(true); } @@ -900,16 +815,7 @@ public class V10LiftAPI { if (block.getType() != mat) return -2; } - BlockFace face; - if (XMaterial.supports(13)) { - face = DirectionUtil.getDirection(block); - } else { - BlockState state = block.getState(); - org.bukkit.material.Ladder ladder = (org.bukkit.material.Ladder) state.getData(); - face = ladder.getAttachedFace(); - } - - LiftRope rope = new LiftRope(mat, face, world.getName(), x, minY, maxY, z); + LiftRope rope = new LiftRope(block, minY, maxY); DataManager.getLift(lift).getRopes().add(rope); return 0; diff --git a/src/main/java/tech/sbdevelopment/v10lift/api/objects/Lift.java b/src/main/java/tech/sbdevelopment/v10lift/api/objects/Lift.java index 330489e..dcd5ab7 100644 --- a/src/main/java/tech/sbdevelopment/v10lift/api/objects/Lift.java +++ b/src/main/java/tech/sbdevelopment/v10lift/api/objects/Lift.java @@ -28,7 +28,6 @@ public class Lift { @Setter private LinkedHashMap queue = null; private final HashSet ropes = new HashSet<>(); - private transient final ArrayList toMove = new ArrayList<>(); @Setter private int speed; @Setter diff --git a/src/main/java/tech/sbdevelopment/v10lift/api/objects/LiftBlock.java b/src/main/java/tech/sbdevelopment/v10lift/api/objects/LiftBlock.java index 407d696..bbd7fd8 100644 --- a/src/main/java/tech/sbdevelopment/v10lift/api/objects/LiftBlock.java +++ b/src/main/java/tech/sbdevelopment/v10lift/api/objects/LiftBlock.java @@ -5,6 +5,7 @@ import lombok.NoArgsConstructor; import lombok.Setter; import lombok.ToString; import org.bukkit.Material; +import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import javax.annotation.Nonnull; @@ -26,11 +27,16 @@ public class LiftBlock implements Comparable { //Only used for cabine blocks, because those need caching! @Setter private Material mat; - private byte data; + @Setter private BlockFace face; + @Setter private String bisected; - private String slabtype; + @Setter + private String slabType; + @Setter private String[] signLines; + @Setter + private Boolean open; //Only used for inputs! private String floor; @@ -41,7 +47,7 @@ public class LiftBlock implements Comparable { public Map[] serializedItemStacks = null; /** - * A floor based liftblock, without material (no caching) + * Create a floor based liftblock, without material (no caching) * * @param world The world * @param x The x-pos @@ -55,182 +61,40 @@ public class LiftBlock implements Comparable { this.y = y; this.z = z; this.mat = null; - this.data = 0; this.face = null; this.signLines = null; this.floor = floor; this.bisected = null; - this.slabtype = null; + this.slabType = null; + this.open = null; } /** - * 1.12 liftblock, with material and data [NO SIGN] + * Create a new liftblock from a block * - * @param world The world - * @param x The x-pos - * @param y The y-pos - * @param z The z-pos - * @param mat The Material of the block - * @param data The data of the block + * @param block The block */ - public LiftBlock(String world, int x, int y, int z, Material mat, byte data) { - this.world = world; - this.x = x; - this.y = y; - this.z = z; - this.mat = mat; - this.face = null; - this.data = data; - this.signLines = null; - this.floor = null; - this.bisected = null; - this.slabtype = null; - } - - /** - * 1.12 liftblock (signs) - * - * @param world The world - * @param x The x-pos - * @param y The y-pos - * @param z The z-pos - * @param mat The Material of the block - * @param data The data of the block - * @param signLines The lines of the sign - */ - public LiftBlock(String world, int x, int y, int z, Material mat, byte data, String[] signLines) { - this.world = world; - this.x = x; - this.y = y; - this.z = z; - this.mat = mat; - this.face = null; - this.data = data; - this.signLines = signLines; - this.floor = null; - this.bisected = null; - this.slabtype = null; - } - - /** - * 1.13 liftblock, without a direction - * - * @param world The world - * @param x The x-pos - * @param y The y-pos - * @param z The z-pos - * @param mat The Material of the block - */ - 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; - this.slabtype = null; - } - - /** - * 1.13 liftblock with a direction - * - * @param world The world - * @param x The x-pos - * @param y The y-pos - * @param z The z-pos - * @param mat The Material of the block - * @param face The blockface of the block - */ - 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; - this.slabtype = null; - } - - /** - * 1.13 liftblock, with a direction and a bisected - * - * @param world The world - * @param x The x-pos - * @param y The y-pos - * @param z The z-pos - * @param mat The Material of the block - * @param face The blockface of the block - * @param bisected The bisected of the block - */ - public LiftBlock(String world, int x, int y, int z, Material mat, BlockFace face, String 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; - this.slabtype = null; - } - - /** - * 1/13 liftblock (sign) - * - * @param world The world - * @param x The x-pos - * @param y The y-pos - * @param z The z-pos - * @param mat The Material of the block - * @param face The blockface of the block - * @param signLines The lines of the 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; - this.slabtype = null; - } - - /** - * 1.13 liftblock (slab) - * - * @param world The world - * @param x The x-pos - * @param y The y-pos - * @param z The z-pos - * @param mat The Material of the block - * @param slabtype The typ of slab (low, high, double) - */ - public LiftBlock(String world, int x, int y, int z, Material mat, String slabtype) { - 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; - this.slabtype = slabtype; + public LiftBlock(Block block) { + this.world = block.getWorld().getName(); + this.x = block.getX(); + this.y = block.getY(); + this.z = block.getZ(); + this.mat = block.getType(); + if (block.getBlockData() instanceof org.bukkit.block.data.Directional) { + this.face = ((org.bukkit.block.data.Directional) block.getBlockData()).getFacing(); + } + if (block.getBlockData() instanceof org.bukkit.block.data.Bisected) { + this.bisected = ((org.bukkit.block.data.Bisected) block.getBlockData()).getHalf().name(); + } + if (block.getBlockData() instanceof org.bukkit.block.data.type.Slab) { + this.slabType = ((org.bukkit.block.data.type.Slab) block.getBlockData()).getType().name(); + } + if (block.getState() instanceof org.bukkit.block.Sign) { + this.signLines = ((org.bukkit.block.Sign) block.getState()).getLines(); + } + if (block.getBlockData() instanceof org.bukkit.block.data.Openable) { + this.open = ((org.bukkit.block.data.Openable) block.getBlockData()).isOpen(); + } } @Override diff --git a/src/main/java/tech/sbdevelopment/v10lift/api/objects/LiftRope.java b/src/main/java/tech/sbdevelopment/v10lift/api/objects/LiftRope.java index fe02fba..ddc8b3e 100644 --- a/src/main/java/tech/sbdevelopment/v10lift/api/objects/LiftRope.java +++ b/src/main/java/tech/sbdevelopment/v10lift/api/objects/LiftRope.java @@ -5,6 +5,7 @@ import lombok.NoArgsConstructor; import lombok.Setter; import lombok.ToString; import org.bukkit.Material; +import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import java.util.Objects; @@ -18,7 +19,10 @@ import java.util.Objects; @ToString public class LiftRope { private Material type; + //If it's Directional private BlockFace face; + //If it's Openable + private boolean open; private String world; private int x; private int minY; @@ -29,23 +33,24 @@ public class LiftRope { /** * Construct a new liftrope * - * @param type The material of the rope - * @param face The face of the rope - * @param world The world - * @param x The x-pos + * @param block The block * @param minY The starting x-pos * @param maxY The stopping x-pos - * @param z The z-pos */ - public LiftRope(Material type, BlockFace face, String world, int x, int minY, int maxY, int z) { - this.type = type; - this.face = face; - this.world = world; - this.x = x; + public LiftRope(Block block, int minY, int maxY) { + this.type = block.getType(); + this.world = block.getWorld().getName(); + this.x = block.getX(); this.minY = minY; this.maxY = maxY; - this.z = z; - this.currently = minY; + this.z = block.getZ(); + this.currently = block.getY(); + if (block.getBlockData() instanceof org.bukkit.block.data.Directional) { + this.face = ((org.bukkit.block.data.Directional) block.getBlockData()).getFacing(); + } + if (block.getBlockData() instanceof org.bukkit.block.data.Openable) { + this.open = ((org.bukkit.block.data.Openable) block.getBlockData()).isOpen(); + } } @Override diff --git a/src/main/java/tech/sbdevelopment/v10lift/api/objects/V10Entity.java b/src/main/java/tech/sbdevelopment/v10lift/api/objects/V10Entity.java deleted file mode 100644 index 0313efa..0000000 --- a/src/main/java/tech/sbdevelopment/v10lift/api/objects/V10Entity.java +++ /dev/null @@ -1,102 +0,0 @@ -package tech.sbdevelopment.v10lift.api.objects; - -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.Setter; -import lombok.ToString; -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.entity.Entity; - -import java.util.UUID; - -/** - * A v10entity object, for an entity in the lift. - */ -@Getter -@NoArgsConstructor -@ToString -public class V10Entity { - private UUID entityUUID; - private String world; - private int locX; - private int locY; - private int locZ; - private int y; - @Setter - private short step; - - /** - * Construct a new V10LiftEntity - * - * @param entityUUID The UUID of the entity - * @param worldName The world - * @param x The x-pos - * @param y The y-pos - * @param z The z-pos - * @param cury The current y-pos - */ - public V10Entity(UUID entityUUID, String worldName, int x, int y, int z, int cury) { - this.entityUUID = entityUUID; - this.world = worldName; - this.locX = x; - this.locY = y; - this.locZ = z; - this.y = cury; - this.step = 0; - } - - /** - * Move a entity up - */ - public void moveUp() { - if (entityUUID == null) return; - Entity entity = Bukkit.getEntity(entityUUID); - if (entity == null || entity.isDead()) return; - locY = y + step; - entity.teleport(new Location(Bukkit.getWorld(world), locX, locY, locZ)); - } - - /** - * Move a entity down - */ - public void moveDown() { - if (entityUUID == null) return; - Entity entity = Bukkit.getEntity(entityUUID); - if (entity == null || entity.isDead()) return; - locY = y - step; - entity.teleport(new Location(Bukkit.getWorld(world), locX, locY, locZ)); - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null) return false; - UUID uuid; - if (o instanceof V10Entity) { - Entity ent = Bukkit.getEntity(((V10Entity) o).getEntityUUID()); - if (ent == null || ent.isDead()) { - Entity e = Bukkit.getEntity(entityUUID); - return e == null || e.isDead(); - } - uuid = ent.getUniqueId(); - } else if (o instanceof Entity) { - Entity ent = (Entity) o; - if (ent.isDead()) { - Entity e = Bukkit.getEntity(entityUUID); - return e == null || e.isDead(); - } - uuid = ((Entity) o).getUniqueId(); - } else - return false; - Entity e = Bukkit.getEntity(entityUUID); - if (e == null || e.isDead()) - return false; - return uuid == e.getUniqueId(); - } - - @Override - public int hashCode() { - return 31 + ((entityUUID == null) ? 0 : entityUUID.hashCode()); - } -} diff --git a/src/main/java/tech/sbdevelopment/v10lift/api/runnables/MoveLift.java b/src/main/java/tech/sbdevelopment/v10lift/api/runnables/MoveLift.java index 0c63d49..b31e14c 100644 --- a/src/main/java/tech/sbdevelopment/v10lift/api/runnables/MoveLift.java +++ b/src/main/java/tech/sbdevelopment/v10lift/api/runnables/MoveLift.java @@ -18,7 +18,7 @@ import tech.sbdevelopment.v10lift.managers.AntiCopyBlockManager; import tech.sbdevelopment.v10lift.managers.DataManager; import tech.sbdevelopment.v10lift.sbutils.LocationSerializer; import tech.sbdevelopment.v10lift.utils.ConfigUtil; -import tech.sbdevelopment.v10lift.utils.DirectionUtil; +import tech.sbdevelopment.v10lift.utils.BlockStateUtil; import java.lang.reflect.Method; import java.util.*; @@ -153,7 +153,6 @@ public class MoveLift implements Runnable { Bukkit.getLogger().info("[V10Lift] Lift " + liftName + " reaches the upper rope end but won't stop!!"); V10LiftAPI.getInstance().setDefective(liftName, true); - lift.getToMove().clear(); queueIterator.remove(); return; } @@ -214,22 +213,17 @@ public class MoveLift implements Runnable { BlockState state = nextBlock.getState(); state.setType(lib.getMat()); - if (!XMaterial.supports(13)) { - state.setRawData(lib.getData()); - } state.update(true); if (XMaterial.supports(13)) { - DirectionUtil.setDirection(nextBlock, lib.getFace()); - DirectionUtil.setBisected(nextBlock, lib.getBisected()); - DirectionUtil.setSlabType(nextBlock, lib.getSlabtype()); + BlockStateUtil.setDirection(nextBlock, lib.getFace()); + BlockStateUtil.setBisected(nextBlock, lib.getBisected()); + BlockStateUtil.setSlabType(nextBlock, lib.getSlabType()); + BlockStateUtil.setOpen(nextBlock, lib.getOpen()); } if (direction == LiftDirection.UP) { //Teleportation is only required if we go up, for down gravity works fine. ;) for (Entity ent : nextBlock.getChunk().getEntities()) { - V10Entity v10ent = new V10Entity(ent.getUniqueId(), null, 0, 0, 0, 0); - if (lift.getToMove().contains(v10ent)) continue; - Location entLoc = ent.getLocation(); if ((entLoc.getBlockY() == lib.getY() || entLoc.getBlockY() + 1 == lib.getY()) && entLoc.getBlockX() == lib.getX() && entLoc.getBlockZ() == lib.getZ()) { entLoc.setY(entLoc.getY() + 1); @@ -247,36 +241,17 @@ public class MoveLift implements Runnable { } } - Iterator toMoveIterator = lift.getToMove().iterator(); - while (toMoveIterator.hasNext()) { - V10Entity v10ent = toMoveIterator.next(); - if (v10ent.getStep() > 0) { - if (direction == LiftDirection.UP) v10ent.moveUp(); - else v10ent.moveDown(); - if (v10ent.getStep() > 16) { - toMoveIterator.remove(); - } - } - v10ent.setStep((short) (v10ent.getStep() + 1)); - } - for (LiftBlock lib : antiCopyBlocks) { Block block = Bukkit.getWorld(lib.getWorld()).getBlockAt(lib.getX(), lib.getY(), lib.getZ()); if (lib.getMat() == null) lib.setMat(Material.AIR); BlockState state = block.getState(); state.setType(lib.getMat()); - if (!XMaterial.supports(13)) { - state.setRawData(lib.getData()); - } state.update(true); - - if (XMaterial.supports(13)) { - DirectionUtil.setDirection(block, lib.getFace()); - DirectionUtil.setBisected(block, lib.getBisected()); - DirectionUtil.setSlabType(block, lib.getSlabtype()); - } - + BlockStateUtil.setDirection(block, lib.getFace()); + BlockStateUtil.setBisected(block, lib.getBisected()); + BlockStateUtil.setSlabType(block, lib.getSlabType()); + BlockStateUtil.setOpen(block, lib.getOpen()); lift.getBlocks().add(lib); if (lib.getSignLines() != null) { @@ -333,7 +308,6 @@ public class MoveLift implements Runnable { Bukkit.getLogger().info("[V10Lift] Lift " + liftName + " reaches the upper rope end but won't stop!!"); V10LiftAPI.getInstance().setDefective(liftName, true); - lift.getToMove().clear(); queueIterator.remove(); stopAfter = true; @@ -345,21 +319,13 @@ public class MoveLift implements Runnable { if (rope.getType() == null) rope.setType(Material.AIR); block.setType(rope.getType()); - if (XMaterial.supports(13)) { - DirectionUtil.setDirection(block, rope.getFace()); - } else { - BlockState state = block.getState(); - org.bukkit.material.Ladder ladder = new org.bukkit.material.Ladder(rope.getType()); - ladder.setFacingDirection(rope.getFace()); - state.setData(ladder); - state.update(true); - } + BlockStateUtil.setDirection(block, rope.getFace()); + BlockStateUtil.setOpen(block, rope.isOpen()); if (stopAfter) return; } } } else { - lift.getToMove().clear(); queueIterator.remove(); for (LiftBlock lib : lift.getBlocks()) { diff --git a/src/main/java/tech/sbdevelopment/v10lift/listeners/PlayerInteractListener.java b/src/main/java/tech/sbdevelopment/v10lift/listeners/PlayerInteractListener.java index e7dbd6e..79d173b 100644 --- a/src/main/java/tech/sbdevelopment/v10lift/listeners/PlayerInteractListener.java +++ b/src/main/java/tech/sbdevelopment/v10lift/listeners/PlayerInteractListener.java @@ -233,7 +233,7 @@ public class PlayerInteractListener implements Listener { LiftBlock start = DataManager.getRopeEditPlayer(p.getUniqueId()); Block now = e.getClickedBlock(); if (start == null) { - ConfigUtil.sendMessage(e.getPlayer(), "Rope.Delete"); + ConfigUtil.sendMessage(e.getPlayer(), "Rope.ClickOnEnd"); DataManager.addRopeEditPlayer(p.getUniqueId(), new LiftBlock(now.getWorld().getName(), now.getX(), now.getY(), now.getZ(), (String) null)); } else if (start.equals(new LiftBlock(now.getWorld().getName(), now.getX(), now.getY(), now.getZ(), (String) null))) { DataManager.addRopeEditPlayer(p.getUniqueId(), null); @@ -283,12 +283,7 @@ public class PlayerInteractListener implements Listener { ConfigUtil.sendMessage(e.getPlayer(), "Door.BlacklistedMaterial", Collections.singletonMap("%Name%", e.getClickedBlock().getType().toString().toLowerCase())); return; } - LiftBlock lb; - if (XMaterial.supports(13)) { - lb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), block.getType()); - } else { - lb = new LiftBlock(block.getWorld().getName(), block.getX(), block.getY(), block.getZ(), block.getType(), block.getState().getRawData()); - } + LiftBlock lb = new LiftBlock(block); Lift lift = DataManager.getLift(DataManager.getEditPlayer(p.getUniqueId())); Floor floor = lift.getFloors().get(DataManager.getDoorEditPlayer(p.getUniqueId())); if (DoorUtil.isOpenable(block)) { diff --git a/src/main/java/tech/sbdevelopment/v10lift/utils/DirectionUtil.java b/src/main/java/tech/sbdevelopment/v10lift/utils/BlockStateUtil.java similarity index 72% rename from src/main/java/tech/sbdevelopment/v10lift/utils/DirectionUtil.java rename to src/main/java/tech/sbdevelopment/v10lift/utils/BlockStateUtil.java index 986bba7..9a64247 100644 --- a/src/main/java/tech/sbdevelopment/v10lift/utils/DirectionUtil.java +++ b/src/main/java/tech/sbdevelopment/v10lift/utils/BlockStateUtil.java @@ -1,17 +1,14 @@ package tech.sbdevelopment.v10lift.utils; -import com.cryptomorin.xseries.XMaterial; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import javax.annotation.Nonnull; import javax.annotation.Nullable; -public class DirectionUtil { - +public class BlockStateUtil { @Nullable public static BlockFace getDirection(@Nonnull Block block) { - if (!XMaterial.supports(13)) return null; if (block.getBlockData() instanceof org.bukkit.block.data.Directional) { org.bukkit.block.data.Directional dir = (org.bukkit.block.data.Directional) block.getBlockData(); return dir.getFacing(); @@ -19,9 +16,8 @@ public class DirectionUtil { return null; } - public static void setDirection(@Nonnull Block block, BlockFace blockFace) { - if (!XMaterial.supports(13)) return; - if (blockFace != null && block.getBlockData() instanceof org.bukkit.block.data.Directional) { + public static void setDirection(@Nonnull Block block, @Nonnull BlockFace blockFace) { + if (block.getBlockData() instanceof org.bukkit.block.data.Directional) { org.bukkit.block.data.BlockData bd = block.getBlockData(); org.bukkit.block.data.Directional dir = (org.bukkit.block.data.Directional) bd; dir.setFacing(blockFace); @@ -31,7 +27,6 @@ public class DirectionUtil { @Nullable public static String getBisected(@Nonnull Block block) { - if (!XMaterial.supports(13)) return null; if (block.getBlockData() instanceof org.bukkit.block.data.Bisected) { org.bukkit.block.data.Bisected bis = (org.bukkit.block.data.Bisected) block.getBlockData(); return bis.getHalf().toString(); @@ -40,9 +35,7 @@ public class DirectionUtil { } public static void setBisected(@Nonnull Block block, String bisected) { - if (!XMaterial.supports(13)) return; if (bisected != null && block.getBlockData() instanceof org.bukkit.block.data.Bisected) { - org.bukkit.block.data.Bisected.Half half; try { half = org.bukkit.block.data.Bisected.Half.valueOf(bisected); @@ -60,7 +53,6 @@ public class DirectionUtil { @Nullable public static String getSlabType(@Nonnull Block block) { - if (!XMaterial.supports(13)) return null; if (block.getBlockData() instanceof org.bukkit.block.data.type.Slab) { org.bukkit.block.data.type.Slab slab = (org.bukkit.block.data.type.Slab) block.getBlockData(); return slab.getType().toString(); @@ -68,10 +60,8 @@ public class DirectionUtil { return null; } - public static void setSlabType(@Nonnull Block block, String slabtype) { - if (!XMaterial.supports(13)) return; - if (slabtype != null && block.getBlockData() instanceof org.bukkit.block.data.type.Slab) { - + public static void setSlabType(@Nonnull Block block, @Nonnull String slabtype) { + if (block.getBlockData() instanceof org.bukkit.block.data.type.Slab) { org.bukkit.block.data.type.Slab.Type type; try { type = org.bukkit.block.data.type.Slab.Type.valueOf(slabtype); @@ -86,4 +76,20 @@ public class DirectionUtil { block.setBlockData(bd); } } + + @Nullable + public static Boolean isOpen(@Nonnull Block block) { + if (block.getBlockData() instanceof org.bukkit.block.data.Openable) { + org.bukkit.block.data.Openable trapdoor = (org.bukkit.block.data.Openable) block.getBlockData(); + return trapdoor.isOpen(); + } + return null; + } + + public static void setOpen(@Nonnull Block block, boolean state) { + if (block.getBlockData() instanceof org.bukkit.block.data.Openable) { + org.bukkit.block.data.Openable trapdoor = (org.bukkit.block.data.Openable) block.getBlockData(); + trapdoor.setOpen(state); + } + } } diff --git a/src/main/java/tech/sbdevelopment/v10lift/utils/DoorUtil.java b/src/main/java/tech/sbdevelopment/v10lift/utils/DoorUtil.java index abc5e18..0aa11e9 100644 --- a/src/main/java/tech/sbdevelopment/v10lift/utils/DoorUtil.java +++ b/src/main/java/tech/sbdevelopment/v10lift/utils/DoorUtil.java @@ -4,14 +4,12 @@ import com.cryptomorin.xseries.XMaterial; import com.cryptomorin.xseries.XSound; import org.bukkit.Location; import org.bukkit.block.Block; -import org.bukkit.block.BlockState; import org.bukkit.block.data.Bisected; import javax.annotation.Nonnull; /* Openable codes sponsored by MrWouter <3 */ public class DoorUtil { - /** * Open a door, with 1.12.x- and 1.13.x+ support * @@ -23,31 +21,16 @@ public class DoorUtil { if (b.getType().toString().contains("DOOR") && b.getType() != XMaterial.IRON_DOOR.parseMaterial()) XSound.BLOCK_WOODEN_DOOR_OPEN.play(b.getLocation()); if (b.getType().toString().contains("GATE")) XSound.BLOCK_FENCE_GATE_OPEN.play(b.getLocation()); - if (XMaterial.supports(13)) { - //1.13+ - 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); - return true; - } - } else { - //1.12- - 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(); - return true; + + 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); + return true; } return false; } @@ -63,31 +46,16 @@ public class DoorUtil { if (b.getType().toString().contains("DOOR") && b.getType() != XMaterial.IRON_DOOR.parseMaterial()) XSound.BLOCK_WOODEN_DOOR_CLOSE.play(b.getLocation()); if (b.getType().toString().contains("GATE")) XSound.BLOCK_FENCE_GATE_CLOSE.play(b.getLocation()); - if (XMaterial.supports(13)) { - //1.13+ - 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 { - //1.12- - 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; + + 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; } return false; } @@ -98,17 +66,8 @@ public class DoorUtil { * @param b The block * @return true if Openable, false if not */ - public static boolean isOpenable(Block b) { - if (b == null) { - return false; - } - if (XMaterial.supports(13)) { - //1.13+ - return b.getBlockData() instanceof org.bukkit.block.data.Openable; - } else { - //1.12- - return b.getState().getData() instanceof org.bukkit.material.Openable; - } + public static boolean isOpenable(@Nonnull Block b) { + return b.getBlockData() instanceof org.bukkit.block.data.Openable; } /** @@ -120,39 +79,20 @@ public class DoorUtil { public static Location getLowerLocationOfDoor(@Nonnull Block block) { if (!isDoor(block)) return block.getLocation(); - if (XMaterial.supports(13)) { - //1.13+ - org.bukkit.block.data.type.Door door = (org.bukkit.block.data.type.Door) block.getBlockData(); - Location lower; - if (door.getHalf() == Bisected.Half.TOP) { - lower = block.getLocation().subtract(0, 1, 0); - } else { - if (!door.isOpen()) { - lower = block.getLocation().subtract(0, 1, 0); - if (isOpenable(lower.getBlock())) - return lower; - else return block.getLocation(); - } - lower = block.getLocation(); - } - return lower; + org.bukkit.block.data.type.Door door = (org.bukkit.block.data.type.Door) block.getBlockData(); + Location lower; + if (door.getHalf() == Bisected.Half.TOP) { + lower = block.getLocation().subtract(0, 1, 0); } else { - //1.12- - org.bukkit.material.Door door = (org.bukkit.material.Door) block.getState().getData(); - Location lower; - if (door.isTopHalf()) { + if (!door.isOpen()) { lower = block.getLocation().subtract(0, 1, 0); - } else { - if (!door.isOpen()) { - lower = block.getLocation().subtract(0, 1, 0); - if (isOpenable(lower.getBlock())) - return lower; - else return block.getLocation(); - } - lower = block.getLocation(); + if (isOpenable(lower.getBlock())) + return lower; + else return block.getLocation(); } - return lower; + lower = block.getLocation(); } + return lower; } /** @@ -161,17 +101,7 @@ public class DoorUtil { * @param b The block * @return true if a Door, false if not */ - public static boolean isDoor(Block b) { - if (b == null) { - return false; - } - if (XMaterial.supports(13)) { - //1.13+ - return b.getBlockData() instanceof org.bukkit.block.data.type.Door; - } else { - //1.12- - return b.getState().getData() instanceof org.bukkit.material.Door; - } + public static boolean isDoor(@Nonnull Block b) { + return b.getBlockData() instanceof org.bukkit.block.data.type.Door; } - } \ No newline at end of file