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);
}
}
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;
}
Aggregations