Started with API overhaul, still WIP

This commit is contained in:
Stijn Bannink 2023-08-20 20:20:23 +02:00
parent cb9ed3b61f
commit e5f31385d5
7 changed files with 785 additions and 862 deletions

View file

@ -5,6 +5,7 @@ import org.bstats.bukkit.Metrics;
import org.bstats.charts.SingleLineChart; import org.bstats.charts.SingleLineChart;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import tech.sbdevelopment.v10lift.api.V10LiftAPI;
import tech.sbdevelopment.v10lift.commands.V10LiftCommand; import tech.sbdevelopment.v10lift.commands.V10LiftCommand;
import tech.sbdevelopment.v10lift.commands.V10LiftTabCompleter; import tech.sbdevelopment.v10lift.commands.V10LiftTabCompleter;
import tech.sbdevelopment.v10lift.listeners.BlockBreakListener; import tech.sbdevelopment.v10lift.listeners.BlockBreakListener;
@ -23,6 +24,8 @@ import java.util.Collections;
public class V10LiftPlugin extends JavaPlugin { public class V10LiftPlugin extends JavaPlugin {
@Getter @Getter
private static V10LiftPlugin instance; private static V10LiftPlugin instance;
@Getter
private static V10LiftAPI api;
private static YamlFile config; private static YamlFile config;
private static DBManager dbManager; private static DBManager dbManager;
@Getter @Getter
@ -36,6 +39,9 @@ public class V10LiftPlugin extends JavaPlugin {
public void onEnable() { public void onEnable() {
instance = this; instance = this;
//Load the API
api = new V10LiftAPI();
//Load the config //Load the config
config = new YamlFile("config"); config = new YamlFile("config");
config.loadDefaults(); config.loadDefaults();

View file

@ -1,24 +1,16 @@
package tech.sbdevelopment.v10lift.api; package tech.sbdevelopment.v10lift.api;
import com.cryptomorin.xseries.XMaterial; import lombok.Getter;
import org.bukkit.*; import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState; import org.bukkit.block.BlockState;
import org.bukkit.block.Sign; import org.bukkit.block.Sign;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import tech.sbdevelopment.v10lift.V10LiftPlugin; import tech.sbdevelopment.v10lift.V10LiftPlugin;
import tech.sbdevelopment.v10lift.api.objects.*; import tech.sbdevelopment.v10lift.api.objects.Lift;
import tech.sbdevelopment.v10lift.api.runnables.DoorCloser; import tech.sbdevelopment.v10lift.api.objects.LiftSign;
import tech.sbdevelopment.v10lift.api.runnables.MoveLift;
import tech.sbdevelopment.v10lift.managers.DataManager; 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 javax.annotation.Nonnull;
import java.util.*; import java.util.*;
@ -27,41 +19,8 @@ import java.util.*;
* The Main API class, for all the API methods * The Main API class, for all the API methods
*/ */
public class V10LiftAPI { public class V10LiftAPI {
private static V10LiftAPI instance; @Getter
private static final List<Lift> lifts = new ArrayList<>();
private V10LiftAPI() {
}
public static V10LiftAPI getInstance() {
if (instance == null) instance = new V10LiftAPI();
return instance;
}
/* Private API methods */
private void sortFloors(@Nonnull Lift lift) {
ArrayList<Map.Entry<String, Floor>> as = new ArrayList<>(lift.getFloors().entrySet());
as.sort(Comparator.comparingInt(o -> o.getValue().getY()));
Iterator<Map.Entry<String, Floor>> iter = as.iterator();
lift.getFloors().clear();
Map.Entry<String, Floor> 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
*/
/** /**
* Create a new Lift * Create a new Lift
@ -70,10 +29,10 @@ public class V10LiftAPI {
* @param liftName The name of the lift * @param liftName The name of the lift
* @return true if created, false if null or already exists * @return true if created, false if null or already exists
*/ */
public boolean createLift(Player p, String liftName) { public boolean createLift(@Nonnull Player p, @Nonnull String liftName) {
if (p == null || liftName == null || DataManager.containsLift(liftName)) return false; 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; return true;
} }
@ -83,8 +42,9 @@ public class V10LiftAPI {
* @param liftName The name of the lift * @param liftName The name of the lift
* @return true if removed, false if null or doesn't exists * @return true if removed, false if null or doesn't exists
*/ */
public boolean removeLift(String liftName) { public boolean deleteLift(@Nonnull String liftName) {
if (liftName == null || !DataManager.containsLift(liftName)) return false; Optional<Lift> liftOpt = getLift(liftName);
if (liftOpt.isEmpty()) return false;
Iterator<Map.Entry<UUID, String>> iter = DataManager.getEditors().entrySet().iterator(); Iterator<Map.Entry<UUID, String>> iter = DataManager.getEditors().entrySet().iterator();
HashSet<UUID> activeEdits = new HashSet<>(); HashSet<UUID> activeEdits = new HashSet<>();
@ -112,27 +72,27 @@ public class V10LiftAPI {
DataManager.removeMovingTask(liftName); DataManager.removeMovingTask(liftName);
} }
DataManager.removeLift(liftName); lifts.remove(liftOpt.get());
V10LiftPlugin.getDBManager().remove(liftName); V10LiftPlugin.getDBManager().remove(liftName);
return true; return true;
} }
public boolean isLift(@Nonnull String name) {
return lifts.stream().anyMatch(lift -> lift.getName().equalsIgnoreCase(name));
}
public Optional<Lift> 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) * Get the name of a lift by a location (checking for cab blocks)
* *
* @param loc The location you want to check for * @param loc The location you want to check for
* @return The liftname * @return The liftname
*/ */
public String getLiftByLocation(Location loc) { public Optional<Lift> getLift(@Nonnull Location loc) {
for (Map.Entry<String, Lift> entry : DataManager.getLifts().entrySet()) { 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();
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;
} }
/** /**
@ -141,13 +101,12 @@ public class V10LiftAPI {
* @param liftName The name of the lift * @param liftName The name of the lift
* @param newName The new name of the lift * @param newName The new name of the lift
*/ */
public void renameLift(String liftName, String newName) { public boolean renameLift(@Nonnull String liftName, @Nonnull String newName) {
if (liftName == null || newName == null || !DataManager.containsLift(liftName)) return; Optional<Lift> liftOpt = getLift(liftName);
if (liftOpt.isEmpty() || isLift(newName)) return false;
Lift lift = DataManager.getLift(liftName); liftOpt.get().setName(newName);
DataManager.removeLift(liftName); for (LiftSign ls : liftOpt.get().getSigns()) {
DataManager.addLift(newName, lift);
for (LiftSign ls : lift.getSigns()) {
Block block = Objects.requireNonNull(Bukkit.getWorld(ls.getWorld()), "World is null at renameLift").getBlockAt(ls.getX(), ls.getY(), ls.getZ()); Block block = Objects.requireNonNull(Bukkit.getWorld(ls.getWorld()), "World is null at renameLift").getBlockAt(ls.getX(), ls.getY(), ls.getZ());
BlockState bs = block.getState(); BlockState bs = block.getState();
if (!(bs instanceof Sign)) continue; if (!(bs instanceof Sign)) continue;
@ -155,557 +114,9 @@ public class V10LiftAPI {
si.setLine(1, newName); si.setLine(1, newName);
si.update(); 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<LiftBlock> 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<LiftBlock> 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<LiftBlock> 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; 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<LiftInput> liter = lift.getInputs().iterator();
LiftInput lb;
ArrayList<LiftInput> 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<UUID> getUserWhitelist(String liftName, String floorName) {
HashSet<UUID> 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<String> getGroupWhitelist(String liftName, String floorName) {
HashSet<String> 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<LiftSign> 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 * Check if a block is a rope
* *
@ -713,218 +124,6 @@ public class V10LiftAPI {
* @return true/false * @return true/false
*/ */
public boolean isRope(Block b) { public boolean isRope(Block b) {
for (String lift : DataManager.getLifts().keySet()) { return getLifts().stream().anyMatch(lift -> lift.containsRope(b));
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<String, Floor> 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<UUID> iter = f.getUserWhitelist().iterator();
Iterator<String> 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<LiftRope> 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<String, Floor> queue) {
if (liftName == null || queue == null || !DataManager.containsLift(liftName)) return false;
Lift lift = DataManager.getLift(liftName);
lift.setQueue(new LinkedHashMap<>());
for (Map.Entry<String, Floor> 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<String, Floor> 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;
} }
} }

View file

@ -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<String, Floor> 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<String, Floor> entry : queue.entrySet()) {
if ((movingUp && entry.getValue().getY() > entry.getValue().getY()) || (!movingUp && entry.getValue().getY() < entry.getValue().getY())) {
nextFloor = entry.getKey();
}
}
return nextFloor;
}
}

View file

@ -5,6 +5,7 @@ import lombok.NoArgsConstructor;
import lombok.Setter; import lombok.Setter;
import lombok.ToString; import lombok.ToString;
import javax.annotation.Nonnull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.UUID; import java.util.UUID;
@ -17,6 +18,7 @@ import java.util.UUID;
@NoArgsConstructor @NoArgsConstructor
@ToString @ToString
public class Floor { public class Floor {
@Nonnull
private String world; private String world;
private int y; private int y;
private ArrayList<LiftBlock> doorBlocks = new ArrayList<>(); private ArrayList<LiftBlock> doorBlocks = new ArrayList<>();

View file

@ -4,8 +4,21 @@ import lombok.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.Setter; import lombok.Setter;
import lombok.ToString; 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.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.*; import java.util.*;
/** /**
@ -15,6 +28,8 @@ import java.util.*;
@NoArgsConstructor @NoArgsConstructor
@ToString @ToString
public class Lift { public class Lift {
@Setter
private String name;
@Setter @Setter
private String worldName; private String worldName;
@Setter @Setter
@ -25,18 +40,15 @@ public class Lift {
private final HashSet<LiftSign> signs = new HashSet<>(); private final HashSet<LiftSign> signs = new HashSet<>();
private final HashSet<LiftInput> inputs = new HashSet<>(); private final HashSet<LiftInput> inputs = new HashSet<>();
private final HashSet<LiftInput> offlineInputs = new HashSet<>(); private final HashSet<LiftInput> offlineInputs = new HashSet<>();
@Setter private final LiftQueue queue = new LiftQueue();
private LinkedHashMap<String, Floor> queue = null;
private final HashSet<LiftRope> ropes = new HashSet<>(); private final HashSet<LiftRope> ropes = new HashSet<>();
@Setter @Setter
private int speed; private int speed;
@Setter @Setter
private boolean realistic; private boolean realistic;
@Setter
private boolean offline = false; private boolean offline = false;
@Setter @Setter
private boolean sound = true; private boolean sound = true;
@Setter
private boolean defective = false; private boolean defective = false;
@Setter @Setter
private String signText = null; private String signText = null;
@ -74,4 +86,684 @@ public class Lift {
this.speed = speed; this.speed = speed;
this.realistic = realistic; 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<LiftBlock> 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<LiftBlock> 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<LiftBlock> 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<Map.Entry<String, Floor>> as = new ArrayList<>(getFloors().entrySet());
as.sort(Comparator.comparingInt(o -> o.getValue().getY()));
Iterator<Map.Entry<String, Floor>> iter = as.iterator();
getFloors().clear();
Map.Entry<String, Floor> 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<LiftInput> liter = getInputs().iterator();
LiftInput lb;
ArrayList<LiftInput> 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<UUID> getUserWhitelist(@Nonnull String floorName) {
HashSet<UUID> 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<String> getGroupWhitelist(@Nonnull String floorName) {
HashSet<String> 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<LiftSign> 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<String, Floor> 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<UUID> iter = f.getUserWhitelist().iterator();
Iterator<String> 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<LiftRope> 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<String, Floor> queue) {
// if (liftName == null || queue == null || !DataManager.containsLift(liftName)) return false;
//
// Lift lift = DataManager.getLift(liftName);
// lift.setQueue(new LinkedHashMap<>());
// for (Map.Entry<String, Floor> 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<String, Floor> 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()));
// }
// }
} }

View file

@ -3,17 +3,18 @@ package tech.sbdevelopment.v10lift.api.runnables;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import tech.sbdevelopment.v10lift.V10LiftPlugin; import tech.sbdevelopment.v10lift.V10LiftPlugin;
import tech.sbdevelopment.v10lift.api.V10LiftAPI; import tech.sbdevelopment.v10lift.api.V10LiftAPI;
import tech.sbdevelopment.v10lift.api.objects.Lift;
import tech.sbdevelopment.v10lift.managers.DataManager; import tech.sbdevelopment.v10lift.managers.DataManager;
/** /**
* The DoorCloser runnable, used for checking if the door can be closed. * The DoorCloser runnable, used for checking if the door can be closed.
*/ */
public class DoorCloser implements Runnable { public class DoorCloser implements Runnable {
private final String liftName; private final Lift lift;
private final int taskID; private final int taskID;
public DoorCloser(String liftName) { public DoorCloser(Lift lift) {
this.liftName = liftName; this.lift = lift;
final long doorCloseTime = V10LiftPlugin.getSConfig().getFile().getLong("DoorCloseTime"); final long doorCloseTime = V10LiftPlugin.getSConfig().getFile().getLong("DoorCloseTime");
this.taskID = Bukkit.getScheduler().runTaskTimer(V10LiftPlugin.getInstance(), this, doorCloseTime, doorCloseTime).getTaskId(); this.taskID = Bukkit.getScheduler().runTaskTimer(V10LiftPlugin.getInstance(), this, doorCloseTime, doorCloseTime).getTaskId();
@ -21,11 +22,11 @@ public class DoorCloser implements Runnable {
@Override @Override
public void run() { public void run() {
if (V10LiftAPI.getInstance().closeDoor(liftName)) stop(); if (lift.closeDoor()) stop();
} }
public void stop() { public void stop() {
Bukkit.getScheduler().cancelTask(taskID); Bukkit.getScheduler().cancelTask(taskID);
if (DataManager.containsLift(liftName)) DataManager.getLift(liftName).setDoorCloser(null); lift.setDoorCloser(null);
} }
} }

View file

@ -7,8 +7,6 @@ import tech.sbdevelopment.v10lift.api.objects.LiftBlock;
import java.util.*; import java.util.*;
public class DataManager { public class DataManager {
/* A manager for general HashMaps */
@Getter private static final Map<String, Lift> lifts = new LinkedHashMap<>();
private static final Map<UUID, TreeSet<LiftBlock>> builds = new LinkedHashMap<>(); private static final Map<UUID, TreeSet<LiftBlock>> builds = new LinkedHashMap<>();
@Getter private static final Map<UUID, String> editors = new LinkedHashMap<>(); @Getter private static final Map<UUID, String> editors = new LinkedHashMap<>();
private static final Map<UUID, String> inputEdits = new LinkedHashMap<>(); private static final Map<UUID, String> inputEdits = new LinkedHashMap<>();
@ -22,26 +20,6 @@ public class DataManager {
private static final List<UUID> whoisReq = new ArrayList<>(); private static final List<UUID> whoisReq = new ArrayList<>();
private static final Map<String, Integer> movingTasks = new HashMap<>(); private static final Map<String, Integer> 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) { public static boolean containsPlayer(UUID player) {
return builds.containsKey(player); return builds.containsKey(player);
} }