-
-
Notifications
You must be signed in to change notification settings - Fork 10
GH-348 Post death command execution #348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Jakubk15
wants to merge
16
commits into
master
Choose a base branch
from
feature/post-death-commands
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8dfe353
WIP: Post death command execution
Jakubk15 e333117
Post death command execution
Jakubk15 16f45ca
Apply suggestions from code review
Jakubk15 13d9c09
fix build
Jakubk15 911ef7b
adjust to gemini's suggestion
Jakubk15 ce87394
Remove CauseOfTag.LOGOUT from the check
Jakubk15 356481e
Update eternalcombat-plugin/src/main/java/com/eternalcode/combat/figh…
Jakubk15 2a99c85
Update eternalcombat-plugin/src/main/java/com/eternalcode/combat/figh…
Jakubk15 320dd90
Adjust to Mike's suggestions. Simplify logic
Jakubk15 903fdaa
Merge remote-tracking branch 'origin/feature/post-death-commands' int…
Jakubk15 cb015b5
Merge branch 'master' into feature/post-death-commands
Jakubk15 15b2267
fix build
Jakubk15 115f8bb
Change EternalCore version to latest stable
Jakubk15 a606358
Adjust to DMK's suggestion
Jakubk15 dc0724b
Move to DeathSettings
Jakubk15 81afc4c
Merge branch 'master' into feature/post-death-commands
Jakubk15 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 169 additions & 0 deletions
169
...ombat-plugin/src/main/java/com/eternalcode/combat/fight/death/DeathCommandController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| package com.eternalcode.combat.fight.death; | ||
|
Jakubk15 marked this conversation as resolved.
|
||
|
|
||
| import com.eternalcode.combat.config.implementation.PluginConfig; | ||
| import com.eternalcode.combat.fight.FightManager; | ||
| import com.eternalcode.combat.fight.FightTag; | ||
| import com.eternalcode.combat.fight.event.CauseOfUnTag; | ||
| import com.eternalcode.combat.fight.event.FightUntagEvent; | ||
| import org.bukkit.Server; | ||
| import org.bukkit.entity.Player; | ||
| import org.bukkit.event.EventHandler; | ||
| import org.bukkit.event.EventPriority; | ||
| import org.bukkit.event.Listener; | ||
| import org.bukkit.event.entity.PlayerDeathEvent; | ||
| import org.bukkit.event.player.PlayerRespawnEvent; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| public class DeathCommandController implements Listener { | ||
|
Jakubk15 marked this conversation as resolved.
|
||
|
|
||
| private final PluginConfig config; | ||
| private final FightManager fightManager; | ||
| private final Server server; | ||
|
|
||
| private final Map<UUID, List<PendingCommand>> pendingCommands = new ConcurrentHashMap<>(); | ||
| private final Set<UUID> handledByUntag = Collections.newSetFromMap(new ConcurrentHashMap<>()); | ||
|
|
||
| public DeathCommandController(PluginConfig config, FightManager fightManager, Server server) { | ||
| this.config = config; | ||
| this.fightManager = fightManager; | ||
| this.server = server; | ||
| } | ||
|
|
||
| @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) | ||
| void onPlayerUntag(FightUntagEvent event) { | ||
| UUID playerUUID = event.getPlayer(); | ||
| CauseOfUnTag cause = event.getCause(); | ||
|
|
||
| if (cause != CauseOfUnTag.DEATH && cause != CauseOfUnTag.DEATH_BY_PLAYER) { | ||
| return; | ||
| } | ||
|
|
||
| Player deadPlayer = this.server.getPlayer(playerUUID); | ||
|
|
||
| if (deadPlayer == null) { | ||
| return; | ||
| } | ||
|
|
||
| this.handledByUntag.add(playerUUID); | ||
| this.executeDeathCommands(deadPlayer); | ||
| } | ||
|
|
||
| @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) | ||
| void onPlayerDeath(PlayerDeathEvent event) { | ||
| if (this.config.commands.onlyExecuteIfTagged) { | ||
| return; | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tested it and I wasn't able to produce any bugs, however I encourage other team members to try it nevertheless |
||
|
|
||
| Player deadPlayer = event.getEntity(); | ||
| UUID playerUUID = deadPlayer.getUniqueId(); | ||
|
|
||
| if (this.handledByUntag.remove(playerUUID)) { | ||
| return; | ||
| } | ||
|
|
||
| this.executeDeathCommands(deadPlayer); | ||
| } | ||
|
|
||
| @EventHandler(priority = EventPriority.MONITOR) | ||
| void onPlayerRespawn(PlayerRespawnEvent event) { | ||
| Player player = event.getPlayer(); | ||
| UUID playerUUID = player.getUniqueId(); | ||
|
|
||
| this.handledByUntag.remove(playerUUID); | ||
|
|
||
| List<PendingCommand> commands = this.pendingCommands.remove(playerUUID); | ||
|
|
||
| if (commands == null) { | ||
| return; | ||
| } | ||
|
|
||
| for (PendingCommand pending : commands) { | ||
| switch (pending.executor()) { | ||
| case CONSOLE -> this.server.dispatchCommand(this.server.getConsoleSender(), pending.command()); | ||
| case DEAD_PLAYER -> this.server.dispatchCommand(player, pending.command()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void executeDeathCommands(Player deadPlayer) { | ||
|
Jakubk15 marked this conversation as resolved.
Outdated
|
||
| UUID playerUUID = deadPlayer.getUniqueId(); | ||
| String deadPlayerName = deadPlayer.getName(); | ||
| String killerName = this.resolveKillerName(playerUUID, deadPlayer); | ||
|
|
||
| List<PendingCommand> deferred = new ArrayList<>(); | ||
|
|
||
| for (String command : this.config.commands.consolePostDeathCommands) { | ||
| String resolved = this.replacePlaceholders(command, deadPlayerName, killerName); | ||
| if (this.config.commands.deferConsoleAfterRespawn) { | ||
| deferred.add(new PendingCommand(CommandSource.CONSOLE, resolved)); | ||
| } else { | ||
| this.server.dispatchCommand(this.server.getConsoleSender(), resolved); | ||
| } | ||
| } | ||
|
|
||
| for (String command : this.config.commands.deadPostDeathCommands) { | ||
| String resolved = this.replacePlaceholders(command, deadPlayerName, killerName); | ||
|
Jakubk15 marked this conversation as resolved.
Outdated
|
||
| if (this.config.commands.deferDeadAfterRespawn) { | ||
| deferred.add(new PendingCommand(CommandSource.DEAD_PLAYER, resolved)); | ||
| } else { | ||
| this.server.dispatchCommand(deadPlayer, resolved); | ||
| } | ||
| } | ||
|
|
||
| Player killer = this.resolveKiller(playerUUID, deadPlayer); | ||
|
|
||
| if (killer != null) { | ||
| for (String command : this.config.commands.killerPostDeathCommands) { | ||
| String resolved = this.replacePlaceholders(command, deadPlayerName, killerName); | ||
| this.server.dispatchCommand(killer, resolved); | ||
| } | ||
| } | ||
|
|
||
| if (!deferred.isEmpty()) { | ||
| this.pendingCommands.put(playerUUID, deferred); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| private String resolveKillerName(UUID deadPlayerUUID, Player deadPlayer) { | ||
| Player killer = this.resolveKiller(deadPlayerUUID, deadPlayer); | ||
| return killer != null ? killer.getName() : this.config.commands.unknownKillerPlaceholder; | ||
|
Jakubk15 marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| private Player resolveKiller(UUID deadPlayerUUID, Player deadPlayer) { | ||
| Player killer = deadPlayer.getKiller(); | ||
|
|
||
| if (killer != null) { | ||
| return killer; | ||
| } | ||
|
|
||
| FightTag tag = this.fightManager.getTag(deadPlayerUUID); | ||
|
|
||
| if (tag != null && tag.getTagger() != null) { | ||
| return this.server.getPlayer(tag.getTagger()); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| private String replacePlaceholders(String command, String playerName, String killerName) { | ||
| return command | ||
| .replace("{player}", playerName) | ||
| .replace("{killer}", killerName); | ||
|
Jakubk15 marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| private enum CommandSource { | ||
| CONSOLE, | ||
| DEAD_PLAYER | ||
| } | ||
|
|
||
| private record PendingCommand(CommandSource executor, String command) { | ||
|
Jakubk15 marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.