Search in sources :

Example 36 with MaterialTag

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

the class StatisticCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    ElementTag action = scriptEntry.getElement("action");
    ListTag players = scriptEntry.getObjectTag("players");
    ElementTag statistic = scriptEntry.getElement("statistic");
    ElementTag amount = scriptEntry.getElement("amount");
    MaterialTag material = scriptEntry.getObjectTag("material");
    EntityTag entity = scriptEntry.getObjectTag("entity");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), action, statistic, amount, players, material, entity);
    }
    Action act = Action.valueOf(action.asString().toUpperCase());
    Statistic stat = Statistic.valueOf(statistic.asString().toUpperCase());
    int amt = amount.asInt();
    switch(stat.getType()) {
        case BLOCK:
        case ITEM:
            Material mat = material.getMaterial();
            switch(act) {
                case ADD:
                    for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
                        player.incrementStatistic(stat, mat, amt);
                    }
                    break;
                case TAKE:
                    for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
                        player.decrementStatistic(stat, mat, amt);
                    }
                    break;
                case SET:
                    for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
                        player.setStatistic(stat, mat, amt);
                    }
                    break;
            }
            break;
        case ENTITY:
            EntityType ent = entity.getBukkitEntityType();
            switch(act) {
                case ADD:
                    for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
                        player.incrementStatistic(stat, ent, amt);
                    }
                    break;
                case TAKE:
                    for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
                        player.decrementStatistic(stat, ent, amt);
                    }
                    break;
                case SET:
                    for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
                        player.setStatistic(stat, ent, amt);
                    }
                    break;
            }
            break;
        case UNTYPED:
            switch(act) {
                case ADD:
                    for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
                        player.incrementStatistic(stat, amt);
                    }
                    break;
                case TAKE:
                    for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
                        player.decrementStatistic(stat, amt);
                    }
                    break;
                case SET:
                    for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
                        player.setStatistic(stat, amt);
                    }
                    break;
            }
            break;
    }
}
Also used : MaterialTag(com.denizenscript.denizen.objects.MaterialTag) EntityType(org.bukkit.entity.EntityType) Statistic(org.bukkit.Statistic) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) EntityTag(com.denizenscript.denizen.objects.EntityTag) Material(org.bukkit.Material) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag)

Example 37 with MaterialTag

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

the class SignCommand method setWallSign.

public void setWallSign(Block sign, BlockFace bf, MaterialTag material) {
    sign.setType(material == null ? Material.OAK_WALL_SIGN : material.getMaterial(), false);
    MaterialTag signMaterial = new MaterialTag(sign);
    MaterialDirectional.getFrom(signMaterial).setFacing(bf);
    sign.setBlockData(signMaterial.getModernData());
}
Also used : MaterialTag(com.denizenscript.denizen.objects.MaterialTag)

Example 38 with MaterialTag

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

the class TakeCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    InventoryTag inventory = scriptEntry.getObjectTag("inventory");
    ElementTag quantity = scriptEntry.getElement("quantity");
    ListTag displayNameList = scriptEntry.getObjectTag("displayname");
    List<ItemTag> scriptItemList = scriptEntry.getObjectTag("scriptitem");
    ListTag slotList = scriptEntry.getObjectTag("slot");
    ListTag titleAuthor = scriptEntry.getObjectTag("cover");
    ElementTag nbtKey = scriptEntry.getElement("nbt_key");
    ElementTag matcherText = scriptEntry.getElement("matcher_text");
    ListTag flagList = scriptEntry.getObjectTag("flag_name");
    List<MaterialTag> materialList = scriptEntry.getObjectTag("material");
    Type type = (Type) scriptEntry.getObject("type");
    List<ItemTag> items = scriptEntry.getObjectTag("items");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), db("Type", type.name()), quantity, inventory, displayNameList, db("scriptname", scriptItemList), db("Items", items), slotList, nbtKey, flagList, matcherText, db("material", materialList), titleAuthor);
    }
    switch(type) {
        case INVENTORY:
            {
                inventory.clear();
                break;
            }
        case ITEMINHAND:
            {
                Player player = Utilities.getEntryPlayer(scriptEntry).getPlayerEntity();
                int inHandAmt = player.getEquipment().getItemInMainHand().getAmount();
                int theAmount = (int) quantity.asDouble();
                ItemStack newHandItem = new ItemStack(Material.AIR);
                if (theAmount > inHandAmt) {
                    Debug.echoDebug(scriptEntry, "...player did not have enough of the item in hand, taking all...");
                    player.getEquipment().setItemInMainHand(newHandItem);
                } else {
                    // amount is just right!
                    if (theAmount == inHandAmt) {
                        player.getEquipment().setItemInMainHand(newHandItem);
                    } else {
                        // amount is less than what's in hand, need to make a new itemstack of what's left...
                        newHandItem = player.getEquipment().getItemInMainHand().clone();
                        newHandItem.setAmount(inHandAmt - theAmount);
                        player.getEquipment().setItemInMainHand(newHandItem);
                        player.updateInventory();
                    }
                }
                break;
            }
        case CURSORITEM:
            {
                Player player = Utilities.getEntryPlayer(scriptEntry).getPlayerEntity();
                int currentAmount = player.getItemOnCursor().getAmount();
                int takeAmount = (int) quantity.asDouble();
                ItemStack newItem = new ItemStack(Material.AIR);
                if (takeAmount > currentAmount) {
                    Debug.echoDebug(scriptEntry, "...player did not have enough of the item on cursor, taking all...");
                    player.setItemOnCursor(newItem);
                } else {
                    if (takeAmount == currentAmount) {
                        player.setItemOnCursor(newItem);
                    } else {
                        newItem = player.getItemOnCursor().clone();
                        newItem.setAmount(currentAmount - takeAmount);
                        player.setItemOnCursor(newItem);
                        player.updateInventory();
                    }
                }
                break;
            }
        case MONEY:
            {
                if (Depends.economy == null) {
                    Debug.echoError(scriptEntry, "No economy loaded! Have you installed Vault and a compatible economy plugin?");
                    return;
                }
                Depends.economy.withdrawPlayer(Utilities.getEntryPlayer(scriptEntry).getOfflinePlayer(), quantity.asDouble());
                break;
            }
        case XP:
            {
                Utilities.getEntryPlayer(scriptEntry).getPlayerEntity().giveExp(-quantity.asInt());
                break;
            }
        case RAWEXACT:
            {
                if (items == null) {
                    Debug.echoError(scriptEntry, "Must specify item/items!");
                    return;
                }
                for (ItemTag targetItem : items) {
                    takeByMatcher(inventory, (item) -> targetItem.matchesRawExact(new ItemTag(item)), quantity.asInt());
                }
                break;
            }
        case ITEM:
            {
                if (items == null) {
                    Debug.echoError(scriptEntry, "Must specify item/items!");
                    return;
                }
                for (ItemTag item : items) {
                    ItemStack is = item.getItemStack().clone();
                    is.setAmount(quantity.asInt());
                    if (!removeItem(inventory.getInventory(), item, item.getAmount())) {
                        Debug.echoDebug(scriptEntry, "Inventory does not contain at least " + quantity.asInt() + " of " + item.identify() + "... Taking all...");
                    }
                }
                break;
            }
        case BYDISPLAY:
            {
                if (displayNameList == null) {
                    Debug.echoError(scriptEntry, "Must specify a displayname!");
                    return;
                }
                for (String name : displayNameList) {
                    takeByMatcher(inventory, (item) -> item.hasItemMeta() && item.getItemMeta().hasDisplayName() && item.getItemMeta().getDisplayName().equalsIgnoreCase(name), quantity.asInt());
                }
                break;
            }
        case BYCOVER:
            {
                if (titleAuthor == null) {
                    Debug.echoError(scriptEntry, "Must specify a cover!");
                    return;
                }
                takeByMatcher(inventory, (item) -> item.hasItemMeta() && item.getItemMeta() instanceof BookMeta && equalOrNull(titleAuthor.get(0), ((BookMeta) item.getItemMeta()).getTitle()) && (titleAuthor.size() == 1 || equalOrNull(titleAuthor.get(1), ((BookMeta) item.getItemMeta()).getAuthor())), quantity.asInt());
                break;
            }
        case FLAGGED:
            {
                if (flagList == null) {
                    Debug.echoError(scriptEntry, "Must specify a flag name!");
                    return;
                }
                for (String flag : flagList) {
                    takeByMatcher(inventory, (item) -> new ItemTag(item).getFlagTracker().hasFlag(flag), quantity.asInt());
                }
                break;
            }
        case NBT:
            {
                if (nbtKey == null) {
                    Debug.echoError(scriptEntry, "Must specify an NBT key!");
                    return;
                }
                takeByMatcher(inventory, (item) -> CustomNBT.hasCustomNBT(item, nbtKey.asString(), CustomNBT.KEY_DENIZEN), quantity.asInt());
                break;
            }
        case SCRIPTNAME:
            {
                if (scriptItemList == null) {
                    Debug.echoError(scriptEntry, "Must specify a valid script name!");
                    return;
                }
                for (ItemTag scriptedItem : scriptItemList) {
                    String script = scriptedItem.getScriptName();
                    if (script == null) {
                        Debug.echoError(scriptEntry, "Item '" + scriptedItem.debuggable() + "' is not a scripted item, cannot take by scriptname.");
                        continue;
                    }
                    takeByMatcher(inventory, (item) -> script.equalsIgnoreCase(new ItemTag(item).getScriptName()), quantity.asInt());
                }
                break;
            }
        case MATERIAL:
            {
                if (materialList == null) {
                    Debug.echoError(scriptEntry, "Must specify a valid material!");
                    return;
                }
                for (MaterialTag material : materialList) {
                    takeByMatcher(inventory, (item) -> item.getType() == material.getMaterial() && !(new ItemTag(item).isItemscript()), quantity.asInt());
                }
                break;
            }
        case MATCHER:
            {
                if (matcherText == null) {
                    Debug.echoError(scriptEntry, "Must specify an item matcher!");
                    return;
                }
                takeByMatcher(inventory, (item) -> BukkitScriptEvent.tryItem(new ItemTag(item), matcherText.asString()), quantity.asInt());
                break;
            }
        case SLOT:
            {
                for (String slot : slotList) {
                    int slotId = SlotHelper.nameToIndexFor(slot, inventory.getInventory().getHolder());
                    if (slotId == -1 || slotId >= inventory.getSize()) {
                        Debug.echoError(scriptEntry, "The input '" + slot + "' is not a valid slot!");
                        return;
                    }
                    ItemStack original = inventory.getInventory().getItem(slotId);
                    if (original != null && original.getType() != Material.AIR) {
                        if (original.getAmount() > quantity.asInt()) {
                            original.setAmount(original.getAmount() - quantity.asInt());
                            inventory.setSlots(slotId, original);
                        } else {
                            inventory.setSlots(slotId, new ItemStack(Material.AIR));
                        }
                    }
                }
                break;
            }
    }
}
Also used : MaterialTag(com.denizenscript.denizen.objects.MaterialTag) Utilities(com.denizenscript.denizen.utilities.Utilities) Arrays(java.util.Arrays) BookMeta(org.bukkit.inventory.meta.BookMeta) NMSHandler(com.denizenscript.denizen.nms.NMSHandler) ItemTag(com.denizenscript.denizen.objects.ItemTag) Player(org.bukkit.entity.Player) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException) Function(java.util.function.Function) Argument(com.denizenscript.denizencore.objects.Argument) Inventory(org.bukkit.inventory.Inventory) HashSet(java.util.HashSet) MaterialTag(com.denizenscript.denizen.objects.MaterialTag) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) Material(org.bukkit.Material) ListTag(com.denizenscript.denizencore.objects.core.ListTag) Depends(com.denizenscript.denizen.utilities.depends.Depends) CustomNBT(com.denizenscript.denizen.utilities.nbt.CustomNBT) BukkitScriptEvent(com.denizenscript.denizen.events.BukkitScriptEvent) ItemStack(org.bukkit.inventory.ItemStack) SlotHelper(com.denizenscript.denizen.utilities.inventory.SlotHelper) List(java.util.List) Deprecations(com.denizenscript.denizencore.utilities.Deprecations) Debug(com.denizenscript.denizen.utilities.debugging.Debug) AbstractCommand(com.denizenscript.denizencore.scripts.commands.AbstractCommand) CoreUtilities(com.denizenscript.denizencore.utilities.CoreUtilities) ItemScriptHelper(com.denizenscript.denizen.scripts.containers.core.ItemScriptHelper) InventoryTag(com.denizenscript.denizen.objects.InventoryTag) Player(org.bukkit.entity.Player) ListTag(com.denizenscript.denizencore.objects.core.ListTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ItemTag(com.denizenscript.denizen.objects.ItemTag) ItemStack(org.bukkit.inventory.ItemStack) InventoryTag(com.denizenscript.denizen.objects.InventoryTag) BookMeta(org.bukkit.inventory.meta.BookMeta)

Example 39 with MaterialTag

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

the class HeadCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    Deprecations.headCommand.warn(scriptEntry);
    List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
    ElementTag skin = scriptEntry.getElement("skin");
    MaterialTag material = scriptEntry.getObjectTag("material");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), db("entities", entities), skin, material);
    }
    ItemStack item = null;
    if (skin != null) {
        item = new ItemStack(Material.PLAYER_HEAD);
        ItemMeta itemMeta = item.getItemMeta();
        ((SkullMeta) itemMeta).setOwner(skin.asString());
        item.setItemMeta(itemMeta);
    } else if (material != null) {
        item = new ItemStack(material.getMaterial());
    }
    for (EntityTag entity : entities) {
        if (entity.isCitizensNPC()) {
            if (!entity.getDenizenNPC().getCitizen().hasTrait(Equipment.class)) {
                entity.getDenizenNPC().getCitizen().addTrait(Equipment.class);
            }
            Equipment trait = entity.getDenizenNPC().getCitizen().getOrAddTrait(Equipment.class);
            trait.set(1, item);
        } else if (entity.isPlayer()) {
            entity.getPlayer().getInventory().setHelmet(item);
        } else {
            if (entity.isLivingEntity() && entity.getLivingEntity().getEquipment() != null) {
                entity.getLivingEntity().getEquipment().setHelmet(item);
            } else {
                Debug.echoError(scriptEntry, entity.identify() + " is not a living entity!");
            }
        }
    }
}
Also used : MaterialTag(com.denizenscript.denizen.objects.MaterialTag) Equipment(net.citizensnpcs.api.trait.trait.Equipment) EntityTag(com.denizenscript.denizen.objects.EntityTag) List(java.util.List) SkullMeta(org.bukkit.inventory.meta.SkullMeta) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ItemStack(org.bukkit.inventory.ItemStack) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 40 with MaterialTag

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

the class ItemBlockMaterial method adjust.

@Override
public void adjust(Mechanism mechanism) {
    // -->
    if (mechanism.matches("block_material") && mechanism.requireObject(MaterialTag.class)) {
        BlockDataMeta meta = (BlockDataMeta) item.getItemMeta();
        MaterialTag mat = mechanism.valueAsType(MaterialTag.class);
        meta.setBlockData(mat.getModernData());
        item.setItemMeta(meta);
    }
}
Also used : MaterialTag(com.denizenscript.denizen.objects.MaterialTag) BlockDataMeta(org.bukkit.inventory.meta.BlockDataMeta)

Aggregations

MaterialTag (com.denizenscript.denizen.objects.MaterialTag)51 LocationTag (com.denizenscript.denizen.objects.LocationTag)33 EventHandler (org.bukkit.event.EventHandler)21 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)9 EntityTag (com.denizenscript.denizen.objects.EntityTag)8 Material (org.bukkit.Material)8 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)7 BlockData (org.bukkit.block.data.BlockData)7 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)6 ListTag (com.denizenscript.denizencore.objects.core.ListTag)6 ItemStack (org.bukkit.inventory.ItemStack)6 List (java.util.List)4 Block (org.bukkit.block.Block)4 PlayerChangesSignScriptEvent (com.denizenscript.denizen.events.player.PlayerChangesSignScriptEvent)3 ItemTag (com.denizenscript.denizen.objects.ItemTag)3 SignChangeEvent (org.bukkit.event.block.SignChangeEvent)3 InvalidArgumentsException (com.denizenscript.denizencore.exceptions.InvalidArgumentsException)2 ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)2 HashSet (java.util.HashSet)2 BlockPos (net.minecraft.core.BlockPos)2