Skip to content

Commit

Permalink
(wip) Use ThingPicker in upgrade waves.
Browse files Browse the repository at this point in the history
Decent start, but maybe explore the idea of a "context"?
  • Loading branch information
garbagemule committed Jul 23, 2024
1 parent d54e559 commit b66f54a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
30 changes: 15 additions & 15 deletions src/main/java/com/garbagemule/MobArena/waves/WaveParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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;
Expand Down Expand Up @@ -252,7 +253,8 @@ private static Wave parseSupplyWave(Arena arena, String name, ConfigurationSecti

private static Wave parseUpgradeWave(Arena arena, String name, ConfigurationSection config) {
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);
}
Expand Down Expand Up @@ -463,7 +465,7 @@ private static List<PotionEffect> getPotionEffects(Arena arena, String name, Con
.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");
if (section == null) {
throw new ConfigError("Missing 'upgrades' node for wave " + name + " of arena " + arena.configName());
Expand All @@ -474,7 +476,7 @@ private static Map<String,List<Thing>> getUpgradeMap(ConfigurationSection config
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.";

for (String className : classes) {
Expand All @@ -483,35 +485,33 @@ private static Map<String,List<Thing>> getUpgradeMap(ConfigurationSection config
// Legacy support
Object val = config.get(path + className, null);
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);
}
// New complex setup
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);
}
}

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()) {
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<Thing> loadUpgradesFromSection(String className, ConfigurationSection classSection, String name, Arena arena, ThingManager thingman) {
List<Thing> list = new ArrayList<>();
private static List<ThingPicker> loadUpgradesFromSection(String className, ConfigurationSection classSection, String name, Arena arena, ThingPickerManager pickman, ThingManager thingman) {
List<ThingPicker> list = new ArrayList<>();

// Items
List<String> items = classSection.getStringList("items");
Expand All @@ -520,13 +520,13 @@ private static List<Thing> loadUpgradesFromSection(String className, Configurati
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());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,12 +12,13 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

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.setType(WaveType.UPGRADE);
}
Expand All @@ -28,15 +29,18 @@ public Map<MACreature,Integer> getMonstersToSpawn(int wave, int playerCount, Are
}

public void grantItems(Player p, String slug) {
List<Thing> list = upgrades.get(slug);
List<ThingPicker> 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<String,List<Thing>> upgrades = new HashMap<>();
for (Map.Entry<String,List<Thing>> entry : this.upgrades.entrySet()) {
Map<String,List<ThingPicker>> upgrades = new HashMap<>();
for (Map.Entry<String,List<ThingPicker>> entry : this.upgrades.entrySet()) {
upgrades.put(entry.getKey(), new ArrayList<>(entry.getValue()));
}
UpgradeWave result = new UpgradeWave(upgrades);
Expand Down

0 comments on commit b66f54a

Please sign in to comment.