Fixed TalismanEquipEvent with /clear
This commit is contained in:
@@ -29,6 +29,13 @@ import java.util.List;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class TalismansPlugin extends AbstractEcoPlugin {
|
||||
/**
|
||||
* Listeners to call {@link com.willfp.talismans.talismans.util.equipevent.TalismanEquipEvent}.
|
||||
* <p>
|
||||
* Instance stored to reschedule timer.
|
||||
*/
|
||||
private final TalismanEquipEventListeners talismanEquipEventListeners = new TalismanEquipEventListeners(this);
|
||||
|
||||
/**
|
||||
* Internal constructor called by bukkit on plugin load.
|
||||
*/
|
||||
@@ -87,6 +94,8 @@ public class TalismansPlugin extends AbstractEcoPlugin {
|
||||
}
|
||||
}, 1);
|
||||
});
|
||||
|
||||
talismanEquipEventListeners.scheduleAutocheck();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -149,7 +158,7 @@ public class TalismansPlugin extends AbstractEcoPlugin {
|
||||
new WatcherTriggers(this),
|
||||
new BlockPlaceListener(),
|
||||
new TalismanCraftListener(),
|
||||
new TalismanEquipEventListeners(this)
|
||||
talismanEquipEventListeners
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -18,9 +18,16 @@ import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
public class TalismanEquipEventListeners extends PluginDependent implements Listener {
|
||||
private final Map<UUID, Set<Talisman>> syncCache = Collections.synchronizedMap(new HashMap<>());
|
||||
|
||||
/**
|
||||
* Initialize new listeners and link them to a plugin.
|
||||
*
|
||||
@@ -123,4 +130,34 @@ public class TalismanEquipEventListeners extends PluginDependent implements List
|
||||
}
|
||||
}, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule sync repeating updater task.
|
||||
*/
|
||||
public void scheduleAutocheck() {
|
||||
this.getPlugin().getScheduler().syncRepeating(() -> {
|
||||
for (Player player : this.getPlugin().getServer().getOnlinePlayers()) {
|
||||
UUID uuid = player.getUniqueId();
|
||||
|
||||
Set<Talisman> before = syncCache.get(uuid);
|
||||
if (before == null) {
|
||||
before = new HashSet<>();
|
||||
}
|
||||
|
||||
Set<Talisman> after = TalismanChecks.getTalismansOnPlayer(player);
|
||||
|
||||
syncCache.put(uuid, after);
|
||||
|
||||
after.removeAll(before);
|
||||
for (Talisman talisman : after) {
|
||||
Bukkit.getPluginManager().callEvent(new TalismanEquipEvent(player, talisman, EquipType.EQUIP));
|
||||
}
|
||||
|
||||
before.removeAll(after);
|
||||
for (Talisman talisman : before) {
|
||||
Bukkit.getPluginManager().callEvent(new TalismanEquipEvent(player, talisman, EquipType.UNEQUIP));
|
||||
}
|
||||
}
|
||||
}, 80, 80);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user