Search in sources :

Example 1 with Map

use of com.voxelgameslib.voxelgameslib.components.map.Map in project VoxelGamesLibv2 by VoxelGamesLib.

the class WorldHandler method loadMap.

/**
 * Tries to load the map data for a name
 *
 * @param name the name of the map to load
 * @return the loaded map
 * @throws MapException when
 */
@Nonnull
public Map loadMap(@Nonnull String name) {
    Optional<Map> map = getMap(name);
    if (!map.isPresent()) {
        Optional<MapInfo> mapInfo = getMapInfo(name);
        if (!mapInfo.isPresent()) {
            throw new MapException("Unknown map " + name + ". Did you register it into the world config?");
        }
        try {
            ZipFile zipFile = new ZipFile(new File(worldsFolder, mapInfo.get().getWorldName() + ".zip"));
            for (FileHeader header : (List<FileHeader>) zipFile.getFileHeaders()) {
                if (header.getFileName().endsWith("config.json")) {
                    InputStream stream = zipFile.getInputStream(header);
                    Map m = gson.fromJson(new JsonReader(new InputStreamReader(stream)), Map.class);
                    m.initMarkers(mapHandler);
                    maps.add(m);
                    return m;
                }
            }
            throw new MapException("Could not load map config for map " + name + ". Fileheader was null. Does it has a map.json?");
        } catch (Exception e) {
            throw new MapException("Error while trying to load map config " + name + "(" + mapInfo.get().getWorldName() + ".zip)", e);
        }
    } else {
        return map.get();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) MapException(com.voxelgameslib.voxelgameslib.api.exception.MapException) MapException(com.voxelgameslib.voxelgameslib.api.exception.MapException) WorldException(com.voxelgameslib.voxelgameslib.api.exception.WorldException) IOException(java.io.IOException) ZipException(net.lingala.zip4j.exception.ZipException) ZipFile(net.lingala.zip4j.ZipFile) MapInfo(com.voxelgameslib.voxelgameslib.components.map.MapInfo) JsonReader(com.google.gson.stream.JsonReader) ArrayList(java.util.ArrayList) List(java.util.List) Map(com.voxelgameslib.voxelgameslib.components.map.Map) File(java.io.File) ZipFile(net.lingala.zip4j.ZipFile) FileHeader(net.lingala.zip4j.model.FileHeader) Nonnull(javax.annotation.Nonnull)

Example 2 with Map

use of com.voxelgameslib.voxelgameslib.components.map.Map in project VoxelGamesLibv2 by VoxelGamesLib.

the class WorldCommands method load.

@Subcommand("load")
@CommandPermission("%admin")
@Syntax("<mapname> - the name of the map to load")
@Description("Loads a map onto the server")
public void load(@Nonnull User sender, @Nonnull String mapName) {
    Optional<Map> o = handler.getMap(mapName);
    Map map = o.orElseGet(() -> handler.loadMap(mapName));
    handler.loadWorld(map, sender.getUuid(), false);
}
Also used : Map(com.voxelgameslib.voxelgameslib.components.map.Map) Description(co.aikar.commands.annotation.Description) Subcommand(co.aikar.commands.annotation.Subcommand) Syntax(co.aikar.commands.annotation.Syntax) CommandPermission(co.aikar.commands.annotation.CommandPermission)

Example 3 with Map

use of com.voxelgameslib.voxelgameslib.components.map.Map 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)

Example 4 with Map

use of com.voxelgameslib.voxelgameslib.components.map.Map in project VoxelGamesLibv2 by VoxelGamesLib.

the class WorldCreator method world.

@Subcommand("world")
@CommandPermission("%admin")
public void world(@Nonnull User sender, @Nonnull String worldName) {
    if (step != 1) {
        Lang.msg(sender, LangKey.WORLD_CREATOR_WRONG_STEP, step, 1);
        return;
    }
    gameHandler.getGames(sender.getUuid(), true).forEach(game -> game.leave(sender, false));
    this.worldName = worldName;
    worldHandler.loadLocalWorld(worldName);
    Location spawnLoc = Bukkit.getWorld(worldName).getSpawnLocation();
    Vector3D spawn = new Vector3D(spawnLoc.getX(), spawnLoc.getY(), spawnLoc.getZ());
    sender.getPlayer().teleportAsync(spawnLoc);
    game = gameHandler.startGame(EditModeGame.GAMEMODE);
    game.getActivePhase().getNextPhase().getFeature(SpawnFeature.class).addSpawn(spawn);
    Map map = new Map(null, worldName, spawn, 100);
    map.load(game.getUuid(), worldName);
    game.getActivePhase().getNextPhase().getFeature(MapFeature.class).setMap(map);
    game.join(editor);
    game.endPhase();
    Lang.msg(sender, LangKey.WORLD_CREATOR_ENTER_CENTER, "/worldcreator center");
    step = 2;
}
Also used : Vector3D(com.voxelgameslib.voxelgameslib.internal.math.Vector3D) MapFeature(com.voxelgameslib.voxelgameslib.api.feature.features.MapFeature) SpawnFeature(com.voxelgameslib.voxelgameslib.api.feature.features.SpawnFeature) Map(com.voxelgameslib.voxelgameslib.components.map.Map) Location(org.bukkit.Location) Subcommand(co.aikar.commands.annotation.Subcommand) CommandPermission(co.aikar.commands.annotation.CommandPermission)

Example 5 with Map

use of com.voxelgameslib.voxelgameslib.components.map.Map in project VoxelGamesLibv2 by VoxelGamesLib.

the class WorldCommands method tp.

@Subcommand("tp")
@CommandPermission("%admin")
@Syntax("<world> - the name of the world to tp to")
@Description("Teleports you to a world")
public void tp(@Nonnull User sender, @Nonnull String world) {
    Optional<Map> o = handler.getMap(world);
    if (o.isPresent()) {
        Map map = o.get();
        sender.getPlayer().teleportAsync(map.getCenter().toLocation(map.getLoadedName(sender.getUuid())));
    } else {
        World w = Bukkit.getWorld(world);
        if (w != null) {
            sender.getPlayer().teleportAsync(Bukkit.getWorld(world).getSpawnLocation());
        }
        Lang.msg(sender, LangKey.WORLD_UNKNOWN_MAP, world);
    }
}
Also used : World(org.bukkit.World) Map(com.voxelgameslib.voxelgameslib.components.map.Map) Description(co.aikar.commands.annotation.Description) Subcommand(co.aikar.commands.annotation.Subcommand) Syntax(co.aikar.commands.annotation.Syntax) CommandPermission(co.aikar.commands.annotation.CommandPermission)

Aggregations

Map (com.voxelgameslib.voxelgameslib.components.map.Map)6 CommandPermission (co.aikar.commands.annotation.CommandPermission)4 Subcommand (co.aikar.commands.annotation.Subcommand)4 Syntax (co.aikar.commands.annotation.Syntax)3 Description (co.aikar.commands.annotation.Description)2 WorldException (com.voxelgameslib.voxelgameslib.api.exception.WorldException)2 MapFeature (com.voxelgameslib.voxelgameslib.api.feature.features.MapFeature)2 SpawnFeature (com.voxelgameslib.voxelgameslib.api.feature.features.SpawnFeature)2 File (java.io.File)2 ZipFile (net.lingala.zip4j.ZipFile)2 ZipException (net.lingala.zip4j.exception.ZipException)2 JsonReader (com.google.gson.stream.JsonReader)1 MapException (com.voxelgameslib.voxelgameslib.api.exception.MapException)1 MapInfo (com.voxelgameslib.voxelgameslib.components.map.MapInfo)1 Scoreboard (com.voxelgameslib.voxelgameslib.components.scoreboard.Scoreboard)1 Vector3D (com.voxelgameslib.voxelgameslib.internal.math.Vector3D)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1