Beautified MoveLift (removed duplicate code)

This commit is contained in:
stijnb1234 2021-04-10 16:56:47 +02:00
parent 4f5ef2cee5
commit a1e158eb01
5 changed files with 196 additions and 302 deletions

View file

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>nl.SBDeveloper</groupId> <groupId>nl.SBDeveloper</groupId>
@ -148,7 +149,7 @@
</execution> </execution>
</executions> </executions>
<configuration> <configuration>
<serverId>nexus</serverId> <serverId>iobyte-nexus</serverId>
<nexusUrl>https://nexus.iobyte.nl/</nexusUrl> <nexusUrl>https://nexus.iobyte.nl/</nexusUrl>
<skipStaging>true</skipStaging> <skipStaging>true</skipStaging>
</configuration> </configuration>
@ -241,5 +242,4 @@
<version>1.6.4</version> <version>1.6.4</version>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>

View file

@ -11,6 +11,7 @@ import nl.SBDeveloper.V10Lift.sbutils.LocationSerializer;
import nl.SBDeveloper.V10Lift.utils.ConfigUtil; import nl.SBDeveloper.V10Lift.utils.ConfigUtil;
import nl.SBDeveloper.V10Lift.utils.DirectionUtil; import nl.SBDeveloper.V10Lift.utils.DirectionUtil;
import nl.SBDeveloper.V10Lift.utils.DoorUtil; import nl.SBDeveloper.V10Lift.utils.DoorUtil;
import org.apache.commons.collections4.map.LinkedMap;
import org.bukkit.*; import org.bukkit.*;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
@ -941,7 +942,7 @@ public class V10LiftAPI {
if (liftName == null || queue == null || !DataManager.containsLift(liftName)) return false; if (liftName == null || queue == null || !DataManager.containsLift(liftName)) return false;
Lift lift = DataManager.getLift(liftName); Lift lift = DataManager.getLift(liftName);
lift.setQueue(new LinkedHashMap<>()); lift.setQueue(new LinkedMap<>());
for (Map.Entry<String, Floor> e : queue.entrySet()) { for (Map.Entry<String, Floor> e : queue.entrySet()) {
addToQueue(liftName, e.getValue(), e.getKey()); addToQueue(liftName, e.getValue(), e.getKey());
} }
@ -989,7 +990,7 @@ public class V10LiftAPI {
Lift l = DataManager.getLift(lift); Lift l = DataManager.getLift(lift);
if (l.getQueue() == null) { if (l.getQueue() == null) {
l.setQueue(new LinkedHashMap<>()); l.setQueue(new LinkedMap<>());
} }
if (!l.getQueue().containsValue(floor)) { if (!l.getQueue().containsValue(floor)) {

View file

@ -0,0 +1,5 @@
package nl.SBDeveloper.V10Lift.api.enums;
public enum LiftDirection {
UP, DOWN, STOP
}

View file

@ -14,12 +14,11 @@ public class Lift {
@Getter @Setter private String worldName; @Getter @Setter private String worldName;
@Getter @Setter private int y; @Getter @Setter private int y;
@Getter private HashSet<UUID> owners; @Getter private HashSet<UUID> owners;
//@Getter @Setter private ArrayList<String> whitelist;
@Getter private final TreeSet<LiftBlock> blocks = new TreeSet<>(); @Getter private final TreeSet<LiftBlock> blocks = new TreeSet<>();
@Getter private final LinkedHashMap<String, Floor> floors = new LinkedHashMap<>(); @Getter private final LinkedHashMap<String, Floor> floors = new LinkedHashMap<>();
@Getter private final HashSet<LiftSign> signs = new HashSet<>(); @Getter private final HashSet<LiftSign> signs = new HashSet<>();
@Getter private final HashSet<LiftBlock> inputs = new HashSet<>(); @Getter private final HashSet<LiftBlock> inputs = new HashSet<>();
@Getter private HashSet<LiftBlock> offlineInputs = new HashSet<>(); @Getter private final HashSet<LiftBlock> offlineInputs = new HashSet<>();
@Getter @Setter private LinkedHashMap<String, Floor> queue = null; @Getter @Setter private LinkedHashMap<String, Floor> queue = null;
@Getter private final HashSet<LiftRope> ropes = new HashSet<>(); @Getter private final HashSet<LiftRope> ropes = new HashSet<>();
@Getter private transient final ArrayList<V10Entity> toMove = new ArrayList<>(); @Getter private transient final ArrayList<V10Entity> toMove = new ArrayList<>();

View file

@ -3,6 +3,7 @@ package nl.SBDeveloper.V10Lift.api.runnables;
import com.cryptomorin.xseries.XMaterial; import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XSound; import com.cryptomorin.xseries.XSound;
import nl.SBDeveloper.V10Lift.V10LiftPlugin; import nl.SBDeveloper.V10Lift.V10LiftPlugin;
import nl.SBDeveloper.V10Lift.api.enums.LiftDirection;
import nl.SBDeveloper.V10Lift.api.objects.*; import nl.SBDeveloper.V10Lift.api.objects.*;
import nl.SBDeveloper.V10Lift.managers.AntiCopyBlockManager; import nl.SBDeveloper.V10Lift.managers.AntiCopyBlockManager;
import nl.SBDeveloper.V10Lift.managers.DataManager; import nl.SBDeveloper.V10Lift.managers.DataManager;
@ -20,11 +21,11 @@ import org.bukkit.inventory.ItemStack;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.*; import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Supplier; import java.util.function.Supplier;
/** The MoveLift runnable, used for moving a lift. */ /** The MoveLift runnable, used for moving a lift. */
public class MoveLift implements Runnable { public class MoveLift implements Runnable {
/* Packet teleportation method */ /* Packet teleportation method */
private final Method[] methods = ((Supplier<Method[]>) () -> { private final Method[] methods = ((Supplier<Method[]>) () -> {
try { try {
@ -62,57 +63,44 @@ public class MoveLift implements Runnable {
@Override @Override
public void run() { public void run() {
Iterator<LiftBlock> iter; //Check if lift exists
ArrayList<LiftBlock> tb = new ArrayList<>(); Lift lift = DataManager.getLift(liftName);
Block block = null;
World world;
Location loc;
BlockState bs;
boolean by;
int y;
Chest c;
V10Entity v10ent;
Iterator<V10Entity> veiter;
Sign sign;
LiftBlock lb;
Lift lift;
LiftSign ls;
Inventory inv;
ItemStack is;
ItemStack[] isa;
lift = DataManager.getLift(liftName);
if (lift == null) { if (lift == null) {
stopMe(); stop();
return; return;
} }
//If the queue is NOT empty and the lift is NOT offline
if (lift.getQueue().isEmpty() || lift.isOffline()) { if (lift.getQueue().isEmpty() || lift.isOffline()) {
lift.setQueue(null); lift.setQueue(null);
stopMe(); stop();
return; return;
} }
//If the lift is NOT in edit mode and the lift is NOT defective
if (DataManager.containsEditLift(liftName) || lift.isDefective()) return; if (DataManager.containsEditLift(liftName) || lift.isDefective()) return;
//If the lift is NOT in delay
if (lift.getCounter() > 0) { if (lift.getCounter() > 0) {
lift.setCounter(lift.getCounter() - 1); lift.setCounter(lift.getCounter() - 1);
return; return;
} }
lb = lift.getBlocks().first(); //Check if the chunk of the first block is loaded
world = Bukkit.getWorld(lb.getWorld()); LiftBlock lb = lift.getBlocks().first();
World world = Bukkit.getWorld(lb.getWorld());
if (world == null) { if (world == null) {
lift.setCounter(ft); lift.setCounter(ft);
return; return;
} }
loc = new Location(world, lb.getX(), lb.getY(), lb.getZ()); Location loc = new Location(world, lb.getX(), lb.getY(), lb.getZ());
if (!loc.getChunk().isLoaded()) { if (!loc.getChunk().isLoaded()) {
lift.setCounter(ft); lift.setCounter(ft);
return; return;
} }
//And if the chunk of the last block is loaded
lb = lift.getBlocks().last(); lb = lift.getBlocks().last();
world = Bukkit.getWorld(lb.getWorld()); world = Bukkit.getWorld(lb.getWorld());
if (world == null) { if (world == null) {
@ -126,82 +114,87 @@ public class MoveLift implements Runnable {
return; return;
} }
//Handle malfunction
double changeOfDefect = V10LiftPlugin.getSConfig().getFile().getDouble("DefectRate"); double changeOfDefect = V10LiftPlugin.getSConfig().getFile().getDouble("DefectRate");
if (changeOfDefect > 0.0D) { if (changeOfDefect > 0.0D) {
y = new Random().nextInt(100); double chance = ThreadLocalRandom.current().nextDouble(100);
double chance;
if (y < 100) {
long co = new Random().nextLong();
if (co < 0) co = -co;
chance = Double.parseDouble(y + "." + co);
} else {
chance = y;
}
if (chance < changeOfDefect) { if (chance < changeOfDefect) {
V10LiftPlugin.getAPI().setDefective(liftName, true); V10LiftPlugin.getAPI().setDefective(liftName, true);
return; return;
} }
} }
Iterator<Map.Entry<String, Floor>> quiter = lift.getQueue().entrySet().iterator(); Iterator<Map.Entry<String, Floor>> queueIterator = lift.getQueue().entrySet().iterator();
Map.Entry<String, Floor> floor = quiter.next(); Map.Entry<String, Floor> floor = queueIterator.next();
Floor to = floor.getValue(); String floorName = floor.getKey();
String fl = floor.getKey(); Floor floorTo = floor.getValue();
boolean up = false;
boolean down = false; LiftDirection direction;
if (lift.getY() < to.getY()) { if (lift.getY() < floorTo.getY()) {
up = true; direction = LiftDirection.UP;
} else if (lift.getY() > to.getY()) { } else if (lift.getY() > floorTo.getY()) {
down = true; direction = LiftDirection.DOWN;
} else {
direction = LiftDirection.STOP;
} }
if (up) { List<LiftBlock> antiCopyBlocks = new ArrayList<>();
if (direction == LiftDirection.UP || direction == LiftDirection.DOWN) {
if (!V10LiftPlugin.getAPI().closeDoor(liftName)) return; if (!V10LiftPlugin.getAPI().closeDoor(liftName)) return;
//MOVE ROPES if (direction == LiftDirection.UP) {
for (LiftRope rope : lift.getRopes()) { //MOVE ROPES
if (rope.getCurrently() > rope.getMaxY()) { for (LiftRope rope : lift.getRopes()) {
Bukkit.getLogger().info("[V10Lift] Lift " + liftName + " reaches the upper rope end but won't stop!! 1"); if (rope.getCurrently() > rope.getMaxY()) {
V10LiftPlugin.getAPI().setDefective(liftName, true); Bukkit.getLogger().info("[V10Lift] Lift " + liftName + " reaches the upper rope end but won't stop!!");
lift.getToMove().clear();
quiter.remove();
return;
}
world = Objects.requireNonNull(Bukkit.getWorld(rope.getWorld()), "World is null at MoveLift");
block = world.getBlockAt(rope.getX(), rope.getCurrently(), rope.getZ());
block.setType(Material.AIR);
rope.setCurrently(rope.getCurrently() + 1);
}
iter = lift.getBlocks().iterator(); V10LiftPlugin.getAPI().setDefective(liftName, true);
while (iter.hasNext()) { lift.getToMove().clear();
lb = iter.next(); queueIterator.remove();
if (AntiCopyBlockManager.isAntiCopy(lb.getMat())) { return;
tb.add(lb); }
iter.remove();
block = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at MoveLift").getBlockAt(lb.getX(), lb.getY(), lb.getZ()); Block currentRopeBlock = Bukkit.getWorld(rope.getWorld()).getBlockAt(rope.getX(), rope.getCurrently(), rope.getZ());
block.setType(Material.AIR); currentRopeBlock.setType(Material.AIR);
lb.setY(lb.getY() + 1);
rope.setCurrently(rope.getCurrently() + 1);
} }
} }
for (LiftBlock lib : lift.getBlocks().descendingSet()) { Iterator<LiftBlock> blockIterator = lift.getBlocks().iterator();
world = Objects.requireNonNull(Bukkit.getWorld(lib.getWorld()), "World is null at MoveLift"); while (blockIterator.hasNext()) {
block = world.getBlockAt(lib.getX(), lib.getY(), lib.getZ()); LiftBlock liftBlock = blockIterator.next();
if (AntiCopyBlockManager.isAntiCopy(liftBlock.getMat())) {
antiCopyBlocks.add(liftBlock);
blockIterator.remove();
Block antiCopyBlock = Bukkit.getWorld(liftBlock.getWorld()).getBlockAt(liftBlock.getX(), liftBlock.getY(), liftBlock.getZ());
antiCopyBlock.setType(Material.AIR);
liftBlock.setY(direction == LiftDirection.UP ? liftBlock.getY() + 1 : liftBlock.getY() - 1);
}
}
Set<LiftBlock> set = direction == LiftDirection.UP ? lift.getBlocks().descendingSet() : lift.getBlocks();
for (LiftBlock lib : set) {
Block block = Bukkit.getWorld(lib.getWorld()).getBlockAt(lib.getX(), lib.getY(), lib.getZ());
if ((lib.getMat() == Material.CHEST || lib.getMat() == Material.TRAPPED_CHEST) && lib.serializedItemStacks == null) { if ((lib.getMat() == Material.CHEST || lib.getMat() == Material.TRAPPED_CHEST) && lib.serializedItemStacks == null) {
c = (Chest) block.getState(); Chest c = (Chest) block.getState();
inv = c.getInventory(); Inventory inv = c.getInventory();
isa = inv.getContents(); ItemStack[] invContents = inv.getContents();
by = false; boolean by = false;
lib.serializedItemStacks = new Map[isa.length];
for (int i = 0; i < isa.length; i++) { lib.serializedItemStacks = new Map[invContents.length];
is = isa[i]; for (int i = 0; i < invContents.length; i++) {
ItemStack is = invContents[i];
if (is != null) { if (is != null) {
by = true; by = true;
lib.serializedItemStacks[i] = is.serialize(); lib.serializedItemStacks[i] = is.serialize();
} }
} }
if (by) { if (by) {
inv.clear(); inv.clear();
c.update(); c.update();
@ -211,84 +204,86 @@ public class MoveLift implements Runnable {
} }
block.setType(Material.AIR); block.setType(Material.AIR);
lib.setY(lib.getY() + 1);
block = Objects.requireNonNull(Bukkit.getWorld(lib.getWorld()), "World is null at MoveLift").getBlockAt(lib.getX(), lib.getY(), lib.getZ()); lib.setY(direction == LiftDirection.UP ? lib.getY() + 1 : lib.getY() - 1);
BlockState state = block.getState(); Block nextBlock = Bukkit.getWorld(lib.getWorld()).getBlockAt(lib.getX(), lib.getY(), lib.getZ());
if (lib.getMat() == null) lib.setMat(Material.AIR); if (lib.getMat() == null) lib.setMat(Material.AIR);
BlockState state = nextBlock.getState();
state.setType(lib.getMat()); state.setType(lib.getMat());
if (!XMaterial.isNewVersion()) { if (!XMaterial.isNewVersion()) {
state.setRawData(lib.getData()); state.setRawData(lib.getData());
} }
state.update(true); state.update(true);
if (XMaterial.isNewVersion()) {
DirectionUtil.setDirection(block, lib.getFace());
DirectionUtil.setBisected(block, lib.getBisected());
DirectionUtil.setSlabType(block, lib.getSlabtype());
}
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()) {
v10ent = new V10Entity(ent.getUniqueId(), null, 0, 0, 0, 0);
if (lift.getToMove().contains(v10ent)) continue;
loc = ent.getLocation();
y = loc.getBlockY();
if (y == lib.getY()) {
by = true;
} else if (y + 1 == lib.getY()) {
by = true;
y++;
} else {
by = false;
}
if (by && loc.getBlockX() == lib.getX() && loc.getBlockZ() == lib.getZ()) { if (XMaterial.isNewVersion()) {
loc.setY(loc.getY() + 1); DirectionUtil.setDirection(nextBlock, lib.getFace());
if (V10LiftPlugin.getSConfig().getFile().getBoolean("PacketTeleport")) { DirectionUtil.setBisected(nextBlock, lib.getBisected());
try { DirectionUtil.setSlabType(nextBlock, lib.getSlabtype());
methods[1].invoke(methods[0].invoke(ent), loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch()); }
} catch (Exception ex) {
Bukkit.getLogger().severe("[V10Lift] PacketTeleportation is enabled, but couldn't get the method."); 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);
if (V10LiftPlugin.getSConfig().getFile().getBoolean("PacketTeleport")) {
try {
methods[1].invoke(methods[0].invoke(ent), entLoc.getX(), entLoc.getY(), entLoc.getZ(), entLoc.getYaw(), entLoc.getPitch());
} catch (Exception ex) {
Bukkit.getLogger().severe("[V10Lift] PacketTeleportation is enabled, but couldn't get the method.");
}
} else {
ent.teleport(entLoc);
} }
} else {
ent.teleport(loc);
} }
} }
} }
} }
veiter = lift.getToMove().iterator();
while (veiter.hasNext()) { Iterator<V10Entity> toMoveIterator = lift.getToMove().iterator();
v10ent = veiter.next(); while (toMoveIterator.hasNext()) {
V10Entity v10ent = toMoveIterator.next();
if (v10ent.getStep() > 0) { if (v10ent.getStep() > 0) {
v10ent.moveUp(); if (direction == LiftDirection.UP) v10ent.moveUp(); else v10ent.moveDown();
if (v10ent.getStep() > 16) { if (v10ent.getStep() > 16) {
veiter.remove(); toMoveIterator.remove();
} }
} }
v10ent.setStep((short) (v10ent.getStep() + 1)); v10ent.setStep((short) (v10ent.getStep() + 1));
} }
for (LiftBlock lib : tb) {
block = Objects.requireNonNull(Bukkit.getWorld(lib.getWorld()), "World is null at MoveLift").getBlockAt(lib.getX(), lib.getY(), lib.getZ()); for (LiftBlock lib : antiCopyBlocks) {
BlockState state = block.getState(); Block block = Bukkit.getWorld(lib.getWorld()).getBlockAt(lib.getX(), lib.getY(), lib.getZ());
if (lib.getMat() == null) lib.setMat(Material.AIR); if (lib.getMat() == null) lib.setMat(Material.AIR);
BlockState state = block.getState();
state.setType(lib.getMat()); state.setType(lib.getMat());
if (!XMaterial.isNewVersion()) { if (!XMaterial.isNewVersion()) {
state.setRawData(lib.getData()); state.setRawData(lib.getData());
} }
state.update(true); state.update(true);
if (XMaterial.isNewVersion()) { if (XMaterial.isNewVersion()) {
DirectionUtil.setDirection(block, lib.getFace()); DirectionUtil.setDirection(block, lib.getFace());
DirectionUtil.setBisected(block, lib.getBisected()); DirectionUtil.setBisected(block, lib.getBisected());
DirectionUtil.setSlabType(block, lib.getSlabtype()); DirectionUtil.setSlabType(block, lib.getSlabtype());
} }
lift.getBlocks().add(lib); lift.getBlocks().add(lib);
if (lib.getSignLines() != null) { if (lib.getSignLines() != null) {
bs = block.getState(); BlockState bs = block.getState();
if (bs instanceof Sign) { if (bs instanceof Sign) {
sign = (Sign) bs; Sign sign = (Sign) bs;
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
sign.setLine(i, lib.getSignLines()[i]); sign.setLine(i, lib.getSignLines()[i]);
if (i == 0 && lib.getSignLines()[i].equalsIgnoreCase(ConfigUtil.getConfigText("SignText")) && lib.getSignLines()[1].equals(liftName)) { if (i == 0 && lib.getSignLines()[i].equalsIgnoreCase(ConfigUtil.getConfigText("SignText")) && lib.getSignLines()[1].equals(liftName)) {
sign.setLine(1, liftName); sign.setLine(1, liftName);
sign.setLine(3, ChatColor.GOLD + fl); sign.setLine(3, ChatColor.GOLD + floorName);
} }
} }
sign.update(); sign.update();
@ -296,155 +291,55 @@ public class MoveLift implements Runnable {
} }
} }
lift.setY(lift.getY() + 1); lift.setY(direction == LiftDirection.UP ? lift.getY() + 1 : lift.getY() - 1);
Iterator<LiftSign> liter = lift.getSigns().iterator();
while (liter.hasNext()) { int signState = direction == LiftDirection.UP ? 1 : 2;
ls = liter.next();
if (ls.getState() == 1) continue; Iterator<LiftSign> signIterator = lift.getSigns().iterator();
block = Objects.requireNonNull(Bukkit.getWorld(ls.getWorld()), "World is null at MoveLift").getBlockAt(ls.getX(), ls.getY(), ls.getZ()); while (signIterator.hasNext()) {
bs = block.getState(); LiftSign ls = signIterator.next();
if (ls.getState() == signState) continue;
Block block = Bukkit.getWorld(ls.getWorld()).getBlockAt(ls.getX(), ls.getY(), ls.getZ());
BlockState bs = block.getState();
if (!(bs instanceof Sign)) { if (!(bs instanceof Sign)) {
Bukkit.getLogger().severe("[V10Lift] Wrong sign removed at: " + LocationSerializer.serialize(block.getLocation())); Bukkit.getLogger().severe("[V10Lift] Wrong sign removed at: " + LocationSerializer.serialize(block.getLocation()));
liter.remove(); signIterator.remove();
continue; continue;
} }
sign = (Sign) bs;
Sign sign = (Sign) bs;
if (ls.getType() == 0) { if (ls.getType() == 0) {
sign.setLine(3, ConfigUtil.getConfigText("UpText")); String text = direction == LiftDirection.UP ? ConfigUtil.getConfigText("UpText") : ConfigUtil.getConfigText("DownText");
sign.setLine(3, text);
} else { } else {
sign.setLine(3, ChatColor.GRAY + ChatColor.stripColor(sign.getLine(3))); sign.setLine(3, ChatColor.GRAY + ChatColor.stripColor(sign.getLine(3)));
} }
sign.update(); sign.update();
ls.setState((byte) 1);
}
} else if (down) {
if (!V10LiftPlugin.getAPI().closeDoor(liftName)) return;
iter = lift.getBlocks().iterator(); ls.setState((byte) signState);
while (iter.hasNext()) {
lb = iter.next();
if (AntiCopyBlockManager.isAntiCopy(lb.getMat())) {
tb.add(lb);
iter.remove();
block = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at MoveLift").getBlockAt(lb.getX(), lb.getY(), lb.getZ());
block.setType(Material.AIR);
lb.setY(lb.getY() - 1);
}
} }
for (LiftBlock lib : lift.getBlocks()) { if (direction == LiftDirection.DOWN) { //TODO Fix for down
block = Objects.requireNonNull(Bukkit.getWorld(lib.getWorld()), "World is null at MoveLift").getBlockAt(lib.getX(), lib.getY(), lib.getZ()); //MOVE ROPES
if ((lib.getMat() == Material.CHEST || lib.getMat() == Material.TRAPPED_CHEST) && lib.serializedItemStacks == null) { for (LiftRope rope : lift.getRopes()) {
c = (Chest) block.getState(); boolean stopAfter = false;
inv = c.getInventory();
isa = inv.getContents();
by = false;
lib.serializedItemStacks = new Map[isa.length];
for (int i = 0; i < isa.length; i++) {
is = isa[i];
if (is != null) {
by = true;
lib.serializedItemStacks[i] = is.serialize();
}
}
if (by) {
inv.clear();
c.update();
} else {
lib.serializedItemStacks = null;
}
}
block.setType(Material.AIR);
lib.setY(lib.getY() - 1);
y = lib.getY();
block = world.getBlockAt(lib.getX(), lib.getY(), lib.getZ());
BlockState state = block.getState();
if (lib.getMat() == null) lib.setMat(Material.AIR);
state.setType(lib.getMat());
if (!XMaterial.isNewVersion()) {
state.setRawData(lib.getData());
}
state.update(true);
if (XMaterial.isNewVersion()) {
DirectionUtil.setDirection(block, lib.getFace());
DirectionUtil.setBisected(block, lib.getBisected());
DirectionUtil.setSlabType(block, lib.getSlabtype());
}
}
veiter = lift.getToMove().iterator();
while (veiter.hasNext()) {
v10ent = veiter.next();
if (v10ent.getStep() > 0) {
v10ent.moveDown();
if (v10ent.getStep() > 16) {
veiter.remove();
}
}
v10ent.setStep((short) (v10ent.getStep() + 1));
}
for (LiftBlock lib : tb) {
block = Objects.requireNonNull(Bukkit.getWorld(lib.getWorld()), "World is null at MoveLift").getBlockAt(lib.getX(), lib.getY(), lib.getZ());
BlockState state = block.getState();
if (lib.getMat() == null) lib.setMat(Material.AIR);
state.setType(lib.getMat());
if (!XMaterial.isNewVersion()) {
state.setRawData(lib.getData());
}
state.update(true);
if (XMaterial.isNewVersion()) {
DirectionUtil.setDirection(block, lib.getFace());
DirectionUtil.setBisected(block, lib.getBisected());
DirectionUtil.setSlabType(block, lib.getSlabtype());
}
lift.getBlocks().add(lib);
if (lib.getSignLines() != null) {
bs = block.getState();
if (bs instanceof Sign) {
sign = (Sign) bs;
for (int i = 0; i < 3; i++) {
sign.setLine(i, lib.getSignLines()[i]);
if (i == 0 && lib.getSignLines()[i].equalsIgnoreCase(ConfigUtil.getConfigText("SignText")) && lib.getSignLines()[1].equals(liftName)) {
sign.setLine(1, liftName);
sign.setLine(3, ChatColor.GOLD + fl);
}
}
sign.update();
}
}
}
lift.setY(lift.getY() - 1);
Iterator<LiftSign> liter = lift.getSigns().iterator();
while (liter.hasNext()) {
ls = liter.next();
if (ls.getState() == 2) continue;
block = Objects.requireNonNull(Bukkit.getWorld(ls.getWorld()), "World is null at MoveLift").getBlockAt(ls.getX(), ls.getY(), ls.getZ());
bs = block.getState();
if (!(bs instanceof Sign)) {
Bukkit.getLogger().severe("[V10Lift] Wrong sign removed at: " + LocationSerializer.serialize(block.getLocation()));
liter.remove();
continue;
}
sign = (Sign) bs;
if (ls.getType() == 0) {
sign.setLine(3, ConfigUtil.getConfigText("DownText"));
} else {
sign.setLine(3, ChatColor.GRAY + ChatColor.stripColor(sign.getLine(3)));
}
sign.update();
ls.setState((byte) 2);
}
//MOVE ROPES if (rope.getCurrently() < rope.getMinY()) {
for (LiftRope rope : lift.getRopes()) { Bukkit.getLogger().info("[V10Lift] Lift " + liftName + " reaches the upper rope end but won't stop!!");
V10LiftPlugin.getAPI().setDefective(liftName, true);
lift.getToMove().clear();
queueIterator.remove();
stopAfter = true;
}
if (rope.getCurrently() < rope.getMinY()) {
Bukkit.getLogger().info("[V10Lift] Lift " + liftName + " reaches the upper rope end but won't stop!! 2");
V10LiftPlugin.getAPI().setDefective(liftName, true);
lift.getToMove().clear();
quiter.remove();
rope.setCurrently(rope.getCurrently() - 1); rope.setCurrently(rope.getCurrently() - 1);
block = world.getBlockAt(rope.getX(), rope.getCurrently(), rope.getZ());
Block block = Bukkit.getWorld(rope.getWorld()).getBlockAt(rope.getX(), rope.getCurrently(), rope.getZ());
if (rope.getType() == null) rope.setType(Material.AIR); if (rope.getType() == null) rope.setType(Material.AIR);
block.setType(rope.getType()); block.setType(rope.getType());
if (XMaterial.isNewVersion()) { if (XMaterial.isNewVersion()) {
DirectionUtil.setDirection(block, rope.getFace()); DirectionUtil.setDirection(block, rope.getFace());
@ -455,71 +350,67 @@ public class MoveLift implements Runnable {
state.setData(ladder); state.setData(ladder);
state.update(true); state.update(true);
} }
return;
} if (stopAfter) return;
world = Objects.requireNonNull(Bukkit.getWorld(rope.getWorld()), "World is null at MoveLift");
rope.setCurrently(rope.getCurrently() - 1);
block = world.getBlockAt(rope.getX(), rope.getCurrently(), rope.getZ());
if (rope.getType() == null) rope.setType(Material.AIR);
block.setType(rope.getType());
if (XMaterial.isNewVersion()) {
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);
} }
} }
} else { } else {
lift.getToMove().clear(); lift.getToMove().clear();
quiter.remove(); queueIterator.remove();
bs = null;
for (LiftBlock lib : lift.getBlocks()) { for (LiftBlock lib : lift.getBlocks()) {
bs = Objects.requireNonNull(Bukkit.getWorld(lib.getWorld()), "World is null at MoveLift").getBlockAt(lib.getX(), lib.getY(), lib.getZ()).getState(); BlockState bs = Bukkit.getWorld(lib.getWorld()).getBlockAt(lib.getX(), lib.getY(), lib.getZ()).getState();
if (!(bs instanceof Sign)) { if (!(bs instanceof Sign)) {
if (bs instanceof Chest && lib.serializedItemStacks != null) { if (bs instanceof Chest && lib.serializedItemStacks != null) {
isa = new ItemStack[lib.serializedItemStacks.length]; ItemStack[] isa = new ItemStack[lib.serializedItemStacks.length];
by = false; boolean by = false;
for (int i = 0; i < lib.serializedItemStacks.length; i++) { for (int i = 0; i < lib.serializedItemStacks.length; i++) {
if (lib.serializedItemStacks[i] != null) { if (lib.serializedItemStacks[i] != null) {
isa[i] = ItemStack.deserialize(lib.serializedItemStacks[i]); isa[i] = ItemStack.deserialize(lib.serializedItemStacks[i]);
by = true; by = true;
} }
} }
if (by) { if (by) {
c = (Chest) bs; Chest c = (Chest) bs;
c.getInventory().setContents(isa); c.getInventory().setContents(isa);
c.update(); c.update();
} }
lib.serializedItemStacks = null; lib.serializedItemStacks = null;
} }
continue; continue;
} }
sign = (Sign) bs;
Sign sign = (Sign) bs;
if (!sign.getLine(0).equalsIgnoreCase(ConfigUtil.getConfigText("SignText"))) continue; if (!sign.getLine(0).equalsIgnoreCase(ConfigUtil.getConfigText("SignText"))) continue;
sign.setLine(1, liftName); sign.setLine(1, liftName);
sign.setLine(3, ChatColor.GREEN + fl); sign.setLine(3, ChatColor.GREEN + floorName);
sign.update(); sign.update();
} }
Iterator<LiftSign> liter = lift.getSigns().iterator();
while (liter.hasNext()) { Block block = null;
ls = liter.next();
Iterator<LiftSign> signIterator = lift.getSigns().iterator();
while (signIterator.hasNext()) {
LiftSign ls = signIterator.next();
if (ls.getState() == 0) continue; if (ls.getState() == 0) continue;
block = Objects.requireNonNull(Bukkit.getWorld(ls.getWorld()), "World is null at MoveLift").getBlockAt(ls.getX(), ls.getY(), ls.getZ());
bs = block.getState(); block = Bukkit.getWorld(ls.getWorld()).getBlockAt(ls.getX(), ls.getY(), ls.getZ());
BlockState bs = block.getState();
if (!(bs instanceof Sign)) { if (!(bs instanceof Sign)) {
Bukkit.getLogger().severe("[V10Lift] Wrong sign removed at: " + LocationSerializer.serialize(block.getLocation())); Bukkit.getLogger().severe("[V10Lift] Wrong sign removed at: " + LocationSerializer.serialize(block.getLocation()));
liter.remove(); signIterator.remove();
continue; continue;
} }
sign = (Sign) bs;
Sign sign = (Sign) bs;
if (ls.getType() == 0) { if (ls.getType() == 0) {
sign.setLine(3, ChatColor.GREEN + fl); sign.setLine(3, ChatColor.GREEN + floorName);
} else { } else {
String l3 = ChatColor.stripColor(sign.getLine(3)); String l3 = ChatColor.stripColor(sign.getLine(3));
if (!fl.equals(l3)) { if (!floorName.equals(l3)) {
sign.setLine(3, ChatColor.GRAY + l3); sign.setLine(3, ChatColor.GRAY + l3);
} else { } else {
sign.setLine(3, ChatColor.GREEN + l3); sign.setLine(3, ChatColor.GREEN + l3);
@ -528,18 +419,16 @@ public class MoveLift implements Runnable {
sign.update(); sign.update();
ls.setState((byte) 0); ls.setState((byte) 0);
} }
V10LiftPlugin.getAPI().openDoor(lift, liftName, to);
V10LiftPlugin.getAPI().openDoor(lift, liftName, floorTo);
if (lift.isRealistic()) lift.setCounter(ft); if (lift.isRealistic()) lift.setCounter(ft);
if (lift.isSound()) {
if (block != null) { if (lift.isSound() && block != null) XSound.ENTITY_EXPERIENCE_ORB_PICKUP.play(block.getLocation(), 2.0F, 63.0F);
loc = block.getLocation();
XSound.ENTITY_EXPERIENCE_ORB_PICKUP.play(loc, 2.0F, 63.0F);
}
}
} }
} }
private void stopMe() { private void stop() {
Bukkit.getServer().getScheduler().cancelTask(DataManager.getMovingTask(liftName)); Bukkit.getServer().getScheduler().cancelTask(DataManager.getMovingTask(liftName));
DataManager.removeMovingTask(liftName); DataManager.removeMovingTask(liftName);
} }