Per-Competition Code
Now that you have an Arena class with functioning game logic, it's time to expand on that.
Creating Competition Classes
As mentioned in the Adding Game Logic page, you will need to create a custom Competition class. The following code below is how to create a custom Competition:
public class MyCompetition extends LiveCompetition<MyCompetition> {
public MyCompetition(MyArena arena, CompetitionType type, ArenaMap map) {
super(arena, type, map);
}
}
As seen above, you will need to extend the LiveCompetition class, which is created for competitions that are live on the server BattleArena is running on. On it's own, this will do nothing, so the next step is to create a custom ArenaMap which is responsible for creating the competition.
public class MyCompetitionMap extends LiveCompetitionMap {
public static final MapFactory FACTORY = MapyFactory.create(MyCompetitionMap.class, MyCompetitionMap::new);
public MyCompetitionMap() {
}
public MyCompetitionMap(String name, Arena arena, MapType type, String world, @Nullable Bounds bounds, @Nullable Spawns spawns) {
super(name, arena, type, world, bounds, spawns);
}
// Override this method in order to use your custom competition class
@Override
public LiveCompetition<?> createCompetition(Arena arena) {
if (!(arena instanceof MyArena myArena)) {
throw new IllegalArgumentException("Arena must be an instance of MyArena!");
}
return new MyCompetition(myArena, arena.getType(), this);
}
}
As mentioned in an earlier segment of this documentation, maps can exist without necessarily having a competition bound to them, meaning they are responsible for actually creating a live competition. In the map class above, it can be seen the createCompetition method is overridden to instead create an instance of our MyCompetition class.
Additionally, a MapFactory is specified at the top of the class - this is important for the next step of linking this to your MyArena so BattleArena knows which ArenaMap (and therefore, Competition) to create for your Arena. It is also very important that both constructors are specified as seen in the example above.
And finally, the last step is to override the getMapFactory method in MyArena, and specify your factory like so:
public class MyArena extends Arena {
@ArenaOption(name = "infection-time", description = "How long a player should be infected once hit.")
private Duration infectionTime = Duration.ofSeconds(5);
private final Set<UUID> infectedPlayers = new HashSet<>();
@Override
public MapFactory getMapFactory() {
return MyCompetitionMap.FACTORY;
}
...
}