From 5f6cb03cef8ef0134aa63c41290c8dd6c2d2047e Mon Sep 17 00:00:00 2001 From: RednedEpic Date: Fri, 27 Dec 2024 10:59:48 -0600 Subject: [PATCH] Add auto arena module and fix a couple bugs --- module/auto-arena/build.gradle.kts | 0 .../arena/module/autoarena/AutoArena.java | 60 +++++++++++++++++++ .../fancyholograms/FancyHologram.java | 8 ++- .../arena/command/ArenaCommandExecutor.java | 4 +- .../feature/hologram/HologramListener.java | 15 ----- settings.gradle.kts | 1 + 6 files changed, 68 insertions(+), 20 deletions(-) create mode 100644 module/auto-arena/build.gradle.kts create mode 100644 module/auto-arena/src/main/java/org/battleplugins/arena/module/autoarena/AutoArena.java diff --git a/module/auto-arena/build.gradle.kts b/module/auto-arena/build.gradle.kts new file mode 100644 index 00000000..e69de29b diff --git a/module/auto-arena/src/main/java/org/battleplugins/arena/module/autoarena/AutoArena.java b/module/auto-arena/src/main/java/org/battleplugins/arena/module/autoarena/AutoArena.java new file mode 100644 index 00000000..a52f3472 --- /dev/null +++ b/module/auto-arena/src/main/java/org/battleplugins/arena/module/autoarena/AutoArena.java @@ -0,0 +1,60 @@ +package org.battleplugins.arena.module.autoarena; + +import org.battleplugins.arena.Arena; +import org.battleplugins.arena.ArenaPlayer; +import org.battleplugins.arena.BattleArenaApi; +import org.battleplugins.arena.competition.Competition; +import org.battleplugins.arena.competition.LiveCompetition; +import org.battleplugins.arena.competition.PlayerRole; +import org.battleplugins.arena.competition.map.options.Bounds; +import org.battleplugins.arena.event.player.ArenaLeaveEvent; +import org.battleplugins.arena.module.ArenaModule; +import org.battleplugins.arena.module.ArenaModuleInitializer; +import org.bukkit.event.EventHandler; +import org.bukkit.event.player.PlayerMoveEvent; + +import java.util.List; + +/** + * A module that automatically places players into an arena when they walk into an arena's bounds. + */ +@ArenaModule(id = AutoArena.ID, name = "Auto Arena", description = "Places players into an arena when they walk into an arena's bounds.", authors = "BattlePlugins") +public class AutoArena implements ArenaModuleInitializer { + public static final String ID = "auto-arena"; + + @EventHandler + public void onMove(PlayerMoveEvent event) { + ArenaPlayer player = ArenaPlayer.getArenaPlayer(event.getPlayer()); + + // Player is in an arena - let's check if their current arena has this module + // enabled and if so, remove them from the arena if they are outside the bounds + if (player != null && player.getArena().isModuleEnabled(ID)) { + Bounds bounds = player.getCompetition().getMap().getBounds(); + if (bounds != null && !bounds.isInside(event.getTo())) { + player.getCompetition().leave(player, ArenaLeaveEvent.Cause.PLUGIN); + } + + return; + } + + // Player is not in an arena - let's check if they are in the bounds of a map + for (Arena arena : BattleArenaApi.get().getArenas()) { + if (!arena.isModuleEnabled(ID)) { + continue; + } + + List> competitions = BattleArenaApi.get().getCompetitions(arena); + for (Competition competition : competitions) { + if (!(competition instanceof LiveCompetition liveCompetition)) { + continue; + } + + Bounds bounds = liveCompetition.getMap().getBounds(); + if (liveCompetition.getMap().getWorld().equals(event.getPlayer().getWorld()) && bounds != null && bounds.isInside(event.getTo())) { + liveCompetition.join(event.getPlayer(), PlayerRole.PLAYING); + return; + } + } + } + } +} diff --git a/module/hologram-integration/src/main/java/org/battleplugins/arena/module/hologram/fancyholograms/FancyHologram.java b/module/hologram-integration/src/main/java/org/battleplugins/arena/module/hologram/fancyholograms/FancyHologram.java index 231314b9..0926e99d 100644 --- a/module/hologram-integration/src/main/java/org/battleplugins/arena/module/hologram/fancyholograms/FancyHologram.java +++ b/module/hologram-integration/src/main/java/org/battleplugins/arena/module/hologram/fancyholograms/FancyHologram.java @@ -2,8 +2,8 @@ import de.oliver.fancyholograms.api.data.TextHologramData; import de.oliver.fancyholograms.libs.chatcolorhandler.ModernChatColorHandler; -import de.oliver.fancyholograms.libs.chatcolorhandler.messengers.MiniMessageMessenger; import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.minimessage.MiniMessage; import org.battleplugins.arena.competition.LiveCompetition; import org.battleplugins.arena.feature.hologram.Hologram; import org.bukkit.Location; @@ -12,6 +12,8 @@ import java.util.List; public class FancyHologram implements Hologram { + private static final MiniMessage MINI_MESSAGE = MiniMessage.miniMessage(); + private final LiveCompetition competition; private final de.oliver.fancyholograms.api.hologram.Hologram impl; @@ -43,7 +45,7 @@ public List getLines() { @Override public void setLines(Component... lines) { if (this.impl.getData() instanceof TextHologramData data) { - List stringLines = Arrays.stream(lines).map(MiniMessageMessenger.MINI_MESSAGE::serialize).toList(); + List stringLines = Arrays.stream(lines).map(MINI_MESSAGE::serialize).toList(); data.setText(stringLines); } } @@ -51,7 +53,7 @@ public void setLines(Component... lines) { @Override public void addLine(Component line) { if (this.impl.getData() instanceof TextHologramData data) { - data.addLine(MiniMessageMessenger.MINI_MESSAGE.serialize(line)); + data.addLine(MINI_MESSAGE.serialize(line)); } } diff --git a/plugin/src/main/java/org/battleplugins/arena/command/ArenaCommandExecutor.java b/plugin/src/main/java/org/battleplugins/arena/command/ArenaCommandExecutor.java index a4e3ba65..1f700b08 100644 --- a/plugin/src/main/java/org/battleplugins/arena/command/ArenaCommandExecutor.java +++ b/plugin/src/main/java/org/battleplugins/arena/command/ArenaCommandExecutor.java @@ -281,7 +281,7 @@ public void list(CommandSender sender) { if (competition instanceof LiveCompetition liveCompetition) { competitionInfo = competitionInfo.append(Component.space()) - .append(Messages.PLAYERS.withContext(String.valueOf(liveCompetition.getPlayers().size()), String.valueOf(liveCompetition.getMaxPlayers())).toComponent()); + .append(Messages.PLAYERS.withContext(String.valueOf(liveCompetition.getPlayers().size()), liveCompetition.getMaxPlayers() == Integer.MAX_VALUE ? "∞" : String.valueOf(liveCompetition.getMaxPlayers())).toComponent()); } sender.sendMessage(competitionInfo); @@ -300,7 +300,7 @@ public void list(CommandSender sender) { if (competition instanceof LiveCompetition liveCompetition) { competitionInfo = competitionInfo.append(Component.space()) - .append(Messages.PLAYERS.withContext(String.valueOf(liveCompetition.getPlayers().size()), String.valueOf(liveCompetition.getMaxPlayers())).toComponent()); + .append(Messages.PLAYERS.withContext(String.valueOf(liveCompetition.getPlayers().size()), liveCompetition.getMaxPlayers() == Integer.MAX_VALUE ? "∞" : String.valueOf(liveCompetition.getMaxPlayers())).toComponent()); } sender.sendMessage(competitionInfo); diff --git a/plugin/src/main/java/org/battleplugins/arena/feature/hologram/HologramListener.java b/plugin/src/main/java/org/battleplugins/arena/feature/hologram/HologramListener.java index 31d05c7e..e37ee554 100644 --- a/plugin/src/main/java/org/battleplugins/arena/feature/hologram/HologramListener.java +++ b/plugin/src/main/java/org/battleplugins/arena/feature/hologram/HologramListener.java @@ -1,8 +1,6 @@ package org.battleplugins.arena.feature.hologram; import org.battleplugins.arena.competition.Competition; -import org.battleplugins.arena.competition.phase.CompetitionPhaseType; -import org.battleplugins.arena.event.arena.ArenaPhaseCompleteEvent; import org.battleplugins.arena.event.arena.ArenaRemoveCompetitionEvent; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -16,19 +14,6 @@ public HologramListener(HologramFeature feature) { this.feature = feature; } - @EventHandler - public void onPhaseComplete(ArenaPhaseCompleteEvent event) { - if (!CompetitionPhaseType.VICTORY.equals(event.getPhase().getType())) { - return; - } - - if (!this.feature.isEnabled()) { - return; - } - - this.clearHolograms(event.getCompetition()); - } - @EventHandler public void onRemoveCompetition(ArenaRemoveCompetitionEvent event) { if (!this.feature.isEnabled()) { diff --git a/settings.gradle.kts b/settings.gradle.kts index eade35f1..c90f5542 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -19,6 +19,7 @@ include("plugin") // Default modules include("module:arena-restoration") +include("module:auto-arena") include("module:boundary-enforcer") include("module:classes") include("module:duels")