Search in sources :

Example 1 with GameStartException

use of com.voxelgameslib.voxelgameslib.exception.GameStartException 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)

Example 2 with GameStartException

use of com.voxelgameslib.voxelgameslib.exception.GameStartException in project VoxelGamesLibv2 by VoxelGamesLib.

the class MapFeature method enable.

@Override
public void enable() {
    // we already set the map externally, no need to do anything of the following, just set the world
    if (map != null) {
        world = Bukkit.getWorld(map.getLoadedName(getPhase().getGame().getUuid()));
        return;
    }
    DefaultGameData gameData = getPhase().getGame().getGameData(DefaultGameData.class).orElse(new DefaultGameData());
    if ((type == Type.LOBBY && gameData.lobbyMap == null) || (type == Type.VOTEWINNER && gameData.voteWinner == null)) {
        throw new GameStartException(getPhase().getGame().getGameMode(), "No map data was stored!");
    }
    String mapName;
    if (type == Type.LOBBY) {
        mapName = gameData.lobbyMap.getName();
    } else if (type == Type.VOTEWINNER) {
        mapName = gameData.voteWinner.getName();
    } else {
        throw new VoxelGameLibException("Unknown maptype");
    }
    try {
        map = worldHandler.loadMap(mapName);
        if (!map.isLoaded(getPhase().getGame().getUuid())) {
            world = worldHandler.loadWorld(map, getPhase().getGame().getUuid(), true);
        } else {
            world = Bukkit.getWorld(map.getLoadedName(getPhase().getGame().getUuid()));
        }
    } catch (Exception ex) {
        throw new GameStartException(getPhase().getGame().getGameMode(), ex);
    }
}
Also used : VoxelGameLibException(com.voxelgameslib.voxelgameslib.exception.VoxelGameLibException) GameStartException(com.voxelgameslib.voxelgameslib.exception.GameStartException) DefaultGameData(com.voxelgameslib.voxelgameslib.game.DefaultGameData) VoxelGameLibException(com.voxelgameslib.voxelgameslib.exception.VoxelGameLibException) GameStartException(com.voxelgameslib.voxelgameslib.exception.GameStartException)

Aggregations

GameStartException (com.voxelgameslib.voxelgameslib.exception.GameStartException)2 EditModeGame (com.voxelgameslib.voxelgameslib.editmode.EditModeGame)1 GameStartEvent (com.voxelgameslib.voxelgameslib.event.events.game.GameStartEvent)1 GameModeNotAvailableException (com.voxelgameslib.voxelgameslib.exception.GameModeNotAvailableException)1 VoxelGameLibException (com.voxelgameslib.voxelgameslib.exception.VoxelGameLibException)1 DefaultGameData (com.voxelgameslib.voxelgameslib.game.DefaultGameData)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