Skip to content

Commit

Permalink
Improve elimination removal after joining
Browse files Browse the repository at this point in the history
  • Loading branch information
zrdzn committed Aug 4, 2023
1 parent 14ad268 commit b49b502
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -25,29 +28,46 @@ 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
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<Elimination> 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<String> 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) {
Expand All @@ -56,25 +76,19 @@ 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);
});

// 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());
});
});
}
Expand Down

0 comments on commit b49b502

Please sign in to comment.