diff --git a/module/arena-restoration/src/main/java/org/battleplugins/arena/module/restoration/RestoreArenaAction.java b/module/arena-restoration/src/main/java/org/battleplugins/arena/module/restoration/RestoreArenaAction.java index be7e2b86..4609db3f 100644 --- a/module/arena-restoration/src/main/java/org/battleplugins/arena/module/restoration/RestoreArenaAction.java +++ b/module/arena-restoration/src/main/java/org/battleplugins/arena/module/restoration/RestoreArenaAction.java @@ -18,6 +18,7 @@ import org.battleplugins.arena.competition.LiveCompetition; import org.battleplugins.arena.competition.map.options.Bounds; import org.battleplugins.arena.event.action.EventAction; +import org.battleplugins.arena.resolver.Resolvable; import java.io.IOException; import java.nio.file.Files; @@ -32,7 +33,7 @@ public RestoreArenaAction(Map params) { } @Override - public void postProcess(Arena arena, Competition competition) { + public void postProcess(Arena arena, Competition competition, Resolvable resolvable) { if (!arena.isModuleEnabled(ArenaRestoration.ID)) { return; } @@ -95,7 +96,7 @@ public void postProcess(Arena arena, Competition competition) { } @Override - public void call(ArenaPlayer arenaPlayer) { + public void call(ArenaPlayer arenaPlayer, Resolvable resolvable) { // No-op } } diff --git a/module/classes/src/main/java/org/battleplugins/arena/module/classes/EquipClassAction.java b/module/classes/src/main/java/org/battleplugins/arena/module/classes/EquipClassAction.java index b7d975ce..b49b6b6e 100644 --- a/module/classes/src/main/java/org/battleplugins/arena/module/classes/EquipClassAction.java +++ b/module/classes/src/main/java/org/battleplugins/arena/module/classes/EquipClassAction.java @@ -4,6 +4,7 @@ import org.battleplugins.arena.ArenaPlayer; import org.battleplugins.arena.event.action.EventAction; import org.battleplugins.arena.options.types.BooleanArenaOption; +import org.battleplugins.arena.resolver.Resolvable; import java.util.Map; import java.util.Optional; @@ -18,7 +19,7 @@ public EquipClassAction(Map params) { } @Override - public void call(ArenaPlayer arenaPlayer) { + public void call(ArenaPlayer arenaPlayer, Resolvable resolvable) { if (!arenaPlayer.getArena().isModuleEnabled(Classes.ID)) { return; } diff --git a/module/join-messages/build.gradle.kts b/module/join-messages/build.gradle.kts deleted file mode 100644 index 0d7ff71b..00000000 --- a/module/join-messages/build.gradle.kts +++ /dev/null @@ -1,3 +0,0 @@ -dependencies { - compileOnlyApi(project(":plugin")) -} \ No newline at end of file diff --git a/module/join-messages/src/main/java/org/battleplugins/arena/module/joinmessages/JoinMessages.java b/module/join-messages/src/main/java/org/battleplugins/arena/module/joinmessages/JoinMessages.java deleted file mode 100644 index 487ab635..00000000 --- a/module/join-messages/src/main/java/org/battleplugins/arena/module/joinmessages/JoinMessages.java +++ /dev/null @@ -1,76 +0,0 @@ -package org.battleplugins.arena.module.joinmessages; - -import org.battleplugins.arena.ArenaPlayer; -import org.battleplugins.arena.competition.LiveCompetition; -import org.battleplugins.arena.event.ArenaEventHandler; -import org.battleplugins.arena.event.ArenaListener; -import org.battleplugins.arena.event.arena.ArenaInitializeEvent; -import org.battleplugins.arena.event.player.ArenaJoinEvent; -import org.battleplugins.arena.event.player.ArenaLeaveEvent; -import org.battleplugins.arena.messages.Message; -import org.battleplugins.arena.messages.Messages; -import org.battleplugins.arena.module.ArenaModule; -import org.battleplugins.arena.module.ArenaModuleInitializer; -import org.bukkit.event.EventHandler; - -/** - * A module that adds join messages to the arena. - */ -@ArenaModule(id = JoinMessages.ID, name = "Join Messages", description = "Adds join messages to BattleArena.", authors = "BattlePlugins") -public class JoinMessages implements ArenaModuleInitializer, ArenaListener { - public static final String ID = "join-messages"; - - private static final Message PLAYER_JOINED = Messages.info("join-messages-player-joined", "{} has joined the game ({}/{})."); - private static final Message PLAYER_JOINED_NO_LIMIT = Messages.info("join-messages-player-joined-no-limit", "{} has joined the game."); - private static final Message PLAYER_LEFT = Messages.info("join-messages-player-left", "{} has left the game ({}/{})."); - private static final Message PLAYER_LEFT_NO_LIMIT = Messages.info("join-messages-player-left-no-limit", "{} has left the game."); - - @EventHandler - public void onArenaInitialize(ArenaInitializeEvent event) { - if (!event.getArena().isModuleEnabled(ID)) { - return; - } - - event.getArena().getEventManager().registerEvents(this); - } - - @ArenaEventHandler - public void onJoin(ArenaJoinEvent event) { - LiveCompetition competition = event.getCompetition(); - boolean hasMax = competition.getArena().getTeams().hasMaxPlayers(); - for (ArenaPlayer player : competition.getPlayers()) { - if (hasMax) { - PLAYER_JOINED.send( - player.getPlayer(), - event.getPlayer().getName(), - Integer.toString(competition.getPlayers().size()), - Integer.toString(competition.getMaxPlayers()) - ); - } else { - PLAYER_JOINED_NO_LIMIT.send(player.getPlayer(), event.getPlayer().getName()); - } - } - } - - @ArenaEventHandler - public void onLeave(ArenaLeaveEvent event) { - if (event.getCause() == ArenaLeaveEvent.Cause.SHUTDOWN || event.getCause() == ArenaLeaveEvent.Cause.GAME) { - return; - } - - LiveCompetition competition = event.getCompetition(); - boolean hasMax = competition.getArena().getTeams().hasMaxPlayers(); - for (ArenaPlayer player : competition.getPlayers()) { - if (hasMax) { - PLAYER_LEFT.send( - player.getPlayer(), - event.getPlayer().getName(), - Integer.toString(competition.getPlayers().size()), - Integer.toString(competition.getMaxPlayers()) - ); - } else { - PLAYER_LEFT_NO_LIMIT.send(player.getPlayer(), event.getPlayer().getName()); - } - } - } -} diff --git a/module/team-heads/src/main/java/org/battleplugins/arena/module/teamheads/TeamHeadsAction.java b/module/team-heads/src/main/java/org/battleplugins/arena/module/teamheads/TeamHeadsAction.java index c91e2360..30ab6890 100644 --- a/module/team-heads/src/main/java/org/battleplugins/arena/module/teamheads/TeamHeadsAction.java +++ b/module/team-heads/src/main/java/org/battleplugins/arena/module/teamheads/TeamHeadsAction.java @@ -5,6 +5,7 @@ import net.kyori.adventure.text.format.TextDecoration; import org.battleplugins.arena.ArenaPlayer; import org.battleplugins.arena.event.action.EventAction; +import org.battleplugins.arena.resolver.Resolvable; import org.battleplugins.arena.team.ArenaTeam; import org.bukkit.inventory.ItemStack; @@ -17,7 +18,7 @@ public TeamHeadsAction(Map params) { } @Override - public void call(ArenaPlayer arenaPlayer) { + public void call(ArenaPlayer arenaPlayer, Resolvable resolvable) { if (!arenaPlayer.getArena().isModuleEnabled(TeamHeads.ID)) { return; } diff --git a/module/vault-integration/src/main/java/org/battleplugins/arena/module/vault/AddPermissionAction.java b/module/vault-integration/src/main/java/org/battleplugins/arena/module/vault/AddPermissionAction.java index 03412c94..ad4caa8d 100644 --- a/module/vault-integration/src/main/java/org/battleplugins/arena/module/vault/AddPermissionAction.java +++ b/module/vault-integration/src/main/java/org/battleplugins/arena/module/vault/AddPermissionAction.java @@ -2,6 +2,7 @@ import org.battleplugins.arena.ArenaPlayer; import org.battleplugins.arena.event.action.EventAction; +import org.battleplugins.arena.resolver.Resolvable; import java.util.Map; import java.util.Optional; @@ -15,7 +16,7 @@ public AddPermissionAction(Map params) { } @Override - public void call(ArenaPlayer arenaPlayer) { + public void call(ArenaPlayer arenaPlayer, Resolvable resolvable) { if (!arenaPlayer.getArena().isModuleEnabled(VaultIntegration.ID)) { return; } diff --git a/module/vault-integration/src/main/java/org/battleplugins/arena/module/vault/EditCurrencyAction.java b/module/vault-integration/src/main/java/org/battleplugins/arena/module/vault/EditCurrencyAction.java index 788a84d0..aab80669 100644 --- a/module/vault-integration/src/main/java/org/battleplugins/arena/module/vault/EditCurrencyAction.java +++ b/module/vault-integration/src/main/java/org/battleplugins/arena/module/vault/EditCurrencyAction.java @@ -2,6 +2,7 @@ import org.battleplugins.arena.ArenaPlayer; import org.battleplugins.arena.event.action.EventAction; +import org.battleplugins.arena.resolver.Resolvable; import java.util.Map; import java.util.Optional; @@ -15,7 +16,7 @@ public EditCurrencyAction(Map params) { } @Override - public void call(ArenaPlayer arenaPlayer) { + public void call(ArenaPlayer arenaPlayer, Resolvable resolvable) { if (!arenaPlayer.getArena().isModuleEnabled(VaultIntegration.ID)) { return; } diff --git a/module/vault-integration/src/main/java/org/battleplugins/arena/module/vault/RemovePermissionAction.java b/module/vault-integration/src/main/java/org/battleplugins/arena/module/vault/RemovePermissionAction.java index 2644bd5e..0b4f199a 100644 --- a/module/vault-integration/src/main/java/org/battleplugins/arena/module/vault/RemovePermissionAction.java +++ b/module/vault-integration/src/main/java/org/battleplugins/arena/module/vault/RemovePermissionAction.java @@ -2,6 +2,7 @@ import org.battleplugins.arena.ArenaPlayer; import org.battleplugins.arena.event.action.EventAction; +import org.battleplugins.arena.resolver.Resolvable; import java.util.Map; import java.util.Optional; @@ -15,7 +16,7 @@ public RemovePermissionAction(Map params) { } @Override - public void call(ArenaPlayer arenaPlayer) { + public void call(ArenaPlayer arenaPlayer, Resolvable resolvable) { if (!arenaPlayer.getArena().isModuleEnabled(VaultIntegration.ID)) { return; } diff --git a/plugin/src/main/java/org/battleplugins/arena/Arena.java b/plugin/src/main/java/org/battleplugins/arena/Arena.java index 4bcd3d4f..4c6cfc76 100644 --- a/plugin/src/main/java/org/battleplugins/arena/Arena.java +++ b/plugin/src/main/java/org/battleplugins/arena/Arena.java @@ -22,6 +22,10 @@ import org.battleplugins.arena.options.ArenaOptionType; import org.battleplugins.arena.options.Lives; import org.battleplugins.arena.options.Teams; +import org.battleplugins.arena.resolver.Resolvable; +import org.battleplugins.arena.resolver.Resolver; +import org.battleplugins.arena.resolver.ResolverKeys; +import org.battleplugins.arena.resolver.ResolverProvider; import org.jetbrains.annotations.Nullable; import java.nio.file.Path; @@ -42,7 +46,7 @@ * active game actions will occur (i.e. game timer, score, etc.) */ @DocumentationSource("https://docs.battleplugins.org/books/user-guide/chapter/configuration") -public class Arena implements ArenaLike, ArenaListener { +public class Arena implements ArenaLike, ArenaListener, Resolvable { @Scoped private BattleArena plugin; @@ -316,4 +320,11 @@ public final E getOption public final Arena getArena() { return this; } + + @Override + public Resolver resolve() { + return Resolver.builder() + .define(ResolverKeys.ARENA, ResolverProvider.simple(this, Arena::getName)) + .build(); + } } diff --git a/plugin/src/main/java/org/battleplugins/arena/ArenaPlayer.java b/plugin/src/main/java/org/battleplugins/arena/ArenaPlayer.java index a7829448..3dee779a 100644 --- a/plugin/src/main/java/org/battleplugins/arena/ArenaPlayer.java +++ b/plugin/src/main/java/org/battleplugins/arena/ArenaPlayer.java @@ -6,6 +6,10 @@ import org.battleplugins.arena.event.player.ArenaStatChangeEvent; import org.battleplugins.arena.event.player.ArenaTeamJoinEvent; import org.battleplugins.arena.event.player.ArenaTeamLeaveEvent; +import org.battleplugins.arena.resolver.Resolvable; +import org.battleplugins.arena.resolver.Resolver; +import org.battleplugins.arena.resolver.ResolverKeys; +import org.battleplugins.arena.resolver.ResolverProvider; import org.battleplugins.arena.stat.ArenaStat; import org.battleplugins.arena.stat.StatHolder; import org.battleplugins.arena.team.ArenaTeam; @@ -21,7 +25,7 @@ /** * Represents a player in an active competition. */ -public class ArenaPlayer implements StatHolder { +public class ArenaPlayer implements StatHolder, Resolvable { private static final String ARENA_PLAYER_META_KEY = "arena-player"; private final Player player; @@ -275,6 +279,24 @@ public void resetState() { this.competition.findAndJoinTeamIfApplicable(this); } + + @Override + public String describe() { + return this.getPlayer().getName(); + } + + @Override + public Resolver resolve() { + Resolver.Builder builder = this.competition.resolve().toBuilder() + .define(ResolverKeys.PLAYER, ResolverProvider.simple(this, this.player::getName)); + + if (this.team != null) { + builder.define(ResolverKeys.TEAM, ResolverProvider.simple(this.team, ArenaTeam::getName, ArenaTeam::getFormattedName)); + } + + return builder.build(); + } + @Override public String toString() { return "ArenaPlayer{" + diff --git a/plugin/src/main/java/org/battleplugins/arena/competition/LiveCompetition.java b/plugin/src/main/java/org/battleplugins/arena/competition/LiveCompetition.java index 9ab18a6e..3cee14a3 100644 --- a/plugin/src/main/java/org/battleplugins/arena/competition/LiveCompetition.java +++ b/plugin/src/main/java/org/battleplugins/arena/competition/LiveCompetition.java @@ -18,9 +18,12 @@ import org.battleplugins.arena.options.ArenaOptionType; import org.battleplugins.arena.options.TeamSelection; import org.battleplugins.arena.options.Teams; +import org.battleplugins.arena.resolver.Resolvable; +import org.battleplugins.arena.resolver.Resolver; +import org.battleplugins.arena.resolver.ResolverKeys; +import org.battleplugins.arena.resolver.ResolverProvider; import org.battleplugins.arena.team.ArenaTeam; import org.battleplugins.arena.team.ArenaTeams; -import org.battleplugins.arena.util.IntRange; import org.bukkit.entity.Player; import org.jetbrains.annotations.Nullable; @@ -37,7 +40,7 @@ * A {@link Competition} that is occurring on the same server * that this plugin is running on. */ -public class LiveCompetition> implements ArenaLike, Competition { +public class LiveCompetition> implements ArenaLike, Competition, Resolvable { private final Arena arena; private final CompetitionType type; private final LiveCompetitionMap map; @@ -364,4 +367,14 @@ private int calculateMaxPlayers() { return maxPlayers; } + + @Override + public Resolver resolve() { + return this.arena.resolve().toBuilder() + .define(ResolverKeys.COMPETITION, ResolverProvider.simple(this.getCompetition(), this.getMap()::getName)) + .define(ResolverKeys.ONLINE_PLAYERS, ResolverProvider.simple(this.getPlayers().size(), String::valueOf)) + .define(ResolverKeys.MAX_PLAYERS, ResolverProvider.simple(this.getMaxPlayers(), String::valueOf)) + .define(ResolverKeys.PLAYERS, ResolverProvider.simple(this.getPlayers(), players -> String.join(", ", players.stream().map(p -> p.getPlayer().getName()).toList()))) + .build(); + } } diff --git a/plugin/src/main/java/org/battleplugins/arena/competition/StatListener.java b/plugin/src/main/java/org/battleplugins/arena/competition/StatListener.java index f018c0b5..be707246 100644 --- a/plugin/src/main/java/org/battleplugins/arena/competition/StatListener.java +++ b/plugin/src/main/java/org/battleplugins/arena/competition/StatListener.java @@ -44,6 +44,10 @@ public void onStatChange(ArenaStatChangeEvent event) { return; } + if (newValue < 0) { + return; + } + this.competition.getArena().getEventManager().callEvent(new ArenaLifeDepleteEvent(this.competition.getArena(), player, newValue)); } } diff --git a/plugin/src/main/java/org/battleplugins/arena/competition/team/TeamStatHolder.java b/plugin/src/main/java/org/battleplugins/arena/competition/team/TeamStatHolder.java index 2ddb58da..91af1bea 100644 --- a/plugin/src/main/java/org/battleplugins/arena/competition/team/TeamStatHolder.java +++ b/plugin/src/main/java/org/battleplugins/arena/competition/team/TeamStatHolder.java @@ -70,6 +70,11 @@ public void computeStat(ArenaStat stat, Function }); } + @Override + public String describe() { + return this.team.getName(); + } + public ArenaTeam getTeam() { return this.team; } diff --git a/plugin/src/main/java/org/battleplugins/arena/event/ArenaEvent.java b/plugin/src/main/java/org/battleplugins/arena/event/ArenaEvent.java index 1ecf198c..d8380541 100644 --- a/plugin/src/main/java/org/battleplugins/arena/event/ArenaEvent.java +++ b/plugin/src/main/java/org/battleplugins/arena/event/ArenaEvent.java @@ -2,12 +2,16 @@ import org.battleplugins.arena.Arena; import org.battleplugins.arena.competition.Competition; +import org.battleplugins.arena.resolver.Resolvable; +import org.battleplugins.arena.resolver.Resolver; +import org.battleplugins.arena.resolver.ResolverKeys; +import org.battleplugins.arena.resolver.ResolverProvider; import org.jetbrains.annotations.Nullable; /** * Represents an event that occurs in an {@link Arena}. */ -public interface ArenaEvent { +public interface ArenaEvent extends Resolvable { /** * Gets the {@link Arena} this event is occurring in. @@ -36,4 +40,11 @@ default EventTrigger getEventTrigger() { return null; } + + default Resolver resolve() { + return Resolver.builder() + .define(ResolverKeys.ARENA, ResolverProvider.simple(this.getArena(), Arena::getName)) + .define(ResolverKeys.COMPETITION, ResolverProvider.simple(this.getCompetition(), c -> c.getMap().getName())) + .build(); + } } diff --git a/plugin/src/main/java/org/battleplugins/arena/event/ArenaEventManager.java b/plugin/src/main/java/org/battleplugins/arena/event/ArenaEventManager.java index 0d0f9de2..fa709fd0 100644 --- a/plugin/src/main/java/org/battleplugins/arena/event/ArenaEventManager.java +++ b/plugin/src/main/java/org/battleplugins/arena/event/ArenaEventManager.java @@ -147,22 +147,22 @@ public T callEvent(T event) { return event; } - this.pollActions(competition, actions.iterator(), players); + this.pollActions(event, competition, actions.iterator(), players); } return event; } - private void pollActions(Competition competition, Iterator iterator, Collection players) { + private void pollActions(T event, Competition competition, Iterator iterator, Collection players) { while (iterator.hasNext()) { EventAction action = iterator.next(); if (action instanceof DelayAction delayAction) { - Bukkit.getScheduler().runTaskLater(BattleArena.getInstance(), () -> this.pollActions(competition, iterator, players), delayAction.getTicks()); + Bukkit.getScheduler().runTaskLater(BattleArena.getInstance(), () -> this.pollActions(event, competition, iterator, players), delayAction.getTicks()); return; } try { - action.preProcess(this.arena, competition); + action.preProcess(this.arena, competition, event); } catch (Throwable e) { this.arena.getPlugin().warn("An error occurred pre-processing event action {}", action, e); return; @@ -170,7 +170,7 @@ private void pollActions(Competition competition, Iterator itera for (ArenaPlayer player : new HashSet<>(players)) { try { - action.call(player); + action.call(player, event); } catch (Throwable e) { this.arena.getPlugin().warn("An error occurred calling event action {}", action, e); return; @@ -178,7 +178,7 @@ private void pollActions(Competition competition, Iterator itera } try { - action.postProcess(this.arena, competition); + action.postProcess(this.arena, competition, event); } catch (Throwable e) { this.arena.getPlugin().warn("An error occurred post-processing event action {}", action, e); return; diff --git a/plugin/src/main/java/org/battleplugins/arena/event/action/EventAction.java b/plugin/src/main/java/org/battleplugins/arena/event/action/EventAction.java index 4cc05b9d..ed8dbbae 100644 --- a/plugin/src/main/java/org/battleplugins/arena/event/action/EventAction.java +++ b/plugin/src/main/java/org/battleplugins/arena/event/action/EventAction.java @@ -3,6 +3,7 @@ import org.battleplugins.arena.Arena; import org.battleplugins.arena.ArenaPlayer; import org.battleplugins.arena.competition.Competition; +import org.battleplugins.arena.resolver.Resolvable; import org.jetbrains.annotations.Nullable; import java.util.Map; @@ -52,8 +53,9 @@ public String getOrDefault(String key, String defaultValue) { * * @param arena the arena the action is occurring in * @param competition the competition the action is occurring in + * @param resolvable the resolvable to call the action for */ - public void preProcess(Arena arena, Competition competition) { + public void preProcess(Arena arena, Competition competition, Resolvable resolvable) { } /** @@ -64,14 +66,16 @@ public void preProcess(Arena arena, Competition competition) { * * @param arena the arena the action is occurring in * @param competition the competition the action is occurring in + * @param resolvable the resolvable to call the action for */ - public void postProcess(Arena arena, Competition competition) { + public void postProcess(Arena arena, Competition competition, Resolvable resolvable) { } /** * Calls the action for the given {@link ArenaPlayer}. * * @param arenaPlayer the player to call the action for + * @param resolvable the resolvable to call the action for */ - public abstract void call(ArenaPlayer arenaPlayer); + public abstract void call(ArenaPlayer arenaPlayer, Resolvable resolvable); } diff --git a/plugin/src/main/java/org/battleplugins/arena/event/action/types/BroadcastAction.java b/plugin/src/main/java/org/battleplugins/arena/event/action/types/BroadcastAction.java index 9fb22ce0..75a06289 100644 --- a/plugin/src/main/java/org/battleplugins/arena/event/action/types/BroadcastAction.java +++ b/plugin/src/main/java/org/battleplugins/arena/event/action/types/BroadcastAction.java @@ -8,6 +8,7 @@ import org.battleplugins.arena.competition.Competition; import org.battleplugins.arena.competition.LiveCompetition; import org.battleplugins.arena.event.action.EventAction; +import org.battleplugins.arena.resolver.Resolvable; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -25,11 +26,11 @@ public BroadcastAction(Map params) { } @Override - public void call(ArenaPlayer arenaPlayer) { + public void call(ArenaPlayer arenaPlayer, Resolvable resolvable) { } @Override - public void postProcess(Arena arena, Competition competition) { + public void postProcess(Arena arena, Competition competition, Resolvable resolvable) { if (!(competition instanceof LiveCompetition liveCompetition)) { return; } @@ -49,7 +50,7 @@ public void postProcess(Arena arena, Competition competition) { }; for (Player player : players) { - Component component = MiniMessage.miniMessage().deserialize(message); + Component component = MiniMessage.miniMessage().deserialize(resolvable.resolve().resolveToString(message)); switch (messageType) { case CHAT -> player.sendMessage(component); case ACTION_BAR -> player.sendActionBar(component); diff --git a/plugin/src/main/java/org/battleplugins/arena/event/action/types/ChangeGamemodeAction.java b/plugin/src/main/java/org/battleplugins/arena/event/action/types/ChangeGamemodeAction.java index 399361f2..ed9b05ac 100644 --- a/plugin/src/main/java/org/battleplugins/arena/event/action/types/ChangeGamemodeAction.java +++ b/plugin/src/main/java/org/battleplugins/arena/event/action/types/ChangeGamemodeAction.java @@ -2,6 +2,7 @@ import org.battleplugins.arena.ArenaPlayer; import org.battleplugins.arena.event.action.EventAction; +import org.battleplugins.arena.resolver.Resolvable; import org.bukkit.GameMode; import java.util.Locale; @@ -15,7 +16,7 @@ public ChangeGamemodeAction(Map params) { } @Override - public void call(ArenaPlayer arenaPlayer) { + public void call(ArenaPlayer arenaPlayer, Resolvable resolvable) { arenaPlayer.getPlayer().setGameMode(GameMode.valueOf(this.get(GAMEMODE_KEY).toUpperCase(Locale.ROOT))); } } diff --git a/plugin/src/main/java/org/battleplugins/arena/event/action/types/ChangeRoleAction.java b/plugin/src/main/java/org/battleplugins/arena/event/action/types/ChangeRoleAction.java index fa226f5b..3ebc129b 100644 --- a/plugin/src/main/java/org/battleplugins/arena/event/action/types/ChangeRoleAction.java +++ b/plugin/src/main/java/org/battleplugins/arena/event/action/types/ChangeRoleAction.java @@ -5,6 +5,7 @@ import org.battleplugins.arena.event.action.EventAction; import org.battleplugins.arena.event.player.ArenaJoinEvent; import org.battleplugins.arena.event.player.ArenaSpectateEvent; +import org.battleplugins.arena.resolver.Resolvable; import java.util.Locale; import java.util.Map; @@ -17,7 +18,7 @@ public ChangeRoleAction(Map params) { } @Override - public void call(ArenaPlayer arenaPlayer) { + public void call(ArenaPlayer arenaPlayer, Resolvable resolvable) { PlayerRole role = PlayerRole.valueOf(this.get(ROLE_KEY).toUpperCase(Locale.ROOT)); boolean changedRole = arenaPlayer.getRole() != role; arenaPlayer.setRole(role); diff --git a/plugin/src/main/java/org/battleplugins/arena/event/action/types/ClearEffectsAction.java b/plugin/src/main/java/org/battleplugins/arena/event/action/types/ClearEffectsAction.java index b93af503..993ef2e8 100644 --- a/plugin/src/main/java/org/battleplugins/arena/event/action/types/ClearEffectsAction.java +++ b/plugin/src/main/java/org/battleplugins/arena/event/action/types/ClearEffectsAction.java @@ -2,6 +2,7 @@ import org.battleplugins.arena.ArenaPlayer; import org.battleplugins.arena.event.action.EventAction; +import org.battleplugins.arena.resolver.Resolvable; import org.bukkit.potion.PotionEffect; import java.util.List; @@ -14,7 +15,7 @@ public ClearEffectsAction(Map params) { } @Override - public void call(ArenaPlayer arenaPlayer) { + public void call(ArenaPlayer arenaPlayer, Resolvable resolvable) { for (PotionEffect effect : List.copyOf(arenaPlayer.getPlayer().getActivePotionEffects())) { arenaPlayer.getPlayer().removePotionEffect(effect.getType()); } diff --git a/plugin/src/main/java/org/battleplugins/arena/event/action/types/ClearInventoryAction.java b/plugin/src/main/java/org/battleplugins/arena/event/action/types/ClearInventoryAction.java index a241fd97..767d938b 100644 --- a/plugin/src/main/java/org/battleplugins/arena/event/action/types/ClearInventoryAction.java +++ b/plugin/src/main/java/org/battleplugins/arena/event/action/types/ClearInventoryAction.java @@ -2,6 +2,7 @@ import org.battleplugins.arena.ArenaPlayer; import org.battleplugins.arena.event.action.EventAction; +import org.battleplugins.arena.resolver.Resolvable; import java.util.Map; @@ -12,7 +13,7 @@ public ClearInventoryAction(Map params) { } @Override - public void call(ArenaPlayer arenaPlayer) { + public void call(ArenaPlayer arenaPlayer, Resolvable resolvable) { arenaPlayer.getPlayer().getInventory().clear(); } } diff --git a/plugin/src/main/java/org/battleplugins/arena/event/action/types/DelayAction.java b/plugin/src/main/java/org/battleplugins/arena/event/action/types/DelayAction.java index a073e607..d75abd9d 100644 --- a/plugin/src/main/java/org/battleplugins/arena/event/action/types/DelayAction.java +++ b/plugin/src/main/java/org/battleplugins/arena/event/action/types/DelayAction.java @@ -2,6 +2,7 @@ import org.battleplugins.arena.ArenaPlayer; import org.battleplugins.arena.event.action.EventAction; +import org.battleplugins.arena.resolver.Resolvable; import java.util.Map; @@ -13,7 +14,7 @@ public DelayAction(Map params) { } @Override - public void call(ArenaPlayer arenaPlayer) { + public void call(ArenaPlayer arenaPlayer, Resolvable resolvable) { // No-op; this is simply used to mark when a delay // should occur between other actions } diff --git a/plugin/src/main/java/org/battleplugins/arena/event/action/types/FlightAction.java b/plugin/src/main/java/org/battleplugins/arena/event/action/types/FlightAction.java index a3bc42ff..0a9e5717 100644 --- a/plugin/src/main/java/org/battleplugins/arena/event/action/types/FlightAction.java +++ b/plugin/src/main/java/org/battleplugins/arena/event/action/types/FlightAction.java @@ -2,6 +2,7 @@ import org.battleplugins.arena.ArenaPlayer; import org.battleplugins.arena.event.action.EventAction; +import org.battleplugins.arena.resolver.Resolvable; import java.util.Map; @@ -13,7 +14,7 @@ public FlightAction(Map params) { } @Override - public void call(ArenaPlayer arenaPlayer) { + public void call(ArenaPlayer arenaPlayer, Resolvable resolvable) { boolean enabled = Boolean.parseBoolean(this.get(FLIGHT_KEY)); arenaPlayer.getPlayer().setAllowFlight(enabled); diff --git a/plugin/src/main/java/org/battleplugins/arena/event/action/types/GiveEffectsAction.java b/plugin/src/main/java/org/battleplugins/arena/event/action/types/GiveEffectsAction.java index 415a98b3..03352bf8 100644 --- a/plugin/src/main/java/org/battleplugins/arena/event/action/types/GiveEffectsAction.java +++ b/plugin/src/main/java/org/battleplugins/arena/event/action/types/GiveEffectsAction.java @@ -4,6 +4,7 @@ import org.battleplugins.arena.config.ParseException; import org.battleplugins.arena.config.SingularValueParser; import org.battleplugins.arena.event.action.EventAction; +import org.battleplugins.arena.resolver.Resolvable; import org.bukkit.potion.PotionEffectType; import java.util.Map; @@ -16,7 +17,7 @@ public GiveEffectsAction(Map params) { } @Override - public void call(ArenaPlayer arenaPlayer) { + public void call(ArenaPlayer arenaPlayer, Resolvable resolvable) { SingularValueParser.ArgumentBuffer buffer; try { buffer = SingularValueParser.parseUnnamed(this.get(EFFECTS_KEY), SingularValueParser.BraceStyle.SQUARE, ','); diff --git a/plugin/src/main/java/org/battleplugins/arena/event/action/types/HealthAction.java b/plugin/src/main/java/org/battleplugins/arena/event/action/types/HealthAction.java index ea93167e..0da2a4bc 100644 --- a/plugin/src/main/java/org/battleplugins/arena/event/action/types/HealthAction.java +++ b/plugin/src/main/java/org/battleplugins/arena/event/action/types/HealthAction.java @@ -2,6 +2,7 @@ import org.battleplugins.arena.ArenaPlayer; import org.battleplugins.arena.event.action.EventAction; +import org.battleplugins.arena.resolver.Resolvable; import java.util.Map; @@ -14,7 +15,7 @@ public HealthAction(Map params) { } @Override - public void call(ArenaPlayer arenaPlayer) { + public void call(ArenaPlayer arenaPlayer, Resolvable resolvable) { double health = Double.parseDouble(this.get(HEALTH_KEY)); int hunger = Integer.parseInt(this.get(HUNGER_KEY)); diff --git a/plugin/src/main/java/org/battleplugins/arena/event/action/types/JoinRandomTeamAction.java b/plugin/src/main/java/org/battleplugins/arena/event/action/types/JoinRandomTeamAction.java index b822e13b..c7ba6327 100644 --- a/plugin/src/main/java/org/battleplugins/arena/event/action/types/JoinRandomTeamAction.java +++ b/plugin/src/main/java/org/battleplugins/arena/event/action/types/JoinRandomTeamAction.java @@ -3,6 +3,7 @@ import org.battleplugins.arena.ArenaPlayer; import org.battleplugins.arena.competition.team.TeamManager; import org.battleplugins.arena.event.action.EventAction; +import org.battleplugins.arena.resolver.Resolvable; import java.util.Map; @@ -12,7 +13,7 @@ public JoinRandomTeamAction(Map params) { } @Override - public void call(ArenaPlayer arenaPlayer) { + public void call(ArenaPlayer arenaPlayer, Resolvable resolvable) { if (arenaPlayer.getTeam() == null) { TeamManager teamManager = arenaPlayer.getCompetition().getTeamManager(); teamManager.joinTeam(arenaPlayer, teamManager.findSuitableTeam()); diff --git a/plugin/src/main/java/org/battleplugins/arena/event/action/types/KillEntitiesAction.java b/plugin/src/main/java/org/battleplugins/arena/event/action/types/KillEntitiesAction.java index 2339f6c4..f86ce0dd 100644 --- a/plugin/src/main/java/org/battleplugins/arena/event/action/types/KillEntitiesAction.java +++ b/plugin/src/main/java/org/battleplugins/arena/event/action/types/KillEntitiesAction.java @@ -8,6 +8,7 @@ import org.battleplugins.arena.config.ParseException; import org.battleplugins.arena.config.SingularValueParser; import org.battleplugins.arena.event.action.EventAction; +import org.battleplugins.arena.resolver.Resolvable; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; @@ -24,11 +25,11 @@ public KillEntitiesAction(Map params, String... requiredKeys) { } @Override - public void call(ArenaPlayer arenaPlayer) { + public void call(ArenaPlayer arenaPlayer, Resolvable resolvable) { } @Override - public void postProcess(Arena arena, Competition competition) { + public void postProcess(Arena arena, Competition competition, Resolvable resolvable) { if (!(competition instanceof LiveCompetition liveCompetition)) { return; } diff --git a/plugin/src/main/java/org/battleplugins/arena/event/action/types/LeaveAction.java b/plugin/src/main/java/org/battleplugins/arena/event/action/types/LeaveAction.java index 19e2c6f9..d182185e 100644 --- a/plugin/src/main/java/org/battleplugins/arena/event/action/types/LeaveAction.java +++ b/plugin/src/main/java/org/battleplugins/arena/event/action/types/LeaveAction.java @@ -3,6 +3,7 @@ import org.battleplugins.arena.ArenaPlayer; import org.battleplugins.arena.event.action.EventAction; import org.battleplugins.arena.event.player.ArenaLeaveEvent; +import org.battleplugins.arena.resolver.Resolvable; import java.util.Map; @@ -13,7 +14,7 @@ public LeaveAction(Map params) { } @Override - public void call(ArenaPlayer arenaPlayer) { + public void call(ArenaPlayer arenaPlayer, Resolvable resolvable) { arenaPlayer.getCompetition().leave(arenaPlayer, ArenaLeaveEvent.Cause.GAME); } } diff --git a/plugin/src/main/java/org/battleplugins/arena/event/action/types/PlaySoundAction.java b/plugin/src/main/java/org/battleplugins/arena/event/action/types/PlaySoundAction.java index d5bf566a..7ac02483 100644 --- a/plugin/src/main/java/org/battleplugins/arena/event/action/types/PlaySoundAction.java +++ b/plugin/src/main/java/org/battleplugins/arena/event/action/types/PlaySoundAction.java @@ -2,6 +2,7 @@ import org.battleplugins.arena.ArenaPlayer; import org.battleplugins.arena.event.action.EventAction; +import org.battleplugins.arena.resolver.Resolvable; import java.util.Map; @@ -15,7 +16,7 @@ public PlaySoundAction(Map params) { } @Override - public void call(ArenaPlayer arenaPlayer) { + public void call(ArenaPlayer arenaPlayer, Resolvable resolvable) { String sound = this.get(SOUND_KEY); float pitch = Float.parseFloat(this.getOrDefault(PITCH_KEY, "1")); float volume = Float.parseFloat(this.getOrDefault(VOLUME_KEY, "1")); diff --git a/plugin/src/main/java/org/battleplugins/arena/event/action/types/ResetStateAction.java b/plugin/src/main/java/org/battleplugins/arena/event/action/types/ResetStateAction.java index 6dfc002a..abb31c7f 100644 --- a/plugin/src/main/java/org/battleplugins/arena/event/action/types/ResetStateAction.java +++ b/plugin/src/main/java/org/battleplugins/arena/event/action/types/ResetStateAction.java @@ -2,6 +2,7 @@ import org.battleplugins.arena.ArenaPlayer; import org.battleplugins.arena.event.action.EventAction; +import org.battleplugins.arena.resolver.Resolvable; import java.util.Map; @@ -12,7 +13,7 @@ public ResetStateAction(Map params) { } @Override - public void call(ArenaPlayer arenaPlayer) { + public void call(ArenaPlayer arenaPlayer, Resolvable resolvable) { arenaPlayer.resetState(); } } diff --git a/plugin/src/main/java/org/battleplugins/arena/event/action/types/RespawnAction.java b/plugin/src/main/java/org/battleplugins/arena/event/action/types/RespawnAction.java index 4d3759e6..1ab1ed8f 100644 --- a/plugin/src/main/java/org/battleplugins/arena/event/action/types/RespawnAction.java +++ b/plugin/src/main/java/org/battleplugins/arena/event/action/types/RespawnAction.java @@ -3,6 +3,7 @@ import org.battleplugins.arena.ArenaPlayer; import org.battleplugins.arena.BattleArena; import org.battleplugins.arena.event.action.EventAction; +import org.battleplugins.arena.resolver.Resolvable; import org.bukkit.Bukkit; import java.util.Map; @@ -14,7 +15,7 @@ public RespawnAction(Map params) { } @Override - public void call(ArenaPlayer arenaPlayer) { + public void call(ArenaPlayer arenaPlayer, Resolvable resolvable) { Bukkit.getScheduler().runTaskLater(BattleArena.getInstance(), arenaPlayer.getPlayer().spigot()::respawn, 1L); } } diff --git a/plugin/src/main/java/org/battleplugins/arena/event/action/types/RestoreAction.java b/plugin/src/main/java/org/battleplugins/arena/event/action/types/RestoreAction.java index 5785de43..ab5865f5 100644 --- a/plugin/src/main/java/org/battleplugins/arena/event/action/types/RestoreAction.java +++ b/plugin/src/main/java/org/battleplugins/arena/event/action/types/RestoreAction.java @@ -3,6 +3,7 @@ import org.battleplugins.arena.ArenaPlayer; import org.battleplugins.arena.event.action.EventAction; import org.battleplugins.arena.competition.PlayerStorage; +import org.battleplugins.arena.resolver.Resolvable; import java.util.Map; import java.util.Set; @@ -15,7 +16,7 @@ public RestoreAction(Map params) { } @Override - public void call(ArenaPlayer arenaPlayer) { + public void call(ArenaPlayer arenaPlayer, Resolvable resolvable) { String[] types = this.get(TYPES_KEY).split(","); PlayerStorage.Type[] toStore = new PlayerStorage.Type[types.length]; for (int i = 0; i < types.length; i++) { diff --git a/plugin/src/main/java/org/battleplugins/arena/event/action/types/RunCommandAction.java b/plugin/src/main/java/org/battleplugins/arena/event/action/types/RunCommandAction.java index e4fc839d..21de314d 100644 --- a/plugin/src/main/java/org/battleplugins/arena/event/action/types/RunCommandAction.java +++ b/plugin/src/main/java/org/battleplugins/arena/event/action/types/RunCommandAction.java @@ -2,6 +2,7 @@ import org.battleplugins.arena.ArenaPlayer; import org.battleplugins.arena.event.action.EventAction; +import org.battleplugins.arena.resolver.Resolvable; import org.bukkit.Bukkit; import java.util.Map; @@ -15,8 +16,8 @@ public RunCommandAction(Map params) { } @Override - public void call(ArenaPlayer arenaPlayer) { - String command = this.get(COMMAND_KEY); + public void call(ArenaPlayer arenaPlayer, Resolvable resolvable) { + String command = resolvable.resolve().resolveToString(this.get(COMMAND_KEY)); String source = this.getOrDefault(SOURCE_KEY, "player"); if (source.equalsIgnoreCase("player")) { arenaPlayer.getPlayer().performCommand(command); diff --git a/plugin/src/main/java/org/battleplugins/arena/event/action/types/SendMessageAction.java b/plugin/src/main/java/org/battleplugins/arena/event/action/types/SendMessageAction.java index d4703e67..bb9bda86 100644 --- a/plugin/src/main/java/org/battleplugins/arena/event/action/types/SendMessageAction.java +++ b/plugin/src/main/java/org/battleplugins/arena/event/action/types/SendMessageAction.java @@ -5,6 +5,7 @@ import net.kyori.adventure.title.Title; import org.battleplugins.arena.ArenaPlayer; import org.battleplugins.arena.event.action.EventAction; +import org.battleplugins.arena.resolver.Resolvable; import java.util.Locale; import java.util.Map; @@ -18,13 +19,13 @@ public SendMessageAction(Map params) { } @Override - public void call(ArenaPlayer arenaPlayer) { + public void call(ArenaPlayer arenaPlayer, Resolvable resolvable) { String message = this.get(MESSAGE_KEY); MessageType messageType = MessageType.valueOf(this.getOrDefault(TYPE_KEY, MessageType.CHAT.name()) .toUpperCase(Locale.ROOT) ); - Component component = MiniMessage.miniMessage().deserialize(message); + Component component = MiniMessage.miniMessage().deserialize(resolvable.resolve().resolveToString(message)); switch (messageType) { case CHAT -> arenaPlayer.getPlayer().sendMessage(component); case ACTION_BAR -> arenaPlayer.getPlayer().sendActionBar(component); diff --git a/plugin/src/main/java/org/battleplugins/arena/event/action/types/StoreAction.java b/plugin/src/main/java/org/battleplugins/arena/event/action/types/StoreAction.java index 457b92c3..b1597e0d 100644 --- a/plugin/src/main/java/org/battleplugins/arena/event/action/types/StoreAction.java +++ b/plugin/src/main/java/org/battleplugins/arena/event/action/types/StoreAction.java @@ -3,6 +3,7 @@ import org.battleplugins.arena.ArenaPlayer; import org.battleplugins.arena.event.action.EventAction; import org.battleplugins.arena.competition.PlayerStorage; +import org.battleplugins.arena.resolver.Resolvable; import java.util.Map; import java.util.Set; @@ -15,7 +16,7 @@ public StoreAction(Map params) { } @Override - public void call(ArenaPlayer arenaPlayer) { + public void call(ArenaPlayer arenaPlayer, Resolvable resolvable) { String[] types = this.get(TYPES_KEY).split(","); PlayerStorage.Type[] toStore = new PlayerStorage.Type[types.length]; for (int i = 0; i < types.length; i++) { diff --git a/plugin/src/main/java/org/battleplugins/arena/event/action/types/TeardownAction.java b/plugin/src/main/java/org/battleplugins/arena/event/action/types/TeardownAction.java index 06a5a16c..f07465cf 100644 --- a/plugin/src/main/java/org/battleplugins/arena/event/action/types/TeardownAction.java +++ b/plugin/src/main/java/org/battleplugins/arena/event/action/types/TeardownAction.java @@ -4,6 +4,7 @@ import org.battleplugins.arena.ArenaPlayer; import org.battleplugins.arena.competition.Competition; import org.battleplugins.arena.event.action.EventAction; +import org.battleplugins.arena.resolver.Resolvable; import java.util.Map; @@ -14,11 +15,11 @@ public TeardownAction(Map params, String... requiredKeys) { } @Override - public void call(ArenaPlayer arenaPlayer) { + public void call(ArenaPlayer arenaPlayer, Resolvable resolvable) { } @Override - public void postProcess(Arena arena, Competition competition) { + public void postProcess(Arena arena, Competition competition, Resolvable resolvable) { arena.getPlugin().removeCompetition(arena, competition); } } diff --git a/plugin/src/main/java/org/battleplugins/arena/event/action/types/TeleportAction.java b/plugin/src/main/java/org/battleplugins/arena/event/action/types/TeleportAction.java index f987a11b..954dbacc 100644 --- a/plugin/src/main/java/org/battleplugins/arena/event/action/types/TeleportAction.java +++ b/plugin/src/main/java/org/battleplugins/arena/event/action/types/TeleportAction.java @@ -4,6 +4,7 @@ import org.battleplugins.arena.competition.Competition; import org.battleplugins.arena.competition.map.options.TeamSpawns; import org.battleplugins.arena.event.action.EventAction; +import org.battleplugins.arena.resolver.Resolvable; import org.battleplugins.arena.util.PositionWithRotation; import org.bukkit.Location; import org.bukkit.entity.Player; @@ -25,7 +26,7 @@ public TeleportAction(Map params) { } @Override - public void call(ArenaPlayer arenaPlayer) { + public void call(ArenaPlayer arenaPlayer, Resolvable resolvable) { Player player = arenaPlayer.getPlayer(); TeleportLocation location = TeleportLocation.valueOf(this.get(LOCATION_KEY).toUpperCase(Locale.ROOT)); boolean randomized = Boolean.parseBoolean(this.getOrDefault(RANDOM, "false")); diff --git a/plugin/src/main/java/org/battleplugins/arena/event/player/ArenaKillEvent.java b/plugin/src/main/java/org/battleplugins/arena/event/player/ArenaKillEvent.java index bb736635..f34f4726 100644 --- a/plugin/src/main/java/org/battleplugins/arena/event/player/ArenaKillEvent.java +++ b/plugin/src/main/java/org/battleplugins/arena/event/player/ArenaKillEvent.java @@ -2,6 +2,9 @@ import org.battleplugins.arena.ArenaPlayer; import org.battleplugins.arena.event.EventTrigger; +import org.battleplugins.arena.resolver.Resolver; +import org.battleplugins.arena.resolver.ResolverKeys; +import org.battleplugins.arena.resolver.ResolverProvider; import org.bukkit.event.HandlerList; import org.jetbrains.annotations.NotNull; @@ -39,6 +42,15 @@ public ArenaPlayer getKiller() { return this.getArenaPlayer(); } + @Override + public Resolver resolve() { + return super.resolve() + .toBuilder() + .define(ResolverKeys.KILLER, ResolverProvider.simple(this.getKiller(), this.getKiller().getPlayer()::getName)) + .define(ResolverKeys.KILLED, ResolverProvider.simple(this.getKilled(), this.getKilled().getPlayer()::getName)) + .build(); + } + @NotNull @Override public HandlerList getHandlers() { diff --git a/plugin/src/main/java/org/battleplugins/arena/event/player/ArenaLeaveEvent.java b/plugin/src/main/java/org/battleplugins/arena/event/player/ArenaLeaveEvent.java index 4cfbd747..83f5f0b4 100644 --- a/plugin/src/main/java/org/battleplugins/arena/event/player/ArenaLeaveEvent.java +++ b/plugin/src/main/java/org/battleplugins/arena/event/player/ArenaLeaveEvent.java @@ -12,7 +12,7 @@ public class ArenaLeaveEvent extends BukkitArenaPlayerEvent { private final static HandlerList HANDLERS = new HandlerList(); - private Cause cause; + private final Cause cause; public ArenaLeaveEvent(ArenaPlayer player, Cause cause) { super(player.getArena(), player); diff --git a/plugin/src/main/java/org/battleplugins/arena/event/player/ArenaLifeDepleteEvent.java b/plugin/src/main/java/org/battleplugins/arena/event/player/ArenaLifeDepleteEvent.java index 3d20576e..df422132 100644 --- a/plugin/src/main/java/org/battleplugins/arena/event/player/ArenaLifeDepleteEvent.java +++ b/plugin/src/main/java/org/battleplugins/arena/event/player/ArenaLifeDepleteEvent.java @@ -3,6 +3,9 @@ import org.battleplugins.arena.Arena; import org.battleplugins.arena.ArenaPlayer; import org.battleplugins.arena.event.EventTrigger; +import org.battleplugins.arena.resolver.Resolver; +import org.battleplugins.arena.resolver.ResolverKeys; +import org.battleplugins.arena.resolver.ResolverProvider; import org.bukkit.event.HandlerList; import org.jetbrains.annotations.NotNull; @@ -35,6 +38,14 @@ public int getLivesLeft() { return this.livesLeft; } + @Override + public Resolver resolve() { + return super.resolve() + .toBuilder() + .define(ResolverKeys.LIVES_LEFT, ResolverProvider.simple(this.livesLeft, String::valueOf)) + .build(); + } + @NotNull @Override public HandlerList getHandlers() { diff --git a/plugin/src/main/java/org/battleplugins/arena/event/player/ArenaPlayerEvent.java b/plugin/src/main/java/org/battleplugins/arena/event/player/ArenaPlayerEvent.java index 83b491d2..2f195835 100644 --- a/plugin/src/main/java/org/battleplugins/arena/event/player/ArenaPlayerEvent.java +++ b/plugin/src/main/java/org/battleplugins/arena/event/player/ArenaPlayerEvent.java @@ -4,6 +4,7 @@ import org.battleplugins.arena.ArenaPlayer; import org.battleplugins.arena.competition.LiveCompetition; import org.battleplugins.arena.event.ArenaEvent; +import org.battleplugins.arena.resolver.Resolver; /** * Represents an event that occurs in an {@link Arena} to a @@ -27,4 +28,14 @@ default LiveCompetition getCompetition() { * @return the arena player this event is occurring to */ ArenaPlayer getArenaPlayer(); + + /** + * Resolves the {@link ArenaPlayer} this event is occurring to + * to a {@link Resolver} object. + * + * @return the resolved object + */ + default Resolver resolve() { + return this.getArenaPlayer().resolve(); + } } diff --git a/plugin/src/main/java/org/battleplugins/arena/event/player/ArenaStatChangeEvent.java b/plugin/src/main/java/org/battleplugins/arena/event/player/ArenaStatChangeEvent.java index 4aa08815..fa056d08 100644 --- a/plugin/src/main/java/org/battleplugins/arena/event/player/ArenaStatChangeEvent.java +++ b/plugin/src/main/java/org/battleplugins/arena/event/player/ArenaStatChangeEvent.java @@ -5,6 +5,9 @@ import org.battleplugins.arena.competition.LiveCompetition; import org.battleplugins.arena.event.ArenaEvent; import org.battleplugins.arena.event.EventTrigger; +import org.battleplugins.arena.resolver.Resolver; +import org.battleplugins.arena.resolver.ResolverKeys; +import org.battleplugins.arena.resolver.ResolverProvider; import org.battleplugins.arena.stat.ArenaStat; import org.battleplugins.arena.stat.StatHolder; import org.bukkit.event.Event; @@ -70,23 +73,33 @@ public T getNewValue() { return this.newValue; } - @NotNull @Override - public HandlerList getHandlers() { - return HANDLERS; + public Arena getArena() { + return this.competition.getArena(); } - public static HandlerList getHandlerList() { - return HANDLERS; + @Override + public Competition getCompetition() { + return this.competition; } @Override - public Arena getArena() { - return this.competition.getArena(); + public Resolver resolve() { + return ArenaEvent.super.resolve().toBuilder() + .define(ResolverKeys.STAT, ResolverProvider.simple(this.stat, ArenaStat::getName)) + .define(ResolverKeys.STAT_HOLDER, ResolverProvider.simple(this.statHolder, StatHolder::describe)) + .define(ResolverKeys.OLD_STAT_VALUE, ResolverProvider.simple(this.oldValue, Object::toString)) + .define(ResolverKeys.NEW_STAT_VALUE, ResolverProvider.simple(this.newValue, Object::toString)) + .build(); } + @NotNull @Override - public Competition getCompetition() { - return this.competition; + public HandlerList getHandlers() { + return HANDLERS; + } + + public static HandlerList getHandlerList() { + return HANDLERS; } } diff --git a/plugin/src/main/java/org/battleplugins/arena/event/player/ArenaTeamJoinEvent.java b/plugin/src/main/java/org/battleplugins/arena/event/player/ArenaTeamJoinEvent.java index b13a7232..8c4a30e9 100644 --- a/plugin/src/main/java/org/battleplugins/arena/event/player/ArenaTeamJoinEvent.java +++ b/plugin/src/main/java/org/battleplugins/arena/event/player/ArenaTeamJoinEvent.java @@ -1,6 +1,9 @@ package org.battleplugins.arena.event.player; import org.battleplugins.arena.ArenaPlayer; +import org.battleplugins.arena.resolver.Resolver; +import org.battleplugins.arena.resolver.ResolverKeys; +import org.battleplugins.arena.resolver.ResolverProvider; import org.battleplugins.arena.team.ArenaTeam; import org.bukkit.event.HandlerList; import org.jetbrains.annotations.NotNull; @@ -28,6 +31,13 @@ public ArenaTeam getTeam() { return this.team; } + @Override + public Resolver resolve() { + return super.resolve().toBuilder() + .define(ResolverKeys.TEAM, ResolverProvider.simple(this.team, ArenaTeam::getName, ArenaTeam::getFormattedName)) + .build(); + } + @NotNull @Override public HandlerList getHandlers() { diff --git a/plugin/src/main/java/org/battleplugins/arena/event/player/ArenaTeamLeaveEvent.java b/plugin/src/main/java/org/battleplugins/arena/event/player/ArenaTeamLeaveEvent.java index b9c46f6f..3c164f0e 100644 --- a/plugin/src/main/java/org/battleplugins/arena/event/player/ArenaTeamLeaveEvent.java +++ b/plugin/src/main/java/org/battleplugins/arena/event/player/ArenaTeamLeaveEvent.java @@ -1,6 +1,9 @@ package org.battleplugins.arena.event.player; import org.battleplugins.arena.ArenaPlayer; +import org.battleplugins.arena.resolver.Resolver; +import org.battleplugins.arena.resolver.ResolverKeys; +import org.battleplugins.arena.resolver.ResolverProvider; import org.battleplugins.arena.team.ArenaTeam; import org.bukkit.event.HandlerList; import org.jetbrains.annotations.NotNull; @@ -28,6 +31,13 @@ public ArenaTeam getTeam() { return this.team; } + @Override + public Resolver resolve() { + return super.resolve().toBuilder() + .define(ResolverKeys.TEAM, ResolverProvider.simple(this.team, ArenaTeam::getName, ArenaTeam::getFormattedName)) + .build(); + } + @NotNull @Override public HandlerList getHandlers() { diff --git a/plugin/src/main/java/org/battleplugins/arena/resolver/Resolvable.java b/plugin/src/main/java/org/battleplugins/arena/resolver/Resolvable.java new file mode 100644 index 00000000..ec8194bd --- /dev/null +++ b/plugin/src/main/java/org/battleplugins/arena/resolver/Resolvable.java @@ -0,0 +1,6 @@ +package org.battleplugins.arena.resolver; + +public interface Resolvable { + + Resolver resolve(); +} diff --git a/plugin/src/main/java/org/battleplugins/arena/resolver/Resolver.java b/plugin/src/main/java/org/battleplugins/arena/resolver/Resolver.java new file mode 100644 index 00000000..76064ac2 --- /dev/null +++ b/plugin/src/main/java/org/battleplugins/arena/resolver/Resolver.java @@ -0,0 +1,23 @@ +package org.battleplugins.arena.resolver; + +public interface Resolver { + + String resolveToString(String string); + + T resolve(ResolverKey key); + + boolean has(ResolverKey key); + + Builder toBuilder(); + + static Builder builder() { + return new ResolverImpl.BuilderImpl(); + } + + interface Builder { + + Builder define(ResolverKey key, ResolverProvider provider); + + Resolver build(); + } +} diff --git a/plugin/src/main/java/org/battleplugins/arena/resolver/ResolverImpl.java b/plugin/src/main/java/org/battleplugins/arena/resolver/ResolverImpl.java new file mode 100644 index 00000000..dc5f4654 --- /dev/null +++ b/plugin/src/main/java/org/battleplugins/arena/resolver/ResolverImpl.java @@ -0,0 +1,60 @@ +package org.battleplugins.arena.resolver; + +import java.util.HashMap; +import java.util.Map; + +class ResolverImpl implements Resolver { + private final Map, ResolverProvider> results; + + ResolverImpl(Map, ResolverProvider> results) { + this.results = new HashMap<>(results); + } + + @Override + public String resolveToString(String string) { + for (Map.Entry, ResolverProvider> entry : this.results.entrySet()) { + String key = "%" + entry.getKey().getName().replace("-", "_") + "%"; + string = string.replace(key, entry.getValue().toString(this)); + } + + return string; + } + + @SuppressWarnings("unchecked") + @Override + public T resolve(ResolverKey key) { + ResolverProvider provider = this.results.get(key); + if (provider == null) { + throw new IllegalArgumentException("No provider defined for key " + key); + } + + return (T) provider.resolve(this); + } + + @Override + public boolean has(ResolverKey key) { + return this.results.containsKey(key); + } + + @Override + public Builder toBuilder() { + BuilderImpl builder = new BuilderImpl(); + builder.results.putAll(this.results); + return builder; + } + + static class BuilderImpl implements Resolver.Builder { + private final Map, ResolverProvider> results = new HashMap<>(); + + @Override + public Builder define(ResolverKey key, ResolverProvider provider) { + this.results.put(key, provider); + return this; + } + + @Override + public Resolver build() { + return new ResolverImpl(this.results); + } + } +} diff --git a/plugin/src/main/java/org/battleplugins/arena/resolver/ResolverKey.java b/plugin/src/main/java/org/battleplugins/arena/resolver/ResolverKey.java new file mode 100644 index 00000000..b869b551 --- /dev/null +++ b/plugin/src/main/java/org/battleplugins/arena/resolver/ResolverKey.java @@ -0,0 +1,34 @@ +package org.battleplugins.arena.resolver; + +import org.battleplugins.arena.util.TypeToken; + +public final class ResolverKey { + private final String name; + private final TypeToken type; + + ResolverKey(String name, Class type) { + this.name = name; + this.type = TypeToken.of(type); + } + + ResolverKey(String name, TypeToken type) { + this.name = name; + this.type = type; + } + + public String getName() { + return this.name; + } + + public TypeToken getType() { + return this.type; + } + + public static ResolverKey create(String name, Class type) { + return new ResolverKey<>(name, type); + } + + public static ResolverKey create(String name, TypeToken type) { + return new ResolverKey<>(name, type); + } +} diff --git a/plugin/src/main/java/org/battleplugins/arena/resolver/ResolverKeys.java b/plugin/src/main/java/org/battleplugins/arena/resolver/ResolverKeys.java new file mode 100644 index 00000000..feea3e6f --- /dev/null +++ b/plugin/src/main/java/org/battleplugins/arena/resolver/ResolverKeys.java @@ -0,0 +1,28 @@ +package org.battleplugins.arena.resolver; + +import org.battleplugins.arena.Arena; +import org.battleplugins.arena.ArenaPlayer; +import org.battleplugins.arena.competition.Competition; +import org.battleplugins.arena.stat.ArenaStat; +import org.battleplugins.arena.stat.StatHolder; +import org.battleplugins.arena.team.ArenaTeam; +import org.battleplugins.arena.util.TypeToken; + +import java.util.Set; + +public final class ResolverKeys { + public static final ResolverKey ARENA = ResolverKey.create("arena", Arena.class); + public static final ResolverKey COMPETITION = ResolverKey.create("competition", Competition.class); + public static final ResolverKey KILLED = ResolverKey.create("killed", ArenaPlayer.class); + public static final ResolverKey KILLER = ResolverKey.create("killer", ArenaPlayer.class); + public static final ResolverKey PLAYER = ResolverKey.create("player", ArenaPlayer.class); + public static final ResolverKey LIVES_LEFT = ResolverKey.create("lives-left", Integer.class); + public static final ResolverKey ONLINE_PLAYERS = ResolverKey.create("online-players", Integer.class); + public static final ResolverKey MAX_PLAYERS = ResolverKey.create("max-players", Integer.class); + public static final ResolverKey> PLAYERS = ResolverKey.create("players", TypeToken.of(Set.class)); + public static final ResolverKey STAT = ResolverKey.create("stat", ArenaStat.class); + public static final ResolverKey STAT_HOLDER = ResolverKey.create("stat-holder", StatHolder.class); + public static final ResolverKey OLD_STAT_VALUE = ResolverKey.create("old-stat-value", Object.class); + public static final ResolverKey NEW_STAT_VALUE = ResolverKey.create("new-stat-value", Object.class); + public static final ResolverKey TEAM = ResolverKey.create("team", ArenaTeam.class); +} diff --git a/plugin/src/main/java/org/battleplugins/arena/resolver/ResolverProvider.java b/plugin/src/main/java/org/battleplugins/arena/resolver/ResolverProvider.java new file mode 100644 index 00000000..9e94b079 --- /dev/null +++ b/plugin/src/main/java/org/battleplugins/arena/resolver/ResolverProvider.java @@ -0,0 +1,27 @@ +package org.battleplugins.arena.resolver; + +import net.kyori.adventure.text.Component; + +import java.util.function.Function; +import java.util.function.Supplier; + +public interface ResolverProvider { + + T resolve(Resolver resolver); + + String toString(Resolver resolver); + + Component toComponent(Resolver resolver); + + static ResolverProvider simple(T value, Supplier toString) { + return new SimpleResolverProvider<>(value, toString); + } + + static ResolverProvider simple(T value, Function toString) { + return new SimpleResolverProvider<>(value, toString); + } + + static ResolverProvider simple(T value, Function toString, Function toComponent) { + return new SimpleResolverProvider<>(value, toString, toComponent); + } +} diff --git a/plugin/src/main/java/org/battleplugins/arena/resolver/SimpleResolverProvider.java b/plugin/src/main/java/org/battleplugins/arena/resolver/SimpleResolverProvider.java new file mode 100644 index 00000000..c70db64c --- /dev/null +++ b/plugin/src/main/java/org/battleplugins/arena/resolver/SimpleResolverProvider.java @@ -0,0 +1,41 @@ +package org.battleplugins.arena.resolver; + +import net.kyori.adventure.text.Component; + +import java.util.function.Function; +import java.util.function.Supplier; + +public class SimpleResolverProvider implements ResolverProvider { + private final T value; + private final Function toString; + private final Function toComponent; + + protected SimpleResolverProvider(T value, Supplier toString) { + this(value, t -> toString.get()); + } + + protected SimpleResolverProvider(T value, Function toString) { + this(value, toString, t -> Component.text(toString.apply(t))); + } + + protected SimpleResolverProvider(T value, Function toString, Function toComponent) { + this.value = value; + this.toString = toString; + this.toComponent = toComponent; + } + + @Override + public T resolve(Resolver resolver) { + return this.value; + } + + @Override + public String toString(Resolver resolver) { + return this.toString.apply(this.value); + } + + @Override + public Component toComponent(Resolver resolver) { + return this.toComponent.apply(this.value); + } +} diff --git a/plugin/src/main/java/org/battleplugins/arena/stat/StatHolder.java b/plugin/src/main/java/org/battleplugins/arena/stat/StatHolder.java index 24a74955..b0111339 100644 --- a/plugin/src/main/java/org/battleplugins/arena/stat/StatHolder.java +++ b/plugin/src/main/java/org/battleplugins/arena/stat/StatHolder.java @@ -15,4 +15,6 @@ public interface StatHolder { void setStat(ArenaStat stat, T value); void computeStat(ArenaStat stat, Function computeFunction); + + String describe(); } diff --git a/plugin/src/main/java/org/battleplugins/arena/util/TypeToken.java b/plugin/src/main/java/org/battleplugins/arena/util/TypeToken.java new file mode 100644 index 00000000..6945f8d6 --- /dev/null +++ b/plugin/src/main/java/org/battleplugins/arena/util/TypeToken.java @@ -0,0 +1,34 @@ +package org.battleplugins.arena.util; + +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; + +public class TypeToken { + private final Type type; + + protected TypeToken() { + this.type = getSuperclassTypeParameter(getClass()); + } + + private TypeToken(Type type) { + this.type = type; + } + + public Type getType() { + return this.type; + } + + public static TypeToken of(Type type) { + return new TypeToken<>(type); + } + + private static Type getSuperclassTypeParameter(Class subclass) { + Type superclass = subclass.getGenericSuperclass(); + if (superclass instanceof Class) { + throw new RuntimeException("Missing type parameter."); + } + + ParameterizedType parameterized = (ParameterizedType) superclass; + return parameterized.getActualTypeArguments()[0]; + } +} diff --git a/settings.gradle.kts b/settings.gradle.kts index e465e763..237435e4 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -21,7 +21,6 @@ include("plugin") include("module:arena-restoration") include("module:boundary-enforcer") include("module:classes") -include("module:join-messages") include("module:team-heads") include("module:tournaments") include("module:vault-integration") \ No newline at end of file