Skip to content

Commit

Permalink
Add team joining and improve canJoin method
Browse files Browse the repository at this point in the history
  • Loading branch information
Redned235 committed Jul 7, 2024
1 parent 3def938 commit 141d785
Show file tree
Hide file tree
Showing 26 changed files with 533 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class ArenaRestoration implements ArenaModuleInitializer {
public static final EventActionType<RestoreArenaAction> RESTORE_ARENA_ACTION = EventActionType.create("restore-arena", RestoreArenaAction.class, RestoreArenaAction::new);

public static final Message NO_BOUNDS = Messages.error("arena-restoration-no-bounds", "You must first set the map bounds before executing this command!");
public static final Message SCHEMATIC_CREATED = Messages.success("arena-restoration-schematic-created", "Schematic created for map {}.");
public static final Message SCHEMATIC_CREATED = Messages.success("arena-restoration-schematic-created", "Schematic created for map <secondary>{}</secondary>.");
public static final Message FAILED_TO_CREATE_SCHEMATIC = Messages.error("arena-restoration-failed-to-create-schematic", "Failed to create schematic! Check the console for more information.");

@EventHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public ArenaRestorationExecutor(ArenaRestoration module, Arena arena) {
this.arena = arena;
}

@ArenaCommand(commands = "schematic", description = "Creates a schematic for the specified arena from your clipboard.", permissionNode = "region")
@ArenaCommand(commands = "schematic", description = "Creates a schematic for the specified arena from the map bounds.", permissionNode = "region")
public void region(Player player, Competition<?> competition) {
if (!(competition instanceof LiveCompetition<?> liveCompetition)) {
return; // Cannot restore a non-live competition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.battleplugins.arena.Arena;
import org.battleplugins.arena.ArenaPlayer;
import org.battleplugins.arena.command.ArenaCommand;
import org.battleplugins.arena.command.Argument;
import org.battleplugins.arena.command.SubCommandExecutor;
import org.battleplugins.arena.messages.Messages;
import org.battleplugins.arena.options.types.BooleanArenaOption;
Expand All @@ -21,7 +22,7 @@ public ClassesExecutor(Classes module, Arena arena) {
}

@ArenaCommand(commands = "equip", description = "Equip a class.", permissionNode = "equip")
public void equip(Player player, ArenaClass arenaClass) {
public void equip(Player player, @Argument(name = "class") ArenaClass arenaClass) {
// Should not get here, but just as an additional safeguard
// *just in case*
if (!this.arena.isModuleEnabled(Classes.ID)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void onJoin(ArenaJoinEvent event) {
player.getPlayer(),
event.getPlayer().getName(),
Integer.toString(competition.getPlayers().size()),
Integer.toString(competition.getArena().getTeams().getTeamAmount().getMax())
Integer.toString(competition.getMaxPlayers())
);
} else {
PLAYER_JOINED_NO_LIMIT.send(player.getPlayer(), event.getPlayer().getName());
Expand All @@ -66,7 +66,7 @@ public void onLeave(ArenaLeaveEvent event) {
player.getPlayer(),
event.getPlayer().getName(),
Integer.toString(competition.getPlayers().size()),
Integer.toString(competition.getArena().getTeams().getTeamAmount().getMax())
Integer.toString(competition.getMaxPlayers())
);
} else {
PLAYER_LEFT_NO_LIMIT.send(player.getPlayer(), event.getPlayer().getName());
Expand Down
10 changes: 10 additions & 0 deletions plugin/src/main/java/org/battleplugins/arena/ArenaPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.battleplugins.arena.competition.PlayerRole;
import org.battleplugins.arena.competition.PlayerStorage;
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.stat.ArenaStat;
import org.battleplugins.arena.stat.StatHolder;
import org.battleplugins.arena.team.ArenaTeam;
Expand Down Expand Up @@ -130,6 +132,14 @@ public ArenaTeam getTeam() {
* @param team the team that this player is on
*/
public void setTeam(@Nullable ArenaTeam team) {
if (this.team != null) {
new ArenaTeamLeaveEvent(this, this.team).callEvent();
}

if (team != null) {
new ArenaTeamJoinEvent(this, team).callEvent();
}

this.team = team;
}

Expand Down
9 changes: 9 additions & 0 deletions plugin/src/main/java/org/battleplugins/arena/BattleArena.java
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,15 @@ public Arena getArena(String name) {
return this.arenas.get(name);
}

/**
* Returns all the {@link Arena}s for the plugin.
*
* @return all the arenas for the plugin
*/
public List<Arena> getArenas() {
return List.copyOf(this.arenas.values());
}

/**
* Registers the given {@link Arena}.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.battleplugins.arena.command;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.battleplugins.arena.Arena;
import org.battleplugins.arena.ArenaPlayer;
import org.battleplugins.arena.competition.Competition;
Expand All @@ -17,6 +19,10 @@
import org.battleplugins.arena.editor.type.MapOption;
import org.battleplugins.arena.event.player.ArenaLeaveEvent;
import org.battleplugins.arena.messages.Messages;
import org.battleplugins.arena.options.ArenaOptionType;
import org.battleplugins.arena.options.TeamSelection;
import org.battleplugins.arena.options.types.BooleanArenaOption;
import org.battleplugins.arena.team.ArenaTeam;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -182,12 +188,12 @@ public void leave(Player player) {
Messages.ARENA_LEFT.send(player, arenaPlayer.getCompetition().getMap().getName());
}

@ArenaCommand(commands = "create", description = "Create a new arena.", permissionNode = "create")
@ArenaCommand(commands = "create", description = "Create a new map.", permissionNode = "create")
public void create(Player player) {
ArenaEditorWizards.MAP_CREATION.openWizard(player, this.arena);
}

@ArenaCommand(commands = { "remove", "delete" }, description = "Removes an arena.", permissionNode = "remove")
@ArenaCommand(commands = { "remove", "delete" }, description = "Removes a map.", permissionNode = "remove")
public void remove(Player player, CompetitionMap map) {
if (!(map instanceof LiveCompetitionMap liveMap)) {
Messages.NO_ARENA_WITH_NAME.send(player);
Expand All @@ -209,7 +215,7 @@ public void remove(Player player, CompetitionMap map) {
}

@ArenaCommand(commands = "edit", description = "Edit an arena map.", permissionNode = "edit")
public void map(Player player, CompetitionMap map, MapOption option) {
public void map(Player player, CompetitionMap map, @Argument(name = "map option") MapOption option) {
if (!(map instanceof LiveCompetitionMap liveMap)) {
Messages.NO_ARENA_WITH_NAME.send(player);
return;
Expand Down Expand Up @@ -239,6 +245,106 @@ public void advance(Player player) {
}
}

@ArenaCommand(commands = "team", subCommands = { "join", "j" }, description = "Joins a team.", permissionNode = "team.join")
public void joinTeam(Player player, ArenaTeam team) {
ArenaPlayer arenaPlayer = ArenaPlayer.getArenaPlayer(player);
if (arenaPlayer == null) {
Messages.NOT_IN_ARENA.send(player);
return;
}

if (this.arena.getTeams().isNonTeamGame()) {
Messages.CANNOT_JOIN_TEAM_SOLO.send(player);
return;
}

if (this.arena.getTeams().getTeamSelection() != TeamSelection.PICK) {
Messages.TEAM_SELECTION_NOT_AVAILABLE.send(player);
return;
}

if (!arenaPlayer.getCompetition().option(ArenaOptionType.TEAM_SELECTION)
.map(BooleanArenaOption::isEnabled)
.orElse(true)) {
Messages.TEAM_SELECTION_NOT_AVAILABLE.send(player);
return;
}

if (team.equals(arenaPlayer.getTeam())) {
Messages.ALREADY_ON_THIS_TEAM.send(player, team.getFormattedName());
return;
}

LiveCompetition<?> competition = arenaPlayer.getCompetition();
if (!competition.getTeamManager().canJoinTeam(team)) {
Messages.TEAM_FULL.send(player, team.getFormattedName());
return;
}

competition.getTeamManager().joinTeam(arenaPlayer, team);
Messages.TEAM_JOINED.send(player, team.getFormattedName());
}

@ArenaCommand(commands = "team", subCommands = { "leave", "l" }, description = "Leaves your current team.", permissionNode = "team.leave")
public void leaveTeam(Player player) {
ArenaPlayer arenaPlayer = ArenaPlayer.getArenaPlayer(player);
if (arenaPlayer == null) {
Messages.NOT_IN_ARENA.send(player);
return;
}

if (this.arena.getTeams().getTeamSelection() != TeamSelection.PICK) {
Messages.TEAM_SELECTION_NOT_AVAILABLE.send(player);
return;
}

if (!arenaPlayer.getCompetition().option(ArenaOptionType.TEAM_SELECTION)
.map(BooleanArenaOption::isEnabled)
.orElse(true)) {
Messages.TEAM_SELECTION_NOT_AVAILABLE.send(player);
return;
}

ArenaTeam team = arenaPlayer.getTeam();
if (team == null) {
Messages.NOT_ON_TEAM.send(player);
return;
}

LiveCompetition<?> competition = arenaPlayer.getCompetition();
competition.getTeamManager().leaveTeam(arenaPlayer);

Messages.TEAM_LEFT.send(player, team.getFormattedName());
}

@ArenaCommand(commands = "team", subCommands = "list", description = "List all available teams.", permissionNode = "team.list")
public void listTeams(Player player) {
ArenaPlayer arenaPlayer = ArenaPlayer.getArenaPlayer(player);
if (arenaPlayer == null) {
Messages.NOT_IN_ARENA.send(player);
return;
}

LiveCompetition<?> competition = arenaPlayer.getCompetition();
Set<ArenaTeam> availableTeams = competition.getTeamManager().getTeams();
if (availableTeams.isEmpty() || this.arena.getTeams().isNonTeamGame()) {
Messages.NO_TEAMS.send(player);
return;
}

Messages.HEADER.sendCentered(player, "Teams");
for (ArenaTeam team : availableTeams) {
int players = competition.getTeamManager().getNumberOfPlayersOnTeam(team);
int max = competition.getTeamManager().getMaximumTeamSize(team);
if (max == 0) {
continue; // Don't display empty teams
}

String playersText = max == Integer.MAX_VALUE ? " (" + players + ")" : " (" + players + "/" + max + ")";
player.sendMessage(Component.text("- ", NamedTextColor.GRAY).append(team.getFormattedName()).append(Component.text(playersText, Messages.PRIMARY_COLOR)));
}
}

@Override
protected Object onVerifyArgument(CommandSender sender, String arg, Class<?> parameter) {
switch (parameter.getSimpleName().toLowerCase()) {
Expand All @@ -253,6 +359,12 @@ protected Object onVerifyArgument(CommandSender sender, String arg, Class<?> par
case "competitionmap" -> {
return this.arena.getPlugin().getMap(this.arena, arg);
}
case "arenateam" -> {
return this.arena.getTeams().getAvailableTeams().stream()
.filter(team -> team.getName().equalsIgnoreCase(arg))
.findFirst()
.orElse(null);
}
}

return super.onVerifyArgument(sender, arg, parameter);
Expand All @@ -265,6 +377,10 @@ protected boolean onInvalidArgument(CommandSender sender, Class<?> parameter, St
Messages.NO_ARENA_WITH_NAME.send(sender);
return true;
}
case "arenateam" -> {
Messages.NO_TEAM_WITH_NAME.send(sender, input);
return true;
}
}

return super.onInvalidArgument(sender, parameter, input);
Expand All @@ -283,6 +399,10 @@ protected List<String> onVerifyTabComplete(String arg, Class<?> parameter) {
.map(CompetitionMap::getName)
.distinct()
.toList();
} else if (parameter.getSimpleName().equalsIgnoreCase("arenateam")) {
return this.arena.getTeams().getAvailableTeams().stream()
.map(ArenaTeam::getName)
.toList();
}

return super.onVerifyTabComplete(arg, parameter);
Expand All @@ -291,9 +411,9 @@ protected List<String> onVerifyTabComplete(String arg, Class<?> parameter) {
@Override
protected String onGetUsageString(Class<?> parameter) {
return switch (parameter.getSimpleName().toLowerCase()) {
case "arena" -> "<arena> ";
case "competition" -> "<map> "; // Best name for player-facing values
case "competitionmap" -> "<map> ";
case "arenateam" -> "<team> ";
default -> super.onGetUsageString(parameter);
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration;
import net.kyori.adventure.text.event.ClickEvent;
import org.apache.commons.lang3.StringUtils;
import org.battleplugins.arena.Arena;
import org.battleplugins.arena.BattleArena;
import org.battleplugins.arena.competition.CompetitionType;
Expand Down Expand Up @@ -49,7 +48,7 @@ public void backups(Player player, @Argument(name = "player") String playerName)
Messages.HEADER.sendCentered(player, Messages.INVENTORY_BACKUPS);

List<OptionSelector.Option> options = backups.stream().map(backup -> new OptionSelector.Option(
Messages.BACKUP_NUMBER.withContext(Integer.toString(backups.indexOf(backup) + 1)),
Messages.BACKUP_INFO.withContext(backup.getFormattedDate()),
"/ba restore " + target.getName() + " " + (backups.indexOf(backup) + 1)
)).toList();
OptionSelector.sendOptions(player, options, ClickEvent.Action.SUGGEST_COMMAND);
Expand All @@ -73,7 +72,6 @@ public void restore(Player player, Player target, int backupIndex) {
return;
}


backup.restore(target);
Messages.BACKUP_RESTORED.send(player, target.getName());
}
Expand Down Expand Up @@ -147,7 +145,7 @@ public void stopAll(Player player) {
}
}

@ArenaCommand(commands = "schedule", description = "Schedules an event to start automatically.", permissionNode = "schedule")
@ArenaCommand(commands = "schedule", description = "Schedules an event to start at the specified time.", permissionNode = "schedule")
public void schedule(Player player, Arena arena, Duration interval) {
if (arena.getType() != CompetitionType.EVENT) {
Messages.NOT_EVENT.send(player);
Expand Down
Loading

0 comments on commit 141d785

Please sign in to comment.