Search in sources :

Example 1 with GameStartEvent

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;
}
Also used : EditModeGame(com.voxelgameslib.voxelgameslib.editmode.EditModeGame) FileWriter(java.io.FileWriter) IOException(java.io.IOException) GameModeNotAvailableException(com.voxelgameslib.voxelgameslib.exception.GameModeNotAvailableException) GameStartEvent(com.voxelgameslib.voxelgameslib.event.events.game.GameStartEvent) File(java.io.File) FileWriter(java.io.FileWriter) Writer(java.io.Writer) GameStartException(com.voxelgameslib.voxelgameslib.exception.GameStartException) GameModeNotAvailableException(com.voxelgameslib.voxelgameslib.exception.GameModeNotAvailableException) IOException(java.io.IOException) Nonnull(javax.annotation.Nonnull)

Aggregations

EditModeGame (com.voxelgameslib.voxelgameslib.editmode.EditModeGame)1 GameStartEvent (com.voxelgameslib.voxelgameslib.event.events.game.GameStartEvent)1 GameModeNotAvailableException (com.voxelgameslib.voxelgameslib.exception.GameModeNotAvailableException)1 GameStartException (com.voxelgameslib.voxelgameslib.exception.GameStartException)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 Writer (java.io.Writer)1 Nonnull (javax.annotation.Nonnull)1