Skip to content

Commit

Permalink
More docs and CompetitionListener API
Browse files Browse the repository at this point in the history
  • Loading branch information
Redned235 committed Jun 30, 2024
1 parent 3a631cb commit 7b9f5b7
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 12 deletions.
126 changes: 117 additions & 9 deletions plugin/src/main/java/org/battleplugins/arena/Arena.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,44 @@ public Arena() {

// API methods

/**
* Creates a new command executor for this arena.
*
* @return a new command executor for this arena
*/
public ArenaCommandExecutor createCommandExecutor() {
return new ArenaCommandExecutor(this);
}

/**
* Returns the {@link LiveCompetitionMap} type for this arena.
*
* @return the {@link LiveCompetitionMap} type for this arena
*/
public Class<? extends LiveCompetitionMap> getCompetitionMapType() {
return LiveCompetitionMap.class;
}

/**
* Returns whether the given module is enabled for this arena.
*
* @param module the module to check
* @return whether the given module is enabled for this arena
*/
public boolean isModuleEnabled(String module) {
return this.modules != null && this.modules.contains(module);
}

// Internal methods (cannot be overridden by extending plugins)

/**
* Creates a new phase for the given competition.
*
* @param type the phase type
* @param competition the competition
* @return the new phase
* @param <C> the type of competition
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public final <C extends Competition<C>> CompetitionPhase<C> createPhase(CompetitionPhaseType<?, ?> type, C competition) {
CompetitionPhaseType.Provider<?, ?> provider = this.phases.get(type);
Expand All @@ -130,63 +154,151 @@ public final <C extends Competition<C>> CompetitionPhase<C> createPhase(Competit
return ((CompetitionPhaseType.Provider) provider).create(competition);
}

/**
* Gets the {@link CompetitionPhaseType phases} for this arena.
*
* @return the phases for this arena
*/
public final Set<CompetitionPhaseType<?, ?>> getPhases() {
return Set.copyOf(this.phases.keySet());
}

public final Path getMapsPath() {
/**
* Gets the path to the map for this arena.
*
* @return the path to the map for this arena
*/
public final Path getMapPath() {
return this.plugin.getMapsPath().resolve(this.name.toLowerCase(Locale.ROOT));
}

/**
* Gets the plugin associated with this arena.
*
* @return the plugin associated with this arena
*/
public final BattleArena getPlugin() {
return this.plugin;
}

/**
* Gets the name of this arena.
*
* @return the name of this arena
*/
public final String getName() {
return this.name;
}

/**
* Gets the command aliases for this arena.
*
* @return the aliases for this arena
*/
public List<String> getAliases() {
return this.aliases == null ? List.of() : List.copyOf(this.aliases);
}

/**
* Gets the {@link CompetitionType} for this arena.
*
* @return the competition type for this arena
*/
public final CompetitionType<?> getType() {
return this.type;
}

/**
* Gets the {@link Teams} for this arena.
*
* @return the teams for this arena
*/
public final Teams getTeams() {
return this.teams;
}

/**
* Gets the {@link Lives} for this arena.
*
* @return the lives for this arena
*/
public final Optional<Lives> lives() {
return Optional.ofNullable(this.lives);
}

/**
* Gets the {@link Lives} for this arena.
*
* @return the lives for this arena, or null if lives are not enabled
*/
@Nullable
public final Lives getLives() {
return this.lives;
}

public final Optional<Lives> lives() {
return Optional.ofNullable(this.lives);
}

/**
* Returns whether lives are enabled for this arena.
*
* @return whether lives are enabled for this arena
*/
public final boolean isLivesEnabled() {
return this.lives != null && this.lives.isEnabled();
}

/**
* Gets the initial {@link CompetitionPhaseType phase} for this arena.
*
* @return the initial phase for this arena
*/
public final CompetitionPhaseType<?, ?> getInitialPhase() {
return this.initialPhase;
}

/**
* Gets the {@link EventAction actions} for this arena.
*
* @return the event actions for this arena
*/
public final Map<ArenaEventType<?>, List<EventAction>> getEventActions() {
return this.eventActions;
}

/**
* Gets the {@link VictoryConditionType victory conditions} for this arena.
*
* @return the victory conditions for this arena
*/
public final Map<VictoryConditionType<?, ?>, VictoryConditionType.Provider<?, ?>> getVictoryConditions() {
return this.victoryConditions;
}

/**
* Gets the {@link ArenaEventManager} for this arena.
*
* @return the event manager for this arena
*/
public final ArenaEventManager getEventManager() {
return this.eventManager;
}

/**
* Gets the {@link org.battleplugins.arena.options.ArenaOption} of the specified type.
*
* @param type the type of option
* @param <E> the type of option
* @return the option of the specified type
*/
public <E extends org.battleplugins.arena.options.ArenaOption> Optional<E> option(ArenaOptionType<E> type) {
return Optional.ofNullable(this.getOption(type));
}

/**
* Gets the {@link org.battleplugins.arena.options.ArenaOption} of the specified type.
*
* @param type the type of option
* @param <E> the type of option
* @return the option of the specified type, or null if the option does not exist
*/
@Nullable
public final <E extends org.battleplugins.arena.options.ArenaOption> E getOption(ArenaOptionType<E> type) {
if (this.options == null) {
Expand All @@ -196,10 +308,6 @@ public final <E extends org.battleplugins.arena.options.ArenaOption> E getOption
return (E) this.options.get(type);
}

public <E extends org.battleplugins.arena.options.ArenaOption> Optional<E> option(ArenaOptionType<E> type) {
return Optional.ofNullable(this.getOption(type));
}

@Override
public final Arena getArena() {
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ public void removeArenaMap(Arena arena, LiveCompetitionMap<?> map) {
}

// Now remove the map from the file system
Path mapPath = arena.getMapsPath().resolve(map.getName().toLowerCase(Locale.ROOT) + ".yml");
Path mapPath = arena.getMapPath().resolve(map.getName().toLowerCase(Locale.ROOT) + ".yml");
try {
Files.deleteIfExists(mapPath);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public final class ArenaEditorWizards {

ctx.saveTo(map);

Path mapPath = ctx.getArena().getMapsPath().resolve(map.getName().toLowerCase(Locale.ROOT) + ".yml");
Path mapPath = ctx.getArena().getMapPath().resolve(map.getName().toLowerCase(Locale.ROOT) + ".yml");

try {
FileConfiguration configuration = YamlConfiguration.loadConfiguration(Files.newBufferedReader(mapPath));
Expand Down Expand Up @@ -137,7 +137,7 @@ public final class ArenaEditorWizards {
BattleArena.getInstance().addCompetition(ctx.getArena(), competition);
}

Path mapsPath = ctx.getArena().getMapsPath();
Path mapsPath = ctx.getArena().getMapPath();
if (Files.notExists(mapsPath)) {
try {
Files.createDirectories(mapsPath);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.battleplugins.arena.event;

import org.battleplugins.arena.competition.Competition;
import org.battleplugins.arena.competition.CompetitionLike;

/**
* A listener for competitions.
* <p>
* This functionality extends upon {@link ArenaListener} and
* further isolates any event actions to the given {@link T competition}.
*
* @param <T> the type of competition
*/
public class CompetitionListener<T extends Competition<T>> implements ArenaListener, CompetitionLike<T> {
private final T competition;

public CompetitionListener(T competition) {
this.competition = competition;
}

@Override
public T getCompetition() {
return competition;
}
}

0 comments on commit 7b9f5b7

Please sign in to comment.