Skip to content

Commit

Permalink
allow rewards to be set for >3 players
Browse files Browse the repository at this point in the history
allow the number of player final positions displayed at end of game to be set in the arena config
  • Loading branch information
steve4744 committed Jan 2, 2024
1 parent 036acc0 commit 068930b
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 51 deletions.
59 changes: 28 additions & 31 deletions src/main/java/tntrun/arena/handlers/GameHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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));
Expand All @@ -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();
Expand Down Expand Up @@ -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) {
Expand All @@ -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<Integer, String> 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.
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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);

Expand Down
64 changes: 44 additions & 20 deletions src/main/java/tntrun/arena/structure/Rewards.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@
package tntrun.arena.structure;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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;
Expand Down Expand Up @@ -56,8 +53,10 @@ public Rewards() {
private Map<Integer, Integer> xpreward = new HashMap<>();
private Map<Integer, List<String>> commandrewards = new HashMap<>();
private Map<Integer, List<ItemStack>> materialrewards = new HashMap<>();
private Map<Integer, Boolean> activereward = new HashMap<>();
private Map<Integer, Integer> minplayersrequired = new HashMap<>();
private List<String> places = new ArrayList<>(List.of("reward"));
private int startingPlayers;
private int maxplaces;
private int index;

public List<ItemStack> getMaterialReward(int place) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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<String> 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));
Expand All @@ -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<String> 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);
Expand All @@ -247,6 +254,23 @@ public void loadFromConfig(FileConfiguration config) {
});
}

private List<String> 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;
Expand All @@ -255,10 +279,10 @@ private boolean isValidReward(String materialreward, int materialamount) {
}

public void listRewards(Player player, String arenaName) {
List<String> 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));
Expand All @@ -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);
}
});
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/tntrun/arena/structure/StructureManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -221,6 +222,10 @@ public double getFee() {
return fee;
}

public int getMaxFinalPositions() {
return maxFinalPositions;
}

public Material getCurrency() {
return Material.getMaterial(currency);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -562,6 +572,7 @@ public void loadFromConfig() {
additionalSpawnPoints = (List<Vector>) config.getList("spawnpoints", new ArrayList<>());
commandOnStart = config.getString("commandOnStart", "");
commandOnStop = config.getString("commandOnStop", "");
maxFinalPositions = config.getInt("displayfinalpositions", maxFinalPositions);
shopEnabled = config.getBoolean("shop.enabled", true);
}

Expand Down

0 comments on commit 068930b

Please sign in to comment.