Search in sources :

Example 1 with MapException

use of com.voxelgameslib.voxelgameslib.exception.MapException 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().getName() + ".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, e);
        }
    } else {
        return map.get();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) MapException(com.voxelgameslib.voxelgameslib.exception.MapException) WorldException(com.voxelgameslib.voxelgameslib.exception.WorldException) MapException(com.voxelgameslib.voxelgameslib.exception.MapException) IOException(java.io.IOException) ZipException(net.lingala.zip4j.exception.ZipException) ZipFile(net.lingala.zip4j.core.ZipFile) MapInfo(com.voxelgameslib.voxelgameslib.map.MapInfo) JsonReader(com.google.gson.stream.JsonReader) ArrayList(java.util.ArrayList) List(java.util.List) Map(com.voxelgameslib.voxelgameslib.map.Map) ZipFile(net.lingala.zip4j.core.ZipFile) File(java.io.File) FileHeader(net.lingala.zip4j.model.FileHeader) Nonnull(javax.annotation.Nonnull)

Example 2 with MapException

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

the class MapScanner method searchForMarkers.

/**
 * Searches the map for "markers". Most of the time these are implemented as tile entities (skulls)
 *
 * @param map    the map to scan
 * @param center the center location
 * @param range  the range in where to scan
 */
public void searchForMarkers(@Nonnull Map map, @Nonnull Vector3D center, int range, @Nonnull UUID gameid) {
    World world = Bukkit.getWorld(map.getLoadedName(gameid));
    if (world == null) {
        throw new MapException("Could not find world " + map.getLoadedName(gameid) + "(" + map.getInfo().getName() + ")" + ". Is it loaded?");
    }
    List<Marker> markers = new ArrayList<>();
    List<ChestMarker> chestMarkers = new ArrayList<>();
    int startX = (int) center.getX();
    int startY = (int) center.getZ();
    int minX = Math.min(startX - range, startX + range);
    int minZ = Math.min(startY - range, startY + range);
    int maxX = Math.max(startX - range, startX + range);
    int maxZ = Math.max(startY - range, startY + range);
    for (int x = minX; x <= maxX; x += 16) {
        for (int z = minZ; z <= maxZ; z += 16) {
            Chunk chunk = world.getChunkAt(x >> 4, z >> 4);
            for (BlockState te : chunk.getTileEntities()) {
                if (te.getType() == Material.SKULL) {
                    Skull skull = (Skull) te;
                    if (skull.getSkullType() == SkullType.PLAYER) {
                        String markerData = getMarkerData(skull);
                        if (markerData == null)
                            continue;
                        MarkerDefinition markerDefinition = mapHandler.createMarkerDefinition(markerData);
                        markers.add(new Marker(new Vector3D(skull.getX(), skull.getY(), skull.getZ()), DirectionUtil.directionToYaw(skull.getRotation()), markerData, markerDefinition));
                    }
                } else if (te.getType() == Material.CHEST) {
                    Chest chest = (Chest) te;
                    String name = chest.getBlockInventory().getName();
                    ItemStack[] items = new ItemStack[chest.getBlockInventory().getStorageContents().length];
                    for (int i = 0; i < items.length; i++) {
                        ItemStack is = chest.getBlockInventory().getItem(i);
                        if (is == null) {
                            items[i] = new ItemStack(Material.AIR);
                        } else {
                            items[i] = is;
                        }
                    }
                    chestMarkers.add(new ChestMarker(new Vector3D(chest.getX(), chest.getY(), chest.getZ()), name, items));
                }
            }
        }
    }
    map.setMarkers(markers);
    map.setChestMarkers(chestMarkers);
}
Also used : Chest(org.bukkit.block.Chest) ArrayList(java.util.ArrayList) MapException(com.voxelgameslib.voxelgameslib.exception.MapException) World(org.bukkit.World) Chunk(org.bukkit.Chunk) BlockState(org.bukkit.block.BlockState) Skull(org.bukkit.block.Skull) ItemStack(org.bukkit.inventory.ItemStack)

Aggregations

MapException (com.voxelgameslib.voxelgameslib.exception.MapException)2 ArrayList (java.util.ArrayList)2 JsonReader (com.google.gson.stream.JsonReader)1 WorldException (com.voxelgameslib.voxelgameslib.exception.WorldException)1 Map (com.voxelgameslib.voxelgameslib.map.Map)1 MapInfo (com.voxelgameslib.voxelgameslib.map.MapInfo)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 List (java.util.List)1 Nonnull (javax.annotation.Nonnull)1 ZipFile (net.lingala.zip4j.core.ZipFile)1 ZipException (net.lingala.zip4j.exception.ZipException)1 FileHeader (net.lingala.zip4j.model.FileHeader)1 Chunk (org.bukkit.Chunk)1 World (org.bukkit.World)1 BlockState (org.bukkit.block.BlockState)1 Chest (org.bukkit.block.Chest)1 Skull (org.bukkit.block.Skull)1