Use Things API for class items.

Swaps out the hard dependecy on ItemStack for class items (not armor), such that the ThingManager - and thus every ThingParser registered within - has a chance at providing "items" for a class.

Armor is not handled in this commit, because it appears to be quite a bit of extra work, and the "equippable" nature of armor needs to be handled differently than regular inventory items.

The getLogo() method on ArenaClass is removed, because it is no longer used (internally at least). It is residue from the old Spout support.
This commit is contained in:
Andreas Troelsen
2018-04-24 13:52:40 +02:00
parent 5d2881383f
commit 8014420c1b
2 changed files with 32 additions and 48 deletions
@@ -26,7 +26,8 @@ public class ArenaClass
{
private String configName, lowercaseName;
private ItemStack helmet, chestplate, leggings, boots, offhand;
private List<ItemStack> items, armor;
private List<ItemStack> armor;
private List<Thing> items;
private Map<String,Boolean> perms;
private Map<String,Boolean> lobbyperms;
private boolean unbreakableWeapons, unbreakableArmor;
@@ -68,18 +69,6 @@ public class ArenaClass
return lowercaseName;
}
/**
* Get the Material type of the first item in the items list.
* If the items list is empty, the method returns Material.STONE
* @return the type of the first item, or STONE if the list is empty
*/
public Material getLogo() {
if (items.isEmpty()) {
return Material.STONE;
}
return items.get(0).getType();
}
/**
* Set the helmet slot for the class.
* @param helmet an item
@@ -122,30 +111,22 @@ public class ArenaClass
/**
* Add an item to the items list.
* @param stack an item
* @param item a Thing
*/
public void addItem(ItemStack stack) {
if (stack == null) return;
if (stack.getAmount() > 64) {
while (stack.getAmount() > 64) {
items.add(new ItemStack(stack.getType(), 64));
stack.setAmount(stack.getAmount() - 64);
}
public void addItem(Thing item) {
if (item != null) {
items.add(item);
}
items.add(stack);
}
/**
* Replace the current items list with a new list of all the items in the given list.
* This method uses the addItem() method for each item to ensure consistency.
* @param stacks a list of items
* @param items a list of Things
*/
public void setItems(List<ItemStack> stacks) {
this.items = new ArrayList<>(stacks.size());
for (ItemStack stack : stacks) {
addItem(stack);
}
public void setItems(List<Thing> items) {
this.items = new ArrayList<>(items.size());
items.forEach(this::addItem);
}
/**
@@ -168,9 +149,7 @@ public class ArenaClass
PlayerInventory inv = p.getInventory();
// Fork over the items.
for (ItemStack stack : items) {
inv.addItem(stack);
}
items.forEach(item -> item.giveTo(p));
// Check for legacy armor-node items
if (!armor.isEmpty()) {
@@ -24,12 +24,15 @@ import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.PluginManager;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
public class ArenaMasterImpl implements ArenaMaster
{
@@ -324,22 +327,8 @@ public class ArenaMasterImpl implements ArenaMaster
? new ArenaClass.MyItems(price, weps, arms, this)
: new ArenaClass(classname, price, weps, arms);
// Parse the items-node
List<String> items = section.getStringList("items");
if (items == null || items.isEmpty()) {
String str = section.getString("items", "");
List<ItemStack> stacks = ItemParser.parseItems(str);
arenaClass.setItems(stacks);
} else {
List<ItemStack> stacks = new ArrayList<>();
for (String item : items) {
ItemStack stack = ItemParser.parseItem(item);
if (stack != null) {
stacks.add(stack);
}
}
arenaClass.setItems(stacks);
}
// Load items
loadClassItems(section, arenaClass);
// And the legacy armor-node
String armor = section.getString("armor", "");
@@ -385,6 +374,22 @@ public class ArenaMasterImpl implements ArenaMaster
return arenaClass;
}
private void loadClassItems(ConfigurationSection section, ArenaClass arenaClass) {
List<String> items = section.getStringList("items");
if (items == null || items.isEmpty()) {
String value = section.getString("items", "");
items = Arrays.asList(value.split(","));
}
List<Thing> things = items.stream()
.map(String::trim)
.map(plugin.getThingManager()::parse)
.filter(Objects::nonNull)
.collect(Collectors.toList());
arenaClass.setItems(things);
}
private void loadClassPermissions(ArenaClass arenaClass, ConfigurationSection section) {
List<String> perms = section.getStringList("permissions");
if (perms.isEmpty()) return;