Skip to content

Commit

Permalink
Add auto arena module and fix a couple bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Redned235 committed Dec 27, 2024
1 parent 1b56225 commit 5f6cb03
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 20 deletions.
Empty file.
Original file line number Diff line number Diff line change
@@ -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<Competition<?>> 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;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -43,15 +45,15 @@ public List<Component> getLines() {
@Override
public void setLines(Component... lines) {
if (this.impl.getData() instanceof TextHologramData data) {
List<String> stringLines = Arrays.stream(lines).map(MiniMessageMessenger.MINI_MESSAGE::serialize).toList();
List<String> stringLines = Arrays.stream(lines).map(MINI_MESSAGE::serialize).toList();
data.setText(stringLines);
}
}

@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));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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()) {
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 5f6cb03

Please sign in to comment.