From 2ccb1efd730d4ee0f19dc3981bdf2ea8f1c8f4e7 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Tue, 9 Mar 2021 19:01:12 +0000 Subject: [PATCH] Continued working on spell recode --- .../enchantments/itemtypes/Spell.java | 46 +++++++------ .../enchantments/util/SpellRunnable.java | 64 ------------------- 2 files changed, 25 insertions(+), 85 deletions(-) delete mode 100644 eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/SpellRunnable.java diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/itemtypes/Spell.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/itemtypes/Spell.java index 79fa4ce6..b227875a 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/itemtypes/Spell.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/itemtypes/Spell.java @@ -6,7 +6,8 @@ import com.willfp.ecoenchants.enchantments.EcoEnchant; import com.willfp.ecoenchants.enchantments.EcoEnchants; import com.willfp.ecoenchants.enchantments.meta.EnchantmentType; import com.willfp.ecoenchants.enchantments.util.EnchantChecks; -import com.willfp.ecoenchants.enchantments.util.SpellRunnable; +import com.willfp.ecoenchants.enchantments.util.SpellActivateEvent; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.Sound; import org.bukkit.SoundCategory; @@ -23,19 +24,20 @@ import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.UUID; public abstract class Spell extends EcoEnchant { /** - * {@link SpellRunnable}s linked to players. + * The cooldown end times linked to players. */ - private final HashMap tracker = new HashMap<>(); + private final Map tracker = new HashMap<>(); /** * Players currently running spells - prevents listener firing twice. */ - private final Set runningSpell = new HashSet<>(); + private final Set preventDuplicateList = new HashSet<>(); /** * Items that must be left-clicked to activate spells for. @@ -83,11 +85,11 @@ public abstract class Spell extends EcoEnchant { public void onUseEventHandler(@NotNull final PlayerInteractEvent event) { Player player = event.getPlayer(); - if (runningSpell.contains(player.getUniqueId())) { + if (preventDuplicateList.contains(player.getUniqueId())) { return; } - runningSpell.add(player.getUniqueId()); - this.getPlugin().getScheduler().runLater(() -> runningSpell.remove(player.getUniqueId()), 5); + preventDuplicateList.add(player.getUniqueId()); + this.getPlugin().getScheduler().runLater(() -> preventDuplicateList.remove(player.getUniqueId()), 5); if (LEFT_CLICK_ITEMS.contains(player.getInventory().getItemInMainHand().getType())) { if (!(event.getAction().equals(Action.LEFT_CLICK_AIR) || event.getAction().equals(Action.LEFT_CLICK_BLOCK))) { @@ -115,12 +117,9 @@ public abstract class Spell extends EcoEnchant { } if (!tracker.containsKey(player.getUniqueId())) { - tracker.put(player.getUniqueId(), new SpellRunnable(this, player)); + tracker.put(player.getUniqueId(), System.currentTimeMillis() + (long) ((this.getCooldownTime() * 1000L) * Spell.getCooldownMultiplier(player))); } - SpellRunnable runnable = tracker.get(player.getUniqueId()); - runnable.setTask(() -> this.onUse(player, level, event)); - int cooldown = getCooldown(this, player); if (event.getClickedBlock() != null) { @@ -139,13 +138,20 @@ public abstract class Spell extends EcoEnchant { String message = this.getPlugin().getLangYml().getMessage("on-cooldown").replace("%seconds%", String.valueOf(cooldown)).replace("%name%", EnchantmentCache.getEntry(this).getRawName()); player.sendMessage(message); player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_BASS, 1, 0.5f); - return; - } + } else { + tracker.remove(player.getUniqueId()); - String message = this.getPlugin().getLangYml().getMessage("used-spell").replace("%name%", EnchantmentCache.getEntry(this).getRawName()); - player.sendMessage(message); - player.playSound(player.getLocation(), this.getActivationSound(), SoundCategory.PLAYERS, 1, 1); - runnable.run(); + SpellActivateEvent spellActivateEvent = new SpellActivateEvent(player, this); + Bukkit.getPluginManager().callEvent(spellActivateEvent); + + if (!spellActivateEvent.isCancelled()) { + String message = this.getPlugin().getLangYml().getMessage("used-spell").replace("%name%", EnchantmentCache.getEntry(this).getRawName()); + player.sendMessage(message); + player.playSound(player.getLocation(), this.getActivationSound(), SoundCategory.PLAYERS, 1, 1); + + onUse(player, level, event); + } + } } /** @@ -178,12 +184,10 @@ public abstract class Spell extends EcoEnchant { public static int getCooldown(@NotNull final Spell spell, @NotNull final Player player) { if (!spell.tracker.containsKey(player.getUniqueId())) { - spell.tracker.put(player.getUniqueId(), new SpellRunnable(spell, player)); + return 0; } - SpellRunnable runnable = spell.tracker.get(player.getUniqueId()); - - long msLeft = runnable.getEndTime() - System.currentTimeMillis(); + long msLeft = spell.tracker.get(player.getUniqueId()) - System.currentTimeMillis(); long secondsLeft = (long) Math.ceil((double) msLeft / 1000); diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/SpellRunnable.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/SpellRunnable.java deleted file mode 100644 index 65c8522c..00000000 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/util/SpellRunnable.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.willfp.ecoenchants.enchantments.util; - - -import com.willfp.ecoenchants.enchantments.itemtypes.Spell; -import lombok.Getter; -import lombok.Setter; -import org.bukkit.entity.Player; -import org.jetbrains.annotations.NotNull; - -public class SpellRunnable { - /** - * The spell that this runnable is for. - */ - @Getter - private final Spell spell; - - /** - * The player that this runnable is for. - */ - private final Player player; - - /** - * The end time of the runnable, in unix time. - */ - @Getter - private long endTime = 0; - - /** - * The actual task to be executed. - *

- * Must be set before execution. - */ - @Setter - private Runnable task = () -> { - // Empty as must be set using this#setTask - }; - - /** - * Create a new Spell Runnable. - * - * @param spell The spell. - * @param player The player. - */ - public SpellRunnable(@NotNull final Spell spell, - @NotNull final Player player) { - this.spell = spell; - this.player = player; - } - - /** - * Run the runnable. - */ - public void run() { - task.run(); - updateEndTime(); - } - - /** - * Update the end time of the spell runnable. - */ - public void updateEndTime() { - endTime = System.currentTimeMillis() + (long) ((spell.getCooldownTime() * 1000L) * Spell.getCooldownMultiplier(player)); - } -}