From 7bb8aa64dc472e949d6f4ec47c32fbe128d0d541 Mon Sep 17 00:00:00 2001 From: Andreas Troelsen Date: Tue, 23 Jul 2024 21:55:50 +0200 Subject: [PATCH] (wip) Use ThingPicker in upgrade waves. Decent start, but maybe explore the idea of a "context"? --- .../MobArena/waves/WaveParser.java | 30 +++++++++---------- .../MobArena/waves/types/UpgradeWave.java | 18 ++++++----- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/garbagemule/MobArena/waves/WaveParser.java b/src/main/java/com/garbagemule/MobArena/waves/WaveParser.java index 0204652..484a87d 100644 --- a/src/main/java/com/garbagemule/MobArena/waves/WaveParser.java +++ b/src/main/java/com/garbagemule/MobArena/waves/WaveParser.java @@ -8,6 +8,7 @@ import com.garbagemule.MobArena.things.InvalidThingInputString; import com.garbagemule.MobArena.things.Thing; import com.garbagemule.MobArena.things.ThingManager; import com.garbagemule.MobArena.things.ThingPicker; +import com.garbagemule.MobArena.things.ThingPickerManager; import com.garbagemule.MobArena.util.ItemParser; import com.garbagemule.MobArena.util.PotionEffectParser; import com.garbagemule.MobArena.util.Slugs; @@ -252,7 +253,8 @@ public class WaveParser private static Wave parseUpgradeWave(Arena arena, String name, ConfigurationSection config) { ThingManager thingman = arena.getPlugin().getThingManager(); - Map> upgrades = getUpgradeMap(config, name, arena, thingman); + ThingPickerManager pickman = arena.getPlugin().getThingPickerManager(); + Map> upgrades = getUpgradeMap(config, name, arena, pickman, thingman); return new UpgradeWave(upgrades); } @@ -463,7 +465,7 @@ public class WaveParser .collect(Collectors.toList()); } - private static Map> getUpgradeMap(ConfigurationSection config, String name, Arena arena, ThingManager thingman) { + private static Map> getUpgradeMap(ConfigurationSection config, String name, Arena arena, ThingPickerManager pickman, ThingManager thingman) { ConfigurationSection section = config.getConfigurationSection("upgrades"); if (section == null) { 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()); } - Map> upgrades = new HashMap<>(); + Map> upgrades = new HashMap<>(); String path = "upgrades."; for (String className : classes) { @@ -483,12 +485,12 @@ public class WaveParser // Legacy support Object val = config.get(path + className, null); if (val instanceof String) { - List things = loadUpgradesFromString(className, (String) val, name, arena, thingman); + List things = loadUpgradesFromString(className, (String) val, name, arena, pickman); upgrades.put(slug, things); } // New complex setup else if (val instanceof ConfigurationSection) { - List list = loadUpgradesFromSection(className, (ConfigurationSection) val, name, arena, thingman); + List list = loadUpgradesFromSection(className, (ConfigurationSection) val, name, arena, pickman, thingman); upgrades.put(slug, list); } } @@ -496,22 +498,20 @@ public class WaveParser return upgrades; } - private static List loadUpgradesFromString(String className, String value, String name, Arena arena, ThingManager thingman) { + private static List loadUpgradesFromString(String className, String value, String name, Arena arena, ThingPickerManager pickman) { if (value == null || value.isEmpty()) { return Collections.emptyList(); } try { - return Arrays.stream(value.split(",")) - .map(String::trim) - .map(thingman::parse) - .collect(Collectors.toList()); + ThingPicker picker = pickman.parse("all(" + value + ")"); + return Collections.singletonList(picker); } catch (InvalidThingInputString e) { throw new ConfigError("Failed to parse upgrades for class " + className + " in wave " + name + " of arena " + arena.configName() + ": " + e.getInput()); } } - private static List loadUpgradesFromSection(String className, ConfigurationSection classSection, String name, Arena arena, ThingManager thingman) { - List list = new ArrayList<>(); + private static List loadUpgradesFromSection(String className, ConfigurationSection classSection, String name, Arena arena, ThingPickerManager pickman, ThingManager thingman) { + List list = new ArrayList<>(); // Items List items = classSection.getStringList("items"); @@ -520,13 +520,13 @@ public class WaveParser if (value == null || value.isEmpty()) { items = Collections.emptyList(); } else { - items = Arrays.asList(value.split(",")); + items = Collections.singletonList("all(" + value + ")"); } } for (String value : items) { try { - Thing thing = thingman.parse(value.trim()); - list.add(thing); + ThingPicker picker = pickman.parse(value.trim()); + list.add(picker); } catch (InvalidThingInputString e) { throw new ConfigError("Failed to parse item upgrade for class " + className + " in wave " + name + " of arena " + arena.configName() + ": " + value.trim()); } diff --git a/src/main/java/com/garbagemule/MobArena/waves/types/UpgradeWave.java b/src/main/java/com/garbagemule/MobArena/waves/types/UpgradeWave.java index 3d6104e..154fee0 100644 --- a/src/main/java/com/garbagemule/MobArena/waves/types/UpgradeWave.java +++ b/src/main/java/com/garbagemule/MobArena/waves/types/UpgradeWave.java @@ -1,7 +1,7 @@ package com.garbagemule.MobArena.waves.types; 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.MACreature; import com.garbagemule.MobArena.waves.Wave; @@ -12,12 +12,13 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; public class UpgradeWave extends AbstractWave { - private Map> upgrades; + private Map> upgrades; - public UpgradeWave(Map> upgrades) { + public UpgradeWave(Map> upgrades) { this.upgrades = upgrades; this.setType(WaveType.UPGRADE); } @@ -28,15 +29,18 @@ public class UpgradeWave extends AbstractWave } public void grantItems(Player p, String slug) { - List list = upgrades.get(slug); + List list = upgrades.get(slug); 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() { - Map> upgrades = new HashMap<>(); - for (Map.Entry> entry : this.upgrades.entrySet()) { + Map> upgrades = new HashMap<>(); + for (Map.Entry> entry : this.upgrades.entrySet()) { upgrades.put(entry.getKey(), new ArrayList<>(entry.getValue())); } UpgradeWave result = new UpgradeWave(upgrades);