Skip to content

Commit

Permalink
Improve map API
Browse files Browse the repository at this point in the history
  • Loading branch information
Redned235 committed Jun 30, 2024
1 parent 7b9f5b7 commit cdab9c8
Show file tree
Hide file tree
Showing 17 changed files with 127 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ private void advance(List<Contestant> contestants) {
// Check to see if any of the open competitions are dynamic and allocate as needed
int requiredCompetitions = mapsNeeded - openCompetitions.size();

List<LiveCompetitionMap<?>> dynamicMaps = this.arena.getPlugin().getMaps(this.arena)
List<LiveCompetitionMap> dynamicMaps = this.arena.getPlugin().getMaps(this.arena)
.stream()
.filter(map -> map.getType() == MapType.DYNAMIC)
.toList();
Expand All @@ -265,7 +265,7 @@ private void advance(List<Contestant> contestants) {

for (int i = 0; i < requiredCompetitions; i++) {
// Now just walk through the dynamic maps and allocate them
LiveCompetitionMap<?> map = dynamicMaps.get(i % dynamicMaps.size());
LiveCompetitionMap map = dynamicMaps.get(i % dynamicMaps.size());

Competition<?> competition = map.createDynamicCompetition(this.arena);
this.arena.getPlugin().addCompetition(this.arena, competition);
Expand Down
2 changes: 0 additions & 2 deletions plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import xyz.jpenilla.runpaper.task.RunServer

plugins {
id("maven-publish")
id("xyz.jpenilla.run-paper") version "2.3.0"
Expand Down
13 changes: 9 additions & 4 deletions plugin/src/main/java/org/battleplugins/arena/Arena.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.battleplugins.arena.competition.Competition;
import org.battleplugins.arena.competition.CompetitionType;
import org.battleplugins.arena.competition.map.LiveCompetitionMap;
import org.battleplugins.arena.competition.map.MapFactory;
import org.battleplugins.arena.competition.phase.CompetitionPhase;
import org.battleplugins.arena.competition.phase.CompetitionPhaseType;
import org.battleplugins.arena.competition.victory.VictoryConditionType;
Expand Down Expand Up @@ -116,12 +117,16 @@ public ArenaCommandExecutor createCommandExecutor() {
}

/**
* Returns the {@link LiveCompetitionMap} type for this arena.
* Returns the {@link MapFactory} for this arena.
* <p>
* This factory controls the creation of maps for this
* arena, and can be overridden to provide custom map
* creation logic.
*
* @return the {@link LiveCompetitionMap} type for this arena
* @return the map factory for this arena
*/
public Class<? extends LiveCompetitionMap> getCompetitionMapType() {
return LiveCompetitionMap.class;
public MapFactory getMapFactory() {
return LiveCompetitionMap.getFactory();
}

/**
Expand Down
22 changes: 11 additions & 11 deletions plugin/src/main/java/org/battleplugins/arena/BattleArena.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class BattleArena extends JavaPlugin implements Listener, LoggerHolder {
final Map<String, Class<? extends Arena>> arenaTypes = new HashMap<>();
final Map<String, Arena> arenas = new HashMap<>();

private final Map<Arena, List<LiveCompetitionMap<?>>> arenaMaps = new HashMap<>();
private final Map<Arena, List<LiveCompetitionMap>> arenaMaps = new HashMap<>();
private final Map<String, ArenaLoader> arenaLoaders = new HashMap<>();

private final CompetitionManager competitionManager = new CompetitionManager(this);
Expand Down Expand Up @@ -257,12 +257,12 @@ private void postInitialize() {
this.loadArenaMaps();

// Initialize matches
for (Map.Entry<Arena, List<LiveCompetitionMap<?>>> entry : this.arenaMaps.entrySet()) {
for (Map.Entry<Arena, List<LiveCompetitionMap>> entry : this.arenaMaps.entrySet()) {
if (entry.getKey().getType() != CompetitionType.MATCH) {
continue;
}

for (LiveCompetitionMap<?> map : entry.getValue()) {
for (LiveCompetitionMap map : entry.getValue()) {
if (map.getType() == MapType.STATIC) {
Competition<?> competition = map.createCompetition(entry.getKey());
this.addCompetition(entry.getKey(), competition);
Expand Down Expand Up @@ -368,8 +368,8 @@ public <T extends Arena> void registerArena(String name, Class<T> arenaClass, Su
* @param arena the arena to get the maps for
* @return all the available maps for the given arena
*/
public List<LiveCompetitionMap<?>> getMaps(Arena arena) {
List<LiveCompetitionMap<?>> maps = this.arenaMaps.get(arena);
public List<LiveCompetitionMap> getMaps(Arena arena) {
List<LiveCompetitionMap> maps = this.arenaMaps.get(arena);
if (maps == null) {
return List.of();
}
Expand All @@ -384,7 +384,7 @@ public List<LiveCompetitionMap<?>> getMaps(Arena arena) {
* @param name the name of the map
* @return the map from the given arena and name
*/
public Optional<LiveCompetitionMap<?>> map(Arena arena, String name) {
public Optional<LiveCompetitionMap> map(Arena arena, String name) {
return Optional.ofNullable(this.getMap(arena, name));
}

Expand All @@ -396,8 +396,8 @@ public Optional<LiveCompetitionMap<?>> map(Arena arena, String name) {
* @return the map from the given arena and name, or null if not found
*/
@Nullable
public LiveCompetitionMap<?> getMap(Arena arena, String name) {
List<LiveCompetitionMap<?>> maps = this.arenaMaps.get(arena);
public LiveCompetitionMap getMap(Arena arena, String name) {
List<LiveCompetitionMap> maps = this.arenaMaps.get(arena);
if (maps == null) {
return null;
}
Expand All @@ -414,7 +414,7 @@ public LiveCompetitionMap<?> getMap(Arena arena, String name) {
* @param arena the arena to add the map to
* @param map the map to add
*/
public void addArenaMap(Arena arena, LiveCompetitionMap<?> map) {
public void addArenaMap(Arena arena, LiveCompetitionMap map) {
this.arenaMaps.computeIfAbsent(arena, k -> new ArrayList<>()).add(map);
}

Expand All @@ -424,7 +424,7 @@ public void addArenaMap(Arena arena, LiveCompetitionMap<?> map) {
* @param arena the arena to remove the map from
* @param map the map to remove
*/
public void removeArenaMap(Arena arena, LiveCompetitionMap<?> map) {
public void removeArenaMap(Arena arena, LiveCompetitionMap map) {
this.arenaMaps.computeIfAbsent(arena, k -> new ArrayList<>()).remove(map);

// If the map is removed, also remove the competition if applicable
Expand Down Expand Up @@ -679,7 +679,7 @@ private void loadArenaMaps() {

try {
Configuration configuration = YamlConfiguration.loadConfiguration(Files.newBufferedReader(mapPath));
LiveCompetitionMap<?> map = ArenaConfigParser.newInstance(mapPath, arena.getCompetitionMapType(), configuration, this);
LiveCompetitionMap map = ArenaConfigParser.newInstance(mapPath, arena.getMapFactory().getMapClass(), configuration, this);
if (map.getBounds() == null && map.getType() == MapType.DYNAMIC) {
// Cannot create dynamic map without bounds
this.warn("Map {} for arena {} is dynamic but does not have bounds!", map.getName(), arena.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public ArenaCommandExecutor(String parentCommand, Arena arena) {

@ArenaCommand(commands = { "join", "j" }, description = "Join an arena.", permissionNode = "join")
public void join(Player player) {
List<LiveCompetitionMap<?>> maps = this.arena.getPlugin().getMaps(this.arena);
List<LiveCompetitionMap> maps = this.arena.getPlugin().getMaps(this.arena);
if (maps.isEmpty()) {
Messages.NO_OPEN_ARENAS.send(player);
return;
Expand All @@ -50,7 +50,7 @@ public void join(Player player) {
}

@ArenaCommand(commands = { "join", "j" }, description = "Join an arena.", permissionNode = "join")
public void join(Player player, @Argument(name = "map") CompetitionMap<?> map) {
public void join(Player player, @Argument(name = "map") CompetitionMap map) {
if (ArenaPlayer.getArenaPlayer(player) != null) {
Messages.ALREADY_IN_ARENA.send(player);
return;
Expand Down Expand Up @@ -188,8 +188,8 @@ public void create(Player player) {
}

@ArenaCommand(commands = { "remove", "delete" }, description = "Removes an arena.", permissionNode = "remove")
public void remove(Player player, CompetitionMap<?> map) {
if (!(map instanceof LiveCompetitionMap<?> liveMap)) {
public void remove(Player player, CompetitionMap map) {
if (!(map instanceof LiveCompetitionMap liveMap)) {
Messages.NO_ARENA_WITH_NAME.send(player);
return;
}
Expand All @@ -209,8 +209,8 @@ 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) {
if (!(map instanceof LiveCompetitionMap<?> liveMap)) {
public void map(Player player, CompetitionMap map, MapOption option) {
if (!(map instanceof LiveCompetitionMap liveMap)) {
Messages.NO_ARENA_WITH_NAME.send(player);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public interface Competition<T extends Competition<T>> extends CompetitionLike<T
*
* @return the map for this competition
*/
CompetitionMap<T> getMap();
CompetitionMap getMap();

/**
* Gets the current phase of the competition.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public CompletableFuture<CompetitionResult> getOrCreateCompetition(Arena arena,
return invalidResult;
}

List<LiveCompetitionMap<?>> maps = this.plugin.getMaps(arena);
List<LiveCompetitionMap> maps = this.plugin.getMaps(arena);
if (maps == null) {
// No maps, return
return invalidResult;
Expand Down Expand Up @@ -94,7 +94,7 @@ public CompletableFuture<CompetitionResult> getOrCreateCompetition(Arena arena,
Collections.shuffle(maps);
}

for (LiveCompetitionMap<?> map : maps) {
for (LiveCompetitionMap map : maps) {
if (map.getType() != MapType.DYNAMIC) {
continue;
}
Expand Down Expand Up @@ -185,7 +185,7 @@ public void removeCompetition(Arena arena, Competition<?> competition) {
}

competitions.remove(competition);
if (competition.getMap().getType() == MapType.DYNAMIC && competition.getMap() instanceof LiveCompetitionMap<?> map) {
if (competition.getMap().getType() == MapType.DYNAMIC && competition.getMap() instanceof LiveCompetitionMap map) {
this.clearDynamicMap(map);
}
}
Expand All @@ -198,7 +198,7 @@ public void completeAllActiveCompetitions() {
}
}

private void clearDynamicMap(LiveCompetitionMap<?> map) {
private void clearDynamicMap(LiveCompetitionMap map) {
if (map.getType() != MapType.DYNAMIC) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Class<T> getCompetitionType() {
return this.clazz;
}

public T create(Arena arena, LiveCompetitionMap<T> map) {
public T create(Arena arena, LiveCompetitionMap map) {
return this.factory.create(arena, map);
}

Expand Down Expand Up @@ -65,6 +65,6 @@ public int hashCode() {

public interface CompetitionFactory<T extends Competition<T>> {

T create(Arena arena, LiveCompetitionMap<T> map);
T create(Arena arena, LiveCompetitionMap map);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
*/
public abstract class LiveCompetition<T extends Competition<T>> implements ArenaLike, Competition<T> {
private final Arena arena;
private final LiveCompetitionMap<T> map;
private final LiveCompetitionMap map;

private final Map<Player, ArenaPlayer> players = new HashMap<>();
private final Map<PlayerRole, Set<ArenaPlayer>> playersByRole = new HashMap<>();
Expand All @@ -51,7 +51,7 @@ public abstract class LiveCompetition<T extends Competition<T>> implements Arena
private final OptionsListener<T> optionsListener;
private final StatListener<T> statListener;

public LiveCompetition(Arena arena, LiveCompetitionMap<T> map) {
public LiveCompetition(Arena arena, LiveCompetitionMap map) {
this.arena = arena;
this.map = map;

Expand Down Expand Up @@ -144,7 +144,7 @@ public final Arena getArena() {
}

@Override
public final LiveCompetitionMap<T> getMap() {
public final LiveCompetitionMap getMap() {
return this.map;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ public void startEvent(Arena arena, EventOptions options) {
}

// Create the competition
List<LiveCompetitionMap<?>> maps = arena.getPlugin().getMaps(arena);
List<LiveCompetitionMap> maps = arena.getPlugin().getMaps(arena);
if (maps.isEmpty()) {
arena.getPlugin().warn("No maps found for arena {}, failed to start event!", arena.getName());
return;
}

// Get a random map
LiveCompetitionMap<?> map = maps.get(ThreadLocalRandom.current().nextInt(maps.size()));
LiveCompetitionMap map = maps.get(ThreadLocalRandom.current().nextInt(maps.size()));
Competition<?> competition = map.getType() == MapType.DYNAMIC ? map.createDynamicCompetition(arena) : map.createCompetition(arena);

// Create the competition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
public class LiveEvent extends LiveCompetition<Event> implements Event {

public LiveEvent(Arena arena, LiveCompetitionMap<Event> map) {
public LiveEvent(Arena arena, LiveCompetitionMap map) {
super(arena, map);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package org.battleplugins.arena.competition.map;

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

/**
* A map for a competition.
*/
public interface CompetitionMap<T extends Competition<T>> {
public interface CompetitionMap {

/**
* Gets the name of the map.
Expand All @@ -15,13 +12,6 @@ public interface CompetitionMap<T extends Competition<T>> {
*/
String getName();

/**
* Gets the competition this map is for.
*
* @return the competition this map is for
*/
CompetitionType<T> getCompetitionType();

/**
* Gets the type of map.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.battleplugins.arena.Arena;
import org.battleplugins.arena.ArenaLike;
import org.battleplugins.arena.competition.Competition;
import org.battleplugins.arena.competition.CompetitionType;
import org.battleplugins.arena.competition.map.options.Bounds;
import org.battleplugins.arena.competition.map.options.Spawns;
import org.battleplugins.arena.config.ArenaOption;
Expand All @@ -22,10 +21,10 @@

/**
* Represents a map for a competition which is live on this server.
*
* @param <T> the type of competition this map is for
*/
public class LiveCompetitionMap<T extends Competition<T>> implements ArenaLike, CompetitionMap<T>, PostProcessable {
public class LiveCompetitionMap implements ArenaLike, CompetitionMap, PostProcessable {
private static final MapFactory FACTORY = MapFactory.create(LiveCompetitionMap.class, LiveCompetitionMap::new);

@ArenaOption(name = "name", description = "The name of the map.", required = true)
private String name;

Expand Down Expand Up @@ -90,11 +89,6 @@ public final Arena getArena() {
return this.arena;
}

@Override
public final CompetitionType<T> getCompetitionType() {
return (CompetitionType<T>) this.arena.getType();
}

/**
* Gets the {@link MapType} of this map.
*
Expand Down Expand Up @@ -186,8 +180,8 @@ public final void setSpawns(Spawns spawns) {
* @param arena the arena to create the competition for
* @return the created competition
*/
public T createCompetition(Arena arena) {
return this.getCompetitionType().create(arena, this);
public Competition<?> createCompetition(Arena arena) {
return arena.getType().create(arena, this);
}

/**
Expand All @@ -200,7 +194,7 @@ public T createCompetition(Arena arena) {
* @return the created dynamic competition
*/
@Nullable
public final T createDynamicCompetition(Arena arena) {
public final Competition<?> createDynamicCompetition(Arena arena) {
if (this.type != MapType.DYNAMIC) {
throw new IllegalStateException("Cannot create dynamic competition for non-dynamic map!");
}
Expand All @@ -222,9 +216,18 @@ public final T createDynamicCompetition(Arena arena) {
return null; // Failed to copy
}

LiveCompetitionMap<T> copy = new LiveCompetitionMap<>(this.name, arena, this.type, worldName, this.bounds, this.spawns);
LiveCompetitionMap copy = arena.getMapFactory().create(this.name, arena, this.type, worldName, this.bounds, this.spawns);
copy.mapWorld = world;

return copy.createCompetition(arena);
}

/**
* Gets the default factory for creating {@link LiveCompetitionMap live maps}.
*
* @return the factory for creating maps
*/
public static MapFactory getFactory() {
return FACTORY;
}
}
Loading

0 comments on commit cdab9c8

Please sign in to comment.