static Pattern fromPattern = Pattern.compile("(\\[)(.*)(\\])");
public dInventory getInventoryFrom(dPlayer player, dNPC npc) {
dInventory inventory = null;
try {
if (contains("INVENTORY")) {
if (InventoryType.valueOf(getString("INVENTORY").toUpperCase()) != null) {
inventory = new dInventory(InventoryType.valueOf(getString("INVENTORY").toUpperCase()));
inventory.setIdentifiers("script", getName());
}
else {
dB.echoError("Invalid inventory type specified. Assuming \"CHEST\"");
}
}
if (contains("SIZE")) {
if (inventory != null && !getInventoryType().name().equalsIgnoreCase("CHEST")) {
dB.echoError("You can only set the size of chest inventories!");
}
else {
int size = aH.getIntegerFrom(getString("SIZE"));
if (size == 0) {
size = 27;
dB.echoError("Inventory size can't be 0. Assuming default of 27...");
}
if (size % 9 != 0) {
size = (int) Math.ceil(size/9)*9;
dB.echoError("Inventory size must be a multiple of 9! Rounding up to " + size + "...");
}
if (size < 0) {
size = size*-1;
dB.echoError("Inventory size must be a positive number! Inverting to " + size + "...");
}
inventory = new dInventory(size,
contains("TITLE") ? TagManager.tag(player, npc, getString("TITLE")) : "Chest");
inventory.setIdentifiers("script", getName());
}
}
if (contains("SLOTS")) {
ItemStack[] finalItems = new ItemStack[getSize()];
int itemsAdded = 0;
for (String items : getStringList("SLOTS")) {
items = TagManager.tag(player, npc, items);
String[] itemsInLine = items.split(" ");
for (String item : itemsInLine) {
Matcher m = fromPattern.matcher(item);
if (!m.matches()) {
dB.echoError("Inventory script \"" + getName() + "\" has an invalid slot item.");
return null;
}
if (contains("DEFINITIONS." + m.group(2)) &&
dItem.matches(getString("DEFINITIONS." + m.group(2)))) {
finalItems[itemsAdded] = dItem.valueOf(TagManager.tag
(player, npc, getString("DEFINITIONS." + m.group(2))))
.getItemStack();
}
else if (dItem.matches(m.group(2))) {
finalItems[itemsAdded] = dItem.valueOf(TagManager.tag(player, npc, m.group(2)))
.getItemStack();
}
else {
finalItems[itemsAdded] = new ItemStack(Material.AIR);
if (!m.group(2).trim().isEmpty()) {
dB.echoError("Inventory script \"" + getName() + "\" has an invalid slot item: ["
+ m.group(2) + "]... Ignoring it and assuming \"AIR\"");
}
}
itemsAdded++;
}
}
if (inventory == null) {
int size = finalItems.length%9==0?finalItems.length:Math.round(finalItems.length/9)*9;
inventory = new dInventory(size==0?9:size,
contains("TITLE") ? TagManager.tag(player, npc, getString("TITLE")) : "Chest");
}
inventory.setContents(finalItems);
}
}
catch (Exception e) {
dB.echoError("Woah! An exception has been called with this inventory script!");
dB.echoError(e);
inventory = null;
}
if (inventory != null)
InventoryScriptHelper.tempInventoryScripts.put(inventory.getInventory(), getName());
return inventory;
}