Search in sources :

Example 31 with ItemTag

use of com.denizenscript.denizen.objects.ItemTag in project Denizen-For-Bukkit by DenizenScript.

the class GiveCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    ElementTag unlimit_stack_size = scriptEntry.getElement("unlimit_stack_size");
    InventoryTag inventory = scriptEntry.getObjectTag("inventory");
    ElementTag quantity = scriptEntry.getElement("quantity");
    Type type = (Type) scriptEntry.getObject("type");
    ElementTag slot = scriptEntry.getElement("slot");
    Object items_object = scriptEntry.getObject("items");
    List<ItemTag> items = null;
    if (items_object != null) {
        items = (List<ItemTag>) items_object;
    }
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), db("Type", type.name()), inventory, quantity, unlimit_stack_size, db("Items", items), slot);
    }
    switch(type) {
        case MONEY:
            if (Depends.economy != null) {
                Depends.economy.depositPlayer(Utilities.getEntryPlayer(scriptEntry).getOfflinePlayer(), quantity.asDouble());
            } else {
                Debug.echoError("No economy loaded! Have you installed Vault and a compatible economy plugin?");
            }
            break;
        case EXP:
            Utilities.getEntryPlayer(scriptEntry).getPlayerEntity().giveExp(quantity.asInt());
            break;
        case ITEM:
            boolean set_quantity = scriptEntry.hasObject("set_quantity");
            boolean limited = !unlimit_stack_size.asBoolean();
            for (ItemTag item : items) {
                ItemStack is = item.getItemStack();
                if (is.getType() == Material.AIR) {
                    Debug.echoError("Cannot give air!");
                    continue;
                }
                if (set_quantity) {
                    is.setAmount(quantity.asInt());
                }
                int slotId = SlotHelper.nameToIndexFor(slot.asString(), inventory.getInventory().getHolder());
                if (slotId == -1) {
                    Debug.echoError(scriptEntry, "The input '" + slot.asString() + "' is not a valid slot!");
                    return;
                }
                List<ItemStack> leftovers = inventory.addWithLeftovers(slotId, limited, is);
                if (!leftovers.isEmpty()) {
                    Debug.echoDebug(scriptEntry, "The inventory didn't have enough space, the rest of the items have been placed on the floor.");
                    for (ItemStack leftoverItem : leftovers) {
                        inventory.getLocation().getWorld().dropItem(inventory.getLocation(), leftoverItem);
                    }
                }
            }
            break;
    }
}
Also used : ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ItemTag(com.denizenscript.denizen.objects.ItemTag) ItemStack(org.bukkit.inventory.ItemStack) InventoryTag(com.denizenscript.denizen.objects.InventoryTag)

Example 32 with ItemTag

use of com.denizenscript.denizen.objects.ItemTag in project Denizen-For-Bukkit by DenizenScript.

the class ScribeCommand method parseArgs.

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
    for (Argument arg : scriptEntry) {
        if (arg.matchesEnum(BookAction.class) && !scriptEntry.hasObject("action")) {
            scriptEntry.addObject("action", BookAction.valueOf(arg.getValue().toUpperCase()));
        } else if (!scriptEntry.hasObject("script") && arg.matchesArgumentType(ScriptTag.class)) {
            scriptEntry.addObject("script", arg.asType(ScriptTag.class));
        } else if (!scriptEntry.hasObject("location") && arg.matchesArgumentType(LocationTag.class)) {
            scriptEntry.addObject("location", arg.asType(LocationTag.class));
            scriptEntry.addObject("action", BookAction.DROP);
        } else if (!scriptEntry.hasObject("item") && arg.matchesArgumentType(ItemTag.class)) {
            scriptEntry.addObject("item", arg.asType(ItemTag.class));
        } else {
            arg.reportUnhandled();
        }
    }
    scriptEntry.defaultObject("action", BookAction.GIVE);
    scriptEntry.defaultObject("item", new ItemTag(Material.WRITTEN_BOOK));
    // Must contain a book script
    if (!scriptEntry.hasObject("script")) {
        throw new InvalidArgumentsException("Missing SCRIPT argument!");
    }
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) Argument(com.denizenscript.denizencore.objects.Argument) ItemTag(com.denizenscript.denizen.objects.ItemTag) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException)

Example 33 with ItemTag

use of com.denizenscript.denizen.objects.ItemTag in project Denizen-For-Bukkit by DenizenScript.

the class EquipCommand method parseArgs.

// <--[command]
// @Name Equip
// @Syntax equip (<entity>|...) (hand:<item>) (offhand:<item>) (head:<item>) (chest:<item>) (legs:<item>) (boots:<item>) (saddle:<item>) (horse_armor:<item>)
// @Required 1
// @Maximum 9
// @Short Equips items and armor on a list of entities.
// @Group entity
// 
// @Description
// This command equips an item or armor to an entity or list of entities to the specified slot(s).
// Set the item to 'air' to unequip any slot.
// 
// @Tags
// <EntityTag.equipment>
// <InventoryTag.equipment>
// 
// @Usage
// Use to equip a stone block on the player's head.
// - equip <player> head:stone
// 
// @Usage
// Use to equip an iron helmet on two defined players.
// - equip <[player]>|<[someplayer]> head:iron_helmet
// 
// @Usage
// Use to unequip all armor off the player.
// - equip <player> head:air chest:air legs:air boots:air
// 
// @Usage
// Use to equip a saddle on the horse the player is riding.
// - equip <player.vehicle> saddle:saddle
// 
// @Usage
// Use to equip a saddle on all nearby pigs.
// - equip <player.location.find_entities[pig].within[10]> saddle:saddle
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
    Map<String, ItemTag> equipment = new HashMap<>();
    for (Argument arg : scriptEntry) {
        if (!scriptEntry.hasObject("entities") && arg.matchesArgumentList(EntityTag.class)) {
            scriptEntry.addObject("entities", arg.asType(ListTag.class).filter(EntityTag.class, scriptEntry));
        } else if (arg.matchesArgumentType(ItemTag.class) && arg.matchesPrefix("head", "helmet")) {
            equipment.put("head", ItemTag.valueOf(arg.getValue(), scriptEntry.getContext()));
        } else if (arg.matchesArgumentType(ItemTag.class) && arg.matchesPrefix("chest", "chestplate")) {
            equipment.put("chest", ItemTag.valueOf(arg.getValue(), scriptEntry.getContext()));
        } else if (arg.matchesArgumentType(ItemTag.class) && arg.matchesPrefix("legs", "leggings")) {
            equipment.put("legs", ItemTag.valueOf(arg.getValue(), scriptEntry.getContext()));
        } else if (arg.matchesArgumentType(ItemTag.class) && arg.matchesPrefix("boots", "feet")) {
            equipment.put("boots", ItemTag.valueOf(arg.getValue(), scriptEntry.getContext()));
        } else if (arg.matchesArgumentType(ItemTag.class) && arg.matchesPrefix("saddle")) {
            equipment.put("saddle", ItemTag.valueOf(arg.getValue(), scriptEntry.getContext()));
        } else if (arg.matchesArgumentType(ItemTag.class) && arg.matchesPrefix("horse_armor", "horse_armour")) {
            equipment.put("horse_armor", ItemTag.valueOf(arg.getValue(), scriptEntry.getContext()));
        } else if (arg.matchesArgumentType(ItemTag.class) && arg.matchesPrefix("offhand")) {
            equipment.put("offhand", ItemTag.valueOf(arg.getValue(), scriptEntry.getContext()));
        } else // Default to item in hand if no prefix is used
        if (arg.matchesArgumentType(ItemTag.class)) {
            equipment.put("hand", ItemTag.valueOf(arg.getValue(), scriptEntry.getContext()));
        } else if (arg.matches("player") && Utilities.entryHasPlayer(scriptEntry)) {
            // Player arg for compatibility with old scripts
            scriptEntry.addObject("entities", Collections.singletonList(Utilities.getEntryPlayer(scriptEntry).getDenizenEntity()));
        } else {
            arg.reportUnhandled();
        }
    }
    if (equipment.isEmpty()) {
        throw new InvalidArgumentsException("Must specify equipment!");
    }
    scriptEntry.addObject("equipment", equipment);
    scriptEntry.defaultObject("entities", Utilities.entryDefaultEntityList(scriptEntry, false));
}
Also used : Argument(com.denizenscript.denizencore.objects.Argument) EntityTag(com.denizenscript.denizen.objects.EntityTag) ItemTag(com.denizenscript.denizen.objects.ItemTag) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException)

Example 34 with ItemTag

use of com.denizenscript.denizen.objects.ItemTag in project Denizen-For-Bukkit by DenizenScript.

the class TradeInputs method adjust.

public void adjust(Mechanism mechanism) {
    // -->
    if (mechanism.matches("inputs")) {
        List<ItemStack> ingredients = new ArrayList<>();
        List<ItemTag> list = mechanism.valueAsType(ListTag.class).filter(ItemTag.class, mechanism.context);
        if (!mechanism.hasValue() || list.isEmpty()) {
            recipe.getRecipe().setIngredients(ingredients);
            return;
        }
        for (ItemTag item : list) {
            ingredients.add(item.getItemStack());
        }
        if (ingredients.size() > 2) {
            mechanism.echoError("Trade recipe input was given " + list.size() + " items. Only using the first two items!");
            ingredients = ingredients.subList(0, 2);
        }
        recipe.getRecipe().setIngredients(ingredients);
    }
}
Also used : ArrayList(java.util.ArrayList) ItemStack(org.bukkit.inventory.ItemStack) ItemTag(com.denizenscript.denizen.objects.ItemTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag)

Example 35 with ItemTag

use of com.denizenscript.denizen.objects.ItemTag in project Denizen-For-Bukkit by DenizenScript.

the class ToastCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    ElementTag text = scriptEntry.getElement("text");
    ElementTag frame = scriptEntry.getElement("frame");
    ItemTag icon = scriptEntry.getObjectTag("icon");
    final List<PlayerTag> targets = (List<PlayerTag>) scriptEntry.getObject("targets");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, name, text, frame, icon, db("targets", targets));
    }
    final Advancement advancement = new Advancement(true, new NamespacedKey(Denizen.getInstance(), UUID.randomUUID().toString()), null, icon.getItemStack(), text.asString(), "", null, Advancement.Frame.valueOf(frame.asString().toUpperCase()), true, false, true, 0, 0, 1);
    final AdvancementHelper advancementHelper = NMSHandler.getAdvancementHelper();
    for (PlayerTag target : targets) {
        Player player = target.getPlayerEntity();
        if (player != null) {
            advancementHelper.grant(advancement, player);
            advancementHelper.revoke(advancement, player);
        }
    }
}
Also used : Player(org.bukkit.entity.Player) AdvancementHelper(com.denizenscript.denizen.nms.interfaces.AdvancementHelper) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) NamespacedKey(org.bukkit.NamespacedKey) List(java.util.List) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ItemTag(com.denizenscript.denizen.objects.ItemTag) Advancement(com.denizenscript.denizen.nms.util.Advancement)

Aggregations

ItemTag (com.denizenscript.denizen.objects.ItemTag)77 EventHandler (org.bukkit.event.EventHandler)35 ItemStack (org.bukkit.inventory.ItemStack)20 LocationTag (com.denizenscript.denizen.objects.LocationTag)19 EntityTag (com.denizenscript.denizen.objects.EntityTag)14 ListTag (com.denizenscript.denizencore.objects.core.ListTag)14 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)13 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)9 Item (org.bukkit.entity.Item)7 Argument (com.denizenscript.denizencore.objects.Argument)6 List (java.util.List)6 InvalidArgumentsException (com.denizenscript.denizencore.exceptions.InvalidArgumentsException)5 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)5 HumanEntity (org.bukkit.entity.HumanEntity)5 Player (org.bukkit.entity.Player)5 InventoryTag (com.denizenscript.denizen.objects.InventoryTag)4 BukkitTagContext (com.denizenscript.denizen.tags.BukkitTagContext)4 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)4 ArrayList (java.util.ArrayList)4 MaterialTag (com.denizenscript.denizen.objects.MaterialTag)3