diff --git a/src/main/java/tntrun/arena/handlers/GameHandler.java b/src/main/java/tntrun/arena/handlers/GameHandler.java index 1bc0f880..22339320 100644 --- a/src/main/java/tntrun/arena/handlers/GameHandler.java +++ b/src/main/java/tntrun/arena/handlers/GameHandler.java @@ -20,6 +20,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Map.Entry; + import org.bukkit.Bukkit; import org.bukkit.Color; import org.bukkit.FireworkEffect; @@ -168,6 +170,7 @@ public void stopArenaCountdown() { // main arena handler private int timeremaining; private int arenahandler; + private int startingPlayers; private boolean forceStartByCmd; private boolean hasTimeLimit; @@ -176,9 +179,10 @@ public void stopArenaCountdown() { */ private void startArena() { arena.getStatusManager().setRunning(true); + startingPlayers = arena.getPlayersManager().getPlayersCount(); if (Utils.debug()) { plugin.getLogger().info("Arena " + arena.getArenaName() + " started"); - plugin.getLogger().info("Players in arena: " + arena.getPlayersManager().getPlayersCount()); + plugin.getLogger().info("Players in arena: " + startingPlayers); } plugin.getServer().getPluginManager().callEvent(new ArenaStartEvent(arena)); @@ -187,8 +191,8 @@ private void startArena() { arena.getScoreboardHandler().removeScoreboard(player); } - arena.getStructureManager().getRewards().setActiveRewards(arena.getPlayersManager().getPlayersCount()); - setActiveStats(arena.getPlayersManager().getPlayersCount()); + arena.getStructureManager().getRewards().setStartingPlayers(startingPlayers); + setActiveStats(startingPlayers); String message = Messages.trprefix; int limit = arena.getStructureManager().getTimeLimit(); @@ -304,7 +308,8 @@ public void stopArena() { /** * Remove the block under the player's feet, monitor player's position for lose. - * Check for winner, i.e. last player remaining, and for 2nd and 3rd places. + * Check for winner, i.e. last player remaining, and for other places. + * * @param player */ private void handlePlayer(final Player player) { @@ -330,16 +335,16 @@ private void handlePlayer(final Player player) { } /** - * Get Map containing the 2nd and 3rd place player names. + * Get map containing the player names for 2nd, 3rd and other places. * - * @return player names finishing 2nd and 3rd. + * @return player names finishing in places to be displayed. */ public Map getPlaces() { return places; } /** - * Store the names of the players finishing in places 2 and 3. + * Store the names of the players finishing in the top places (2nd and 3rd by default). * The players can be at the lose level or can have quit the game. * Exit if the map already contains the player - 3rd placed spectator could * subsequently quit and be rewarded multiple prizes. @@ -351,9 +356,7 @@ public void setPlaces(String playerName) { return; } int remainingPlayers = arena.getPlayersManager().getPlayersCount(); - if (remainingPlayers < 4 && remainingPlayers > 1) { - places.put(remainingPlayers, playerName); - } + places.put(remainingPlayers, playerName); } /** @@ -636,38 +639,32 @@ public boolean isTimedOut() { } /** - * Gets the players in the first 3 positions at the end of the game. + * Gets the player positions at the end of the game up to the maximum set in the configuration. * * @param winner player - * @return string podium places + * @return string places */ private String getPodiumPlaces(Player winner) { StringBuilder sb = new StringBuilder(200); String header = Messages.resultshead.replace("{ARENA}", arena.getArenaName()); sb.append("\n" + header); sb.append("\n "); - sb.append("\n" + Messages.playerfirstplace.replace("{RANK}", Utils.getRank(winner.getName())) + sb.append("\n" + Messages.playerposition.replace("{POS}", "1").replace("{RANK}", Utils.getRank(winner.getName())) .replace("{COLOR}", Utils.getColourMeta(winner)).replace("{PLAYER}", winner.getName())); - if (places.get(2) != null) { - String playerName = places.get(2); - sb.append("\n" + Messages.playersecondplace.replace("{RANK}", Utils.getRank(playerName)) - .replace("{COLOR}", Utils.getColourMeta(playerName)) - .replace("{PLAYER}", playerName)); - - } else { - sb.append("\n" + Messages.playersecondplace.replace("{RANK}", "").replace("{COLOR}", "").replace("{PLAYER}", "-")); - } - - if (places.get(3) != null) { - String playerName = places.get(3); - sb.append("\n" + Messages.playerthirdplace.replace("{RANK}", Utils.getRank(playerName)) - .replace("{COLOR}", Utils.getColourMeta(playerName)) - .replace("{PLAYER}", playerName)); + places.entrySet().stream() + .sorted(Entry.comparingByKey()) + .forEach(e -> { + if (e.getKey() <= Math.min(arena.getStructureManager().getMaxFinalPositions(), startingPlayers)) { + String playerName = e.getValue(); + sb.append("\n" + Messages.playerposition.replace("{POS}", String.valueOf(e.getKey())) + .replace("{RANK}", Utils.getRank(playerName)) + .replace("{COLOR}", Utils.getColourMeta(playerName)) + .replace("{PLAYER}", playerName)); + } + } + ); - } else { - sb.append("\n" + Messages.playerthirdplace.replace("{RANK}", "").replace("{COLOR}", "").replace("{PLAYER}", "-")); - } sb.append("\n "); sb.append("\n" + header); diff --git a/src/main/java/tntrun/arena/structure/Rewards.java b/src/main/java/tntrun/arena/structure/Rewards.java index 0461515a..40a886a7 100644 --- a/src/main/java/tntrun/arena/structure/Rewards.java +++ b/src/main/java/tntrun/arena/structure/Rewards.java @@ -18,7 +18,6 @@ package tntrun.arena.structure; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -26,8 +25,6 @@ import java.util.Set; import java.util.StringJoiner; import java.util.stream.IntStream; -import java.util.stream.Stream; - import net.milkbowl.vault.economy.Economy; import org.bukkit.Bukkit; @@ -56,8 +53,10 @@ public Rewards() { private Map xpreward = new HashMap<>(); private Map> commandrewards = new HashMap<>(); private Map> materialrewards = new HashMap<>(); - private Map activereward = new HashMap<>(); private Map minplayersrequired = new HashMap<>(); + private List places = new ArrayList<>(List.of("reward")); + private int startingPlayers; + private int maxplaces; private int index; public List getMaterialReward(int place) { @@ -80,8 +79,22 @@ public int getMinPlayersRequired(int place) { return minplayersrequired.getOrDefault(place, 0); } - public boolean isActiveReward(int place) { - return activereward.getOrDefault(place, false); + public int getMaxPlaces() { + return maxplaces; + } + + /** + * Check if the player's finishing position has a reward. + * + * @param place + * @return + */ + private boolean isActiveReward(int place) { + if (Utils.debug()) { + Bukkit.getLogger().info("[TNTRun] place = " + place +", maxplaces = " + maxplaces + + ", starters = " + startingPlayers + ", min = " + getMinPlayersRequired(place)); + } + return place <= (maxplaces + 1) && startingPlayers >= getMinPlayersRequired(place); } public void setMaterialReward(String item, String amount, String label, boolean isFirstItem, int place) { @@ -200,19 +213,13 @@ private void rewardMoney(OfflinePlayer offplayer, int money) { } } - public void setActiveRewards(int playercount) { - for (int place = 1; place < 4; place++) { - activereward.put(place, false); - if (playercount >= getMinPlayersRequired(place)) { - activereward.put(place, true); - } - } + public void setStartingPlayers(int starters) { + startingPlayers = starters; } public void saveToConfig(FileConfiguration config) { index = 1; - Stream stream = Stream.of("reward", "places.second", "places.third"); - stream.forEach(path -> { + places.stream().forEach(path -> { config.set(path + ".minPlayers", getMinPlayersRequired(index)); config.set(path + ".money", getMoneyReward(index)); config.set(path + ".xp", getXPReward(index)); @@ -227,9 +234,9 @@ public void saveToConfig(FileConfiguration config) { } public void loadFromConfig(FileConfiguration config) { + maxplaces = config.isConfigurationSection("places") ? config.getConfigurationSection("places").getKeys(false).size() : 0; index = 1; - Stream stream = Stream.of("reward", "places.second", "places.third"); - stream.forEach(path -> { + getRewardPlaces(config).stream().forEach(path -> { setMinPlayersRequired(config.getInt(path + ".minPlayers"), index); setMoneyReward(config.getInt(path + ".money"), index); setXPReward(config.getInt(path + ".xp"), index); @@ -247,6 +254,23 @@ public void loadFromConfig(FileConfiguration config) { }); } + private List getRewardPlaces(FileConfiguration config) { + for (String key : config.getConfigurationSection("places").getKeys(false)) { + // temp code to rewrite config from v9.27 + if (key.equalsIgnoreCase("second")) { + places.add("places.2"); + continue; + } else if (key.equalsIgnoreCase("third")) { + places.add("places.3"); + continue; + } + + places.add("places." + key); + } + + return places; + } + private boolean isValidReward(String materialreward, int materialamount) { if (Material.getMaterial(materialreward) != null && materialamount > 0) { return true; @@ -255,10 +279,10 @@ private boolean isValidReward(String materialreward, int materialamount) { } public void listRewards(Player player, String arenaName) { - List places = Arrays.asList(Messages.playerfirstplace, Messages.playersecondplace, Messages.playerthirdplace); Messages.sendMessage(player, Messages.rewardshead.replace("{ARENA}", arenaName), false); - IntStream.range(1, 4).forEach(i -> { + IntStream.range(1, maxplaces + 2).forEach(i -> { + StringBuilder sb = new StringBuilder(200); if (getXPReward(i) != 0) { sb.append("\n " + Messages.playerrewardxp + getXPReward(i)); @@ -279,7 +303,7 @@ public void listRewards(Player player, String arenaName) { sb.setLength(sb.length() - 2); } if (sb.length() != 0) { - sb.insert(0, places.get(i-1).replace("{RANK}", "").replace("{COLOR}", "").replace("{PLAYER}", "")); + sb.insert(0, Messages.rewardlistposition.replace("{POS}", String.valueOf(i))); Messages.sendMessage(player, sb.toString(), false); } }); diff --git a/src/main/java/tntrun/arena/structure/StructureManager.java b/src/main/java/tntrun/arena/structure/StructureManager.java index 0e3a9689..38ce2bce 100644 --- a/src/main/java/tntrun/arena/structure/StructureManager.java +++ b/src/main/java/tntrun/arena/structure/StructureManager.java @@ -76,6 +76,7 @@ public StructureManager(Arena arena) { private String commandOnStart; private String commandOnStop; private boolean shopEnabled = true; + private int maxFinalPositions = 3; public String getWorldName() { return world; @@ -221,6 +222,10 @@ public double getFee() { return fee; } + public int getMaxFinalPositions() { + return maxFinalPositions; + } + public Material getCurrency() { return Material.getMaterial(currency); } @@ -446,6 +451,10 @@ public void setFee(double fee) { this.fee = fee; } + public void setMaxFinalPositions(int size) { + this.maxFinalPositions = size; + } + public void setCurrency(Material currency) { if (currency.isAir()) { this.currency = null; @@ -515,6 +524,7 @@ public void saveToConfig() { config.set("spawnpoints", additionalSpawnPoints); config.set("commandOnStart", getCommandOnStart()); config.set("commandOnStop", getCommandOnStop()); + config.set("displayfinalpositions", maxFinalPositions); config.set("shop.enabled", shopEnabled); rewards.saveToConfig(config); try { @@ -562,6 +572,7 @@ public void loadFromConfig() { additionalSpawnPoints = (List) config.getList("spawnpoints", new ArrayList<>()); commandOnStart = config.getString("commandOnStart", ""); commandOnStop = config.getString("commandOnStop", ""); + maxFinalPositions = config.getInt("displayfinalpositions", maxFinalPositions); shopEnabled = config.getBoolean("shop.enabled", true); }