(wip) Use ThingPicker in upgrade waves.

Decent start, but maybe explore the idea of a "context"?
This commit is contained in:
Andreas Troelsen 2024-07-23 21:55:50 +02:00
parent b7aedf6c8b
commit 7bb8aa64dc
2 changed files with 26 additions and 22 deletions

View File

@ -8,6 +8,7 @@ import com.garbagemule.MobArena.things.InvalidThingInputString;
import com.garbagemule.MobArena.things.Thing; import com.garbagemule.MobArena.things.Thing;
import com.garbagemule.MobArena.things.ThingManager; import com.garbagemule.MobArena.things.ThingManager;
import com.garbagemule.MobArena.things.ThingPicker; import com.garbagemule.MobArena.things.ThingPicker;
import com.garbagemule.MobArena.things.ThingPickerManager;
import com.garbagemule.MobArena.util.ItemParser; import com.garbagemule.MobArena.util.ItemParser;
import com.garbagemule.MobArena.util.PotionEffectParser; import com.garbagemule.MobArena.util.PotionEffectParser;
import com.garbagemule.MobArena.util.Slugs; import com.garbagemule.MobArena.util.Slugs;
@ -252,7 +253,8 @@ public class WaveParser
private static Wave parseUpgradeWave(Arena arena, String name, ConfigurationSection config) { private static Wave parseUpgradeWave(Arena arena, String name, ConfigurationSection config) {
ThingManager thingman = arena.getPlugin().getThingManager(); ThingManager thingman = arena.getPlugin().getThingManager();
Map<String,List<Thing>> upgrades = getUpgradeMap(config, name, arena, thingman); ThingPickerManager pickman = arena.getPlugin().getThingPickerManager();
Map<String,List<ThingPicker>> upgrades = getUpgradeMap(config, name, arena, pickman, thingman);
return new UpgradeWave(upgrades); return new UpgradeWave(upgrades);
} }
@ -463,7 +465,7 @@ public class WaveParser
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
private static Map<String,List<Thing>> getUpgradeMap(ConfigurationSection config, String name, Arena arena, ThingManager thingman) { private static Map<String,List<ThingPicker>> getUpgradeMap(ConfigurationSection config, String name, Arena arena, ThingPickerManager pickman, ThingManager thingman) {
ConfigurationSection section = config.getConfigurationSection("upgrades"); ConfigurationSection section = config.getConfigurationSection("upgrades");
if (section == null) { if (section == null) {
throw new ConfigError("Missing 'upgrades' node for wave " + name + " of arena " + arena.configName()); throw new ConfigError("Missing 'upgrades' node for wave " + name + " of arena " + arena.configName());
@ -474,7 +476,7 @@ public class WaveParser
throw new ConfigError("Empty 'upgrades' node for wave " + name + " of arena " + arena.configName()); throw new ConfigError("Empty 'upgrades' node for wave " + name + " of arena " + arena.configName());
} }
Map<String,List<Thing>> upgrades = new HashMap<>(); Map<String,List<ThingPicker>> upgrades = new HashMap<>();
String path = "upgrades."; String path = "upgrades.";
for (String className : classes) { for (String className : classes) {
@ -483,12 +485,12 @@ public class WaveParser
// Legacy support // Legacy support
Object val = config.get(path + className, null); Object val = config.get(path + className, null);
if (val instanceof String) { if (val instanceof String) {
List<Thing> things = loadUpgradesFromString(className, (String) val, name, arena, thingman); List<ThingPicker> things = loadUpgradesFromString(className, (String) val, name, arena, pickman);
upgrades.put(slug, things); upgrades.put(slug, things);
} }
// New complex setup // New complex setup
else if (val instanceof ConfigurationSection) { else if (val instanceof ConfigurationSection) {
List<Thing> list = loadUpgradesFromSection(className, (ConfigurationSection) val, name, arena, thingman); List<ThingPicker> list = loadUpgradesFromSection(className, (ConfigurationSection) val, name, arena, pickman, thingman);
upgrades.put(slug, list); upgrades.put(slug, list);
} }
} }
@ -496,22 +498,20 @@ public class WaveParser
return upgrades; return upgrades;
} }
private static List<Thing> loadUpgradesFromString(String className, String value, String name, Arena arena, ThingManager thingman) { private static List<ThingPicker> loadUpgradesFromString(String className, String value, String name, Arena arena, ThingPickerManager pickman) {
if (value == null || value.isEmpty()) { if (value == null || value.isEmpty()) {
return Collections.emptyList(); return Collections.emptyList();
} }
try { try {
return Arrays.stream(value.split(",")) ThingPicker picker = pickman.parse("all(" + value + ")");
.map(String::trim) return Collections.singletonList(picker);
.map(thingman::parse)
.collect(Collectors.toList());
} catch (InvalidThingInputString e) { } catch (InvalidThingInputString e) {
throw new ConfigError("Failed to parse upgrades for class " + className + " in wave " + name + " of arena " + arena.configName() + ": " + e.getInput()); throw new ConfigError("Failed to parse upgrades for class " + className + " in wave " + name + " of arena " + arena.configName() + ": " + e.getInput());
} }
} }
private static List<Thing> loadUpgradesFromSection(String className, ConfigurationSection classSection, String name, Arena arena, ThingManager thingman) { private static List<ThingPicker> loadUpgradesFromSection(String className, ConfigurationSection classSection, String name, Arena arena, ThingPickerManager pickman, ThingManager thingman) {
List<Thing> list = new ArrayList<>(); List<ThingPicker> list = new ArrayList<>();
// Items // Items
List<String> items = classSection.getStringList("items"); List<String> items = classSection.getStringList("items");
@ -520,13 +520,13 @@ public class WaveParser
if (value == null || value.isEmpty()) { if (value == null || value.isEmpty()) {
items = Collections.emptyList(); items = Collections.emptyList();
} else { } else {
items = Arrays.asList(value.split(",")); items = Collections.singletonList("all(" + value + ")");
} }
} }
for (String value : items) { for (String value : items) {
try { try {
Thing thing = thingman.parse(value.trim()); ThingPicker picker = pickman.parse(value.trim());
list.add(thing); list.add(picker);
} catch (InvalidThingInputString e) { } catch (InvalidThingInputString e) {
throw new ConfigError("Failed to parse item upgrade for class " + className + " in wave " + name + " of arena " + arena.configName() + ": " + value.trim()); throw new ConfigError("Failed to parse item upgrade for class " + className + " in wave " + name + " of arena " + arena.configName() + ": " + value.trim());
} }

View File

@ -1,7 +1,7 @@
package com.garbagemule.MobArena.waves.types; package com.garbagemule.MobArena.waves.types;
import com.garbagemule.MobArena.framework.Arena; import com.garbagemule.MobArena.framework.Arena;
import com.garbagemule.MobArena.things.Thing; import com.garbagemule.MobArena.things.ThingPicker;
import com.garbagemule.MobArena.waves.AbstractWave; import com.garbagemule.MobArena.waves.AbstractWave;
import com.garbagemule.MobArena.waves.MACreature; import com.garbagemule.MobArena.waves.MACreature;
import com.garbagemule.MobArena.waves.Wave; import com.garbagemule.MobArena.waves.Wave;
@ -12,12 +12,13 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
public class UpgradeWave extends AbstractWave public class UpgradeWave extends AbstractWave
{ {
private Map<String,List<Thing>> upgrades; private Map<String,List<ThingPicker>> upgrades;
public UpgradeWave(Map<String,List<Thing>> upgrades) { public UpgradeWave(Map<String,List<ThingPicker>> upgrades) {
this.upgrades = upgrades; this.upgrades = upgrades;
this.setType(WaveType.UPGRADE); this.setType(WaveType.UPGRADE);
} }
@ -28,15 +29,18 @@ public class UpgradeWave extends AbstractWave
} }
public void grantItems(Player p, String slug) { public void grantItems(Player p, String slug) {
List<Thing> list = upgrades.get(slug); List<ThingPicker> list = upgrades.get(slug);
if (list == null) return; if (list == null) return;
list.forEach(thing -> thing.giveTo(p)); list.stream()
.map(ThingPicker::pick)
.filter(Objects::nonNull)
.forEach(thing -> thing.giveTo(p));
} }
public Wave copy() { public Wave copy() {
Map<String,List<Thing>> upgrades = new HashMap<>(); Map<String,List<ThingPicker>> upgrades = new HashMap<>();
for (Map.Entry<String,List<Thing>> entry : this.upgrades.entrySet()) { for (Map.Entry<String,List<ThingPicker>> entry : this.upgrades.entrySet()) {
upgrades.put(entry.getKey(), new ArrayList<>(entry.getValue())); upgrades.put(entry.getKey(), new ArrayList<>(entry.getValue()));
} }
UpgradeWave result = new UpgradeWave(upgrades); UpgradeWave result = new UpgradeWave(upgrades);