-
-
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
base: master
Are you sure you want to change the base?
Changes from 23 commits
8dfe353
e333117
16f45ca
13d9c09
911ef7b
ce87394
356481e
2a99c85
320dd90
903fdaa
cb015b5
15b2267
115f8bb
a606358
dc0724b
81afc4c
bc83f4d
34b1252
6b09a6f
7bc712d
ca25b99
b9d6a5b
ca05679
fd94fe1
f6f3e3b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package com.eternalcode.combat.fight.death; | ||
|
Jakubk15 marked this conversation as resolved.
|
||
|
|
||
| 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; | ||
|
|
||
| public class DeathCommandController implements Listener { | ||
|
Jakubk15 marked this conversation as resolved.
Outdated
|
||
|
|
||
| private final DeathCommandService deathCommandService; | ||
| private final Server server; | ||
|
|
||
| public DeathCommandController(DeathCommandService deathCommandService, Server server) { | ||
| this.deathCommandService = deathCommandService; | ||
| this.server = server; | ||
| } | ||
|
|
||
| @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) | ||
| void onPlayerUntag(FightUntagEvent event) { | ||
| Player player = this.server.getPlayer(event.getPlayer()); | ||
|
|
||
| if (player == null) { | ||
| return; | ||
| } | ||
|
|
||
| this.deathCommandService.handleUntag(player, event.getCause()); | ||
| } | ||
|
|
||
| @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) | ||
| void onPlayerDeath(PlayerDeathEvent event) { | ||
| this.deathCommandService.handleDeath(event.getEntity()); | ||
| } | ||
|
|
||
| @EventHandler(priority = EventPriority.MONITOR) | ||
| void onPlayerRespawn(PlayerRespawnEvent event) { | ||
| this.deathCommandService.handleRespawn(event.getPlayer()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| 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 DeathCommandExecutor { | ||
|
|
||
| private final Server server; | ||
|
|
||
| public DeathCommandExecutor(Server server) { | ||
| this.server = server; | ||
| } | ||
|
|
||
| public void dispatch(DeathSettings.PostDeathCommandSettings.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 resolved = this.replacePlaceholders(command, playerName, killerName); | ||
| this.server.dispatchCommand(sender, resolved); | ||
| } | ||
| } | ||
|
|
||
| private String replacePlaceholders(String command, String playerName, String killerName) { | ||
| return command | ||
| .replace("{PLAYER}", playerName) | ||
| .replace("{KILLER}", killerName); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| package com.eternalcode.combat.fight.death; | ||
|
|
||
| import com.eternalcode.combat.config.implementation.PluginConfig; | ||
| import com.eternalcode.combat.fight.FightManager; | ||
| import com.eternalcode.combat.fight.event.CauseOfUnTag; | ||
| import com.eternalcode.commons.scheduler.Scheduler; | ||
| import org.bukkit.entity.Player; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| public class DeathCommandService { | ||
|
|
||
| private final PluginConfig config; | ||
| private final FightManager fightManager; | ||
| private final KillerResolver killerResolver; | ||
| private final DeathCommandExecutor executor; | ||
| private final Scheduler scheduler; | ||
|
|
||
| private final Map<UUID, String> killerNames = new ConcurrentHashMap<>(); | ||
| private final Set<UUID> handledByUntag = Collections.newSetFromMap(new ConcurrentHashMap<>()); | ||
|
|
||
| public DeathCommandService(PluginConfig config, FightManager fightManager, KillerResolver killerResolver, DeathCommandExecutor executor, Scheduler scheduler) { | ||
| this.config = config; | ||
| this.fightManager = fightManager; | ||
| this.killerResolver = killerResolver; | ||
| this.executor = executor; | ||
| this.scheduler = scheduler; | ||
| } | ||
|
|
||
| /** | ||
| * scheduleUntagCommands runs in next tick, | ||
| * because we wait for the untag to fully complete, | ||
| * see {@link com.eternalcode.combat.fight.FightManagerImpl#untag(UUID, CauseOfUnTag)} | ||
| */ | ||
|
|
||
| public void handleUntag(Player player, CauseOfUnTag cause) { | ||
| if (cause != CauseOfUnTag.DEATH && cause != CauseOfUnTag.DEATH_BY_PLAYER) { | ||
| this.scheduleUntagCommands(player); | ||
| return; | ||
| } | ||
|
|
||
| UUID playerUniqueId = player.getUniqueId(); | ||
|
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. Why is the UUID called playerUniqueId in some places and playerUUID in others?
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. These names were picked during coding, no particular reason
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. Yeah, just pointing it out. My bad asking it as a question |
||
| String killerName = this.killerResolver.resolveKillerName(playerUniqueId, player); | ||
|
|
||
| this.handledByUntag.add(playerUniqueId); | ||
| this.killerNames.put(playerUniqueId, killerName); | ||
| } | ||
|
|
||
| public void handleDeath(Player player) { | ||
| UUID playerUUID = player.getUniqueId(); | ||
| String killerName = this.killerNames.computeIfAbsent( | ||
| playerUUID, | ||
| ignored -> this.killerResolver.resolveKillerName(playerUUID, player) | ||
| ); | ||
|
|
||
| this.executor.dispatch(this.config.death.postDeathCommands.onAnyDeath, player, killerName); | ||
|
|
||
| if (this.handledByUntag.remove(playerUUID)) { | ||
| return; | ||
| } | ||
|
|
||
| if (this.fightManager.isInCombat(playerUUID)) { | ||
| this.handleDeathInCombat(player, killerName); | ||
| } | ||
| } | ||
|
|
||
| public void handleRespawn(Player player) { | ||
| UUID playerUUID = player.getUniqueId(); | ||
|
|
||
| this.handledByUntag.remove(playerUUID); | ||
|
|
||
| String killerName = this.killerNames.remove(playerUUID); | ||
| if (killerName == null) { | ||
| killerName = this.config.death.postDeathCommands.unknownKillerPlaceholder; | ||
| } | ||
|
|
||
| this.executor.dispatch(this.config.death.postDeathCommands.afterRespawn, player, killerName); | ||
| } | ||
|
|
||
| private void handleDeathInCombat(Player player, String killerName) { | ||
| this.executor.dispatch(this.config.death.postDeathCommands.onDeathInCombat, player, killerName); | ||
|
|
||
| Player killer = this.killerResolver.resolveKiller(player.getUniqueId(), player); | ||
| if (killer != null) { | ||
| this.executor.dispatch(this.config.death.postDeathCommands.killerPostDeathCommands, killer, player.getName(), killerName); | ||
| } | ||
| } | ||
|
|
||
| private void scheduleUntagCommands(Player player) { | ||
| this.scheduler.run( | ||
| () -> this.executor.dispatch( | ||
| this.config.death.postDeathCommands.onUntag, | ||
| player, | ||
| this.config.death.postDeathCommands.unknownKillerPlaceholder | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| 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 org.bukkit.Server; | ||
| import org.bukkit.entity.Player; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| public class KillerResolver { | ||
|
|
||
| private final FightManager fightManager; | ||
| private final Server server; | ||
| private final PluginConfig config; | ||
|
|
||
| public KillerResolver(FightManager fightManager, Server server, PluginConfig config) { | ||
| this.fightManager = fightManager; | ||
| this.server = server; | ||
| this.config = config; | ||
| } | ||
|
|
||
| public 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; | ||
| } | ||
|
|
||
| public String resolveKillerName(UUID deadPlayerUUID, Player deadPlayer) { | ||
| Player killer = this.resolveKiller(deadPlayerUUID, deadPlayer); | ||
| return killer != null ? killer.getName() : this.config.death.postDeathCommands.unknownKillerPlaceholder; | ||
| } | ||
| } | ||
|
|
Uh oh!
There was an error while loading. Please reload this page.