From 96379db923593088b3a3807adcd77f2a7361ab0f Mon Sep 17 00:00:00 2001 From: montlikadani Date: Thu, 20 May 2021 16:17:44 +0200 Subject: [PATCH] Cancelled event methods with ignoreCancelled annotation value will not be called --- .../jobs/config/BossBarManager.java | 2 +- .../gamingmesh/jobs/config/ConfigManager.java | 16 +++++----- .../jobs/config/ScheduleManager.java | 17 +++++------ .../java/com/gamingmesh/jobs/dao/JobsDAO.java | 29 +------------------ .../com/gamingmesh/jobs/dao/JobsManager.java | 20 +++++++++---- .../jobs/listeners/JobsListener.java | 18 ++++-------- .../jobs/listeners/JobsPayment14Listener.java | 8 ++--- .../jobs/listeners/JobsPaymentListener.java | 21 ++++---------- .../listeners/PistonProtectionListener.java | 6 ++-- 9 files changed, 50 insertions(+), 87 deletions(-) diff --git a/src/main/java/com/gamingmesh/jobs/config/BossBarManager.java b/src/main/java/com/gamingmesh/jobs/config/BossBarManager.java index 372182fe..b2a7fdb9 100644 --- a/src/main/java/com/gamingmesh/jobs/config/BossBarManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/BossBarManager.java @@ -28,7 +28,7 @@ public class BossBarManager { if (Version.getCurrent().isLower(Version.v1_9_R1) || player == null) return; - for (JobProgression oneJob : player.getJobProgression()) { + for (JobProgression oneJob : player.progression) { if (oneJob.getLastExperience() != 0) { ShowJobProgression(player, oneJob, oneJob.getLastExperience()); } diff --git a/src/main/java/com/gamingmesh/jobs/config/ConfigManager.java b/src/main/java/com/gamingmesh/jobs/config/ConfigManager.java index 8a1a638c..865bc9ed 100644 --- a/src/main/java/com/gamingmesh/jobs/config/ConfigManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/ConfigManager.java @@ -476,7 +476,7 @@ public class ConfigManager { cfg.save(); } - private static void generate(ConfigReader cfg, String pt) { + private void generate(ConfigReader cfg, String pt) { cfg.get(pt + ".income", 1D); cfg.get(pt + ".points", 1D); cfg.get(pt + ".experience", 1D); @@ -554,20 +554,19 @@ public class ConfigManager { @SuppressWarnings("deprecation") public KeyValues getKeyValue(String myKey, ActionType actionType, String jobName) { - String type = null, - subType = "", - meta = ""; + String subType = "", meta = ""; + final String finalMyKey = myKey; - int id = 0; if (myKey.contains("-")) { - // uses subType String[] split = myKey.split("-", 2); + if (split.length >= 2) { subType = ":" + split[1]; meta = split[1]; - myKey = split[0]; } + + myKey = split[0]; } else if (myKey.contains(":")) { // when we uses tipped arrow effect types String[] split = myKey.split(":", 2); meta = split.length > 1 ? split[1] : myKey; @@ -575,6 +574,9 @@ public class ConfigManager { myKey = split[0]; } + String type = null; + int id = 0; + CMIMaterial material = CMIMaterial.NONE; switch (actionType) { diff --git a/src/main/java/com/gamingmesh/jobs/config/ScheduleManager.java b/src/main/java/com/gamingmesh/jobs/config/ScheduleManager.java index 168998ac..c331fc5c 100644 --- a/src/main/java/com/gamingmesh/jobs/config/ScheduleManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/ScheduleManager.java @@ -63,16 +63,15 @@ public class ScheduleManager { int from = one.getFrom(); int until = one.getUntil(); - List days = one.getDays(); - if (one.isStarted() && one.getBroadcastInfoOn() < System.currentTimeMillis() && one.getBroadcastInterval() > 0) { one.setBroadcastInfoOn(System.currentTimeMillis() + one.getBroadcastInterval() * 60 * 1000); plugin.getComplement().broadcastMessage(one.getMessageToBroadcast()); } + boolean containsDays = one.getDays().contains(currentDayName) || one.getDays().contains("all"); + if (((one.isNextDay() && (current >= from && current < until || current >= one.getNextFrom() && current < one.getNextUntil()) && !one - .isStarted()) || !one.isNextDay() && (current >= from && current < until)) && (days.contains(currentDayName) || days.contains("all")) && !one - .isStarted()) { + .isStarted()) || !one.isNextDay() && (current >= from && current < until)) && containsDays && !one.isStarted()) { JobsScheduleStartEvent event = new JobsScheduleStartEvent(one); Bukkit.getPluginManager().callEvent(event); @@ -95,8 +94,8 @@ public class ScheduleManager { one.setStarted(true); one.setStoped(false); break; - } else if (((one.isNextDay() && current > one.getNextUntil() && current < one.getFrom() && !one.isStoped()) || !one.isNextDay() && current > until - && ((days.contains(currentDayName)) || days.contains("all"))) && !one.isStoped()) { + } else if (((one.isNextDay() && current > one.getNextUntil() && current < one.getFrom() && !one.isStoped()) || (!one.isNextDay() && current > until + && containsDays)) && !one.isStoped()) { JobsScheduleStopEvent event = new JobsScheduleStopEvent(one); Bukkit.getPluginManager().callEvent(event); if (event.isCancelled()) { @@ -194,11 +193,11 @@ public class ScheduleManager { sched.setMessageToBroadcast(path.getStringList("BroadcastMessage"), path.getString("From"), path.getString("Until")); if (path.isDouble("Exp")) - sched.setBoost(CurrencyType.EXP, path.getDouble("Exp", 0D)); + sched.setBoost(CurrencyType.EXP, path.getDouble("Exp")); if (path.isDouble("Money")) - sched.setBoost(CurrencyType.MONEY, path.getDouble("Money", 0D)); + sched.setBoost(CurrencyType.MONEY, path.getDouble("Money")); if (path.isDouble("Points")) - sched.setBoost(CurrencyType.POINTS, path.getDouble("Points", 0D)); + sched.setBoost(CurrencyType.POINTS, path.getDouble("Points")); BOOSTSCHEDULE.add(sched); } diff --git a/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java b/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java index 0c358f1a..128d2428 100644 --- a/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java +++ b/src/main/java/com/gamingmesh/jobs/dao/JobsDAO.java @@ -516,29 +516,6 @@ public abstract class JobsDAO { } } - public void setAutoCommit(boolean state) { - JobsConnection conn = getConnection(); - if (conn == null) - return; - try { - conn.setAutoCommit(state); - } catch (SQLException e) { - e.printStackTrace(); - } - } - - public void commit() { - JobsConnection conn = getConnection(); - if (conn == null) - return; - - try { - conn.commit(); - } catch (SQLException e) { - e.printStackTrace(); - } - } - private boolean createDefaultTable(DBTables table) { if (isTable(table.getTableName())) return true; @@ -2623,12 +2600,8 @@ public abstract class JobsDAO { return; } - Statement stmt = null; - try { - stmt = conn.createStatement(); + try (Statement stmt = conn.createStatement()) { stmt.execute(sql); - } finally { - close(stmt); } } diff --git a/src/main/java/com/gamingmesh/jobs/dao/JobsManager.java b/src/main/java/com/gamingmesh/jobs/dao/JobsManager.java index a6cffad4..b2b18d54 100644 --- a/src/main/java/com/gamingmesh/jobs/dao/JobsManager.java +++ b/src/main/java/com/gamingmesh/jobs/dao/JobsManager.java @@ -32,6 +32,7 @@ public class JobsManager { // If it MySQL lets change to SqLite dbType = DataBaseType.SqLite; dao = startSqlite(); + if (dao != null) dao.setDbType(dbType); break; @@ -39,6 +40,7 @@ public class JobsManager { // If it SqLite lets change to MySQL dbType = DataBaseType.MySQL; dao = startMysql(); + if (dao != null) dao.setDbType(dbType); break; @@ -64,6 +66,7 @@ public class JobsManager { public void start() { ConfigReader c = Jobs.getGCManager().getConfig(); + c.addComment("storage.method", "storage method, can be MySQL or sqlite"); String storageMethod = c.get("storage.method", "sqlite"); c.addComment("mysql", "Requires Mysql"); @@ -82,26 +85,31 @@ public class JobsManager { if (storageMethod.equalsIgnoreCase("mysql")) { dbType = DataBaseType.MySQL; dao = startMysql(); - } else if (storageMethod.equalsIgnoreCase("sqlite")) { - dbType = DataBaseType.SqLite; - dao = startSqlite(); } else { - Jobs.consoleMsg("&cInvalid storage method! Changing method to sqlite!"); - c.set("storage.method", "sqlite"); + if (!storageMethod.equalsIgnoreCase("sqlite")) { + Jobs.consoleMsg("&cInvalid storage method! Changing method to sqlite!"); + c.set("storage.method", "sqlite"); + } + dbType = DataBaseType.SqLite; dao = startSqlite(); } + Jobs.setDAO(dao); } private synchronized JobsMySQL startMysql() { ConfigReader c = Jobs.getGCManager().getConfig(); String legacyUrl = c.getC().getString("mysql.url"); + if (legacyUrl != null) { String jdbcString = "jdbc:mysql://"; + if (legacyUrl.toLowerCase().startsWith(jdbcString)) { legacyUrl = legacyUrl.substring(jdbcString.length()); - String[] parts = legacyUrl.split("/"); + + String[] parts = legacyUrl.split("/", 2); + if (parts.length >= 2) { hostname = c.get("mysql.hostname", parts[0]); database = c.get("mysql.database", parts[1]); diff --git a/src/main/java/com/gamingmesh/jobs/listeners/JobsListener.java b/src/main/java/com/gamingmesh/jobs/listeners/JobsListener.java index 7f48d153..9dc51267 100644 --- a/src/main/java/com/gamingmesh/jobs/listeners/JobsListener.java +++ b/src/main/java/com/gamingmesh/jobs/listeners/JobsListener.java @@ -437,12 +437,9 @@ public class JobsListener implements Listener { return false; } - @EventHandler + @EventHandler(ignoreCancelled = true) public void onChunkChangeMove(PlayerMoveEvent event) { - if (event.isCancelled() || !event.getPlayer().isOnline()) - return; - - if (event.getTo() != null && !Jobs.getGCManager().canPerformActionInWorld(event.getTo().getWorld())) + if (!event.getPlayer().isOnline() || event.getTo() != null && !Jobs.getGCManager().canPerformActionInWorld(event.getTo().getWorld())) return; Chunk from = event.getFrom().getChunk(); @@ -451,12 +448,10 @@ public class JobsListener implements Listener { plugin.getServer().getPluginManager().callEvent(new JobsChunkChangeEvent(event.getPlayer(), from, to)); } - @EventHandler + @EventHandler(ignoreCancelled = true) public void onInventoryClick(InventoryClickEvent event) { - if (event.isCancelled()) - return; - boolean shift = false, numberkey = false; + if (event.getClick() == ClickType.SHIFT_LEFT || event.getClick() == ClickType.SHIFT_RIGHT) shift = true; @@ -563,11 +558,8 @@ public class JobsListener implements Listener { } } - @EventHandler + @EventHandler(ignoreCancelled = true) public void dispenserFireEvent(BlockDispenseEvent event) { - if (event.isCancelled()) - return; - ItemStack item = event.getItem(); ArmorTypes type = ArmorTypes.matchType(item); if (type == null) diff --git a/src/main/java/com/gamingmesh/jobs/listeners/JobsPayment14Listener.java b/src/main/java/com/gamingmesh/jobs/listeners/JobsPayment14Listener.java index 5d6a6fa1..4d2802fc 100644 --- a/src/main/java/com/gamingmesh/jobs/listeners/JobsPayment14Listener.java +++ b/src/main/java/com/gamingmesh/jobs/listeners/JobsPayment14Listener.java @@ -24,9 +24,9 @@ public final class JobsPayment14Listener implements Listener { // BlockCookEvent does not have "cooking owner" private final Map> campPlayers = new HashMap<>(); - @EventHandler(priority = EventPriority.LOW) + @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) public void onCook(BlockCookEvent event) { - if (event.isCancelled() || !(event.getBlock().getType() != Material.CAMPFIRE) || campPlayers.isEmpty()) + if (!(event.getBlock().getType() != Material.CAMPFIRE) || campPlayers.isEmpty()) return; if (!Jobs.getGCManager().canPerformActionInWorld(event.getBlock().getWorld())) @@ -59,7 +59,7 @@ public final class JobsPayment14Listener implements Listener { } } - @EventHandler(ignoreCancelled = true) + @EventHandler public void onBlockBreak(BlockBreakEvent event) { if (event.getBlock().getType() != Material.CAMPFIRE || campPlayers.isEmpty()) return; @@ -95,7 +95,7 @@ public final class JobsPayment14Listener implements Listener { if (click == null || click.getType() != Material.CAMPFIRE || !ev.hasItem()) return; - if (!Jobs.getGCManager().canPerformActionInWorld(click.getWorld()) || !JobsPaymentListener.payIfCreative(ev.getPlayer())) + if (!JobsPaymentListener.payIfCreative(ev.getPlayer()) || !Jobs.getGCManager().canPerformActionInWorld(click.getWorld())) return; List camps = campPlayers.getOrDefault(ev.getPlayer().getUniqueId(), new ArrayList<>()); diff --git a/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java b/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java index e955910e..c99a5b74 100644 --- a/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java +++ b/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java @@ -120,12 +120,6 @@ public class JobsPaymentListener implements Listener { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void villagerTradeInventoryClick(InventoryClickEvent event) { - if (event.isCancelled()) - return; - - if (!Jobs.getGCManager().canPerformActionInWorld(event.getWhoClicked().getWorld())) - return; - // If event is nothing or place, do nothing switch (event.getAction()) { case NOTHING: @@ -137,6 +131,9 @@ public class JobsPaymentListener implements Listener { break; } + if (!Jobs.getGCManager().canPerformActionInWorld(event.getWhoClicked().getWorld())) + return; + if (event.getInventory().getType() != InventoryType.MERCHANT || event.getSlot() != 2 || event.getSlotType() != SlotType.RESULT) return; @@ -915,9 +912,6 @@ public class JobsPaymentListener implements Listener { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onEnchantItem(EnchantItemEvent event) { - if (event.isCancelled()) - return; - if (!Jobs.getGCManager().canPerformActionInWorld(event.getEnchanter().getWorld())) return; @@ -1548,11 +1542,8 @@ public class JobsPaymentListener implements Listener { } } - @EventHandler(priority = EventPriority.MONITOR) + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onPlayerInteract(PlayerInteractEvent event) { - if (event.isCancelled()) - return; - Player p = event.getPlayer(); if (!Jobs.getGCManager().canPerformActionInWorld(p.getWorld())) return; @@ -1643,9 +1634,9 @@ public class JobsPaymentListener implements Listener { } } - @EventHandler + @EventHandler(ignoreCancelled = true) public void onExplore(JobsChunkChangeEvent event) { - if (event.isCancelled() || !Jobs.getExplore().isExploreEnabled()) + if (!Jobs.getExplore().isExploreEnabled()) return; Player player = event.getPlayer(); diff --git a/src/main/java/com/gamingmesh/jobs/listeners/PistonProtectionListener.java b/src/main/java/com/gamingmesh/jobs/listeners/PistonProtectionListener.java index 00c31145..77899065 100644 --- a/src/main/java/com/gamingmesh/jobs/listeners/PistonProtectionListener.java +++ b/src/main/java/com/gamingmesh/jobs/listeners/PistonProtectionListener.java @@ -17,8 +17,7 @@ public class PistonProtectionListener implements Listener { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onBlockMove(BlockPistonExtendEvent event) { - if (!Jobs.getGCManager().useBlockProtection || event.isCancelled() - || !Jobs.getGCManager().canPerformActionInWorld(event.getBlock().getWorld())) + if (!Jobs.getGCManager().useBlockProtection || !Jobs.getGCManager().canPerformActionInWorld(event.getBlock().getWorld())) return; BlockFace dir = event.getDirection(); @@ -39,8 +38,7 @@ public class PistonProtectionListener implements Listener { @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void onBlockRetractMove(BlockPistonRetractEvent event) { - if (!Jobs.getGCManager().useBlockProtection || event.isCancelled() - || !Jobs.getGCManager().canPerformActionInWorld(event.getBlock().getWorld())) + if (!Jobs.getGCManager().useBlockProtection || !Jobs.getGCManager().canPerformActionInWorld(event.getBlock().getWorld())) return; BlockFace dir = event.getDirection();