From 3b5fde475dabcd9466fc587871d8f6a932a252a7 Mon Sep 17 00:00:00 2001 From: MetallicGoat Date: Thu, 23 May 2024 13:06:35 -0400 Subject: [PATCH] Optimize AlwaysSword feature --- pom.xml | 2 +- .../tweaks/advancedswords/AlwaysSword.java | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index 7991e06..a7df659 100644 --- a/pom.xml +++ b/pom.xml @@ -33,7 +33,7 @@ me.clip placeholderapi - 2.11.3 + 2.11.6 provided diff --git a/src/main/java/me/metallicgoat/tweaksaddon/tweaks/advancedswords/AlwaysSword.java b/src/main/java/me/metallicgoat/tweaksaddon/tweaks/advancedswords/AlwaysSword.java index 4b37576..3ed18d3 100644 --- a/src/main/java/me/metallicgoat/tweaksaddon/tweaks/advancedswords/AlwaysSword.java +++ b/src/main/java/me/metallicgoat/tweaksaddon/tweaks/advancedswords/AlwaysSword.java @@ -16,10 +16,15 @@ import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; +import org.bukkit.scheduler.BukkitTask; + +import java.util.HashMap; +import java.util.Map; public class AlwaysSword implements Listener { - // TODO keep track of running instances + final Map runningTasks = new HashMap<>(); + @EventHandler public void onClick(InventoryClickEvent e) { if (!SwordsToolsConfig.always_sword_drop_enabled) @@ -32,7 +37,10 @@ public void onClick(InventoryClickEvent e) { && arena.getStatus() == ArenaStatus.RUNNING && player.getGameMode() != GameMode.SPECTATOR) { - Bukkit.getServer().getScheduler().runTaskLater((MBedwarsTweaksPlugin.getInstance()), () -> { + if (runningTasks.containsKey(player)) + runningTasks.get(player).cancel(); + + runningTasks.put(player, Bukkit.getServer().getScheduler().runTaskLater((MBedwarsTweaksPlugin.getInstance()), () -> { final Inventory pi = player.getInventory(); final int i = ToolSwordHelper.getSwordsAmount(player); @@ -54,9 +62,10 @@ public void onClick(InventoryClickEvent e) { (e.getCursor() == null || e.getCursor() != null && !e.getCursor().getType().name().endsWith("SWORD") && ToolSwordHelper.isNotToIgnore(e.getCursor()))) { pi.addItem(ToolSwordHelper.getDefaultWoodSword(player, arena)); - } - }, 25L); + + runningTasks.remove(player); + }, 25L)); } }