Skip to content

Commit

Permalink
update reward conversation to allow >3 positions
Browse files Browse the repository at this point in the history
  • Loading branch information
steve4744 committed Jan 5, 2024
1 parent 068930b commit b0f884a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 36 deletions.
46 changes: 32 additions & 14 deletions src/main/java/tntrun/arena/structure/Rewards.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ public Rewards() {
private Map<Integer, List<String>> commandrewards = new HashMap<>();
private Map<Integer, List<ItemStack>> materialrewards = 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;
private String path;

public List<ItemStack> getMaterialReward(int place) {
return materialrewards.get(place);
Expand Down Expand Up @@ -91,10 +91,10 @@ public int getMaxPlaces() {
*/
private boolean isActiveReward(int place) {
if (Utils.debug()) {
Bukkit.getLogger().info("[TNTRun] place = " + place +", maxplaces = " + maxplaces +
Bukkit.getLogger().info("[TNTRun] place = " + place +", maxplaces = " + getMaxPlaces() +
", starters = " + startingPlayers + ", min = " + getMinPlayersRequired(place));
}
return place <= (maxplaces + 1) && startingPlayers >= getMinPlayersRequired(place);
return place <= (getMaxPlaces()) && startingPlayers >= getMinPlayersRequired(place);
}

public void setMaterialReward(String item, String amount, String label, boolean isFirstItem, int place) {
Expand All @@ -110,6 +110,7 @@ public void setMaterialReward(String item, String amount, String label, boolean
setMaterialDisplayName(reward, label);
}
materialrewards.computeIfAbsent(place, k -> new ArrayList<>()).add(reward);
maxplaces = Math.max(maxplaces, place);

if (Utils.debug()) {
Bukkit.getLogger().info("[TNTRun] reward(" + place + ") = " + materialrewards.toString());
Expand All @@ -124,6 +125,7 @@ private void setMaterialDisplayName(ItemStack is, String label) {

public void setMoneyReward(int money, int place) {
moneyreward.put(place, money);
maxplaces = Math.max(maxplaces, place);
}

public void setCommandReward(String cmdreward, boolean isFirstCmd, int place) {
Expand All @@ -134,6 +136,8 @@ public void setCommandReward(String cmdreward, boolean isFirstCmd, int place) {
Bukkit.getLogger().info("[TNTRun] reward(" + place + ") = " + commandrewards.toString());
}
commandrewards.computeIfAbsent(place, k -> new ArrayList<>()).add(cmdreward);
maxplaces = Math.max(maxplaces, place);

if (Utils.debug()) {
Bukkit.getLogger().info("[TNTRun] reward(" + place + ") = " + commandrewards.toString());
}
Expand All @@ -145,6 +149,7 @@ public void setCommandRewards(List<String> cmdreward, int place) {

public void setXPReward(int xprwd, int place) {
xpreward.put(place, xprwd);
maxplaces = Math.max(maxplaces, place);
}

public void deleteMaterialReward(int place) {
Expand Down Expand Up @@ -218,8 +223,9 @@ public void setStartingPlayers(int starters) {
}

public void saveToConfig(FileConfiguration config) {
index = 1;
places.stream().forEach(path -> {
path = "reward";
IntStream.range(1, getMaxPlaces() + 1).forEach(index -> {

config.set(path + ".minPlayers", getMinPlayersRequired(index));
config.set(path + ".money", getMoneyReward(index));
config.set(path + ".xp", getXPReward(index));
Expand All @@ -229,14 +235,17 @@ public void saveToConfig(FileConfiguration config) {
config.set(path + ".material." + is.getType().toString() + ".amount", is.getAmount());
});
}
index++;
path = "places." + (index + 1);
});
}

public void loadFromConfig(FileConfiguration config) {
maxplaces = config.isConfigurationSection("places") ? config.getConfigurationSection("places").getKeys(false).size() : 0;
if (!config.isConfigurationSection("reward")) {
return;
}
index = 1;
getRewardPlaces(config).stream().forEach(path -> {
getPlacesFromFile(config).stream().forEach(path -> {

setMinPlayersRequired(config.getInt(path + ".minPlayers"), index);
setMoneyReward(config.getInt(path + ".money"), index);
setXPReward(config.getInt(path + ".xp"), index);
Expand All @@ -252,23 +261,32 @@ public void loadFromConfig(FileConfiguration config) {
}
index++;
});

maxplaces = index - 1;
}

private List<String> getRewardPlaces(FileConfiguration config) {
private List<String> getPlacesFromFile(FileConfiguration config) {
List<String> paths = new ArrayList<>(List.of("reward"));
if (!config.isConfigurationSection("places")) {
return paths;
}
for (String key : config.getConfigurationSection("places").getKeys(false)) {
// temp code to rewrite config from v9.27
if (key.equalsIgnoreCase("second")) {
places.add("places.2");
paths.add("places." + 2);
continue;
} else if (key.equalsIgnoreCase("third")) {
places.add("places.3");
paths.add("places." + 3);
continue;
}

places.add("places." + key);
paths.add("places." + key);
}
if (Utils.debug()) {
Bukkit.getLogger().info("[TNTRun] reward paths = " + paths.toString());
}

return places;
return paths;
}

private boolean isValidReward(String materialreward, int materialamount) {
Expand All @@ -281,7 +299,7 @@ private boolean isValidReward(String materialreward, int materialamount) {
public void listRewards(Player player, String arenaName) {
Messages.sendMessage(player, Messages.rewardshead.replace("{ARENA}", arenaName), false);

IntStream.range(1, maxplaces + 2).forEach(i -> {
IntStream.range(1, getMaxPlaces() + 1).forEach(i -> {

StringBuilder sb = new StringBuilder(200);
if (getXPReward(i) != 0) {
Expand Down
38 changes: 16 additions & 22 deletions src/main/java/tntrun/conversation/ArenaRewardConversation.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,43 +31,37 @@
import tntrun.arena.Arena;
import tntrun.utils.Utils;

public class ArenaRewardConversation extends FixedSetPrompt {
public class ArenaRewardConversation extends NumericPrompt {
private Arena arena;
private boolean isFirstItem = true;
private String podium;
private int place;
private static final String PREFIX = GRAY + "[" + GOLD + "TNTRun_reloaded" + GRAY + "] ";

public ArenaRewardConversation(Arena arena) {
super("first", "second", "third");
this.arena = arena;
}

@Override
public String getPromptText(ConversationContext context) {
return GOLD + " What reward place would you like to set?\n"
+ GREEN + formatFixedSet();
return GOLD + " What position would you like to set a reward for? (1, 2, 3, ...)\n";
}

@Override
protected Prompt acceptValidatedInput(ConversationContext context, String choice) {
switch(choice.toLowerCase()) {
case "first":
podium = GOLD + "First " + GRAY + "place ";
place = 1;
break;
case "second":
podium = GOLD + "Second " + GRAY + "place ";
place = 2;
break;
case "third":
podium = GOLD + "Third " + GRAY + "place ";
place = 3;
break;
default:
place = 0;
}
return place != 0 ? new ChooseRewardType() : null;
protected boolean isNumberValid(ConversationContext context, Number input) {
return input.intValue() > 0 && input.intValue() < 100;
}

@Override
protected String getFailedValidationText(ConversationContext context, Number invalidInput) {
return "Position must be between 1 and 99.";
}

@Override
protected Prompt acceptValidatedInput(ConversationContext context, Number position) {
place = (int)position;
podium = GRAY + "Position " + GOLD + place + GRAY + ": ";
return new ChooseRewardType();
}

private class ChooseRewardType extends FixedSetPrompt {
Expand Down

0 comments on commit b0f884a

Please sign in to comment.