diff --git a/src/dev/w1zzrd/invtweaks/command/MagnetCommandExecutor.java b/src/dev/w1zzrd/invtweaks/command/MagnetCommandExecutor.java index 2d01f49..c7bb9d6 100644 --- a/src/dev/w1zzrd/invtweaks/command/MagnetCommandExecutor.java +++ b/src/dev/w1zzrd/invtweaks/command/MagnetCommandExecutor.java @@ -241,7 +241,15 @@ public class MagnetCommandExecutor implements CommandExecutor { divIndex = (divIndex + 1) % subdivide; } + public void onMagnetLogout(final UUID magnet) { + if (magnetData.removeLogoutOnline(magnet)) + updateMagnetismTask(true); + } + public void onMagnetLogin(final UUID magnet) { + if (magnetData.addLoginOnline(magnet)) + updateMagnetismTask(false); + } public void onDisable() { data.storeData("magnets", magnetData); diff --git a/src/dev/w1zzrd/invtweaks/listener/MagnetismListener.java b/src/dev/w1zzrd/invtweaks/listener/MagnetismListener.java new file mode 100644 index 0000000..2c43663 --- /dev/null +++ b/src/dev/w1zzrd/invtweaks/listener/MagnetismListener.java @@ -0,0 +1,41 @@ +package dev.w1zzrd.invtweaks.listener; + +import dev.w1zzrd.invtweaks.command.MagnetCommandExecutor; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerKickEvent; +import org.bukkit.event.player.PlayerQuitEvent; + +public class MagnetismListener implements Listener { + + private final MagnetCommandExecutor magnetCommandExecutor; + + public MagnetismListener(final MagnetCommandExecutor magnetCommandExecutor) { + this.magnetCommandExecutor = magnetCommandExecutor; + } + + @EventHandler + public void onPlayerJoinEvent(final PlayerJoinEvent event) { + magnetCommandExecutor.onMagnetLogin(event.getPlayer().getUniqueId()); + } + + @EventHandler + public void onPlayerDisconnectEvent(final PlayerQuitEvent event) { + onLeave(event.getPlayer()); + } + + @EventHandler + public void onPlayerKickedEvent(final PlayerKickEvent event) { + onLeave(event.getPlayer()); + } + + private void onLeave(final Player player) { + magnetCommandExecutor.onMagnetLogout(player.getUniqueId()); + } + + private boolean shouldNotifyExecutor(final Player player) { + return magnetCommandExecutor.isMagnet(player); + } +}