Use Things API in upgrade waves.

This changes the upgrade waves from the local Upgrade interface to the Thing interface. This means that all Things can now be used in upgrade waves, including commands, money, potion effects, and permissions.

Unfortunately, the weapons upgrade/replace functionality has been gutted as a result of the Things API not supporting it. We could add it back in later down the road, but for now let's just see if it causes anyone any inconvenience.

Closes #468
This commit is contained in:
Andreas Troelsen 2018-06-25 00:09:31 +02:00
parent a9d0ca0828
commit 0f34e1fdb7
2 changed files with 50 additions and 171 deletions

View File

@ -1,9 +1,9 @@
package com.garbagemule.MobArena.waves; package com.garbagemule.MobArena.waves;
import com.garbagemule.MobArena.ArenaClass;
import com.garbagemule.MobArena.framework.Arena; import com.garbagemule.MobArena.framework.Arena;
import com.garbagemule.MobArena.region.ArenaRegion; import com.garbagemule.MobArena.region.ArenaRegion;
import com.garbagemule.MobArena.things.Thing; import com.garbagemule.MobArena.things.Thing;
import com.garbagemule.MobArena.things.ThingManager;
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.waves.ability.Ability; import com.garbagemule.MobArena.waves.ability.Ability;
@ -20,18 +20,13 @@ import com.garbagemule.MobArena.waves.types.SpecialWave;
import com.garbagemule.MobArena.waves.types.SupplyWave; import com.garbagemule.MobArena.waves.types.SupplyWave;
import com.garbagemule.MobArena.waves.types.SwarmWave; import com.garbagemule.MobArena.waves.types.SwarmWave;
import com.garbagemule.MobArena.waves.types.UpgradeWave; import com.garbagemule.MobArena.waves.types.UpgradeWave;
import com.garbagemule.MobArena.waves.types.UpgradeWave.ArmorUpgrade;
import com.garbagemule.MobArena.waves.types.UpgradeWave.GenericUpgrade;
import com.garbagemule.MobArena.waves.types.UpgradeWave.PermissionUpgrade;
import com.garbagemule.MobArena.waves.types.UpgradeWave.Upgrade;
import com.garbagemule.MobArena.waves.types.UpgradeWave.WeaponUpgrade;
import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -242,7 +237,8 @@ public class WaveParser
} }
private static Wave parseUpgradeWave(Arena arena, String name, ConfigurationSection config) { private static Wave parseUpgradeWave(Arena arena, String name, ConfigurationSection config) {
Map<String,List<Upgrade>> upgrades = getUpgradeMap(config); ThingManager thingman = arena.getPlugin().getThingManager();
Map<String,List<Thing>> upgrades = getUpgradeMap(config, thingman);
if (upgrades == null || upgrades.isEmpty()) { if (upgrades == null || upgrades.isEmpty()) {
arena.getPlugin().getLogger().warning(WaveError.UPGRADE_MAP_MISSING.format(name, arena.configName())); arena.getPlugin().getLogger().warning(WaveError.UPGRADE_MAP_MISSING.format(name, arena.configName()));
return null; return null;
@ -435,7 +431,7 @@ public class WaveParser
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
private static Map<String,List<Upgrade>> getUpgradeMap(ConfigurationSection config) { private static Map<String,List<Thing>> getUpgradeMap(ConfigurationSection config, ThingManager thingman) {
ConfigurationSection section = config.getConfigurationSection("upgrades"); ConfigurationSection section = config.getConfigurationSection("upgrades");
if (section == null) { if (section == null) {
return null; return null;
@ -446,50 +442,59 @@ public class WaveParser
return null; return null;
} }
Map<String,List<Upgrade>> upgrades = new HashMap<>(); Map<String,List<Thing>> upgrades = new HashMap<>();
String path = "upgrades."; String path = "upgrades.";
for (String className : classes) { for (String className : classes) {
String itemList;
// 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) {
itemList = (String) val; String s = (String) val;
List<ItemStack> stacks = ItemParser.parseItems(itemList); List<Thing> things = Arrays.asList(s.split(",")).stream()
List<Upgrade> list = new ArrayList<>(); .map(String::trim)
for (ItemStack stack : stacks) { .map(thingman::parse)
list.add(new GenericUpgrade(stack)); .filter(Objects::nonNull)
} .collect(Collectors.toList());
upgrades.put(className.toLowerCase(), list); upgrades.put(className.toLowerCase(), things);
} }
// New complex setup // New complex setup
else if (val instanceof ConfigurationSection) { else if (val instanceof ConfigurationSection) {
ConfigurationSection classSection = (ConfigurationSection) val; ConfigurationSection classSection = (ConfigurationSection) val;
List<Upgrade> list = new ArrayList<>(); List<Thing> list = new ArrayList<>();
// Items (Generic + Weapons) // Items
itemList = classSection.getString("items", null); List<String> items = classSection.getStringList("items");
if (itemList != null) { if (items == null || items.isEmpty()) {
for (ItemStack stack : ItemParser.parseItems(itemList)) { String value = classSection.getString("items", "");
list.add(ArenaClass.isWeapon(stack) ? new WeaponUpgrade(stack) : new GenericUpgrade(stack)); items = Arrays.asList(value.split(","));
}
} }
items.stream()
.map(String::trim)
.map(thingman::parse)
.filter(Objects::nonNull)
.forEach(list::add);
// Armor // Armor
itemList = classSection.getString("armor", null); List<String> armor = classSection.getStringList("armor");
if (itemList != null) { if (armor == null || armor.isEmpty()) {
for (ItemStack stack : ItemParser.parseItems(itemList)) { String value = classSection.getString("armor", "");
list.add(new ArmorUpgrade(stack)); armor = Arrays.asList(value.split(","));
}
} }
// Permissions // Prepend "armor:" for the armor thing parser
List<String> perms = classSection.getStringList("permissions"); armor.stream()
if (!perms.isEmpty()) { .map(String::trim)
for (String perm : perms) { .map(s -> "armor:" + s)
list.add(new PermissionUpgrade(perm)); .map(thingman::parse)
} .filter(Objects::nonNull)
} .forEach(list::add);
// Prepend "perm:" for the permission thing parser
classSection.getStringList("permissions").stream()
.map(perm -> "perm:" + perm)
.map(thingman::parse)
.filter(Objects::nonNull)
.forEach(list::add);
// Put in the map // Put in the map
upgrades.put(className.toLowerCase(), list); upgrades.put(className.toLowerCase(), list);

View File

@ -1,14 +1,12 @@
package com.garbagemule.MobArena.waves.types; package com.garbagemule.MobArena.waves.types;
import com.garbagemule.MobArena.ArenaClass.ArmorType;
import com.garbagemule.MobArena.framework.Arena; import com.garbagemule.MobArena.framework.Arena;
import com.garbagemule.MobArena.things.Thing;
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;
import com.garbagemule.MobArena.waves.enums.WaveType; import com.garbagemule.MobArena.waves.enums.WaveType;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.permissions.PermissionAttachment;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@ -18,10 +16,10 @@ import java.util.Random;
public class UpgradeWave extends AbstractWave public class UpgradeWave extends AbstractWave
{ {
private Map<String,List<Upgrade>> upgrades; private Map<String,List<Thing>> upgrades;
private boolean giveAll; private boolean giveAll;
public UpgradeWave(Map<String,List<Upgrade>> upgrades) { public UpgradeWave(Map<String,List<Thing>> upgrades) {
this.upgrades = upgrades; this.upgrades = upgrades;
this.setType(WaveType.UPGRADE); this.setType(WaveType.UPGRADE);
} }
@ -32,16 +30,14 @@ public class UpgradeWave extends AbstractWave
} }
public void grantItems(Arena arena, Player p, String className) { public void grantItems(Arena arena, Player p, String className) {
List<Upgrade> list = upgrades.get(className); List<Thing> list = upgrades.get(className);
if (list == null) return; if (list == null) return;
if (giveAll) { if (giveAll) {
for (Upgrade upgrade : list) { list.forEach(thing -> thing.giveTo(p));
upgrade.upgrade(arena, p);
}
} else { } else {
int index = new Random().nextInt(list.size()); int index = new Random().nextInt(list.size());
list.get(index).upgrade(arena, p); list.get(index).giveTo(p);
} }
} }
@ -50,8 +46,8 @@ public class UpgradeWave extends AbstractWave
} }
public Wave copy() { public Wave copy() {
Map<String,List<Upgrade>> upgrades = new HashMap<>(); Map<String,List<Thing>> upgrades = new HashMap<>();
for (Map.Entry<String,List<Upgrade>> entry : this.upgrades.entrySet()) { for (Map.Entry<String,List<Thing>> 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);
@ -65,126 +61,4 @@ public class UpgradeWave extends AbstractWave
result.setEffects(getEffects()); result.setEffects(getEffects());
return result; return result;
} }
/**
* Represents an upgrade for an upgrade wave
*/
public interface Upgrade {
void upgrade(Arena arena, Player p);
}
/**
* Armor upgrades
* Replace the item in the specific slot
*/
public static class ArmorUpgrade implements Upgrade {
private ItemStack item;
private ArmorType type;
public ArmorUpgrade(ItemStack item) {
this.item = item;
this.type = ArmorType.getType(item);
}
@Override
public void upgrade(Arena arena, Player p) {
if (item == null || type == null) return;
switch (type) {
case HELMET: p.getInventory().setHelmet(item); break;
case CHESTPLATE: p.getInventory().setChestplate(item); break;
case LEGGINGS: p.getInventory().setLeggings(item); break;
case BOOTS: p.getInventory().setBoots(item); break;
}
}
}
/**
* Weapon upgrades
* Replace the first item that matches the ID on the quickbar
*/
public static class WeaponUpgrade implements Upgrade {
private ItemStack item;
public WeaponUpgrade(ItemStack item) {
this.item = item;
}
@Override
public void upgrade(Arena arena, Player p) {
if (item == null) return;
ItemStack[] items = p.getInventory().getContents();
int firstEmpty = -1;
// Find a matching ID and upgrade it
for (int i = 0; i < 9; i++) {
// Save the first null index
if (items[i] == null) {
if (firstEmpty < 0) firstEmpty = i;
continue;
}
// If we find an ID, upgrade and quit
if (items[i].getTypeId() == item.getTypeId()) {
items[i] = item;
p.getInventory().setContents(items);
return;
}
}
// If nothing was found, just give them a new weapon
if (firstEmpty > 0) {
items[firstEmpty] = item;
p.getInventory().setContents(items);
} else {
p.getInventory().addItem(item);
}
}
}
/**
* Generic upgrades
* Add the item to the player's inventory
*/
public static class GenericUpgrade implements Upgrade {
private ItemStack item;
public GenericUpgrade(ItemStack item) {
this.item = item;
}
@Override
public void upgrade(Arena arena, Player p) {
if (item == null) return;
p.getInventory().addItem(item);
}
}
/**
* Permission upgrades
* Set the given permission
*/
public static class PermissionUpgrade implements Upgrade {
private String perm;
private boolean value;
public PermissionUpgrade(String perm) {
if (perm.startsWith("-") || perm.startsWith("^")) {
perm = perm.substring(1).trim();
value = false;
} else {
value = true;
}
this.perm = perm;
}
@Override
public void upgrade(Arena arena, Player p) {
if (perm == null) return;
PermissionAttachment attachment = p.addAttachment(arena.getPlugin());
attachment.setPermission(perm, value);
}
}
} }