Skip to content

Commit

Permalink
add createArena in ArenaManager
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Jul 24, 2023
1 parent f577f5d commit b47a87c
Showing 1 changed file with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import me.hsgamer.minigamecore.base.Arena;
import me.hsgamer.minigamecore.base.FeatureUnit;

import java.lang.reflect.Constructor;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -92,4 +94,43 @@ public void clearAllArenas() {
});
arenaList.clear();
}

/**
* Create an arena
*
* @param name the name of the arena
* @param arenaClass the class of the arena
* @param onCreateConsumer the consumer that will be called when the arena is created
* @param <T> the type of the arena
* @return the arena or empty if it cannot be created
*/
public <T extends Arena> Optional<T> createArena(String name, Class<T> arenaClass, Consumer<T> onCreateConsumer) {
T arena = null;

try {
Constructor<T> constructor = arenaClass.getConstructor(String.class, FeatureUnit.class);
arena = constructor.newInstance(name, this);
} catch (Exception ignored) {
// IGNORED
}

if (arena == null) {
try {
Constructor<T> constructor = arenaClass.getConstructor(String.class);
arena = constructor.newInstance(name);
} catch (Exception ignored) {
// IGNORED
}
}

if (arena == null) return Optional.empty();

onCreateConsumer.accept(arena);

if (addArena(arena)) {
return Optional.of(arena);
} else {
return Optional.empty();
}
}
}

0 comments on commit b47a87c

Please sign in to comment.