Search in sources :

Example 1 with BoundedItemModifier

use of pl.themolka.arcade.attribute.BoundedItemModifier in project Arcade2 by ShootGame.

the class ItemStackParser method parseTree.

@Override
protected ParserResult<ItemStack> parseTree(Node node, String name) throws ParserException {
    Property amountProperty = node.property("amount", "total");
    Property durabilityProperty = node.property("durability");
    MaterialData type = this.typeParser.parse(node.property(MaterialParser.MATERIAL_ELEMENT_NAMES)).orFail();
    int amount = this.amountParser.parse(amountProperty).orDefault(1);
    String displayName = this.displayNameParser.parse(node.firstChild("name", "display-name")).orDefaultNull();
    List<String> description = this.parseDescription(node);
    short durability = this.durabilityParser.parse(durabilityProperty).orDefault((short) 0);
    List<ItemEnchantment> enchantments = this.parseEnchantments(node);
    List<Material> canDestroy = this.parseDestroy(node);
    List<Material> canPlaceOn = this.parseCanPlaceOn(node);
    boolean unbreakable = this.unbreakableParser.parse(node.property("unbreakable", "permanent")).orDefault(false);
    List<ItemFlag> flags = this.parseFlags(node);
    if (amount <= 0) {
        throw this.fail(amountProperty, "Amount must be positive (greater than 0)");
    } else if (type.getData() != 0 && durability != 0) {
        // Notch made a huge mistake here... :(
        throw this.fail(durabilityProperty, "Sorry! Durability cannot be combined with material data!");
    }
    ItemStack itemStack = new ItemStack(type.getItemType());
    itemStack.setData(type);
    itemStack.setAmount(amount);
    itemStack.setDurability(durability);
    for (ItemEnchantment enchantment : enchantments) {
        enchantment.apply(itemStack);
    }
    ItemMeta itemMeta = itemStack.getItemMeta();
    if (displayName != null) {
        itemMeta.setDisplayName(displayName);
    }
    if (!description.isEmpty()) {
        itemMeta.setLore(description);
    }
    if (!canDestroy.isEmpty()) {
        itemMeta.setCanDestroy(canDestroy);
    }
    if (!canPlaceOn.isEmpty()) {
        itemMeta.setCanPlaceOn(canPlaceOn);
    }
    for (Node modifierNode : node.children("modifier", "attribute-modifier", "attributemodifier", "attribute")) {
        BoundedItemModifier modifier = this.modifierParser.parse(modifierNode).orFail();
        itemMeta.addAttributeModifier(modifier.getKey().key(), modifier.getItemModifier());
    }
    itemMeta.setUnbreakable(unbreakable);
    itemMeta.addItemFlags(flags.toArray(new ItemFlag[flags.size()]));
    itemStack.setItemMeta(this.itemMetaParser.parse(node, itemStack, itemMeta));
    return ParserResult.fine(node, name, itemStack);
}
Also used : Node(pl.themolka.arcade.dom.Node) Material(org.bukkit.Material) BoundedItemModifier(pl.themolka.arcade.attribute.BoundedItemModifier) MaterialData(org.bukkit.material.MaterialData) ItemFlag(org.bukkit.inventory.ItemFlag) ItemStack(org.bukkit.inventory.ItemStack) Property(pl.themolka.arcade.dom.Property) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Aggregations

Material (org.bukkit.Material)1 ItemFlag (org.bukkit.inventory.ItemFlag)1 ItemStack (org.bukkit.inventory.ItemStack)1 ItemMeta (org.bukkit.inventory.meta.ItemMeta)1 MaterialData (org.bukkit.material.MaterialData)1 BoundedItemModifier (pl.themolka.arcade.attribute.BoundedItemModifier)1 Node (pl.themolka.arcade.dom.Node)1 Property (pl.themolka.arcade.dom.Property)1