use of com.voxelgameslib.voxelgameslib.event.events.game.GameStartEvent in project VoxelGamesLibv2 by VoxelGamesLib.
the class GameHandler method startGame.
/**
* Starts a new {@link Game} instance of that {@link GameMode}.
*
* @param mode the {@link GameMode} that should be started.
* @return the started {@link Game}
* @throws GameModeNotAvailableException if that {@link GameMode} is not registered on this server
* @throws GameStartException if something goes wrong while starting
*/
@Nonnull
public Game startGame(@Nonnull GameMode mode) {
if (!modes.contains(mode)) {
throw new GameModeNotAvailableException(mode);
}
Game game = injector.getInstance(mode.getGameClass());
game.setUuid(UUID.randomUUID());
games.add(game);
Optional<GameDefinition> def = getGameDefinition(mode);
if (def.isPresent()) {
game.initGameFromDefinition(def.get());
} else {
game.initGameFromModule();
GameDefinition definition = game.saveGameDefinition();
File file = new File(gameDefinitionFolder, mode.getName() + ".json");
if (!file.exists()) {
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
Writer writer = new FileWriter(file, false);
gson.toJson(definition, writer);
writer.close();
} catch (Exception ex) {
log.log(Level.WARNING, "Could not save game definition to file " + file.getAbsolutePath(), ex);
}
}
// registering calles enable
tickHandler.registerTickable(game);
Bukkit.getServer().getPluginManager().callEvent(new GameStartEvent(game));
return game;
}
Aggregations