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);
}
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();
}
}
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());
}
}
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));
}
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);
}
}
Aggregations