mirror of
https://github.com/Auxilor/EcoEnchants.git
synced 2024-11-22 15:05:18 +01:00
Continued working on spell recode
This commit is contained in:
parent
4b058ab73a
commit
2ccb1efd73
@ -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<UUID, SpellRunnable> tracker = new HashMap<>();
|
||||
private final Map<UUID, Long> tracker = new HashMap<>();
|
||||
|
||||
/**
|
||||
* Players currently running spells - prevents listener firing twice.
|
||||
*/
|
||||
private final Set<UUID> runningSpell = new HashSet<>();
|
||||
private final Set<UUID> 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);
|
||||
|
||||
|
@ -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.
|
||||
* <p>
|
||||
* 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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user