From 17214098cd92d306f1d0410e74bc7fe0594c0684 Mon Sep 17 00:00:00 2001 From: stijnb1234 Date: Mon, 3 Feb 2020 15:10:28 +0100 Subject: [PATCH] Added defaults to config --- .../V10Lift/API/Runnables/MoveLift.java | 3 +- .../SBDeveloper/V10Lift/API/V10LiftAPI.java | 56 ++++++++++++------- .../V10Lift/Commands/V10LiftCommand.java | 32 +++++++---- .../Listeners/PlayerInteractListener.java | 22 ++++++-- .../V10Lift/Managers/DataManager.java | 2 + .../SBDeveloper/V10Lift/Utils/ConfigUtil.java | 14 +++++ .../nl/SBDeveloper/V10Lift/V10LiftPlugin.java | 3 +- src/main/resources/config.yml | 3 + 8 files changed, 95 insertions(+), 40 deletions(-) create mode 100644 src/main/java/nl/SBDeveloper/V10Lift/Utils/ConfigUtil.java diff --git a/src/main/java/nl/SBDeveloper/V10Lift/API/Runnables/MoveLift.java b/src/main/java/nl/SBDeveloper/V10Lift/API/Runnables/MoveLift.java index 831f4be..e46e75a 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/API/Runnables/MoveLift.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/API/Runnables/MoveLift.java @@ -96,8 +96,7 @@ public class MoveLift implements Runnable { return; } - //TODO Add defaults to config (chanceOfDefect) - double changeOfDefect = 0.0D; + double changeOfDefect = V10LiftPlugin.getSConfig().getFile().getDouble("DefectRate"); if (changeOfDefect > 0.0D) { y = new Random().nextInt(100); double chance; diff --git a/src/main/java/nl/SBDeveloper/V10Lift/API/V10LiftAPI.java b/src/main/java/nl/SBDeveloper/V10Lift/API/V10LiftAPI.java index 94f9791..5c6e30a 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/API/V10LiftAPI.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/API/V10LiftAPI.java @@ -6,6 +6,7 @@ import nl.SBDeveloper.V10Lift.API.Runnables.MoveLift; import nl.SBDeveloper.V10Lift.Managers.AntiCopyBlockManager; import nl.SBDeveloper.V10Lift.Managers.DataManager; import nl.SBDeveloper.V10Lift.Managers.ForbiddenBlockManager; +import nl.SBDeveloper.V10Lift.Utils.ConfigUtil; import nl.SBDeveloper.V10Lift.Utils.DirectionUtil; import nl.SBDeveloper.V10Lift.Utils.LocationSerializer; import nl.SBDeveloper.V10Lift.Utils.XMaterial; @@ -83,8 +84,7 @@ public class V10LiftAPI { Bukkit.getLogger().info("[V10Lift] Creating lift " + liftName); if (p == null || liftName == null || DataManager.containsLift(liftName)) return false; - //TODO Add defaults to config - DataManager.addLift(liftName, new Lift(p.getUniqueId(), 16, true)); + DataManager.addLift(liftName, new Lift(p.getUniqueId(), V10LiftPlugin.getSConfig().getFile().getInt("DefaultSpeed"), V10LiftPlugin.getSConfig().getFile().getBoolean("DefaultRealistic"))); return true; } @@ -98,9 +98,31 @@ public class V10LiftAPI { Bukkit.getLogger().info("[V10Lift] Removing lift " + liftName); if (liftName == null || !DataManager.containsLift(liftName)) return false; - //TODO Remove lift from all data maps + Iterator> iter = DataManager.getEditors().entrySet().iterator(); + HashSet activeEdits = new HashSet<>(); + while (iter.hasNext()) { + Map.Entry entry = iter.next(); + if (entry.getValue().equals(liftName)) { + activeEdits.add(entry.getKey()); + iter.remove(); + } + } - //TODO Stop movingtask if running + for (UUID puuid : activeEdits) { + DataManager.removeInputEditsPlayer(puuid); + DataManager.removeInputRemovesPlayer(puuid); + DataManager.removeOfflineEditsPlayer(puuid); + DataManager.removeOfflineRemovesPlayer(puuid); + DataManager.removeBuilderPlayer(puuid); + DataManager.removeRopeEditPlayer(puuid); + DataManager.removeRopeRemovesPlayer(puuid); + DataManager.removeDoorEditPlayer(puuid); + } + + if (DataManager.containsMovingTask(liftName)) { + Bukkit.getScheduler().cancelTask(DataManager.getMovingTask(liftName)); + DataManager.removeMovingTask(liftName); + } DataManager.removeLift(liftName); return true; @@ -336,8 +358,8 @@ public class V10LiftAPI { if (lift.isRealistic()) { lift.setDoorCloser(new DoorCloser(liftName)); - //TODO Add defaults (doorclosetime) to config - long doorCloseTime = 5 * 20; + long doorCloseTime = V10LiftPlugin.getSConfig().getFile().getLong("DoorCloseTime"); + int pid = Bukkit.getScheduler().scheduleSyncRepeatingTask(V10LiftPlugin.getInstance(), lift.getDoorCloser(), doorCloseTime, doorCloseTime); lift.getDoorCloser().setPid(pid); } @@ -366,8 +388,8 @@ public class V10LiftAPI { if (lift.isRealistic()) { lift.setDoorCloser(new DoorCloser(liftName)); - //TODO Add defaults (doorclosetime) to config - long doorCloseTime = 5 * 20; + long doorCloseTime = V10LiftPlugin.getSConfig().getFile().getLong("DoorCloseTime"); + int pid = Bukkit.getScheduler().scheduleSyncRepeatingTask(V10LiftPlugin.getInstance(), lift.getDoorCloser(), doorCloseTime, doorCloseTime); lift.getDoorCloser().setPid(pid); } @@ -560,8 +582,7 @@ public class V10LiftAPI { Sign s = (Sign) bs; ls.setOldText(s.getLine(3)); - //TODO Add defaults to config - s.setLine(3, ChatColor.MAGIC + "Defect!"); + s.setLine(3, ConfigUtil.getColored("DefectText")); s.update(); //Update all other signs @@ -571,7 +592,7 @@ public class V10LiftAPI { s = (Sign) bs; lift.setSignText(s.getLine(3)); - s.setLine(3, ChatColor.MAGIC + "Defect!"); + s.setLine(3, ConfigUtil.getColored("DefectText")); s.update(); } } else { @@ -587,7 +608,7 @@ public class V10LiftAPI { continue; } s = (Sign) bs; - if (s.getLine(3).equals(ChatColor.MAGIC + "Defect!")) { + if (s.getLine(3).equals(ConfigUtil.getColored("DefectText"))) { s.setLine(3, ls.getOldText()); s.update(); ls.setOldText(null); @@ -656,9 +677,8 @@ public class V10LiftAPI { 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; - //TODO Add defaults - if (!sign.getLine(0).equalsIgnoreCase("[v10lift]")) continue; - sign.setLine(3, ChatColor.RED + "Disabled"); + if (!sign.getLine(0).equalsIgnoreCase(ConfigUtil.getColored("SignText"))) continue; + sign.setLine(3, ConfigUtil.getColored("DisabledText")); sign.update(); } @@ -672,8 +692,7 @@ public class V10LiftAPI { } sign = (Sign) bs; ls.setOldText(sign.getLine(3)); - //TODO Add defaults - sign.setLine(3, ChatColor.RED + "Disabled"); + sign.setLine(3, ConfigUtil.getColored("DisabledText")); sign.update(); } } else { @@ -681,8 +700,7 @@ public class V10LiftAPI { 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; - //TODO Add defaults - if (!sign.getLine(0).equalsIgnoreCase("[v10lift]")) continue; + if (!sign.getLine(0).equalsIgnoreCase(ConfigUtil.getColored("SignText"))) continue; sign.setLine(3, ""); sign.update(); } diff --git a/src/main/java/nl/SBDeveloper/V10Lift/Commands/V10LiftCommand.java b/src/main/java/nl/SBDeveloper/V10Lift/Commands/V10LiftCommand.java index b7028a5..bd18a86 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/Commands/V10LiftCommand.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/Commands/V10LiftCommand.java @@ -5,7 +5,9 @@ import nl.SBDeveloper.V10Lift.API.Objects.Lift; import nl.SBDeveloper.V10Lift.API.Objects.LiftBlock; import nl.SBDeveloper.V10Lift.API.Objects.LiftSign; import nl.SBDeveloper.V10Lift.Managers.DataManager; +import nl.SBDeveloper.V10Lift.Utils.ConfigUtil; import nl.SBDeveloper.V10Lift.Utils.LocationSerializer; +import nl.SBDeveloper.V10Lift.Utils.XMaterial; import nl.SBDeveloper.V10Lift.V10LiftPlugin; import org.bukkit.*; import org.bukkit.block.Block; @@ -227,9 +229,17 @@ public class V10LiftCommand implements CommandExecutor { return true; } - //TODO Add defaults to config!!! - int masterAmount = 2; - Material masterItem = Material.DIAMOND; + int masterAmount = V10LiftPlugin.getSConfig().getFile().getInt("MasterRepairAmount"); + Optional mat = XMaterial.matchXMaterial(Objects.requireNonNull(V10LiftPlugin.getSConfig().getFile().getString("MasterRepairItem"), "MasterRepairItem is null")); + if (!mat.isPresent()) { + Bukkit.getLogger().severe("[V10Lift] The material for MasterRepairItem is undefined!"); + return true; + } + Material masterItem = mat.get().parseMaterial(); + if (masterItem == null) { + Bukkit.getLogger().severe("[V10Lift] The material for MasterRepairItem is undefined!"); + return true; + } if (p.getGameMode() != GameMode.CREATIVE && masterAmount > 0) { if (!p.getInventory().contains(masterItem)) { sender.sendMessage(ChatColor.RED + "You need " + masterAmount + "x " + masterItem.toString().toLowerCase() + "!"); @@ -662,8 +672,7 @@ public class V10LiftCommand implements CommandExecutor { bs = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at edit command").getBlockAt(lb.getX(), lb.getY(), lb.getZ()).getState(); if (!(bs instanceof Sign)) continue; sign = (Sign) bs; - //TODO Add defaults - if (!sign.getLine(0).equalsIgnoreCase("[v10lift]")) continue; + if (!sign.getLine(0).equalsIgnoreCase(ConfigUtil.getColored("SignText"))) continue; sign.setLine(3, ""); sign.update(); } @@ -678,7 +687,6 @@ public class V10LiftCommand implements CommandExecutor { continue; } sign = (Sign) bs; - //TODO Add defaults sign.setLine(3, ls.getOldText()); sign.update(); ls.setOldText(null); @@ -712,8 +720,7 @@ public class V10LiftCommand implements CommandExecutor { bs = Objects.requireNonNull(Bukkit.getWorld(lb.getWorld()), "World is null at edit command").getBlockAt(lb.getX(), lb.getY(), lb.getZ()).getState(); if (!(bs instanceof Sign)) continue; sign = (Sign) bs; - //TODO Add defaults - if (!sign.getLine(0).equalsIgnoreCase("[v10lift]")) continue; + if (!sign.getLine(0).equalsIgnoreCase(ConfigUtil.getColored("SignText"))) continue; sign.setLine(3, ChatColor.RED + "Maintenance"); sign.update(); } @@ -729,8 +736,7 @@ public class V10LiftCommand implements CommandExecutor { } sign = (Sign) bs; ls.setOldText(sign.getLine(3)); - //TODO Add defaults - sign.setLine(3, ChatColor.RED + "Maintenance"); + sign.setLine(3, ConfigUtil.getColored("MaintenanceText")); sign.update(); } sender.sendMessage(ChatColor.GREEN + "Editor turned on!"); @@ -750,8 +756,10 @@ public class V10LiftCommand implements CommandExecutor { sender.sendMessage(ChatColor.RED + "You don't have the permission to remove that lift."); } - //TODO Fix ignoring of result - V10LiftPlugin.getAPI().removeLift(args[1]); + if (!V10LiftPlugin.getAPI().removeLift(args[1])) { + sender.sendMessage(ChatColor.RED + "The lift " + args[1] + " couldn't be removed!"); + return true; + } sender.sendMessage(ChatColor.GREEN + "The lift " + args[1] + " is removed successfully!"); return true; diff --git a/src/main/java/nl/SBDeveloper/V10Lift/Listeners/PlayerInteractListener.java b/src/main/java/nl/SBDeveloper/V10Lift/Listeners/PlayerInteractListener.java index e2c5331..4cbb055 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/Listeners/PlayerInteractListener.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/Listeners/PlayerInteractListener.java @@ -4,6 +4,7 @@ import nl.SBDeveloper.V10Lift.API.Objects.Floor; import nl.SBDeveloper.V10Lift.API.Objects.Lift; import nl.SBDeveloper.V10Lift.API.Objects.LiftBlock; import nl.SBDeveloper.V10Lift.Managers.DataManager; +import nl.SBDeveloper.V10Lift.Utils.ConfigUtil; import nl.SBDeveloper.V10Lift.Utils.XMaterial; import nl.SBDeveloper.V10Lift.V10LiftPlugin; import org.bukkit.Bukkit; @@ -24,6 +25,8 @@ import org.bukkit.inventory.ItemStack; import java.util.Iterator; import java.util.Map; +import java.util.Objects; +import java.util.Optional; public class PlayerInteractListener implements Listener { //BUTTON CLICK @@ -257,8 +260,7 @@ public class PlayerInteractListener implements Listener { if (!(bs instanceof Sign)) return; Sign sign = (Sign) bs; - //TODO Add defaults to config! - if (!sign.getLine(0).equalsIgnoreCase("[v10lift]")) return; + if (!sign.getLine(0).equalsIgnoreCase(ConfigUtil.getColored("SignText"))) return; String liftName = sign.getLine(1); if (!DataManager.containsLift(liftName)) return; @@ -269,10 +271,18 @@ public class PlayerInteractListener implements Listener { } if (lift.isDefective()) { - //TODO Add defaults to config!!! - if (sign.getLine(3).equals(ChatColor.MAGIC + "Defect!") && p.hasPermission("v10lift.repair") && a == Action.RIGHT_CLICK_BLOCK) { - int masterAmount = 2; - Material masterItem = Material.DIAMOND; + if (sign.getLine(3).equals(ConfigUtil.getColored("DefectText")) && p.hasPermission("v10lift.repair") && a == Action.RIGHT_CLICK_BLOCK) { + int masterAmount = V10LiftPlugin.getSConfig().getFile().getInt("RepairAmount"); + Optional mat = XMaterial.matchXMaterial(Objects.requireNonNull(V10LiftPlugin.getSConfig().getFile().getString("RepairItem"), "RepairItem is null")); + if (!mat.isPresent()) { + Bukkit.getLogger().severe("[V10Lift] The material for RepairItem is undefined!"); + return; + } + Material masterItem = mat.get().parseMaterial(); + if (masterItem == null) { + Bukkit.getLogger().severe("[V10Lift] The material for RepairItem is undefined!"); + return; + } if (p.getGameMode() != GameMode.CREATIVE && masterAmount > 0) { if (!p.getInventory().contains(masterItem)) { p.sendMessage(ChatColor.RED + "You need " + masterAmount + "x " + masterItem.toString().toLowerCase() + "!"); diff --git a/src/main/java/nl/SBDeveloper/V10Lift/Managers/DataManager.java b/src/main/java/nl/SBDeveloper/V10Lift/Managers/DataManager.java index 00ca320..6e9e604 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/Managers/DataManager.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/Managers/DataManager.java @@ -114,6 +114,8 @@ public class DataManager { return editors.get(player); } + public static LinkedHashMap getEditors() { return editors; } + // // public static void addMovingTask(String liftName, int taskid) { movingTasks.put(liftName, taskid); diff --git a/src/main/java/nl/SBDeveloper/V10Lift/Utils/ConfigUtil.java b/src/main/java/nl/SBDeveloper/V10Lift/Utils/ConfigUtil.java new file mode 100644 index 0000000..4ec9b12 --- /dev/null +++ b/src/main/java/nl/SBDeveloper/V10Lift/Utils/ConfigUtil.java @@ -0,0 +1,14 @@ +package nl.SBDeveloper.V10Lift.Utils; + +import nl.SBDeveloper.V10Lift.V10LiftPlugin; +import org.bukkit.ChatColor; + +import javax.annotation.Nonnull; +import java.util.Objects; + +public class ConfigUtil { + @Nonnull + public static String getColored(@Nonnull String path) { + return ChatColor.translateAlternateColorCodes('&', Objects.requireNonNull(V10LiftPlugin.getSConfig().getFile().getString(path), "Message " + path + " not found!")); + } +} diff --git a/src/main/java/nl/SBDeveloper/V10Lift/V10LiftPlugin.java b/src/main/java/nl/SBDeveloper/V10Lift/V10LiftPlugin.java index c29e378..c514c93 100644 --- a/src/main/java/nl/SBDeveloper/V10Lift/V10LiftPlugin.java +++ b/src/main/java/nl/SBDeveloper/V10Lift/V10LiftPlugin.java @@ -13,6 +13,7 @@ import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; import java.sql.SQLException; +import java.util.Objects; public class V10LiftPlugin extends JavaPlugin { @@ -38,7 +39,7 @@ public class V10LiftPlugin extends JavaPlugin { api = new V10LiftAPI(); - getCommand("v10lift").setExecutor(new V10LiftCommand()); + Objects.requireNonNull(getCommand("v10lift"), "Internal error! Command not found.").setExecutor(new V10LiftCommand()); Bukkit.getPluginManager().registerEvents(new PlayerInteractListener(), this); Bukkit.getPluginManager().registerEvents(new BlockBreakListener(), this); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 967d5f5..ee7d594 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,4 +1,7 @@ SignText: "[v10lift]" +DefectText: "&kDefect!" +DisabledText: "&cDisabled" +MaintenanceText: "&cMaintenance" DefectRate: 0.0 RepairItem: REDSTONE RepairAmount: 5