Skip to content

Commit b0f884a

Browse files
committed
update reward conversation to allow >3 positions
1 parent 068930b commit b0f884a

File tree

2 files changed

+48
-36
lines changed

2 files changed

+48
-36
lines changed

src/main/java/tntrun/arena/structure/Rewards.java

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ public Rewards() {
5454
private Map<Integer, List<String>> commandrewards = new HashMap<>();
5555
private Map<Integer, List<ItemStack>> materialrewards = new HashMap<>();
5656
private Map<Integer, Integer> minplayersrequired = new HashMap<>();
57-
private List<String> places = new ArrayList<>(List.of("reward"));
5857
private int startingPlayers;
5958
private int maxplaces;
6059
private int index;
60+
private String path;
6161

6262
public List<ItemStack> getMaterialReward(int place) {
6363
return materialrewards.get(place);
@@ -91,10 +91,10 @@ public int getMaxPlaces() {
9191
*/
9292
private boolean isActiveReward(int place) {
9393
if (Utils.debug()) {
94-
Bukkit.getLogger().info("[TNTRun] place = " + place +", maxplaces = " + maxplaces +
94+
Bukkit.getLogger().info("[TNTRun] place = " + place +", maxplaces = " + getMaxPlaces() +
9595
", starters = " + startingPlayers + ", min = " + getMinPlayersRequired(place));
9696
}
97-
return place <= (maxplaces + 1) && startingPlayers >= getMinPlayersRequired(place);
97+
return place <= (getMaxPlaces()) && startingPlayers >= getMinPlayersRequired(place);
9898
}
9999

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

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

125126
public void setMoneyReward(int money, int place) {
126127
moneyreward.put(place, money);
128+
maxplaces = Math.max(maxplaces, place);
127129
}
128130

129131
public void setCommandReward(String cmdreward, boolean isFirstCmd, int place) {
@@ -134,6 +136,8 @@ public void setCommandReward(String cmdreward, boolean isFirstCmd, int place) {
134136
Bukkit.getLogger().info("[TNTRun] reward(" + place + ") = " + commandrewards.toString());
135137
}
136138
commandrewards.computeIfAbsent(place, k -> new ArrayList<>()).add(cmdreward);
139+
maxplaces = Math.max(maxplaces, place);
140+
137141
if (Utils.debug()) {
138142
Bukkit.getLogger().info("[TNTRun] reward(" + place + ") = " + commandrewards.toString());
139143
}
@@ -145,6 +149,7 @@ public void setCommandRewards(List<String> cmdreward, int place) {
145149

146150
public void setXPReward(int xprwd, int place) {
147151
xpreward.put(place, xprwd);
152+
maxplaces = Math.max(maxplaces, place);
148153
}
149154

150155
public void deleteMaterialReward(int place) {
@@ -218,8 +223,9 @@ public void setStartingPlayers(int starters) {
218223
}
219224

220225
public void saveToConfig(FileConfiguration config) {
221-
index = 1;
222-
places.stream().forEach(path -> {
226+
path = "reward";
227+
IntStream.range(1, getMaxPlaces() + 1).forEach(index -> {
228+
223229
config.set(path + ".minPlayers", getMinPlayersRequired(index));
224230
config.set(path + ".money", getMoneyReward(index));
225231
config.set(path + ".xp", getXPReward(index));
@@ -229,14 +235,17 @@ public void saveToConfig(FileConfiguration config) {
229235
config.set(path + ".material." + is.getType().toString() + ".amount", is.getAmount());
230236
});
231237
}
232-
index++;
238+
path = "places." + (index + 1);
233239
});
234240
}
235241

236242
public void loadFromConfig(FileConfiguration config) {
237-
maxplaces = config.isConfigurationSection("places") ? config.getConfigurationSection("places").getKeys(false).size() : 0;
243+
if (!config.isConfigurationSection("reward")) {
244+
return;
245+
}
238246
index = 1;
239-
getRewardPlaces(config).stream().forEach(path -> {
247+
getPlacesFromFile(config).stream().forEach(path -> {
248+
240249
setMinPlayersRequired(config.getInt(path + ".minPlayers"), index);
241250
setMoneyReward(config.getInt(path + ".money"), index);
242251
setXPReward(config.getInt(path + ".xp"), index);
@@ -252,23 +261,32 @@ public void loadFromConfig(FileConfiguration config) {
252261
}
253262
index++;
254263
});
264+
265+
maxplaces = index - 1;
255266
}
256267

257-
private List<String> getRewardPlaces(FileConfiguration config) {
268+
private List<String> getPlacesFromFile(FileConfiguration config) {
269+
List<String> paths = new ArrayList<>(List.of("reward"));
270+
if (!config.isConfigurationSection("places")) {
271+
return paths;
272+
}
258273
for (String key : config.getConfigurationSection("places").getKeys(false)) {
259274
// temp code to rewrite config from v9.27
260275
if (key.equalsIgnoreCase("second")) {
261-
places.add("places.2");
276+
paths.add("places." + 2);
262277
continue;
263278
} else if (key.equalsIgnoreCase("third")) {
264-
places.add("places.3");
279+
paths.add("places." + 3);
265280
continue;
266281
}
267282

268-
places.add("places." + key);
283+
paths.add("places." + key);
284+
}
285+
if (Utils.debug()) {
286+
Bukkit.getLogger().info("[TNTRun] reward paths = " + paths.toString());
269287
}
270288

271-
return places;
289+
return paths;
272290
}
273291

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

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

286304
StringBuilder sb = new StringBuilder(200);
287305
if (getXPReward(i) != 0) {

src/main/java/tntrun/conversation/ArenaRewardConversation.java

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,43 +31,37 @@
3131
import tntrun.arena.Arena;
3232
import tntrun.utils.Utils;
3333

34-
public class ArenaRewardConversation extends FixedSetPrompt {
34+
public class ArenaRewardConversation extends NumericPrompt {
3535
private Arena arena;
3636
private boolean isFirstItem = true;
3737
private String podium;
3838
private int place;
3939
private static final String PREFIX = GRAY + "[" + GOLD + "TNTRun_reloaded" + GRAY + "] ";
4040

4141
public ArenaRewardConversation(Arena arena) {
42-
super("first", "second", "third");
4342
this.arena = arena;
4443
}
4544

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

5250
@Override
53-
protected Prompt acceptValidatedInput(ConversationContext context, String choice) {
54-
switch(choice.toLowerCase()) {
55-
case "first":
56-
podium = GOLD + "First " + GRAY + "place ";
57-
place = 1;
58-
break;
59-
case "second":
60-
podium = GOLD + "Second " + GRAY + "place ";
61-
place = 2;
62-
break;
63-
case "third":
64-
podium = GOLD + "Third " + GRAY + "place ";
65-
place = 3;
66-
break;
67-
default:
68-
place = 0;
69-
}
70-
return place != 0 ? new ChooseRewardType() : null;
51+
protected boolean isNumberValid(ConversationContext context, Number input) {
52+
return input.intValue() > 0 && input.intValue() < 100;
53+
}
54+
55+
@Override
56+
protected String getFailedValidationText(ConversationContext context, Number invalidInput) {
57+
return "Position must be between 1 and 99.";
58+
}
59+
60+
@Override
61+
protected Prompt acceptValidatedInput(ConversationContext context, Number position) {
62+
place = (int)position;
63+
podium = GRAY + "Position " + GOLD + place + GRAY + ": ";
64+
return new ChooseRewardType();
7165
}
7266

7367
private class ChooseRewardType extends FixedSetPrompt {

0 commit comments

Comments
 (0)