Skip to content

Commit

Permalink
Add FightMessageController
Browse files Browse the repository at this point in the history
  • Loading branch information
CitralFlo committed Oct 12, 2023
1 parent 5ac1d96 commit 724516f
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 69 deletions.
47 changes: 22 additions & 25 deletions src/main/java/com/eternalcode/combat/CombatCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import com.eternalcode.combat.config.ConfigService;
import com.eternalcode.combat.config.implementation.PluginConfig;
import com.eternalcode.combat.fight.FightManager;
import com.eternalcode.combat.fight.bossbar.FightBossBarService;
import com.eternalcode.combat.fight.event.CauseOfTag;
import com.eternalcode.combat.fight.event.CauseOfUnTag;
import com.eternalcode.combat.fight.event.FightTagEvent;
import com.eternalcode.combat.fight.event.FightUntagEvent;
import com.eternalcode.combat.notification.NotificationAnnouncer;
import dev.rollczi.litecommands.argument.Arg;
import dev.rollczi.litecommands.command.async.Async;
Expand All @@ -24,14 +25,12 @@ public class CombatCommand {

private final FightManager fightManager;
private final ConfigService configService;
private final FightBossBarService bossBarService;
private final NotificationAnnouncer announcer;
private final PluginConfig config;

public CombatCommand(FightManager fightManager, ConfigService configService, FightBossBarService bossBarService, NotificationAnnouncer announcer, PluginConfig config) {
public CombatCommand(FightManager fightManager, ConfigService configService, NotificationAnnouncer announcer, PluginConfig config) {
this.fightManager = fightManager;
this.configService = configService;
this.bossBarService = bossBarService;
this.announcer = announcer;
this.config = config;
}
Expand Down Expand Up @@ -59,7 +58,12 @@ void tag(CommandSender sender, @Arg Player target) {
Formatter formatter = new Formatter()
.register("{PLAYER}", target.getName());

this.fightManager.tag(targetUniqueId, time, CauseOfTag.COMMAND);
FightTagEvent event = this.fightManager.tag(targetUniqueId, time, CauseOfTag.COMMAND);

if (event.isCancelled()) {
this.announcer.sendMessage(sender, event.getCancelMessage());
return;
}

String format = formatter.format(this.config.messages.admin.adminTagPlayer);
this.announcer.sendMessage(sender, format);
Expand All @@ -75,33 +79,29 @@ void tagMultiple(CommandSender sender, @Arg Player firstTarget, @Arg Player seco
this.announcer.sendMessage(sender, messages.admin.adminCannotTagSelf);
return;
}
boolean isTaggedFirst = this.fightManager.tag(firstTarget.getUniqueId(), combatTime, CauseOfTag.COMMAND);
boolean isTaggedSecond = this.fightManager.tag(secondTarget.getUniqueId(), combatTime, CauseOfTag.COMMAND);

FightTagEvent firstTagEvent = this.fightManager.tag(firstTarget.getUniqueId(), combatTime, CauseOfTag.COMMAND);
FightTagEvent secondTagEvent = this.fightManager.tag(secondTarget.getUniqueId(), combatTime, CauseOfTag.COMMAND);

Formatter formatter = new Formatter()
.register("{FIRST_PLAYER}", firstTarget.getName())
.register("{SECOND_PLAYER}", secondTarget.getName());

String format = formatter.format(messages.admin.adminTagMultiplePlayers);

if (isTaggedFirst) {
this.announcer.sendMessage(firstTarget, messages.playerTagged);
}
else {
this.announcer.sendMessage(sender, messages.admin.cannotTagPlayer);
if (firstTagEvent.isCancelled()) {
this.announcer.sendMessage(sender, firstTagEvent.getCancelMessage());
}

if (isTaggedSecond) {
this.announcer.sendMessage(secondTarget, messages.playerTagged);
}
else {
this.announcer.sendMessage(sender, messages.admin.cannotTagPlayer);
if (secondTagEvent.isCancelled()) {
this.announcer.sendMessage(sender, firstTagEvent.getCancelMessage());
}

if (isTaggedFirst || isTaggedSecond) {
this.announcer.sendMessage(sender, format);
if (firstTagEvent.isCancelled() && secondTagEvent.isCancelled()) {
return;
}

this.announcer.sendMessage(sender, format);
}

@Async
Expand All @@ -122,14 +122,11 @@ void untag(Player sender, @Arg Player target) {
return;
}

if (!this.fightManager.untag(targetUniqueId, CauseOfUnTag.COMMAND)) {
this.announcer.sendMessage(target, this.config.messages.admin.cannotTagPlayer);
FightUntagEvent event = this.fightManager.untag(targetUniqueId, CauseOfUnTag.COMMAND);
if (event.isCancelled()) {
return;
}

this.announcer.sendMessage(target, this.config.messages.playerUntagged);
this.bossBarService.hide(targetUniqueId);

Formatter formatter = new Formatter()
.register("{PLAYER}", target.getName());

Expand Down
13 changes: 5 additions & 8 deletions src/main/java/com/eternalcode/combat/CombatPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,12 @@
import com.eternalcode.combat.drop.DropManager;
import com.eternalcode.combat.drop.impl.PercentDropModifier;
import com.eternalcode.combat.drop.impl.PlayersHealthDropModifier;
import com.eternalcode.combat.fight.controller.*;
import com.eternalcode.combat.fight.effect.FightEffectController;
import com.eternalcode.combat.event.EventCaller;
import com.eternalcode.combat.fight.FightManager;
import com.eternalcode.combat.fight.FightTask;
import com.eternalcode.combat.fight.bossbar.FightBossBarService;
import com.eternalcode.combat.fight.controller.FightActionBlockerController;
import com.eternalcode.combat.fight.controller.FightDeathCauseController;
import com.eternalcode.combat.fight.controller.FightEscapeController;
import com.eternalcode.combat.fight.controller.FightTagController;
import com.eternalcode.combat.fight.controller.FightUnTagController;
import com.eternalcode.combat.fight.effect.FightEffectService;
import com.eternalcode.combat.fight.pearl.FightPearlController;
import com.eternalcode.combat.fight.pearl.FightPearlManager;
Expand Down Expand Up @@ -98,7 +94,7 @@ public void onEnable() {
.invalidUsageHandler(new InvalidUsage(pluginConfig, notificationAnnouncer))
.permissionHandler(new PermissionMessage(pluginConfig, notificationAnnouncer))

.commandInstance(new CombatCommand(this.fightManager, configService, this.fightBossBarService, notificationAnnouncer, pluginConfig))
.commandInstance(new CombatCommand(this.fightManager, configService, notificationAnnouncer, pluginConfig))
.commandInstance(new TagOutCommand(fightTagOutService, notificationAnnouncer, pluginConfig))

.register();
Expand All @@ -122,14 +118,15 @@ public void onEnable() {
new FightDeathCauseController(this.fightManager),
new DropController(dropManager, keepInventoryManager, pluginConfig.dropSettings, this.fightManager),
new FightTagController(this.fightManager, pluginConfig, notificationAnnouncer),
new FightUnTagController(this.fightManager, pluginConfig, notificationAnnouncer),
new FightUnTagController(this.fightManager, pluginConfig),
new FightEscapeController(this.fightManager, pluginConfig, notificationAnnouncer),
new FightActionBlockerController(this.fightManager, notificationAnnouncer, pluginConfig),
new FightPearlController(pluginConfig.pearl, notificationAnnouncer, this.fightManager, fightPearlManager),
new UpdaterNotificationController(updaterService, pluginConfig, this.audienceProvider, miniMessage),
new RegionController(notificationAnnouncer, bridgeService.getRegionProvider(), this.fightManager, pluginConfig),
new FightEffectController(pluginConfig.effect, effectService, this.fightManager, this.getServer()),
new FightTagOutController(fightTagOutService)
new FightTagOutController(fightTagOutService),
new FightMessageController(this.fightManager, notificationAnnouncer, this.fightBossBarService, pluginConfig, this.getServer())
).forEach(listener -> this.getServer().getPluginManager().registerEvents(listener, this));

long millis = started.elapsed(TimeUnit.MILLISECONDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,6 @@ public static class AdminMessages extends OkaeriConfig {
@Comment("# Message sent when a player is tagged by an admin")
public String adminTagMultiplePlayers = "&7You have tagged &e{FIRST_PLAYER}&7 and &e{SECOND_PLAYER}&7.";

@Comment("# Message sent when a player is untaggable by the command due to tagOut system")
public String cannotTagPlayer = "&cYou cannot tag this player due to tagOut system!";

@Comment("# Message sent to admin when they remove a player from combat")
public String adminUntagPlayer = "&7You have removed &e{PLAYER}&7 from the fight.";

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/eternalcode/combat/fight/FightManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,29 @@ public boolean isInCombat(UUID player) {
return !fightTag.isExpired();
}

public boolean untag(UUID player, CauseOfUnTag causeOfUnTag) {
public FightUntagEvent untag(UUID player, CauseOfUnTag causeOfUnTag) {
FightUntagEvent event = this.eventCaller.publishEvent(new FightUntagEvent(player, causeOfUnTag));
if (event.isCancelled()) {
return false;
return event;
}

this.fights.remove(player);
return true;
return event;
}

public boolean tag(UUID target, Duration delay, CauseOfTag causeOfTag) {
public FightTagEvent tag(UUID target, Duration delay, CauseOfTag causeOfTag) {
FightTagEvent event = this.eventCaller.publishEvent(new FightTagEvent(target, causeOfTag));

if (event.isCancelled()) {
return false;
return event;
}
Instant now = Instant.now();
Instant endOfCombatLog = now.plus(delay);

FightTag fightTag = new FightTag(target, endOfCombatLog);

this.fights.put(target, fightTag);
return true;
return event;
}

public Collection<FightTag> getFights() {
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/eternalcode/combat/fight/FightTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ public void run() {
continue;
}

this.announcer.sendMessage(player, this.config.messages.playerUntagged);

this.fightManager.untag(playerUniqueId, CauseOfUnTag.TIME_EXPIRED);
this.bossBarService.hide(playerUniqueId);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.eternalcode.combat.fight.controller;

import com.eternalcode.combat.config.implementation.PluginConfig;
import com.eternalcode.combat.fight.FightManager;
import com.eternalcode.combat.fight.bossbar.FightBossBarService;
import com.eternalcode.combat.fight.event.FightTagEvent;
import com.eternalcode.combat.notification.NotificationAnnouncer;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;

public class FightMessageController implements Listener {

private final FightManager fightManager;
private final NotificationAnnouncer announcer;
private final FightBossBarService bossBarService;
private final PluginConfig config;
private final Server server;

public FightMessageController(FightManager fightManager, NotificationAnnouncer announcer, FightBossBarService bossBarService, PluginConfig config, Server server) {
this.fightManager = fightManager;
this.announcer = announcer;
this.bossBarService = bossBarService;
this.config = config;
this.server = server;
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
void onTag(FightTagEvent event) {
Player player = this.server.getPlayer(event.getPlayer());

if (player == null) {
throw new IllegalStateException("Player cannot be null!");
}

if (this.fightManager.isInCombat(player.getUniqueId())) {
return;
}

this.announcer.sendMessage(player, this.config.messages.playerTagged);
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
void onUnTag(FightTagEvent event) {
Player player = this.server.getPlayer(event.getPlayer());

if (player == null) {
throw new IllegalStateException("Player cannot be null!");
}

if (!this.fightManager.isInCombat(player.getUniqueId())) {
return;
}

this.announcer.sendMessage(player, this.config.messages.playerUntagged);
this.bossBarService.hide(event.getPlayer());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,9 @@ void onEntityDamageByEntity(EntityDamageByEntityEvent event) {

UUID attackedUniqueId = attackedPlayerByPerson.getUniqueId();
UUID personToAddCombatTimeUniqueId = personToAddCombatTime.getUniqueId();
boolean isTaggedAttacker = this.fightManager.tag(attackedUniqueId, combatTime, CauseOfTag.PLAYER);
boolean isTaggedPerson = this.fightManager.tag(personToAddCombatTimeUniqueId, combatTime, CauseOfTag.PLAYER);


if (isTaggedAttacker) {
this.announcer.sendMessage(personToAddCombatTime, this.config.messages.playerTagged);
}

if (isTaggedPerson) {
this.announcer.sendMessage(attackedPlayerByPerson, this.config.messages.playerTagged);
}
this.fightManager.tag(attackedUniqueId, combatTime, CauseOfTag.PLAYER);
this.fightManager.tag(personToAddCombatTimeUniqueId, combatTime, CauseOfTag.PLAYER);
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
Expand Down Expand Up @@ -99,11 +91,7 @@ void onEntityDamage(EntityDamageEvent event) {
return;
}

boolean isTagged = this.fightManager.tag(uuid, combatTime, CauseOfTag.NON_PLAYER);
if (isTagged) {
this.announcer.sendMessage(player, this.config.messages.playerTagged);
}

this.fightManager.tag(uuid, combatTime, CauseOfTag.NON_PLAYER);
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.eternalcode.combat.fight.event.CauseOfUnTag;
import com.eternalcode.combat.config.implementation.PluginConfig;
import com.eternalcode.combat.fight.FightManager;
import com.eternalcode.combat.notification.NotificationAnnouncer;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
Expand All @@ -13,12 +12,10 @@ public class FightUnTagController implements Listener {

private final FightManager fightManager;
private final PluginConfig config;
private final NotificationAnnouncer announcer;

public FightUnTagController(FightManager fightManager, PluginConfig config, NotificationAnnouncer announcer) {
public FightUnTagController(FightManager fightManager, PluginConfig config) {
this.fightManager = fightManager;
this.config = config;
this.announcer = announcer;
}

@EventHandler
Expand All @@ -30,15 +27,12 @@ void onPlayerDeath(PlayerDeathEvent event) {
return;
}

this.announcer.sendMessage(player, this.config.messages.playerUntagged);

CauseOfUnTag causeOfUnTag = (killer == null ? CauseOfUnTag.PLAYER_DEATH : CauseOfUnTag.NON_PLAYER_DEATH);

this.fightManager.untag(player.getUniqueId(), causeOfUnTag);

if (killer != null && this.config.settings.shouldReleaseAttacker) {
this.fightManager.untag(killer.getUniqueId(), CauseOfUnTag.ATTACKER_RELEASE);
this.announcer.sendMessage(killer, this.config.messages.playerUntagged);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class FightTagEvent extends Event implements Cancellable {
private final UUID player;
private final CauseOfTag cause;
private boolean isCancelled = false;
private String cancelMessage = "";

public FightTagEvent(UUID player, CauseOfTag cause) {
this.player = player;
Expand All @@ -33,7 +34,21 @@ public boolean isCancelled() {

@Override
public void setCancelled(boolean cancelled) {
this.isCancelled = cancelled;
throw new UnsupportedOperationException("Use #cancel(String) instead");
}

public void cancel(String cancelMessage) {
this.isCancelled = true;
this.cancelMessage = cancelMessage;
}

public void allow() {
this.isCancelled = false;
this.cancelMessage = "";
}

public String getCancelMessage() {
return this.cancelMessage;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void onTagOut(FightTagEvent event) {
UUID uniqueId = event.getPlayer();

if (this.tagOutService.isTaggedOut(uniqueId)) {
event.setCancelled(true);
event.cancel("Tag-out system prevents tag!");
}
}
}

0 comments on commit 724516f

Please sign in to comment.