From e5f31385d53b79defc5016559e5dd3c882227e39 Mon Sep 17 00:00:00 2001 From: Stijn Bannink Date: Sun, 20 Aug 2023 20:20:23 +0200 Subject: [PATCH] Started with API overhaul, still WIP --- .../sbdevelopment/v10lift/V10LiftPlugin.java | 6 + .../sbdevelopment/v10lift/api/V10LiftAPI.java | 861 +----------------- .../v10lift/api/lists/LiftQueue.java | 45 + .../v10lift/api/objects/Floor.java | 2 + .../v10lift/api/objects/Lift.java | 700 +++++++++++++- .../v10lift/api/runnables/DoorCloser.java | 11 +- .../v10lift/managers/DataManager.java | 22 - 7 files changed, 785 insertions(+), 862 deletions(-) create mode 100644 src/main/java/tech/sbdevelopment/v10lift/api/lists/LiftQueue.java diff --git a/src/main/java/tech/sbdevelopment/v10lift/V10LiftPlugin.java b/src/main/java/tech/sbdevelopment/v10lift/V10LiftPlugin.java index 48a6321..298c71e 100644 --- a/src/main/java/tech/sbdevelopment/v10lift/V10LiftPlugin.java +++ b/src/main/java/tech/sbdevelopment/v10lift/V10LiftPlugin.java @@ -5,6 +5,7 @@ import org.bstats.bukkit.Metrics; import org.bstats.charts.SingleLineChart; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; +import tech.sbdevelopment.v10lift.api.V10LiftAPI; import tech.sbdevelopment.v10lift.commands.V10LiftCommand; import tech.sbdevelopment.v10lift.commands.V10LiftTabCompleter; import tech.sbdevelopment.v10lift.listeners.BlockBreakListener; @@ -23,6 +24,8 @@ import java.util.Collections; public class V10LiftPlugin extends JavaPlugin { @Getter private static V10LiftPlugin instance; + @Getter + private static V10LiftAPI api; private static YamlFile config; private static DBManager dbManager; @Getter @@ -36,6 +39,9 @@ public class V10LiftPlugin extends JavaPlugin { public void onEnable() { instance = this; + //Load the API + api = new V10LiftAPI(); + //Load the config config = new YamlFile("config"); config.loadDefaults(); diff --git a/src/main/java/tech/sbdevelopment/v10lift/api/V10LiftAPI.java b/src/main/java/tech/sbdevelopment/v10lift/api/V10LiftAPI.java index b09ecd4..da1c8f2 100644 --- a/src/main/java/tech/sbdevelopment/v10lift/api/V10LiftAPI.java +++ b/src/main/java/tech/sbdevelopment/v10lift/api/V10LiftAPI.java @@ -1,24 +1,16 @@ package tech.sbdevelopment.v10lift.api; -import com.cryptomorin.xseries.XMaterial; -import org.bukkit.*; +import lombok.Getter; +import org.bukkit.Bukkit; +import org.bukkit.Location; import org.bukkit.block.Block; -import org.bukkit.block.BlockFace; import org.bukkit.block.BlockState; import org.bukkit.block.Sign; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import tech.sbdevelopment.v10lift.V10LiftPlugin; -import tech.sbdevelopment.v10lift.api.objects.*; -import tech.sbdevelopment.v10lift.api.runnables.DoorCloser; -import tech.sbdevelopment.v10lift.api.runnables.MoveLift; +import tech.sbdevelopment.v10lift.api.objects.Lift; +import tech.sbdevelopment.v10lift.api.objects.LiftSign; 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.BlockStateUtil; -import tech.sbdevelopment.v10lift.utils.DoorUtil; import javax.annotation.Nonnull; import java.util.*; @@ -27,41 +19,8 @@ import java.util.*; * The Main API class, for all the API methods */ public class V10LiftAPI { - private static V10LiftAPI instance; - - private V10LiftAPI() { - } - - public static V10LiftAPI getInstance() { - if (instance == null) instance = new V10LiftAPI(); - return instance; - } - - /* Private API methods */ - private void sortFloors(@Nonnull Lift lift) { - ArrayList> as = new ArrayList<>(lift.getFloors().entrySet()); - as.sort(Comparator.comparingInt(o -> o.getValue().getY())); - Iterator> iter = as.iterator(); - lift.getFloors().clear(); - Map.Entry e; - while (iter.hasNext()) { - e = iter.next(); - lift.getFloors().put(e.getKey(), e.getValue()); - } - } - - private void startLift(String liftName) { - if (!DataManager.containsMovingTask(liftName)) { - Lift lift = DataManager.getLift(liftName); - DataManager.addMovingTask(liftName, Bukkit.getScheduler().scheduleSyncRepeatingTask(V10LiftPlugin.getInstance(), new MoveLift(liftName, lift.getSpeed()), lift.getSpeed(), lift.getSpeed())); - } - } - - /* API methods */ - - /* - @todo Fix creating lifts in adventure not working - */ + @Getter + private static final List lifts = new ArrayList<>(); /** * Create a new Lift @@ -70,10 +29,10 @@ public class V10LiftAPI { * @param liftName The name of the lift * @return true if created, false if null or already exists */ - public boolean createLift(Player p, String liftName) { - if (p == null || liftName == null || DataManager.containsLift(liftName)) return false; + public boolean createLift(@Nonnull Player p, @Nonnull String liftName) { + if (isLift(liftName)) return false; - DataManager.addLift(liftName, new Lift(p.getUniqueId(), V10LiftPlugin.getSConfig().getFile().getInt("DefaultSpeed"), V10LiftPlugin.getSConfig().getFile().getBoolean("DefaultRealistic"))); + lifts.add(new Lift(p.getUniqueId(), V10LiftPlugin.getSConfig().getFile().getInt("DefaultSpeed"), V10LiftPlugin.getSConfig().getFile().getBoolean("DefaultRealistic"))); return true; } @@ -83,8 +42,9 @@ public class V10LiftAPI { * @param liftName The name of the lift * @return true if removed, false if null or doesn't exists */ - public boolean removeLift(String liftName) { - if (liftName == null || !DataManager.containsLift(liftName)) return false; + public boolean deleteLift(@Nonnull String liftName) { + Optional liftOpt = getLift(liftName); + if (liftOpt.isEmpty()) return false; Iterator> iter = DataManager.getEditors().entrySet().iterator(); HashSet activeEdits = new HashSet<>(); @@ -112,27 +72,27 @@ public class V10LiftAPI { DataManager.removeMovingTask(liftName); } - DataManager.removeLift(liftName); + lifts.remove(liftOpt.get()); V10LiftPlugin.getDBManager().remove(liftName); return true; } + public boolean isLift(@Nonnull String name) { + return lifts.stream().anyMatch(lift -> lift.getName().equalsIgnoreCase(name)); + } + + public Optional getLift(@Nonnull String name) { + return lifts.stream().filter(lift -> lift.getName().equalsIgnoreCase(name)).findFirst(); + } + /** * Get the name of a lift by a location (checking for cab blocks) * * @param loc The location you want to check for * @return The liftname */ - public String getLiftByLocation(Location loc) { - for (Map.Entry entry : DataManager.getLifts().entrySet()) { - for (LiftBlock block : entry.getValue().getBlocks()) { - //Check for world, x and z - if (block.getWorld().equals(Objects.requireNonNull(loc.getWorld(), "World is null at getLiftByLocation").getName()) && block.getX() == loc.getBlockX() && block.getZ() == loc.getBlockZ()) { - return entry.getKey(); - } - } - } - return null; + public Optional getLift(@Nonnull Location loc) { + return lifts.stream().filter(lift -> lift.getBlocks().stream().anyMatch(block -> block.getWorld().equals(loc.getWorld().getName()) && block.getX() == loc.getBlockX() && block.getZ() == loc.getBlockZ())).findFirst(); } /** @@ -141,13 +101,12 @@ public class V10LiftAPI { * @param liftName The name of the lift * @param newName The new name of the lift */ - public void renameLift(String liftName, String newName) { - if (liftName == null || newName == null || !DataManager.containsLift(liftName)) return; + public boolean renameLift(@Nonnull String liftName, @Nonnull String newName) { + Optional liftOpt = getLift(liftName); + if (liftOpt.isEmpty() || isLift(newName)) return false; - Lift lift = DataManager.getLift(liftName); - DataManager.removeLift(liftName); - DataManager.addLift(newName, lift); - for (LiftSign ls : lift.getSigns()) { + liftOpt.get().setName(newName); + for (LiftSign ls : liftOpt.get().getSigns()) { Block block = Objects.requireNonNull(Bukkit.getWorld(ls.getWorld()), "World is null at renameLift").getBlockAt(ls.getX(), ls.getY(), ls.getZ()); BlockState bs = block.getState(); if (!(bs instanceof Sign)) continue; @@ -155,557 +114,9 @@ public class V10LiftAPI { si.setLine(1, newName); si.update(); } - } - - /** - * Add a block to a lift - * Use {@link V10LiftAPI#sortLiftBlocks(String liftName)} after! - * - * @param liftName The name of the lift - * @param block The block - * @return 0 if added, -1 if null or doesn't exists, -2 if forbidden, -3 if already added - */ - public int addBlockToLift(String liftName, Block block) { - if (liftName == null || block == null || !DataManager.containsLift(liftName)) return -1; - Lift lift = DataManager.getLift(liftName); - return addBlockToLift(lift.getBlocks(), block); - } - - /** - * Add a block to a lift - * Use {@link V10LiftAPI#sortLiftBlocks(String liftName)} after! - * - * @param blocks The blockset - * @param block The block - * @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) { - return addBlockToLift(blocks, new LiftBlock(block)); - } - - /** - * Add a block to a lift - * Use {@link V10LiftAPI#sortLiftBlocks(String liftName)} after! - * - * @param blocks The blockset - * @param block The LiftBlock - * @return 0 if added, -2 if forbidden, -3 if already added - */ - public int addBlockToLift(@Nonnull Set blocks, @Nonnull LiftBlock block) { - if (ForbiddenBlockManager.isForbidden(block.getMat())) return -2; - if (blocks.contains(block)) return -3; - blocks.add(block); - return 0; - } - - /** - * Remove a block from a lift - * Use {@link V10LiftAPI#sortLiftBlocks(String liftName)} after! - * - * @param liftName The name of the lift - * @param block The block - * @return 0 if removed, -1 if null or doesn't exists, -2 if not added - */ - public int removeBlockFromLift(String liftName, Block block) { - if (liftName == null || block == null || !DataManager.containsLift(liftName)) return -1; - Lift lift = DataManager.getLift(liftName); - LiftBlock lb = new LiftBlock(block); - if (!lift.getBlocks().contains(lb)) return -2; - lift.getBlocks().remove(lb); - return 0; - } - - /** - * Switch a block at a lift - * Use {@link V10LiftAPI#sortLiftBlocks(String liftName)} after! - * - * @param liftName The name of the lift - * @param block The block - * @return 0 if added, 1 if removed, -1 if null or doesn't exists, -2 if not added - */ - public int switchBlockAtLift(String liftName, Block block) { - if (liftName == null || block == null || !DataManager.containsLift(liftName)) return -1; - return switchBlockAtLift(DataManager.getLift(liftName).getBlocks(), block); - } - - /** - * Switch a block at a lift - * Use {@link V10LiftAPI#sortLiftBlocks(String liftName)} after! - * - * @param blocks The blockset - * @param block The block - * @return 0 if added, 1 if removed, -1 if null or doesn't exists, -2 if not added - */ - public int switchBlockAtLift(TreeSet blocks, Block block) { - if (blocks == null || block == null) return -1; - if (ForbiddenBlockManager.isForbidden(block.getType())) return -2; - LiftBlock lb = new LiftBlock(block); - if (blocks.contains(lb)) { - blocks.remove(lb); - return 1; - } - blocks.add(lb); - return 0; - } - - /** - * Sort the blocks of a lift. - * Use this after they have been modified. - * - * @param liftName The name of the lift - */ - public void sortLiftBlocks(String liftName) { - if (liftName != null && DataManager.containsLift(liftName)) { - Lift lift = DataManager.getLift(liftName); - if (lift.getWorldName() == null) lift.setWorldName(lift.getBlocks().first().getWorld()); - World world = Bukkit.getWorld(lift.getWorldName()); - if (world == null) return; - lift.setY(world.getMaxHeight()); - for (LiftBlock lb : lift.getBlocks()) { - if (lb.getY() < lift.getY()) { - lift.setY(lb.getY()); - lift.setWorldName(lb.getWorld()); - } - } - } - } - - /** - * Open the door - * - * @param liftName The name of the lift - * @return true/false - */ - public boolean openDoor(String liftName) { - if (liftName == null || !DataManager.containsLift(liftName)) return false; - - Lift lift = DataManager.getLift(liftName); - - if (lift.getQueue() != null) return false; - - Floor f = null; - for (Floor fl : lift.getFloors().values()) { - if (fl.getY() == lift.getY() && fl.getWorld().equals(lift.getWorldName())) { - f = fl; - break; - } - } - - if (f == null) return false; - - if (lift.getDoorOpen() != null && !closeDoor(liftName)) return false; - - for (LiftBlock lb : f.getDoorBlocks()) { - Block block = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at openDoor").getBlockAt(lb.getX(), lb.getY(), lb.getZ()); - block.setType(Material.AIR); - } - - for (LiftBlock lb : f.getRealDoorBlocks()) { - Block block = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at openDoor").getBlockAt(lb.getX(), lb.getY(), lb.getZ()); - DoorUtil.openDoor(block); - } - - lift.setDoorOpen(f); - - if (lift.isRealistic()) lift.setDoorCloser(new DoorCloser(liftName)); return true; } - /** - * Open the door - * - * @param lift The lift - * @param liftName The name of the lift - * @param f The floor - * @return true/false - */ - public boolean openDoor(Lift lift, String liftName, Floor f) { - if (lift == null || liftName == null || f == null) return false; - if (lift.getDoorOpen() != null && !closeDoor(liftName)) return false; - - for (LiftBlock lb : f.getDoorBlocks()) { - Block block = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at openDoor").getBlockAt(lb.getX(), lb.getY(), lb.getZ()); - block.setType(Material.AIR); - } - - for (LiftBlock lb : f.getRealDoorBlocks()) { - Block block = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at openDoor").getBlockAt(lb.getX(), lb.getY(), lb.getZ()); - DoorUtil.openDoor(block); - } - - lift.setDoorOpen(f); - - if (lift.isRealistic()) lift.setDoorCloser(new DoorCloser(liftName)); - return true; - } - - /** - * Close a lift door - * - * @param liftName The name of the lift - * @return true if door was closed, false if else. - */ - public boolean closeDoor(String liftName) { - if (liftName == null || !DataManager.containsLift(liftName)) return false; - - Lift lift = DataManager.getLift(liftName); - - boolean blocked = false; - if (lift.getDoorOpen() == null) { - return true; - } - - if (lift.isRealistic()) { - for (LiftBlock lb : lift.getDoorOpen().getDoorBlocks()) { - Block block = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at closeDoor").getBlockAt(lb.getX(), lb.getY(), lb.getZ()); - for (Entity ent : block.getChunk().getEntities()) { - Location loc = ent.getLocation(); - if (loc.getBlockX() == lb.getX() && loc.getBlockY() == lb.getY() && loc.getBlockZ() == lb.getZ()) { - blocked = true; - break; - } - } - if (blocked) break; - } - - for (LiftBlock lb : lift.getDoorOpen().getRealDoorBlocks()) { - Block block = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at closeDoor").getBlockAt(lb.getX(), lb.getY(), lb.getZ()); - for (Entity ent : block.getChunk().getEntities()) { - Location loc = ent.getLocation(); - - if (loc.getBlockX() == lb.getX() && loc.getBlockY() == lb.getY() && loc.getBlockZ() == lb.getZ()) { - blocked = true; - break; - } - } - if (blocked) break; - } - } - - if (!blocked) { - for (LiftBlock lb : lift.getDoorOpen().getDoorBlocks()) { - 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()); - state.update(true); - } - - for (LiftBlock lb : lift.getDoorOpen().getRealDoorBlocks()) { - Block block = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at closeDoor").getBlockAt(lb.getX(), lb.getY(), lb.getZ()); - DoorUtil.closeDoor(block); - } - - lift.setDoorOpen(null); - if (lift.getDoorCloser() != null) lift.getDoorCloser().stop(); - } - - return !blocked; - } - - /** - * To check if a lift has an open door. - * - * @param liftName The name of the lift - * @return true if open, false if else - */ - public boolean hasDoorOpen(String liftName) { - return (liftName != null && DataManager.containsLift(liftName)) && DataManager.getLift(liftName).getDoorOpen() != null; - } - - /** - * Adds a new floor to a lift - * - * @param liftName The name of the lift - * @param floorName The name of the floor - * @param floor The floor object - * @return 0 if added, -1 if null or doesn't exists, -2 if height is to high, -3 if floor already exists - */ - public int addFloor(String liftName, String floorName, Floor floor) { - if (liftName == null || floorName == null || floor == null || !DataManager.containsLift(liftName) || floor.getWorld() == null) - return -1; - if (floor.getY() > Objects.requireNonNull(Bukkit.getServer().getWorld(floor.getWorld()), "World is null at addNewFloor!").getMaxHeight()) - return -2; - if (floorName.length() > 13) floorName = floorName.substring(0, 13).trim(); - Lift lift = DataManager.getLift(liftName); - if (lift.getFloors().containsKey(floorName) || lift.getFloors().containsValue(floor)) return -3; - - lift.getFloors().put(floorName, floor); - sortFloors(lift); - return 0; - } - - /** - * Removes a floor from a lift - * - * @param liftName The name of the lift - * @param floorName The name of the floor - * @return true if removed, false if null or doesn't exists - */ - public boolean removeFloor(String liftName, String floorName) { - if (liftName == null || floorName == null || !DataManager.containsLift(liftName)) return false; - Lift lift = DataManager.getLift(liftName); - if (!lift.getFloors().containsKey(floorName)) return false; - - lift.getFloors().remove(floorName); - lift.getInputs().removeIf(liftBlock -> liftBlock.getFloor().equals(floorName)); - return true; - } - - /** - * Rename a floor from a lift - * - * @param liftName The name of the lift - * @param oldName The old name of the floor - * @param newName The new name of the floor - * @return 0 if renamed, -1 if null or doesn't exists, -2 if floor doesn't exists, -3 if floor already exists - */ - public int renameFloor(String liftName, String oldName, String newName) { - if (liftName == null || oldName == null || newName == null || !DataManager.containsLift(liftName)) return -1; - Lift lift = DataManager.getLift(liftName); - if (!lift.getFloors().containsKey(oldName)) return -2; - if (newName.length() > 13) newName = newName.substring(0, 13).trim(); - if (lift.getFloors().containsKey(newName)) return -3; - - Floor f = lift.getFloors().get(oldName); - lift.getFloors().remove(oldName); - lift.getFloors().put(newName, f); - sortFloors(lift); - Iterator liter = lift.getInputs().iterator(); - LiftInput lb; - ArrayList newBlocks = new ArrayList<>(); - while (liter.hasNext()) { - lb = liter.next(); - if (lb.getFloor().equals(oldName)) { - liter.remove(); - newBlocks.add(new LiftInput(lb.getWorld(), lb.getX(), lb.getY(), lb.getZ(), newName)); - } - } - newBlocks.forEach(nlb -> lift.getInputs().add(nlb)); - return 0; - } - - /** - * Check if a lift is defective - * - * @param liftName The name of the lift - * @return true/false - */ - public boolean isDefective(String liftName) { - if (liftName == null || !DataManager.containsLift(liftName)) return false; - return DataManager.getLift(liftName).isDefective(); - } - - /** - * Set a lift to (not) defective - * - * @param liftName The name of the lift - * @param state true/false - * @return 0 if set, -1 if null or doesn't exists, -2 if same state, -3 if no signs, -4 if wrong sign - */ - public int setDefective(String liftName, boolean state) { - if (liftName == null || !DataManager.containsLift(liftName)) return -1; - Lift lift = DataManager.getLift(liftName); - boolean oldState = lift.isDefective(); - if (oldState == state) return -2; - lift.setDefective(state); - if (state) { - //SET DEFECTIVE - //Update sign - for (LiftSign ls : lift.getSigns()) { - Block block = Objects.requireNonNull(Bukkit.getWorld(ls.getWorld()), "World is null at setDefective").getBlockAt(ls.getX(), ls.getY(), ls.getZ()); - BlockState bs = block.getState(); - if (!(bs instanceof Sign)) { - Bukkit.getLogger().severe("[V10Lift] Wrong sign removed at: " + LocationSerializer.serialize(block.getLocation())); - return -4; - } - - Sign s = (Sign) bs; - ls.setOldText(s.getLine(3)); - s.setLine(3, ConfigUtil.getConfigText("DefectText")); - s.update(); - } - - //Update all cab signs - for (LiftBlock lb : lift.getBlocks()) { - BlockState bs = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at setDefective").getBlockAt(lb.getX(), lb.getY(), lb.getZ()).getState(); - if (!(bs instanceof Sign)) continue; - - Sign s = (Sign) bs; - lift.setSignText(s.getLine(3)); - s.setLine(3, ConfigUtil.getConfigText("DefectText")); - s.update(); - } - } else { - - //Update sign - for (LiftSign ls : lift.getSigns()) { - Block block = Objects.requireNonNull(Bukkit.getWorld(ls.getWorld()), "World is null at setDefective").getBlockAt(ls.getX(), ls.getY(), ls.getZ()); - BlockState bs = block.getState(); - if (!(bs instanceof Sign)) { - Bukkit.getLogger().severe("[V10Lift] Wrong sign removed at: " + LocationSerializer.serialize(block.getLocation())); - return -4; - } - - Sign s = (Sign) bs; - s.setLine(3, ls.getOldText()); - ls.setOldText(null); - s.update(); - } - - //Update all cab signs - for (LiftBlock lb : lift.getBlocks()) { - BlockState bs = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at setDefective").getBlockAt(lb.getX(), lb.getY(), lb.getZ()).getState(); - if (!(bs instanceof Sign)) continue; - - Sign s = (Sign) bs; - s.setLine(3, lift.getSignText()); - s.update(); - } - lift.setSignText(null); - } - return 0; - } - - /** - * Get the userWhitelist of a lift - * - * @param liftName The name of the lift - * @param floorName The name of the floor - * @return list with UUIDs of the players - */ - public HashSet getUserWhitelist(String liftName, String floorName) { - HashSet ret = new HashSet<>(); - if (liftName != null && floorName != null && DataManager.containsLift(liftName)) { - Lift lift = DataManager.getLift(liftName); - if (lift.getFloors().containsKey(floorName)) { - ret = lift.getFloors().get(floorName).getUserWhitelist(); - } - } - return ret; - } - - /** - * Get the groupWhitelist of a lift - * - * @param liftName The name of the lift - * @param floorName The name of the floor - * @return list with groupnames - */ - public HashSet getGroupWhitelist(String liftName, String floorName) { - HashSet ret = new HashSet<>(); - if (liftName != null && floorName != null && DataManager.containsLift(liftName)) { - Lift lift = DataManager.getLift(liftName); - if (lift.getFloors().containsKey(floorName)) { - ret = lift.getFloors().get(floorName).getGroupWhitelist(); - } - } - return ret; - } - - /** - * Check if a lift is offline - * - * @param liftName The name of the lift - * @return true/false - */ - public boolean isOffline(String liftName) { - if (liftName == null || !DataManager.containsLift(liftName)) return false; - return DataManager.getLift(liftName).isOffline(); - } - - /** - * Set a lift to (not) offline - * - * @param liftName The name of the lift - * @param state true/false - * @return 0 if set, -1 if null or doesn't exists, -2 if same state - */ - public int setOffline(String liftName, boolean state) { - if (liftName == null || !DataManager.containsLift(liftName)) return -1; - Lift lift = DataManager.getLift(liftName); - boolean oldState = lift.isOffline(); - if (oldState == state) return -2; - lift.setOffline(state); - Iterator liter = lift.getSigns().iterator(); - BlockState bs; - Sign sign; - if (state) { - for (LiftBlock lb : lift.getBlocks()) { - bs = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at setOffline").getBlockAt(lb.getX(), lb.getY(), lb.getZ()).getState(); - if (!(bs instanceof Sign)) continue; - sign = (Sign) bs; - if (!sign.getLine(0).equalsIgnoreCase(ConfigUtil.getConfigText("SignText"))) continue; - sign.setLine(3, ConfigUtil.getConfigText("DisabledText")); - sign.update(); - } - - while (liter.hasNext()) { - LiftSign ls = liter.next(); - bs = Objects.requireNonNull(Bukkit.getWorld(ls.getWorld()), "World is null at setOffline").getBlockAt(ls.getX(), ls.getY(), ls.getZ()).getState(); - if (!(bs instanceof Sign)) { - Bukkit.getLogger().severe("[V10Lift] Wrong sign removed at: " + LocationSerializer.serialize(bs.getBlock().getLocation())); - liter.remove(); - continue; - } - sign = (Sign) bs; - ls.setOldText(sign.getLine(3)); - sign.setLine(3, ConfigUtil.getConfigText("DisabledText")); - sign.update(); - } - } else { - for (LiftBlock lb : lift.getBlocks()) { - bs = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at setOffline").getBlockAt(lb.getX(), lb.getY(), lb.getZ()).getState(); - if (!(bs instanceof Sign)) continue; - sign = (Sign) bs; - if (!sign.getLine(0).equalsIgnoreCase(ConfigUtil.getConfigText("SignText"))) continue; - sign.setLine(3, ""); - sign.update(); - } - - while (liter.hasNext()) { - LiftSign ls = liter.next(); - bs = Objects.requireNonNull(Bukkit.getWorld(ls.getWorld()), "World is null at setOffline").getBlockAt(ls.getX(), ls.getY(), ls.getZ()).getState(); - if (!(bs instanceof Sign)) { - Bukkit.getLogger().severe("[V10Lift] Wrong sign removed at: " + LocationSerializer.serialize(bs.getBlock().getLocation())); - liter.remove(); - continue; - } - sign = (Sign) bs; - sign.setLine(3, ls.getOldText()); - sign.update(); - ls.setOldText(null); - } - } - return 0; - } - - /** - * Check if a lift contains the block as rope - * - * @param liftName The name of the lift - * @param block The block - * @return true/false - */ - public boolean containsRope(String liftName, Block block) { - if (liftName == null || block == null || !DataManager.containsLift(liftName)) return false; - - Lift lift = DataManager.getLift(liftName); - if (lift.getRopes().isEmpty()) return false; - - String world = block.getWorld().getName(); - int x = block.getX(); - int y = block.getY(); - int z = block.getZ(); - - for (LiftRope rope : lift.getRopes()) { - if (x != rope.getX() || z != rope.getZ()) continue; - if (y >= rope.getMinY() && y <= rope.getMaxY()) { - return true; - } - } - return false; - } - /** * Check if a block is a rope * @@ -713,218 +124,6 @@ public class V10LiftAPI { * @return true/false */ public boolean isRope(Block b) { - for (String lift : DataManager.getLifts().keySet()) { - if (containsRope(lift, b)) return true; - } - return false; - } - - /** - * Send info about a lift to a player - * - * @param sender Where you want to send it to - * @param liftName The name of the lift - */ - public void sendLiftInfo(CommandSender sender, String liftName) { - sendLiftInfo(sender, liftName, DataManager.getLift(liftName)); - } - - /** - * Send info about a lift to a player - * - * @param ent Where you want to send it to - * @param liftName The name of the lift - * @param lift The lift - */ - public void sendLiftInfo(@Nonnull CommandSender ent, String liftName, @Nonnull Lift lift) { - - ent.sendMessage(ChatColor.GOLD + "Elevator: " + ChatColor.YELLOW + liftName); - ent.sendMessage(ChatColor.GOLD + "Settings:"); - ent.sendMessage(ChatColor.GREEN + " Speed: " + ChatColor.YELLOW + lift.getSpeed()); - ent.sendMessage(ChatColor.GREEN + " Realistic Mode: " + ChatColor.YELLOW + lift.isRealistic()); - ent.sendMessage(ChatColor.GREEN + " Malfunction: " + ChatColor.YELLOW + lift.isDefective()); - ent.sendMessage(ChatColor.GOLD + "Floors:"); - if (lift.getFloors().isEmpty()) { - ent.sendMessage(ChatColor.RED + "None."); - } else { - for (Map.Entry entry : lift.getFloors().entrySet()) { - ent.sendMessage(ChatColor.GREEN + " " + entry.getKey() + ":"); - Floor f = entry.getValue(); - ent.sendMessage(ChatColor.YELLOW + " World: " + ChatColor.GREEN + f.getWorld()); - ent.sendMessage(ChatColor.YELLOW + " Height: " + ChatColor.GREEN + f.getY()); - ent.sendMessage(ChatColor.YELLOW + " Whitelist:"); - if (f.getUserWhitelist().isEmpty() && f.getGroupWhitelist().isEmpty()) { - ent.sendMessage(ChatColor.GOLD + " None."); - } else { - ChatColor color = ChatColor.DARK_PURPLE; - Iterator iter = f.getUserWhitelist().iterator(); - Iterator iter2 = f.getGroupWhitelist().iterator(); - StringBuilder sb = new StringBuilder(); - sb.append(" ").append(color).append(Bukkit.getOfflinePlayer(iter.next()).getName()); - while (iter.hasNext()) { - if (color == ChatColor.DARK_PURPLE) { - color = ChatColor.LIGHT_PURPLE; - } else { - color = ChatColor.DARK_PURPLE; - } - sb.append(ChatColor.AQUA).append(", ").append(color).append(Bukkit.getOfflinePlayer(iter.next()).getName()); - } - while (iter2.hasNext()) { - if (color == ChatColor.DARK_PURPLE) { - color = ChatColor.LIGHT_PURPLE; - } else { - color = ChatColor.DARK_PURPLE; - } - sb.append(ChatColor.AQUA).append(", ").append(color).append("Group: ").append(iter2.next()); - } - ent.sendMessage(sb.toString()); - } - } - } - } - - /** - * Add a rope to a lift - * - * @param lift The name of the lift - * @param world The world - * @param x The x-pos - * @param minY The min y-pos - * @param maxY The max y-pos - * @param z The z-pos - * @return 0 if added, -1 if null or doesn't exists, -2 if not same mat, -3 if already a rope, -4 if forbidden material - */ - public int addRope(String lift, World world, int x, int minY, int maxY, int z) { - if (lift == null || !DataManager.containsLift(lift) || world == null) return -1; - - //minY = maxY, so reverse - if (minY > maxY) { - int tempY = minY; - minY = maxY; - maxY = tempY; - } - - Block block = world.getBlockAt(x, minY, z); - if (isRope(block)) return -3; - Material mat = block.getType(); - if (ForbiddenBlockManager.isForbidden(mat)) return -4; - - for (int i = minY + 1; i <= maxY; i++) { - block = world.getBlockAt(x, i, z); - if (isRope(block)) return -3; - if (block.getType() != mat) return -2; - } - - LiftRope rope = new LiftRope(block, minY, maxY); - DataManager.getLift(lift).getRopes().add(rope); - - return 0; - } - - /** - * Remove a rope from a lift - * - * @param lift The name of the lift - * @param block The block - * @return true/false - */ - public boolean removeRope(String lift, Block block) { - if (lift == null || block == null || !DataManager.containsLift(lift) || !containsRope(lift, block)) - return false; - - String world = block.getWorld().getName(); - int x = block.getX(); - int y = block.getY(); - int z = block.getZ(); - Iterator riter = DataManager.getLift(lift).getRopes().iterator(); - while (riter.hasNext()) { - LiftRope rope = riter.next(); - if (x != rope.getX() || z != rope.getZ()) continue; - if (world.equals(rope.getWorld())) { - if (y >= rope.getMinY() && y <= rope.getMaxY()) { - riter.remove(); - return true; - } - } - } - return false; - } - - /** - * Set the queue of a lift - * - * @param liftName The name of the lift - * @param queue The queue - * @return true/false - */ - public boolean setQueue(String liftName, LinkedHashMap queue) { - if (liftName == null || queue == null || !DataManager.containsLift(liftName)) return false; - - Lift lift = DataManager.getLift(liftName); - lift.setQueue(new LinkedHashMap<>()); - for (Map.Entry e : queue.entrySet()) { - addToQueue(liftName, e.getValue(), e.getKey()); - } - return true; - } - - /** - * Add a location to the queue - * - * @param lift The name of the lift - * @param y The y-pos - * @param world The world - * @return true/false - */ - public boolean addToQueue(String lift, int y, World world) { - return addToQueue(lift, y, world, null); - } - - /** - * Add a location to the queue - * - * @param lift The name of the lift - * @param y The y-pos - * @param world The world - * @param floorName The name of the flor - * @return true/false - */ - public boolean addToQueue(String lift, int y, @Nonnull World world, String floorName) { - return addToQueue(lift, new Floor(y, world.getName()), floorName); - } - - /** - * Add a location to the queue - * - * @param lift The name of the lift - * @param floor The {@link Floor} - * @param floorName The name of the flor - * @return true/false - */ - public boolean addToQueue(String lift, Floor floor, String floorName) { - if (lift == null || floor == null || !DataManager.containsLift(lift)) return false; - - Lift l = DataManager.getLift(lift); - if (l.getQueue() == null) { - l.setQueue(new LinkedHashMap<>()); - } - - if (!l.getQueue().containsValue(floor)) { - if (floorName == null) { - floorName = ChatColor.MAGIC + "-----"; - for (Map.Entry e : l.getFloors().entrySet()) { - if (e.getValue().equals(floor)) { - floorName = e.getKey(); - floor = e.getValue(); - break; - } - } - } - - l.getQueue().put(floorName, floor); - startLift(lift); - return true; - } - return false; + return getLifts().stream().anyMatch(lift -> lift.containsRope(b)); } } diff --git a/src/main/java/tech/sbdevelopment/v10lift/api/lists/LiftQueue.java b/src/main/java/tech/sbdevelopment/v10lift/api/lists/LiftQueue.java new file mode 100644 index 0000000..3e91c43 --- /dev/null +++ b/src/main/java/tech/sbdevelopment/v10lift/api/lists/LiftQueue.java @@ -0,0 +1,45 @@ +package tech.sbdevelopment.v10lift.api.lists; + +import lombok.Setter; +import tech.sbdevelopment.v10lift.api.objects.Floor; + +import java.util.LinkedHashMap; +import java.util.Map; + +public class LiftQueue { + private final LinkedHashMap queue = new LinkedHashMap<>(); + + @Setter + private LiftQueueListener listener; + + public interface LiftQueueListener { + void onQueueChange(); + } + + public void requestFloor(String floorName, Floor floor) { + if (!queue.containsKey(floorName)) { + queue.put(floorName, floor); + + if (listener != null) { + listener.onQueueChange(); + } + } + } + + public boolean hasRequests() { + return !queue.isEmpty(); + } + + public String getNextFloor(String currentFloor, boolean movingUp) { + if (!hasRequests()) return null; + + String nextFloor = currentFloor; + for (Map.Entry entry : queue.entrySet()) { + if ((movingUp && entry.getValue().getY() > entry.getValue().getY()) || (!movingUp && entry.getValue().getY() < entry.getValue().getY())) { + nextFloor = entry.getKey(); + } + } + + return nextFloor; + } +} diff --git a/src/main/java/tech/sbdevelopment/v10lift/api/objects/Floor.java b/src/main/java/tech/sbdevelopment/v10lift/api/objects/Floor.java index 38ad25c..cb55a41 100644 --- a/src/main/java/tech/sbdevelopment/v10lift/api/objects/Floor.java +++ b/src/main/java/tech/sbdevelopment/v10lift/api/objects/Floor.java @@ -5,6 +5,7 @@ import lombok.NoArgsConstructor; import lombok.Setter; import lombok.ToString; +import javax.annotation.Nonnull; import java.util.ArrayList; import java.util.HashSet; import java.util.UUID; @@ -17,6 +18,7 @@ import java.util.UUID; @NoArgsConstructor @ToString public class Floor { + @Nonnull private String world; private int y; private ArrayList doorBlocks = new ArrayList<>(); 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 e1d3874..e7e6963 100644 --- a/src/main/java/tech/sbdevelopment/v10lift/api/objects/Lift.java +++ b/src/main/java/tech/sbdevelopment/v10lift/api/objects/Lift.java @@ -4,8 +4,21 @@ import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; import lombok.ToString; +import org.bukkit.*; +import org.bukkit.block.Block; +import org.bukkit.block.BlockState; +import org.bukkit.block.Sign; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Entity; +import tech.sbdevelopment.v10lift.V10LiftPlugin; +import tech.sbdevelopment.v10lift.api.lists.LiftQueue; import tech.sbdevelopment.v10lift.api.runnables.DoorCloser; +import tech.sbdevelopment.v10lift.managers.ForbiddenBlockManager; +import tech.sbdevelopment.v10lift.sbutils.LocationSerializer; +import tech.sbdevelopment.v10lift.utils.ConfigUtil; +import tech.sbdevelopment.v10lift.utils.DoorUtil; +import javax.annotation.Nonnull; import java.util.*; /** @@ -15,6 +28,8 @@ import java.util.*; @NoArgsConstructor @ToString public class Lift { + @Setter + private String name; @Setter private String worldName; @Setter @@ -25,18 +40,15 @@ public class Lift { private final HashSet signs = new HashSet<>(); private final HashSet inputs = new HashSet<>(); private final HashSet offlineInputs = new HashSet<>(); - @Setter - private LinkedHashMap queue = null; + private final LiftQueue queue = new LiftQueue(); private final HashSet ropes = new HashSet<>(); @Setter private int speed; @Setter private boolean realistic; - @Setter private boolean offline = false; @Setter private boolean sound = true; - @Setter private boolean defective = false; @Setter private String signText = null; @@ -74,4 +86,684 @@ public class Lift { this.speed = speed; this.realistic = realistic; } + + /** + * Add a block to a lift + * Use {@link Lift#sortLiftBlocks()} after! + * + * @param block The block + * @return 0 if added, -2 if forbidden, -3 if already added + */ + public int addBlock(@Nonnull Block block) { + return addBlock(getBlocks(), block); + } + + /** + * Add a block to a lift + * Use {@link Lift#sortLiftBlocks()} after! + * + * @param blocks The blockset + * @param block The block + * @return 0 if added, -2 if forbidden, -3 if already added + */ + public int addBlock(@Nonnull Set blocks, @Nonnull Block block) { + return addBlock(blocks, new LiftBlock(block)); + } + + /** + * Add a block to a lift + * Use {@link Lift#sortLiftBlocks()} after! + * + * @param blocks The blockset + * @param block The LiftBlock + * @return 0 if added, -2 if forbidden, -3 if already added + */ + public int addBlock(@Nonnull Set blocks, @Nonnull LiftBlock block) { + if (ForbiddenBlockManager.isForbidden(block.getMat())) return -2; + if (blocks.contains(block)) return -3; + blocks.add(block); + return 0; + } + + /** + * Remove a block from a lift + * Use {@link Lift#sortLiftBlocks()} after! + * + * @param block The block + * @return true if removed, false if doesn't exists + */ + public boolean removeBlock(@Nonnull Block block) { + LiftBlock lb = new LiftBlock(block); + if (!getBlocks().contains(lb)) return false; + getBlocks().remove(lb); + return true; + } + + /** + * Switch a block at a lift + * Use {@link Lift#sortLiftBlocks()} after! + * + * @param block The block + * @return 0 if added, 1 if removed, -2 if not added + */ + public int switchBlock(@Nonnull Block block) { + return switchBlock(getBlocks(), block); + } + + /** + * Switch a block at a lift + * Use {@link Lift#sortLiftBlocks()} after! + * + * @param blocks The blockset + * @param block The block + * @return 0 if added, 1 if removed, -2 if not added + */ + public int switchBlock(@Nonnull TreeSet blocks, @Nonnull Block block) { + if (ForbiddenBlockManager.isForbidden(block.getType())) return -2; + LiftBlock lb = new LiftBlock(block); + if (blocks.contains(lb)) { + blocks.remove(lb); + return 1; + } + blocks.add(lb); + return 0; + } + + /** + * Sort the blocks of a lift. + * Use this after they have been modified. + */ + public void sortLiftBlocks() { + if (getWorldName() == null) setWorldName(getBlocks().first().getWorld()); + World world = Bukkit.getWorld(getWorldName()); + if (world == null) return; + setY(world.getMaxHeight()); + for (LiftBlock lb : getBlocks()) { + if (lb.getY() < getY()) { + setY(lb.getY()); + setWorldName(lb.getWorld()); + } + } + } + + /** + * Open the door + * + * @return true/false + */ + public boolean openDoor() { + if (getQueue() != null) return false; + + Floor f = null; + for (Floor fl : getFloors().values()) { + if (fl.getY() == getY() && fl.getWorld().equals(getWorldName())) { + f = fl; + break; + } + } + + if (f == null) return false; + + if (getDoorOpen() != null && !closeDoor()) return false; + + for (LiftBlock lb : f.getDoorBlocks()) { + Block block = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at openDoor").getBlockAt(lb.getX(), lb.getY(), lb.getZ()); + block.setType(Material.AIR); + } + + for (LiftBlock lb : f.getRealDoorBlocks()) { + Block block = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at openDoor").getBlockAt(lb.getX(), lb.getY(), lb.getZ()); + DoorUtil.openDoor(block); + } + + setDoorOpen(f); + + if (isRealistic()) setDoorCloser(new DoorCloser(this)); + return true; + } + + /** + * Open the door + * + * @param f The floor + * @return true/false + */ + public boolean openDoor(@Nonnull Floor f) { + if (getDoorOpen() != null && !closeDoor()) return false; + + for (LiftBlock lb : f.getDoorBlocks()) { + Block block = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at openDoor").getBlockAt(lb.getX(), lb.getY(), lb.getZ()); + block.setType(Material.AIR); + } + + for (LiftBlock lb : f.getRealDoorBlocks()) { + Block block = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at openDoor").getBlockAt(lb.getX(), lb.getY(), lb.getZ()); + DoorUtil.openDoor(block); + } + + setDoorOpen(f); + + if (isRealistic()) setDoorCloser(new DoorCloser(this)); + return true; + } + + /** + * Close a lift door + * + * @return true if door was closed, false if else. + */ + public boolean closeDoor() { + boolean blocked = false; + if (getDoorOpen() == null) { + return true; + } + + if (isRealistic()) { + for (LiftBlock lb : getDoorOpen().getDoorBlocks()) { + Block block = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at closeDoor").getBlockAt(lb.getX(), lb.getY(), lb.getZ()); + for (Entity ent : block.getChunk().getEntities()) { + Location loc = ent.getLocation(); + if (loc.getBlockX() == lb.getX() && loc.getBlockY() == lb.getY() && loc.getBlockZ() == lb.getZ()) { + blocked = true; + break; + } + } + if (blocked) break; + } + + for (LiftBlock lb : getDoorOpen().getRealDoorBlocks()) { + Block block = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at closeDoor").getBlockAt(lb.getX(), lb.getY(), lb.getZ()); + for (Entity ent : block.getChunk().getEntities()) { + Location loc = ent.getLocation(); + + if (loc.getBlockX() == lb.getX() && loc.getBlockY() == lb.getY() && loc.getBlockZ() == lb.getZ()) { + blocked = true; + break; + } + } + if (blocked) break; + } + } + + if (!blocked) { + for (LiftBlock lb : getDoorOpen().getDoorBlocks()) { + 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()); + state.update(true); + } + + for (LiftBlock lb : getDoorOpen().getRealDoorBlocks()) { + Block block = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at closeDoor").getBlockAt(lb.getX(), lb.getY(), lb.getZ()); + DoorUtil.closeDoor(block); + } + + setDoorOpen(null); + if (getDoorCloser() != null) getDoorCloser().stop(); + } + + return !blocked; + } + + /** + * To check if a lift has an open door. + * + * @return true if open, false if else + */ + public boolean hasDoorOpen() { + return getDoorOpen() != null; + } + + private void sortFloors() { + ArrayList> as = new ArrayList<>(getFloors().entrySet()); + as.sort(Comparator.comparingInt(o -> o.getValue().getY())); + Iterator> iter = as.iterator(); + getFloors().clear(); + Map.Entry e; + while (iter.hasNext()) { + e = iter.next(); + getFloors().put(e.getKey(), e.getValue()); + } + } + + /** + * Adds a new floor to a lift + * + * @param floorName The name of the floor + * @param floor The floor object + * @return 0 if added, -1 if world doesn't exist, -2 if height is too high, -3 if floor already exists + */ + public int addFloor(@Nonnull String floorName, @Nonnull Floor floor) { + if (Bukkit.getWorld(floor.getWorld()) == null) return -1; + if (floor.getY() > Bukkit.getServer().getWorld(floor.getWorld()).getMaxHeight()) + return -2; + if (floorName.length() > 13) floorName = floorName.substring(0, 13).trim(); + if (getFloors().containsKey(floorName) || getFloors().containsValue(floor)) return -3; + + getFloors().put(floorName, floor); + sortFloors(); + return 0; + } + + /** + * Removes a floor from a lift + * + * @param floorName The name of the floor + * @return true if removed, false if null or doesn't exists + */ + public boolean removeFloor(@Nonnull String floorName) { + if (!getFloors().containsKey(floorName)) return false; + + getFloors().remove(floorName); + getInputs().removeIf(liftBlock -> liftBlock.getFloor().equals(floorName)); + return true; + } + + /** + * Rename a floor from a lift + * + * @param oldName The old name of the floor + * @param newName The new name of the floor + * @return 0 if renamed, -2 if floor doesn't exists, -3 if floor already exists + */ + public int renameFloor(@Nonnull String oldName, @Nonnull String newName) { + if (!getFloors().containsKey(oldName)) return -2; + if (newName.length() > 13) newName = newName.substring(0, 13).trim(); + if (getFloors().containsKey(newName)) return -3; + + Floor f = getFloors().get(oldName); + getFloors().remove(oldName); + getFloors().put(newName, f); + sortFloors(); + Iterator liter = getInputs().iterator(); + LiftInput lb; + ArrayList newBlocks = new ArrayList<>(); + while (liter.hasNext()) { + lb = liter.next(); + if (lb.getFloor().equals(oldName)) { + liter.remove(); + newBlocks.add(new LiftInput(lb.getWorld(), lb.getX(), lb.getY(), lb.getZ(), newName)); + } + } + newBlocks.forEach(nlb -> getInputs().add(nlb)); + return 0; + } + + /** + * Set a lift to (not) defective + * + * @param state true/false + * @return 0 if set, -2 if same state, -3 if no signs, -4 if wrong sign + */ + public int setDefective(boolean state) { + if (defective == state) return -2; + defective = state; + + if (state) { + //SET DEFECTIVE + //Update sign + for (LiftSign ls : getSigns()) { + Block block = Objects.requireNonNull(Bukkit.getWorld(ls.getWorld()), "World is null at setDefective").getBlockAt(ls.getX(), ls.getY(), ls.getZ()); + BlockState bs = block.getState(); + if (!(bs instanceof Sign)) { + Bukkit.getLogger().severe("[V10Lift] Wrong sign removed at: " + LocationSerializer.serialize(block.getLocation())); + return -4; + } + + Sign s = (Sign) bs; + ls.setOldText(s.getLine(3)); + s.setLine(3, ConfigUtil.getConfigText("DefectText")); + s.update(); + } + + //Update all cab signs + for (LiftBlock lb : getBlocks()) { + BlockState bs = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at setDefective").getBlockAt(lb.getX(), lb.getY(), lb.getZ()).getState(); + if (!(bs instanceof Sign)) continue; + + Sign s = (Sign) bs; + setSignText(s.getLine(3)); + s.setLine(3, ConfigUtil.getConfigText("DefectText")); + s.update(); + } + } else { + + //Update sign + for (LiftSign ls : getSigns()) { + Block block = Objects.requireNonNull(Bukkit.getWorld(ls.getWorld()), "World is null at setDefective").getBlockAt(ls.getX(), ls.getY(), ls.getZ()); + BlockState bs = block.getState(); + if (!(bs instanceof Sign)) { + Bukkit.getLogger().severe("[V10Lift] Wrong sign removed at: " + LocationSerializer.serialize(block.getLocation())); + return -4; + } + + Sign s = (Sign) bs; + s.setLine(3, ls.getOldText()); + ls.setOldText(null); + s.update(); + } + + //Update all cab signs + for (LiftBlock lb : getBlocks()) { + BlockState bs = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at setDefective").getBlockAt(lb.getX(), lb.getY(), lb.getZ()).getState(); + if (!(bs instanceof Sign)) continue; + + Sign s = (Sign) bs; + s.setLine(3, getSignText()); + s.update(); + } + setSignText(null); + } + return 0; + } + + /** + * Get the userWhitelist of a lift + * + * @param floorName The name of the floor + * @return set with UUIDs of the players + */ + public HashSet getUserWhitelist(@Nonnull String floorName) { + HashSet ret = new HashSet<>(); + if (getFloors().containsKey(floorName)) { + ret = getFloors().get(floorName).getUserWhitelist(); + } + return ret; + } + + /** + * Get the groupWhitelist of a lift + * + * @param floorName The name of the floor + * @return set with groupnames + */ + public HashSet getGroupWhitelist(@Nonnull String floorName) { + HashSet ret = new HashSet<>(); + if (getFloors().containsKey(floorName)) { + ret = getFloors().get(floorName).getGroupWhitelist(); + } + return ret; + } + + /** + * Set a lift to (not) offline + * + * @param state true/false + * @return 0 if set, -2 if same state + */ + public int setOffline(boolean state) { + if (offline == state) return -2; + offline = state; + Iterator liter = getSigns().iterator(); + BlockState bs; + Sign sign; + if (state) { + for (LiftBlock lb : getBlocks()) { + bs = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at setOffline").getBlockAt(lb.getX(), lb.getY(), lb.getZ()).getState(); + if (!(bs instanceof Sign)) continue; + sign = (Sign) bs; + if (!sign.getLine(0).equalsIgnoreCase(ConfigUtil.getConfigText("SignText"))) continue; + sign.setLine(3, ConfigUtil.getConfigText("DisabledText")); + sign.update(); + } + + while (liter.hasNext()) { + LiftSign ls = liter.next(); + bs = Objects.requireNonNull(Bukkit.getWorld(ls.getWorld()), "World is null at setOffline").getBlockAt(ls.getX(), ls.getY(), ls.getZ()).getState(); + if (!(bs instanceof Sign)) { + Bukkit.getLogger().severe("[V10Lift] Wrong sign removed at: " + LocationSerializer.serialize(bs.getBlock().getLocation())); + liter.remove(); + continue; + } + sign = (Sign) bs; + ls.setOldText(sign.getLine(3)); + sign.setLine(3, ConfigUtil.getConfigText("DisabledText")); + sign.update(); + } + } else { + for (LiftBlock lb : getBlocks()) { + bs = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at setOffline").getBlockAt(lb.getX(), lb.getY(), lb.getZ()).getState(); + if (!(bs instanceof Sign)) continue; + sign = (Sign) bs; + if (!sign.getLine(0).equalsIgnoreCase(ConfigUtil.getConfigText("SignText"))) continue; + sign.setLine(3, ""); + sign.update(); + } + + while (liter.hasNext()) { + LiftSign ls = liter.next(); + bs = Objects.requireNonNull(Bukkit.getWorld(ls.getWorld()), "World is null at setOffline").getBlockAt(ls.getX(), ls.getY(), ls.getZ()).getState(); + if (!(bs instanceof Sign)) { + Bukkit.getLogger().severe("[V10Lift] Wrong sign removed at: " + LocationSerializer.serialize(bs.getBlock().getLocation())); + liter.remove(); + continue; + } + sign = (Sign) bs; + sign.setLine(3, ls.getOldText()); + sign.update(); + ls.setOldText(null); + } + } + return 0; + } + + /** + * Check if a lift contains the block as rope + * + * @param block The block + * @return true/false + */ + public boolean containsRope(@Nonnull Block block) { + if (getRopes().isEmpty()) return false; + + int x = block.getX(); + int y = block.getY(); + int z = block.getZ(); + for (LiftRope rope : getRopes()) { + if (x != rope.getX() || z != rope.getZ()) continue; + if (y >= rope.getMinY() && y <= rope.getMaxY()) { + return true; + } + } + return false; + } + + /** + * Send info about a lift to a player + * + * @param ent Where you want to send it to + */ + public void sendInfo(@Nonnull CommandSender ent) { + ent.sendMessage(ChatColor.GOLD + "Elevator: " + ChatColor.YELLOW + name); + ent.sendMessage(ChatColor.GOLD + "Settings:"); + ent.sendMessage(ChatColor.GREEN + " Speed: " + ChatColor.YELLOW + getSpeed()); + ent.sendMessage(ChatColor.GREEN + " Realistic Mode: " + ChatColor.YELLOW + isRealistic()); + ent.sendMessage(ChatColor.GREEN + " Malfunction: " + ChatColor.YELLOW + isDefective()); + ent.sendMessage(ChatColor.GOLD + "Floors:"); + if (getFloors().isEmpty()) { + ent.sendMessage(ChatColor.RED + "None."); + } else { + for (Map.Entry entry : getFloors().entrySet()) { + ent.sendMessage(ChatColor.GREEN + " " + entry.getKey() + ":"); + Floor f = entry.getValue(); + ent.sendMessage(ChatColor.YELLOW + " World: " + ChatColor.GREEN + f.getWorld()); + ent.sendMessage(ChatColor.YELLOW + " Height: " + ChatColor.GREEN + f.getY()); + ent.sendMessage(ChatColor.YELLOW + " Whitelist:"); + if (f.getUserWhitelist().isEmpty() && f.getGroupWhitelist().isEmpty()) { + ent.sendMessage(ChatColor.GOLD + " None."); + } else { + ChatColor color = ChatColor.DARK_PURPLE; + Iterator iter = f.getUserWhitelist().iterator(); + Iterator iter2 = f.getGroupWhitelist().iterator(); + StringBuilder sb = new StringBuilder(); + sb.append(" ").append(color).append(Bukkit.getOfflinePlayer(iter.next()).getName()); + while (iter.hasNext()) { + if (color == ChatColor.DARK_PURPLE) { + color = ChatColor.LIGHT_PURPLE; + } else { + color = ChatColor.DARK_PURPLE; + } + sb.append(ChatColor.AQUA).append(", ").append(color).append(Bukkit.getOfflinePlayer(iter.next()).getName()); + } + while (iter2.hasNext()) { + if (color == ChatColor.DARK_PURPLE) { + color = ChatColor.LIGHT_PURPLE; + } else { + color = ChatColor.DARK_PURPLE; + } + sb.append(ChatColor.AQUA).append(", ").append(color).append("Group: ").append(iter2.next()); + } + ent.sendMessage(sb.toString()); + } + } + } + } + + /** + * Add a rope to a lift + * + * @param world The world + * @param x The x-pos + * @param minY The min y-pos + * @param maxY The max y-pos + * @param z The z-pos + * @return 0 if added, -2 if not same mat, -3 if already a rope, -4 if forbidden material + */ + public int addRope(@Nonnull World world, int x, int minY, int maxY, int z) { + //minY = maxY, so reverse + if (minY > maxY) { + int tempY = minY; + minY = maxY; + maxY = tempY; + } + + Block block = world.getBlockAt(x, minY, z); + if (V10LiftPlugin.getApi().isRope(block)) return -3; + Material mat = block.getType(); + if (ForbiddenBlockManager.isForbidden(mat)) return -4; + + for (int i = minY + 1; i <= maxY; i++) { + block = world.getBlockAt(x, i, z); + if (V10LiftPlugin.getApi().isRope(block)) return -3; + if (block.getType() != mat) return -2; + } + + LiftRope rope = new LiftRope(block, minY, maxY); + getRopes().add(rope); + + return 0; + } + + /** + * Remove a rope from a lift + * + * @param block The block + * @return true/false + */ + public boolean removeRope(@Nonnull Block block) { + if (!containsRope(block)) + return false; + + String world = block.getWorld().getName(); + int x = block.getX(); + int y = block.getY(); + int z = block.getZ(); + Iterator riter = getRopes().iterator(); + while (riter.hasNext()) { + LiftRope rope = riter.next(); + if (x != rope.getX() || z != rope.getZ()) continue; + if (world.equals(rope.getWorld())) { + if (y >= rope.getMinY() && y <= rope.getMaxY()) { + riter.remove(); + return true; + } + } + } + return false; + } + +// /** +// * Set the queue of a lift +// * +// * @param liftName The name of the lift +// * @param queue The queue +// * @return true/false +// */ +// public boolean setQueue(String liftName, LinkedHashMap queue) { +// if (liftName == null || queue == null || !DataManager.containsLift(liftName)) return false; +// +// Lift lift = DataManager.getLift(liftName); +// lift.setQueue(new LinkedHashMap<>()); +// for (Map.Entry e : queue.entrySet()) { +// addToQueue(liftName, e.getValue(), e.getKey()); +// } +// return true; +// } +// +// /** +// * Add a location to the queue +// * +// * @param lift The name of the lift +// * @param y The y-pos +// * @param world The world +// * @return true/false +// */ +// public boolean addToQueue(String lift, int y, World world) { +// return addToQueue(lift, y, world, null); +// } +// +// /** +// * Add a location to the queue +// * +// * @param lift The name of the lift +// * @param y The y-pos +// * @param world The world +// * @param floorName The name of the flor +// * @return true/false +// */ +// public boolean addToQueue(String lift, int y, @Nonnull World world, String floorName) { +// return addToQueue(lift, new Floor(y, world.getName()), floorName); +// } +// +// /** +// * Add a location to the queue +// * +// * @param lift The name of the lift +// * @param floor The {@link Floor} +// * @param floorName The name of the flor +// * @return true/false +// */ +// public boolean addToQueue(String lift, Floor floor, String floorName) { +// if (lift == null || floor == null || !DataManager.containsLift(lift)) return false; +// +// Lift l = DataManager.getLift(lift); +// if (l.getQueue() == null) { +// l.setQueue(new LinkedHashMap<>()); +// } +// +// if (!l.getQueue().containsValue(floor)) { +// if (floorName == null) { +// floorName = ChatColor.MAGIC + "-----"; +// for (Map.Entry e : l.getFloors().entrySet()) { +// if (e.getValue().equals(floor)) { +// floorName = e.getKey(); +// floor = e.getValue(); +// break; +// } +// } +// } +// +// l.getQueue().put(floorName, floor); +// startLift(lift); +// return true; +// } +// return false; +// } +// +// private void start(String liftName) { +// if (!DataManager.containsMovingTask(liftName)) { +// Lift lift = DataManager.getLift(liftName); +// DataManager.addMovingTask(liftName, Bukkit.getScheduler().scheduleSyncRepeatingTask(V10LiftPlugin.getInstance(), new MoveLift(liftName, lift.getSpeed()), lift.getSpeed(), lift.getSpeed())); +// } +// } } diff --git a/src/main/java/tech/sbdevelopment/v10lift/api/runnables/DoorCloser.java b/src/main/java/tech/sbdevelopment/v10lift/api/runnables/DoorCloser.java index b07440b..0e31e07 100644 --- a/src/main/java/tech/sbdevelopment/v10lift/api/runnables/DoorCloser.java +++ b/src/main/java/tech/sbdevelopment/v10lift/api/runnables/DoorCloser.java @@ -3,17 +3,18 @@ package tech.sbdevelopment.v10lift.api.runnables; import org.bukkit.Bukkit; import tech.sbdevelopment.v10lift.V10LiftPlugin; import tech.sbdevelopment.v10lift.api.V10LiftAPI; +import tech.sbdevelopment.v10lift.api.objects.Lift; import tech.sbdevelopment.v10lift.managers.DataManager; /** * The DoorCloser runnable, used for checking if the door can be closed. */ public class DoorCloser implements Runnable { - private final String liftName; + private final Lift lift; private final int taskID; - public DoorCloser(String liftName) { - this.liftName = liftName; + public DoorCloser(Lift lift) { + this.lift = lift; final long doorCloseTime = V10LiftPlugin.getSConfig().getFile().getLong("DoorCloseTime"); this.taskID = Bukkit.getScheduler().runTaskTimer(V10LiftPlugin.getInstance(), this, doorCloseTime, doorCloseTime).getTaskId(); @@ -21,11 +22,11 @@ public class DoorCloser implements Runnable { @Override public void run() { - if (V10LiftAPI.getInstance().closeDoor(liftName)) stop(); + if (lift.closeDoor()) stop(); } public void stop() { Bukkit.getScheduler().cancelTask(taskID); - if (DataManager.containsLift(liftName)) DataManager.getLift(liftName).setDoorCloser(null); + lift.setDoorCloser(null); } } diff --git a/src/main/java/tech/sbdevelopment/v10lift/managers/DataManager.java b/src/main/java/tech/sbdevelopment/v10lift/managers/DataManager.java index 46b212d..717475b 100644 --- a/src/main/java/tech/sbdevelopment/v10lift/managers/DataManager.java +++ b/src/main/java/tech/sbdevelopment/v10lift/managers/DataManager.java @@ -7,8 +7,6 @@ import tech.sbdevelopment.v10lift.api.objects.LiftBlock; import java.util.*; public class DataManager { - /* A manager for general HashMaps */ - @Getter private static final Map lifts = new LinkedHashMap<>(); private static final Map> builds = new LinkedHashMap<>(); @Getter private static final Map editors = new LinkedHashMap<>(); private static final Map inputEdits = new LinkedHashMap<>(); @@ -22,26 +20,6 @@ public class DataManager { private static final List whoisReq = new ArrayList<>(); private static final Map movingTasks = new HashMap<>(); - /* HashMap methods */ - - // // - public static void addLift(String liftName, Lift lift) { - lifts.put(liftName, lift); - } - - public static void removeLift(String liftName) { - lifts.remove(liftName); - } - - public static boolean containsLift(String liftName) { - return lifts.containsKey(liftName); - } - - public static Lift getLift(String liftName) { - return lifts.get(liftName); - } - - // // public static boolean containsPlayer(UUID player) { return builds.containsKey(player); }