Search in sources :

Example 1 with MapInfo

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

the class VoteFeature method sendVoteMessage.

/**
 * Sends the vote message to that user
 *
 * @param user the user that should receive the message
 */
public void sendVoteMessage(@Nonnull User user) {
    Lang.msg(user, LangKey.VOTE_MESSAGE_TOP);
    for (int id : availableMaps.keySet()) {
        MapInfo info = availableMaps.get(id);
        Lang.msg(user, LangKey.VOTE_MESSAGE_MAP, "/vote " + id, id, info.getWorldName(), info.getAuthor());
    }
    Lang.msg(user, LangKey.VOTE_MESSAGE_BOT);
}
Also used : MapInfo(com.voxelgameslib.voxelgameslib.components.map.MapInfo)

Example 2 with MapInfo

use of com.voxelgameslib.voxelgameslib.components.map.MapInfo 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 3 with MapInfo

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

the class VoteFeature method openVoteMenu.

@GameEvent
public void openVoteMenu(@Nonnull PlayerInteractEvent event, User user) {
    if ((event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) && openMenuItem.equals(event.getItem())) {
        InventoryMenuBuilder builder = new InventoryMenuBuilder().withSize(9).withTitle("Vote for a map");
        for (int id : availableMaps.keySet()) {
            MapInfo info = availableMaps.get(id);
            ItemStack item = new ItemBuilder(Material.PAPER).amount(id).name(info.getDisplayName()).lore(info.getAuthor()).build();
            builder.withItem(id - 1, item, (player, clickType, itemStack) -> confirmVote(user, id), ClickType.LEFT);
        }
        builder.show(user.getPlayer());
    }
}
Also used : ItemBuilder(com.voxelgameslib.voxelgameslib.util.utils.ItemBuilder) InventoryMenuBuilder(org.inventivetalent.menubuilder.inventory.InventoryMenuBuilder) MapInfo(com.voxelgameslib.voxelgameslib.components.map.MapInfo) ItemStack(org.bukkit.inventory.ItemStack) GameEvent(com.voxelgameslib.voxelgameslib.api.event.GameEvent)

Example 4 with MapInfo

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

the class VoteFeature method disable.

@Override
public void disable() {
    Map<Integer, Integer> votes = new HashMap<>();
    int max = -1;
    int maxMap = -1;
    for (Integer map : this.votes.values()) {
        int old = votes.getOrDefault(map, 0);
        old++;
        if (old > max) {
            max = old;
            maxMap = map;
        }
        votes.put(map, old);
    }
    MapInfo winner = availableMaps.get(maxMap);
    if (winner == null) {
        // use first map if nobody won
        winner = availableMaps.values().iterator().next();
    }
    DefaultGameData gameData = getPhase().getGame().getGameData(DefaultGameData.class).orElse(new DefaultGameData());
    gameData.voteWinner = winner;
    getPhase().getGame().putGameData(gameData);
    getPhase().getGame().broadcastMessage(LangKey.VOTE_END, winner.getDisplayName(), winner.getAuthor(), Math.max(max, 0));
}
Also used : HashMap(java.util.HashMap) MapInfo(com.voxelgameslib.voxelgameslib.components.map.MapInfo) DefaultGameData(com.voxelgameslib.voxelgameslib.api.game.DefaultGameData)

Example 5 with MapInfo

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

the class VoteFeature method enable.

@Override
public void enable() {
    String mode = getPhase().getGame().getGameMode().getName();
    int id = 1;
    for (MapInfo info : config.maps) {
        if (info.getGamemodes().contains(mode)) {
            availableMaps.put(id++, info);
        }
        if (id == maxMaps - 1) {
            break;
        }
    }
    if (availableMaps.size() == 0) {
        getPhase().getGame().broadcastMessage(LangKey.VOTE_NO_MAPS_FOUND);
        getPhase().getGame().abortGame();
        log.warning("Game " + getPhase().getGame().getUuid() + "(" + getPhase().getGame().getGameMode().getName() + ")" + " was aborted because it didn't find any maps to play!");
        return;
    }
    getPhase().getGame().getPlayers().forEach(this::sendVoteMessage);
    if (enableVoteMenu) {
        getPhase().getGame().getPlayers().forEach(this::giveVoteMenuItem);
    }
}
Also used : MapInfo(com.voxelgameslib.voxelgameslib.components.map.MapInfo)

Aggregations

MapInfo (com.voxelgameslib.voxelgameslib.components.map.MapInfo)6 JsonReader (com.google.gson.stream.JsonReader)1 GameEvent (com.voxelgameslib.voxelgameslib.api.event.GameEvent)1 MapException (com.voxelgameslib.voxelgameslib.api.exception.MapException)1 WorldException (com.voxelgameslib.voxelgameslib.api.exception.WorldException)1 DefaultGameData (com.voxelgameslib.voxelgameslib.api.game.DefaultGameData)1 Map (com.voxelgameslib.voxelgameslib.components.map.Map)1 ItemBuilder (com.voxelgameslib.voxelgameslib.util.utils.ItemBuilder)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Nonnull (javax.annotation.Nonnull)1 ZipFile (net.lingala.zip4j.ZipFile)1 ZipException (net.lingala.zip4j.exception.ZipException)1 FileHeader (net.lingala.zip4j.model.FileHeader)1 ItemStack (org.bukkit.inventory.ItemStack)1