Search in sources :

Example 1 with WorldException

use of com.voxelgameslib.voxelgameslib.api.exception.WorldException in project VoxelGamesLibv2 by VoxelGamesLib.

the class WorldHandler method loadWorld.

/**
 * Loads a world. Needs to copy the file from the repo, unzip it and let the implementation load it <br><b>Always
 * needs to call super! Super needs to be called first (because it copies the world)</b>
 *
 * @param map    the map that should be loaded
 * @param gameid the id of the game this map belongs to
 * @return the loaded world
 * @throws WorldException something goes wrong
 */
@Nonnull
public World loadWorld(@Nonnull Map map, @Nonnull UUID gameid, boolean replaceMarkers) {
    map.load(gameid, "TEMP_" + map.getWorldName() + "_" + gameid.toString().split("-")[0]);
    log.finer("Loading map " + map.getInfo().getDisplayName() + " as " + map.getLoadedName(gameid));
    File file = new File(worldContainer, map.getLoadedName(gameid));
    try {
        ZipFile zip = new ZipFile(new File(worldsFolder, map.getWorldName() + ".zip"));
        zip.extractAll(file.getAbsolutePath());
        FileUtils.delete(new File(file, "uid.dat"));
    } catch (ZipException e) {
        throw new WorldException("Could not unzip world " + map.getInfo().getDisplayName() + " (" + map.getWorldName() + ".zip).", e);
    }
    World world = loadLocalWorld(map.getLoadedName(gameid));
    // load chunks based on markers
    int i = 0;
    int a = 0;
    List<CompletableFuture<Chunk>> futures = new ArrayList<>();
    for (Marker m : map.getMarkers()) {
        Location l = m.getLoc().toLocation(world.getName());
        if (!l.isChunkLoaded()) {
            futures.add(l.getWorld().getChunkAtAsync(l));
            i++;
        } else {
            a++;
        }
    }
    if (replaceMarkers) {
        CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).thenRun(() -> {
            replaceMarkers(world, map);
        });
    }
    log.finer("Loaded " + i + " chunks and ignored " + a + " already loaded");
    return world;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ZipFile(net.lingala.zip4j.ZipFile) WorldException(com.voxelgameslib.voxelgameslib.api.exception.WorldException) ArrayList(java.util.ArrayList) ZipException(net.lingala.zip4j.exception.ZipException) Marker(com.voxelgameslib.voxelgameslib.components.map.Marker) World(org.bukkit.World) File(java.io.File) ZipFile(net.lingala.zip4j.ZipFile) Location(org.bukkit.Location) Nonnull(javax.annotation.Nonnull)

Example 2 with WorldException

use of com.voxelgameslib.voxelgameslib.api.exception.WorldException in project VoxelGamesLibv2 by VoxelGamesLib.

the class WorldModifyCommands method start.

@Subcommand("start")
@CommandPermission("%admin")
@Syntax("<world> - the name of the map you want to modify")
public void start(@Nonnull User user, @Nonnull String world) {
    if (editor != null) {
        Lang.msg(user, LangKey.WORLD_CREATOR_IN_USE, editor.getDisplayName());
        return;
    }
    editor = user;
    // load data
    Optional<Map> o = worldHandler.getMap(world);
    this.map = o.orElseGet(() -> worldHandler.loadMap(world));
    // load world
    map.load(editor.getUuid(), map.getWorldName());
    File file = new File(worldHandler.getWorldContainer(), map.getLoadedName(editor.getUuid()));
    try {
        ZipFile zip = new ZipFile(new File(worldHandler.getWorldsFolder(), map.getWorldName() + ".zip"));
        zip.extractAll(file.getAbsolutePath());
        FileUtils.delete(new File(file, "uid.dat"));
    } catch (ZipException e) {
        throw new WorldException("Could not unzip world " + map.getInfo().getDisplayName() + "(" + map.getWorldName() + ".zip).", e);
    }
    worldHandler.loadLocalWorld(map.getLoadedName(editor.getUuid()));
    // tp
    user.getPlayer().teleportAsync(map.getCenter().toLocation(map.getLoadedName(user.getUuid())));
    if (gameHandler.getDefaultGame().isParticipating(editor.getUuid())) {
        gameHandler.getDefaultGame().leave(editor, false);
    }
    game = gameHandler.startGame(EditModeGame.GAMEMODE);
    game.getActivePhase().getNextPhase().getFeature(SpawnFeature.class).addSpawn(map.getCenter());
    game.getActivePhase().getNextPhase().getFeature(MapFeature.class).setMap(map);
    map.load(game.getUuid(), map.getWorldName());
    game.join(editor);
    game.endPhase();
    map.getInfo().setWorldName(map.getWorldName());
    Lang.msg(user, LangKey.WORLD_MODIFY_START);
// TODO use inventory for world creator
}
Also used : ZipFile(net.lingala.zip4j.ZipFile) WorldException(com.voxelgameslib.voxelgameslib.api.exception.WorldException) MapFeature(com.voxelgameslib.voxelgameslib.api.feature.features.MapFeature) ZipException(net.lingala.zip4j.exception.ZipException) Map(com.voxelgameslib.voxelgameslib.components.map.Map) File(java.io.File) ZipFile(net.lingala.zip4j.ZipFile) SpawnFeature(com.voxelgameslib.voxelgameslib.api.feature.features.SpawnFeature) Subcommand(co.aikar.commands.annotation.Subcommand) Syntax(co.aikar.commands.annotation.Syntax) CommandPermission(co.aikar.commands.annotation.CommandPermission)

Aggregations

WorldException (com.voxelgameslib.voxelgameslib.api.exception.WorldException)2 File (java.io.File)2 ZipFile (net.lingala.zip4j.ZipFile)2 ZipException (net.lingala.zip4j.exception.ZipException)2 CommandPermission (co.aikar.commands.annotation.CommandPermission)1 Subcommand (co.aikar.commands.annotation.Subcommand)1 Syntax (co.aikar.commands.annotation.Syntax)1 MapFeature (com.voxelgameslib.voxelgameslib.api.feature.features.MapFeature)1 SpawnFeature (com.voxelgameslib.voxelgameslib.api.feature.features.SpawnFeature)1 Map (com.voxelgameslib.voxelgameslib.components.map.Map)1 Marker (com.voxelgameslib.voxelgameslib.components.map.Marker)1 ArrayList (java.util.ArrayList)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Nonnull (javax.annotation.Nonnull)1 Location (org.bukkit.Location)1 World (org.bukkit.World)1