Implement login/logout events for /magnet

This commit is contained in:
Gabriel Tofvesson 2021-05-02 21:58:59 +02:00
parent f545ce86c8
commit b0bf3ed601
2 changed files with 49 additions and 0 deletions

View File

@ -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);

View File

@ -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);
}
}