Search in sources :

Example 1 with MapInfo

use of com.voxelgameslib.voxelgameslib.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.getName(), info.getAuthor());
    }
    Lang.msg(user, LangKey.VOTE_MESSAGE_BOT);
}
Also used : MapInfo(com.voxelgameslib.voxelgameslib.map.MapInfo)

Example 2 with MapInfo

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

the class VoteFeature method confirmVote.

/**
 * Confirms a vote for a map
 */
private void confirmVote(@Nonnull User voter, @Nonnull Integer mapId) {
    if (votes.containsKey(voter.getUuid())) {
        Lang.msg(voter, LangKey.VOTE_ALREADY_VOTED);
    } else {
        MapInfo mapInfo = availableMaps.get(mapId);
        if (mapInfo == null) {
            Lang.msg(voter, LangKey.VOTE_UNKNOWN_MAP, mapId);
            return;
        }
        votes.put(voter.getUuid(), mapId);
        Lang.msg(voter, LangKey.VOTE_SUBMITTED, mapInfo.getName(), mapId);
    }
}
Also used : MapInfo(com.voxelgameslib.voxelgameslib.map.MapInfo)

Example 3 with MapInfo

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

Example 4 with MapInfo

use of com.voxelgameslib.voxelgameslib.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.getName(), winner.getAuthor(), Math.max(max, 0));
}
Also used : HashMap(java.util.HashMap) MapInfo(com.voxelgameslib.voxelgameslib.map.MapInfo) DefaultGameData(com.voxelgameslib.voxelgameslib.game.DefaultGameData)

Example 5 with MapInfo

use of com.voxelgameslib.voxelgameslib.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().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)

Aggregations

MapInfo (com.voxelgameslib.voxelgameslib.map.MapInfo)6 JsonReader (com.google.gson.stream.JsonReader)1 GameEvent (com.voxelgameslib.voxelgameslib.event.GameEvent)1 MapException (com.voxelgameslib.voxelgameslib.exception.MapException)1 WorldException (com.voxelgameslib.voxelgameslib.exception.WorldException)1 DefaultGameData (com.voxelgameslib.voxelgameslib.game.DefaultGameData)1 Map (com.voxelgameslib.voxelgameslib.map.Map)1 ItemBuilder (com.voxelgameslib.voxelgameslib.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.core.ZipFile)1 ZipException (net.lingala.zip4j.exception.ZipException)1 FileHeader (net.lingala.zip4j.model.FileHeader)1 ItemStack (org.bukkit.inventory.ItemStack)1