Search in sources :

Example 1 with GameStartException

use of com.voxelgameslib.voxelgameslib.api.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.getWorldName();
    } else if (type == Type.VOTEWINNER) {
        mapName = gameData.voteWinner.getWorldName();
    } 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.api.exception.VoxelGameLibException) GameStartException(com.voxelgameslib.voxelgameslib.api.exception.GameStartException) DefaultGameData(com.voxelgameslib.voxelgameslib.api.game.DefaultGameData) VoxelGameLibException(com.voxelgameslib.voxelgameslib.api.exception.VoxelGameLibException) GameStartException(com.voxelgameslib.voxelgameslib.api.exception.GameStartException)

Example 2 with GameStartException

use of com.voxelgameslib.voxelgameslib.api.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.components.editmode.EditModeGame) FileWriter(java.io.FileWriter) IOException(java.io.IOException) GameModeNotAvailableException(com.voxelgameslib.voxelgameslib.api.exception.GameModeNotAvailableException) GameStartEvent(com.voxelgameslib.voxelgameslib.api.event.events.game.GameStartEvent) File(java.io.File) FileWriter(java.io.FileWriter) Writer(java.io.Writer) GameModeNotAvailableException(com.voxelgameslib.voxelgameslib.api.exception.GameModeNotAvailableException) GameStartException(com.voxelgameslib.voxelgameslib.api.exception.GameStartException) IOException(java.io.IOException) Nonnull(javax.annotation.Nonnull)

Aggregations

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