-
-
Notifications
You must be signed in to change notification settings - Fork 13
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
25
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 all commits
Commits
Show all changes
25 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 bc83f4d
Merge branch 'master' into feature/post-death-commands
Jakubk15 34b1252
Fix build
Jakubk15 6b09a6f
Add additional comments per Mike's suggestion
Jakubk15 7bc712d
Update eternalcombat-plugin/src/main/java/com/eternalcode/combat/figh…
Jakubk15 ca25b99
Update eternalcombat-plugin/src/main/java/com/eternalcode/combat/figh…
Jakubk15 b9d6a5b
Update placeholders in settings
Jakubk15 ca05679
Add scheduler support for post-death command execution, improve logic…
Jakubk15 fd94fe1
Simplify whole code.
vLuckyyy f6f3e3b
Simplify config declaration.
vLuckyyy 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
Some comments aren't visible on the classic Files Changed page.
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
138 changes: 138 additions & 0 deletions
138
...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,138 @@ | ||
| package com.eternalcode.combat.fight.death; | ||
|
|
||
| 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 com.eternalcode.commons.scheduler.Scheduler; | ||
| import com.github.benmanes.caffeine.cache.Cache; | ||
| import com.github.benmanes.caffeine.cache.Caffeine; | ||
| import java.time.Duration; | ||
| import java.util.UUID; | ||
| 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.PlayerQuitEvent; | ||
| import org.bukkit.event.player.PlayerRespawnEvent; | ||
|
|
||
| public final class DeathCommandController implements Listener { | ||
|
|
||
| private final Cache<UUID, String> killerNames = Caffeine.newBuilder() | ||
| .expireAfterWrite(Duration.ofMinutes(2)) | ||
| .build(); | ||
|
|
||
| private final PluginConfig config; | ||
| private final FightManager fightManager; | ||
| private final DeathCommandDispatcher dispatcher; | ||
| private final Scheduler scheduler; | ||
| private final Server server; | ||
|
|
||
| public DeathCommandController( | ||
| PluginConfig config, | ||
| FightManager fightManager, | ||
| DeathCommandDispatcher dispatcher, | ||
| Scheduler scheduler, | ||
| Server server | ||
| ) { | ||
| this.config = config; | ||
| this.fightManager = fightManager; | ||
| this.dispatcher = dispatcher; | ||
| this.scheduler = scheduler; | ||
| this.server = server; | ||
| } | ||
|
|
||
| @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) | ||
| void onUntag(FightUntagEvent event) { | ||
| Player player = this.server.getPlayer(event.getPlayer()); | ||
|
|
||
| if (player == null) { | ||
| return; | ||
| } | ||
|
|
||
| if (!this.isDeath(event.getCause())) { | ||
| this.scheduler.run(() -> this.dispatchUntag(player)); | ||
| return; | ||
| } | ||
|
|
||
| Player killer = this.resolveKiller(player); | ||
| String killerName = killer != null ? killer.getName() : this.unknownKiller(); | ||
|
|
||
| this.killerNames.put(player.getUniqueId(), killerName); | ||
| this.dispatcher.dispatch(this.config.death.onDeathInCombat, player, killerName); | ||
|
|
||
| if (killer != null) { | ||
| this.dispatcher.dispatch( | ||
| this.config.death.killerPostDeathCommands, | ||
| killer, | ||
| player.getName(), | ||
| killerName | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) | ||
| void onDeath(PlayerDeathEvent event) { | ||
| Player player = event.getEntity(); | ||
|
|
||
| this.dispatcher.dispatch( | ||
| this.config.death.onAnyDeath, | ||
| player, | ||
| this.killerNames.get(player.getUniqueId(), ignored -> this.resolveKillerName(player)) | ||
| ); | ||
| } | ||
|
|
||
| @EventHandler(priority = EventPriority.MONITOR) | ||
| void onRespawn(PlayerRespawnEvent event) { | ||
| Player player = event.getPlayer(); | ||
| String killerName = this.killerNames.asMap().remove(player.getUniqueId()); | ||
|
|
||
| this.dispatcher.dispatch( | ||
| this.config.death.afterRespawn, | ||
| player, | ||
| killerName != null ? killerName : this.unknownKiller() | ||
| ); | ||
| } | ||
|
|
||
| @EventHandler | ||
| void onQuit(PlayerQuitEvent event) { | ||
| this.killerNames.invalidate(event.getPlayer().getUniqueId()); | ||
| } | ||
|
|
||
| private Player resolveKiller(Player player) { | ||
| Player killer = player.getKiller(); | ||
|
|
||
| if (killer != null) { | ||
| return killer; | ||
| } | ||
|
|
||
| FightTag tag = this.fightManager.getTag(player.getUniqueId()); | ||
| return tag == null ? null : this.server.getPlayer(tag.getTagger()); | ||
| } | ||
|
|
||
| private String resolveKillerName(Player player) { | ||
| Player killer = this.resolveKiller(player); | ||
| return killer != null ? killer.getName() : this.unknownKiller(); | ||
| } | ||
|
|
||
| private void dispatchUntag(Player player) { | ||
| if (player.isOnline()) { | ||
| this.dispatcher.dispatch( | ||
| this.config.death.onUntag, | ||
| player, | ||
| this.unknownKiller() | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| private boolean isDeath(CauseOfUnTag cause) { | ||
| return cause == CauseOfUnTag.DEATH || cause == CauseOfUnTag.DEATH_BY_PLAYER; | ||
| } | ||
|
|
||
| private String unknownKiller() { | ||
| return this.config.death.unknownKillerPlaceholder; | ||
| } | ||
| } | ||
46 changes: 46 additions & 0 deletions
46
...ombat-plugin/src/main/java/com/eternalcode/combat/fight/death/DeathCommandDispatcher.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,46 @@ | ||
| package com.eternalcode.combat.fight.death; | ||
|
|
||
| import org.bukkit.Server; | ||
| import org.bukkit.command.CommandSender; | ||
| import org.bukkit.entity.Player; | ||
|
|
||
| import java.util.Collection; | ||
|
|
||
| public class DeathCommandDispatcher { | ||
|
|
||
| private final Server server; | ||
|
|
||
| public DeathCommandDispatcher(Server server) { | ||
| this.server = server; | ||
| } | ||
|
|
||
| public void dispatch(DeathSettings.PostDeathCommands settings, Player player, String killerName) { | ||
| String playerName = player.getName(); | ||
|
|
||
| this.dispatch(settings.console, this.server.getConsoleSender(), playerName, killerName); | ||
| this.dispatch(settings.player, player, playerName, killerName); | ||
| } | ||
|
|
||
| public void dispatch(Collection<String> commands, CommandSender sender, String playerName, String killerName) { | ||
| for (String command : commands) { | ||
| String trimmed = command.trim(); | ||
|
|
||
| if (trimmed.isEmpty()) { | ||
| continue; | ||
| } | ||
|
|
||
| if (trimmed.startsWith("/")) { | ||
| trimmed = trimmed.substring(1); | ||
| } | ||
|
|
||
| String resolved = this.replacePlaceholders(trimmed, playerName, killerName); | ||
| this.server.dispatchCommand(sender, resolved); | ||
| } | ||
| } | ||
|
|
||
| private String replacePlaceholders(String command, String playerName, String killerName) { | ||
| return command | ||
| .replace("{PLAYER}", playerName) | ||
| .replace("{KILLER}", killerName); | ||
| } | ||
| } |
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
Oops, something went wrong.
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.