From db397894edd80867b7ef2a1b357916d01794227c Mon Sep 17 00:00:00 2001 From: Simon Date: Sun, 3 May 2020 20:54:27 +0200 Subject: [PATCH] feat: kick and ban player on death --- .../varo/listeners/PlayerDeathListener.java | 39 +++++++++++++++++++ .../listeners/PlayerPreLoginListener.java | 28 +++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 varo/src/main/java/de/cliffbreak/varo/listeners/PlayerDeathListener.java create mode 100644 varo/src/main/java/de/cliffbreak/varo/listeners/PlayerPreLoginListener.java diff --git a/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerDeathListener.java b/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerDeathListener.java new file mode 100644 index 0000000..95db3e7 --- /dev/null +++ b/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerDeathListener.java @@ -0,0 +1,39 @@ +package de.cliffbreak.varo.listeners; + +import java.util.ArrayList; + +import org.bukkit.Bukkit; +import org.bukkit.GameMode; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.PlayerDeathEvent; + +import de.cliffbreak.varo.Varo; + +public class PlayerDeathListener implements Listener { + + private final Varo plugin; + + public PlayerDeathListener(final Varo plugin) { + this.plugin = plugin; + } + + @EventHandler() + public void onPlayerDeath(final PlayerDeathEvent e) { + Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { + @Override + public void run() { + e.getEntity().kickPlayer("§4Du bist gestorben.\n \n§cDamit bist du aus §eVaro§c ausgeschieden."); + } + }, 20); + + e.getEntity().setHealth(20d); // Reset health to prevent death dialog after rejoin (if unbanned) + e.getEntity().setGameMode(GameMode.SPECTATOR); + @SuppressWarnings("unchecked") + final ArrayList bans = (ArrayList) this.plugin.config.get("Varo.Bans"); + bans.add(e.getEntity().getUniqueId().toString().replace("-", "")); + this.plugin.config.set("Varo.Bans", bans); + this.plugin.saveConfiguration(); + } + +} \ No newline at end of file diff --git a/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerPreLoginListener.java b/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerPreLoginListener.java new file mode 100644 index 0000000..aad6e97 --- /dev/null +++ b/varo/src/main/java/de/cliffbreak/varo/listeners/PlayerPreLoginListener.java @@ -0,0 +1,28 @@ +package de.cliffbreak.varo.listeners; + +import java.util.ArrayList; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.AsyncPlayerPreLoginEvent; +import org.bukkit.event.player.AsyncPlayerPreLoginEvent.Result; + +import de.cliffbreak.varo.Varo; + +public class PlayerPreLoginListener implements Listener { + + private final Varo plugin; + + public PlayerPreLoginListener(final Varo plugin) { + this.plugin = plugin; + } + + @EventHandler() + public void onPlayerPreLogin(final AsyncPlayerPreLoginEvent e) { + @SuppressWarnings("unchecked") + final ArrayList bans = (ArrayList) this.plugin.config.get("Varo.Bans"); + if (bans.contains(e.getPlayerProfile().getId().toString().replace("-", ""))) { + e.disallow(Result.KICK_BANNED, "§4Du bist gestorben.\n \n§cDamit bist du aus §eVaro§c ausgeschieden."); + } + } +} \ No newline at end of file