Search in sources :

Example 36 with ItemTag

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

the class ToastCommand method parseArgs.

// <--[command]
// @Name Toast
// @Syntax toast [<text>] (targets:<player>|...) (icon:<item>) (frame:{task}/challenge/goal)
// @Required 1
// @Maximum 4
// @Short Shows the player a custom advancement toast.
// @Group player
// 
// @Description
// Displays a client-side custom advancement "toast" notification popup to the player(s).
// If no target is specified it will default to the attached player.
// The icon argument changes the icon displayed in the toast pop-up notification.
// The frame argument changes the type of advancement.
// 
// @Tags
// None
// 
// @Usage
// Welcomes the player with an advancement toast.
// - toast "Welcome <player.name>!"
// 
// @Usage
// Sends the player an advancement toast with a custom icon.
// - toast "Diggy Diggy Hole" icon:iron_spade
// 
// @Usage
// Sends the player a "Challenge Complete!" type advancement toast.
// - toast "You finished a challenge!" frame:challenge icon:diamond
// 
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
    for (Argument arg : scriptEntry) {
        if (!scriptEntry.hasObject("targets") && arg.matchesPrefix("target", "targets", "t") && arg.matchesArgumentList(PlayerTag.class)) {
            scriptEntry.addObject("targets", arg.asType(ListTag.class).filter(PlayerTag.class, scriptEntry));
        } else if (!scriptEntry.hasObject("icon") && arg.matchesPrefix("icon", "i") && arg.matchesArgumentType(ItemTag.class)) {
            scriptEntry.addObject("icon", arg.asType(ItemTag.class));
        } else if (!scriptEntry.hasObject("frame") && arg.matchesPrefix("frame", "f") && arg.matchesEnum(Advancement.Frame.class)) {
            scriptEntry.addObject("frame", arg.asElement());
        } else if (!scriptEntry.hasObject("text")) {
            scriptEntry.addObject("text", arg.getRawElement());
        } else {
            arg.reportUnhandled();
        }
    }
    if (!scriptEntry.hasObject("text")) {
        throw new InvalidArgumentsException("Must specify a message!");
    }
    if (!scriptEntry.hasObject("targets")) {
        if (!Utilities.entryHasPlayer(scriptEntry)) {
            throw new InvalidArgumentsException("Must specify valid player targets!");
        } else {
            scriptEntry.addObject("targets", Collections.singletonList(Utilities.getEntryPlayer(scriptEntry)));
        }
    }
    scriptEntry.defaultObject("icon", new ItemTag(Material.AIR));
    scriptEntry.defaultObject("frame", new ElementTag("TASK"));
}
Also used : Argument(com.denizenscript.denizencore.objects.Argument) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ItemTag(com.denizenscript.denizen.objects.ItemTag) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException)

Example 37 with ItemTag

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

the class TakeCommand method removeItem.

public boolean removeItem(Inventory inventory, ItemTag item, int amount) {
    if (item == null) {
        return false;
    }
    item = new ItemTag(item.getItemStack().clone());
    item.setAmount(1);
    String myItem = CoreUtilities.toLowerCase(item.identify());
    for (int i = 0; i < inventory.getSize(); i++) {
        ItemStack is = inventory.getItem(i);
        if (is == null) {
            continue;
        }
        is = is.clone();
        int count = is.getAmount();
        is.setAmount(1);
        // Note: this double-parsing is intentional, as part of a hotfix for a larger issue
        String newItem = CoreUtilities.toLowerCase(ItemTag.valueOf(new ItemTag(is).identify(), false).identify());
        if (myItem.equals(newItem)) {
            if (count <= amount) {
                NMSHandler.getItemHelper().setInventoryItem(inventory, null, i);
                amount -= count;
                if (amount == 0) {
                    return true;
                }
            } else {
                is.setAmount(count - amount);
                NMSHandler.getItemHelper().setInventoryItem(inventory, is, i);
                return true;
            }
        }
    }
    return false;
}
Also used : ItemTag(com.denizenscript.denizen.objects.ItemTag) ItemStack(org.bukkit.inventory.ItemStack)

Example 38 with ItemTag

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

the class EnchantmentScriptContainer method canEnchant.

public boolean canEnchant(ItemStack item) {
    if (!Bukkit.isPrimaryThread()) {
        return false;
    }
    ContextSource.SimpleMap src = new ContextSource.SimpleMap();
    src.contexts = new HashMap<>();
    src.contexts.put("item", new ItemTag(item));
    String res = autoTag(canEnchantTaggable, src);
    return CoreUtilities.toLowerCase(res).equals("true");
}
Also used : ContextSource(com.denizenscript.denizencore.scripts.queues.ContextSource) ItemTag(com.denizenscript.denizen.objects.ItemTag)

Example 39 with ItemTag

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

the class ItemScriptHelper method textToItemArray.

public ItemStack[] textToItemArray(ItemScriptContainer container, String text, boolean exact) {
    if (CoreUtilities.toLowerCase(text).equals("air")) {
        return new ItemStack[0];
    }
    List<String> ingredientText = splitByNonBracketedSlashes(text);
    List<ItemStack> outputItems = new ArrayList<>(ingredientText.size());
    for (int i = 0; i < ingredientText.size(); i++) {
        String entry = ingredientText.get(i);
        if (ScriptEvent.isAdvancedMatchable(entry)) {
            boolean any = false;
            for (Material material : Material.values()) {
                if (BukkitScriptEvent.tryMaterial(material, entry)) {
                    outputItems.add(new ItemStack(material, 1));
                    any = true;
                }
            }
            if (exact) {
                for (ItemScriptContainer possibleContainer : ItemScriptHelper.item_scripts.values()) {
                    if (BukkitScriptEvent.tryItem(possibleContainer.getCleanReference(), entry)) {
                        outputItems.add(possibleContainer.getCleanReference().getItemStack());
                        any = true;
                    }
                }
            }
            if (!any) {
                Debug.echoError("Invalid ItemTag ingredient (empty advanced matcher), recipe will not be registered for item script '" + container.getName() + "': " + entry);
                return null;
            }
        } else {
            ItemTag ingredient = ItemTag.valueOf(entry, container);
            if (ingredient == null) {
                Debug.echoError("Invalid ItemTag ingredient, recipe will not be registered for item script '" + container.getName() + "': " + entry);
                return null;
            }
            outputItems.add(ingredient.getItemStack().clone());
        }
    }
    return outputItems.toArray(new ItemStack[0]);
}
Also used : ItemTag(com.denizenscript.denizen.objects.ItemTag)

Example 40 with ItemTag

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

the class ItemScriptHelper method onItemSmithing.

@EventHandler(priority = EventPriority.LOW)
public void onItemSmithing(PrepareSmithingEvent event) {
    if (!isItemscript(event.getInventory().getItem(0))) {
        return;
    }
    Recipe recipe = event.getInventory().getRecipe();
    SmithingRecipe smithRecipe = (SmithingRecipe) recipe;
    if (smithRecipe == null || !(smithRecipe.getKey().getNamespace().equals("denizen"))) {
        if (!isAllowedToCraftWith(event.getInventory().getItem(0))) {
            event.setResult(new ItemStack(Material.AIR));
        }
        return;
    }
    ItemScriptContainer realResult = recipeIdToItemScript.get(smithRecipe.getKey().toString());
    if (realResult == null) {
        if (!isAllowedToCraftWith(event.getInventory().getItem(0))) {
            event.setResult(new ItemStack(Material.AIR));
        }
        return;
    }
    String[] retain = smithingRetain.get(smithRecipe.getKey().getKey());
    if (retain == null) {
        Debug.echoError("Smithing recipe mis-registered for script item: " + realResult.getName());
        if (!isAllowedToCraftWith(event.getInventory().getItem(0))) {
            event.setResult(new ItemStack(Material.AIR));
        }
        return;
    }
    PlayerTag player = null;
    if (!event.getInventory().getViewers().isEmpty()) {
        HumanEntity human = event.getInventory().getViewers().get(0);
        if (!EntityTag.isNPC(human) && human instanceof Player) {
            player = new PlayerTag((Player) human);
        }
    }
    ItemTag got = realResult.getItemFrom(new BukkitTagContext(player, null, new ScriptTag(realResult)));
    if (got == null) {
        return;
    }
    if (retain.length > 0) {
        ItemMeta originalMeta = event.getInventory().getItem(0).getItemMeta();
        ItemMeta newMeta = got.getItemMeta();
        for (String retainable : retain) {
            switch(retainable) {
                case "display":
                    newMeta.setDisplayName(originalMeta.hasDisplayName() ? originalMeta.getDisplayName() : null);
                    break;
                case "enchantments":
                    if (newMeta.hasEnchants()) {
                        for (Enchantment enchant : new ArrayList<>(newMeta.getEnchants().keySet())) {
                            newMeta.removeEnchant(enchant);
                        }
                    }
                    if (originalMeta.hasEnchants()) {
                        for (Map.Entry<Enchantment, Integer> enchant : originalMeta.getEnchants().entrySet()) {
                            newMeta.addEnchant(enchant.getKey(), enchant.getValue(), true);
                        }
                    }
                    break;
            }
        }
        got.setItemMeta(newMeta);
    }
    event.setResult(got.getItemStack());
}
Also used : Player(org.bukkit.entity.Player) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) BigInteger(java.math.BigInteger) BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) HumanEntity(org.bukkit.entity.HumanEntity) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) ItemTag(com.denizenscript.denizen.objects.ItemTag) Enchantment(org.bukkit.enchantments.Enchantment) ItemMeta(org.bukkit.inventory.meta.ItemMeta) EventHandler(org.bukkit.event.EventHandler)

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