From b49b5027ae5960e44b82dfa4e7ffc1cda62da2d0 Mon Sep 17 00:00:00 2001 From: zrdzn Date: Fri, 4 Aug 2023 20:13:57 +0200 Subject: [PATCH] Improve elimination removal after joining --- .../greatlifesteal/GreatLifeStealPlugin.java | 10 +-- .../EliminationJoinPreventListener.java | 68 ------------------- .../revive/ReviveAwaitingQueue.java | 27 -------- .../revive/ReviveRestoreHealthListener.java | 48 ++++++++----- 4 files changed, 32 insertions(+), 121 deletions(-) delete mode 100644 greatlifesteal-plugin/src/main/java/io/github/zrdzn/minecraft/greatlifesteal/elimination/EliminationJoinPreventListener.java delete mode 100644 greatlifesteal-plugin/src/main/java/io/github/zrdzn/minecraft/greatlifesteal/elimination/revive/ReviveAwaitingQueue.java diff --git a/greatlifesteal-plugin/src/main/java/io/github/zrdzn/minecraft/greatlifesteal/GreatLifeStealPlugin.java b/greatlifesteal-plugin/src/main/java/io/github/zrdzn/minecraft/greatlifesteal/GreatLifeStealPlugin.java index 42cf024..8ead7e2 100644 --- a/greatlifesteal-plugin/src/main/java/io/github/zrdzn/minecraft/greatlifesteal/GreatLifeStealPlugin.java +++ b/greatlifesteal-plugin/src/main/java/io/github/zrdzn/minecraft/greatlifesteal/GreatLifeStealPlugin.java @@ -14,8 +14,6 @@ import io.github.zrdzn.minecraft.greatlifesteal.command.LifeStealTabCompleter; import io.github.zrdzn.minecraft.greatlifesteal.elimination.EliminationFacade; import io.github.zrdzn.minecraft.greatlifesteal.elimination.EliminationFacadeFactory; -import io.github.zrdzn.minecraft.greatlifesteal.elimination.EliminationJoinPreventListener; -import io.github.zrdzn.minecraft.greatlifesteal.elimination.revive.ReviveAwaitingQueue; import io.github.zrdzn.minecraft.greatlifesteal.elimination.revive.ReviveRestoreHealthListener; import io.github.zrdzn.minecraft.greatlifesteal.heart.HeartFacade; import io.github.zrdzn.minecraft.greatlifesteal.heart.HeartItem; @@ -108,17 +106,11 @@ public void onEnable() { UserListener userListener = new UserListener(this, config, eliminationFacade, damageableAdapter, heartFacade, this.heartItem); - ReviveAwaitingQueue reviveAwaitingQueue = new ReviveAwaitingQueue(); - - EliminationJoinPreventListener eliminationJoinPreventListener = new EliminationJoinPreventListener(this, config, - eliminationFacade, reviveAwaitingQueue); - ReviveRestoreHealthListener reviveRestoreHealthListener = new ReviveRestoreHealthListener(this, config, - eliminationFacade, spigotServer.getDamageableAdapter(), reviveAwaitingQueue); + eliminationFacade, spigotServer.getDamageableAdapter()); pluginManager.registerEvents(updateListener, this); pluginManager.registerEvents(userListener, this); - pluginManager.registerEvents(eliminationJoinPreventListener, this); pluginManager.registerEvents(reviveRestoreHealthListener, this); pluginManager.registerEvents(heartUseListener, this); diff --git a/greatlifesteal-plugin/src/main/java/io/github/zrdzn/minecraft/greatlifesteal/elimination/EliminationJoinPreventListener.java b/greatlifesteal-plugin/src/main/java/io/github/zrdzn/minecraft/greatlifesteal/elimination/EliminationJoinPreventListener.java deleted file mode 100644 index 032c404..0000000 --- a/greatlifesteal-plugin/src/main/java/io/github/zrdzn/minecraft/greatlifesteal/elimination/EliminationJoinPreventListener.java +++ /dev/null @@ -1,68 +0,0 @@ -package io.github.zrdzn.minecraft.greatlifesteal.elimination; - -import java.util.List; -import java.util.Optional; -import java.util.UUID; -import io.github.zrdzn.minecraft.greatlifesteal.PluginConfig; -import io.github.zrdzn.minecraft.greatlifesteal.elimination.revive.ReviveAwaitingQueue; -import io.github.zrdzn.minecraft.greatlifesteal.elimination.revive.ReviveStatus; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.player.AsyncPlayerPreLoginEvent; -import org.bukkit.plugin.Plugin; -import org.bukkit.scheduler.BukkitScheduler; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class EliminationJoinPreventListener implements Listener { - - private final Logger logger = LoggerFactory.getLogger(EliminationJoinPreventListener.class); - - private final Plugin plugin; - private final BukkitScheduler scheduler; - private final PluginConfig config; - private final EliminationFacade eliminationFacade; - private final ReviveAwaitingQueue reviveAwaitingQueue; - - public EliminationJoinPreventListener(Plugin plugin, PluginConfig config, EliminationFacade eliminationFacade, - ReviveAwaitingQueue reviveAwaitingQueue) { - this.plugin = plugin; - this.scheduler = plugin.getServer().getScheduler(); - this.config = config; - this.eliminationFacade = eliminationFacade; - this.reviveAwaitingQueue = reviveAwaitingQueue; - } - - @EventHandler - public void checkIfPlayerIsRevived(AsyncPlayerPreLoginEvent event) { - UUID playerUuid = event.getUniqueId(); - - this.scheduler.runTaskAsynchronously(this.plugin, () -> { - Elimination elimination; - try { - Optional eliminationMaybe = this.eliminationFacade.findEliminationByPlayerUuid(playerUuid); - if (!eliminationMaybe.isPresent()) { - return; - } - - elimination = eliminationMaybe.get(); - } catch (EliminationException exception) { - this.logger.error("An error occurred while finding an elimination by player's unique id.", exception); - return; - } - - List disabledWorlds = this.config.getDisabledWorlds().getEliminations(); - - if (disabledWorlds.contains(elimination.getLastWorld())) { - return; - } - - this.scheduler.runTask(this.plugin, () -> { - if (elimination.getRevive() == ReviveStatus.COMPLETED) { - this.reviveAwaitingQueue.addPlayer(playerUuid, elimination.getEliminationKey()); - } - }); - }); - } - -} diff --git a/greatlifesteal-plugin/src/main/java/io/github/zrdzn/minecraft/greatlifesteal/elimination/revive/ReviveAwaitingQueue.java b/greatlifesteal-plugin/src/main/java/io/github/zrdzn/minecraft/greatlifesteal/elimination/revive/ReviveAwaitingQueue.java deleted file mode 100644 index 946acd4..0000000 --- a/greatlifesteal-plugin/src/main/java/io/github/zrdzn/minecraft/greatlifesteal/elimination/revive/ReviveAwaitingQueue.java +++ /dev/null @@ -1,27 +0,0 @@ -package io.github.zrdzn.minecraft.greatlifesteal.elimination.revive; - -import java.util.HashMap; -import java.util.Map; -import java.util.UUID; - -public class ReviveAwaitingQueue { - - private final Map playersWaitingForRevive = new HashMap<>(); - - public void addPlayer(UUID playerUuid, String reviveKey) { - this.playersWaitingForRevive.put(playerUuid, reviveKey); - } - - public boolean isPlayerPresent(UUID playerUuid) { - return this.playersWaitingForRevive.containsKey(playerUuid); - } - - public String getReviveKey(UUID playerUuid) { - return this.playersWaitingForRevive.get(playerUuid); - } - - public void removePlayer(UUID playerUuid) { - this.playersWaitingForRevive.remove(playerUuid); - } - -} diff --git a/greatlifesteal-plugin/src/main/java/io/github/zrdzn/minecraft/greatlifesteal/elimination/revive/ReviveRestoreHealthListener.java b/greatlifesteal-plugin/src/main/java/io/github/zrdzn/minecraft/greatlifesteal/elimination/revive/ReviveRestoreHealthListener.java index 1db41a1..f019805 100644 --- a/greatlifesteal-plugin/src/main/java/io/github/zrdzn/minecraft/greatlifesteal/elimination/revive/ReviveRestoreHealthListener.java +++ b/greatlifesteal-plugin/src/main/java/io/github/zrdzn/minecraft/greatlifesteal/elimination/revive/ReviveRestoreHealthListener.java @@ -1,7 +1,10 @@ package io.github.zrdzn.minecraft.greatlifesteal.elimination.revive; +import java.util.List; +import java.util.Optional; import java.util.UUID; import io.github.zrdzn.minecraft.greatlifesteal.PluginConfig; +import io.github.zrdzn.minecraft.greatlifesteal.elimination.Elimination; import io.github.zrdzn.minecraft.greatlifesteal.elimination.EliminationException; import io.github.zrdzn.minecraft.greatlifesteal.elimination.EliminationFacade; import io.github.zrdzn.minecraft.greatlifesteal.message.MessageFacade; @@ -25,17 +28,14 @@ public class ReviveRestoreHealthListener implements Listener { private final PluginConfig config; private final EliminationFacade eliminationFacade; private final DamageableAdapter damageableAdapter; - private final ReviveAwaitingQueue reviveAwaitingQueue; public ReviveRestoreHealthListener(Plugin plugin, PluginConfig config, EliminationFacade eliminationFacade, - DamageableAdapter damageableAdapter, - ReviveAwaitingQueue reviveAwaitingQueue) { + DamageableAdapter damageableAdapter) { this.plugin = plugin; this.scheduler = plugin.getServer().getScheduler(); this.config = config; this.eliminationFacade = eliminationFacade; this.damageableAdapter = damageableAdapter; - this.reviveAwaitingQueue = reviveAwaitingQueue; } @EventHandler @@ -43,11 +43,31 @@ public void restorePlayerHealth(PlayerJoinEvent event) { Player player = event.getPlayer(); UUID playerUuid = player.getUniqueId(); - if (!this.reviveAwaitingQueue.isPlayerPresent(playerUuid)) { - return; - } - this.scheduler.runTaskAsynchronously(this.plugin, () -> { + Elimination elimination; + try { + Optional eliminationMaybe = this.eliminationFacade.findEliminationByPlayerUuid(playerUuid); + if (!eliminationMaybe.isPresent()) { + return; + } + + elimination = eliminationMaybe.get(); + } catch (EliminationException exception) { + this.logger.error("An error occurred while finding an elimination by player's unique id.", exception); + return; + } + + List disabledWorlds = this.config.getDisabledWorlds().getEliminations(); + + if (disabledWorlds.contains(elimination.getLastWorld())) { + return; + } + + if (elimination.getRevive() != ReviveStatus.COMPLETED) { + return; + } + + // Remove elimination from database. try { this.eliminationFacade.removeEliminationByPlayerUuid(playerUuid); } catch (EliminationException exception) { @@ -56,16 +76,11 @@ public void restorePlayerHealth(PlayerJoinEvent event) { return; } - String reviveKey = this.reviveAwaitingQueue.getReviveKey(playerUuid); - if (reviveKey == null) { - MessageFacade.send(player, this.config.getMessages().getSomethingWentWrong()); - throw new IllegalStateException("Revive key is null."); - } - - ReviveConfig revive = this.config.getRevives().get(reviveKey); + ReviveConfig revive = this.config.getRevives().get(elimination.getEliminationKey()); this.scheduler.runTask(this.plugin, () -> { // Perform post-revive commands. + this.logger.info("Executing post-revive commands for player {}.", player.getName()); revive.getCommands().getAfter().forEach(reviveCommand -> { reviveCommand = MessageFacade.formatPlaceholders(reviveCommand, "{victim}", player.getName()); Bukkit.dispatchCommand(Bukkit.getConsoleSender(), reviveCommand); @@ -73,8 +88,7 @@ public void restorePlayerHealth(PlayerJoinEvent event) { // Set default health. this.damageableAdapter.setMaxHealth(player, this.config.getHealth().getDefaultMaximumHealth()); - - this.reviveAwaitingQueue.removePlayer(playerUuid); + this.logger.info("Set default maximum health for player {}.", player.getName()); }); }); }