Search in sources :

Example 1 with DataAction

use of com.denizenscript.denizencore.utilities.data.DataAction in project Denizen-For-Bukkit by DenizenScript.

the class ItemFlags method adjust.

@Override
public void adjust(Mechanism mechanism) {
    // -->
    if (mechanism.matches("flag_map") && mechanism.requireObject(MapTag.class)) {
        item.reapplyTracker(new MapTagFlagTracker(mechanism.valueAsType(MapTag.class)));
    }
    // -->
    if (mechanism.matches("flag")) {
        FlagCommand.FlagActionProvider provider = new FlagCommand.FlagActionProvider();
        provider.tracker = item.getFlagTracker();
        DataAction action = DataActionHelper.parse(provider, mechanism.getValue().asString(), mechanism.context);
        action.execute(mechanism.context);
        item.reapplyTracker(provider.tracker);
    }
}
Also used : FlagCommand(com.denizenscript.denizencore.scripts.commands.core.FlagCommand) DataAction(com.denizenscript.denizencore.utilities.data.DataAction) MapTagFlagTracker(com.denizenscript.denizencore.flags.MapTagFlagTracker) MapTag(com.denizenscript.denizencore.objects.core.MapTag)

Example 2 with DataAction

use of com.denizenscript.denizencore.utilities.data.DataAction in project Denizen-For-Bukkit by DenizenScript.

the class InventoryCommand method execute.

@Override
public void execute(final ScriptEntry scriptEntry) {
    List<String> actions = (List<String>) scriptEntry.getObject("actions");
    AbstractMap.SimpleEntry<Integer, InventoryTag> originentry = (AbstractMap.SimpleEntry<Integer, InventoryTag>) scriptEntry.getObject("origin");
    InventoryTag origin = originentry != null ? originentry.getValue() : null;
    AbstractMap.SimpleEntry<Integer, InventoryTag> destinationentry = (AbstractMap.SimpleEntry<Integer, InventoryTag>) scriptEntry.getObject("destination");
    InventoryTag destination = destinationentry.getValue();
    ElementTag slot = scriptEntry.getElement("slot");
    ElementTag mechanism = scriptEntry.getElement("mechanism");
    ObjectTag mechanismValue = scriptEntry.getObjectTag("mechanism_value");
    DataAction flagAction = (DataAction) scriptEntry.getObject("flag_action");
    TimeTag expiration = scriptEntry.getObjectTag("expiration");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), db("actions", actions), destination, origin, mechanism, mechanismValue, flagAction, expiration, slot);
    }
    int slotId = SlotHelper.nameToIndexFor(slot.asString(), destination.getInventory().getHolder());
    if (slotId < 0) {
        if (slotId == -1) {
            Debug.echoError(scriptEntry, "The input '" + slot.asString() + "' is not a valid slot (unrecognized)!");
        } else {
            Debug.echoError(scriptEntry, "The input '" + slot.asString() + "' is not a valid slot (negative values are invalid)!");
        }
        return;
    }
    InventoryTag.trackTemporaryInventory(destination);
    if (origin != null) {
        InventoryTag.trackTemporaryInventory(origin);
    }
    for (String action : actions) {
        switch(Action.valueOf(action.toUpperCase())) {
            // Make the attached player open the destination inventory
            case OPEN:
                // Use special method to make opening workbenches work properly
                if (destination.getIdType().equals("workbench") || destination.getIdHolder().equals(new ElementTag("workbench"))) {
                    Utilities.getEntryPlayer(scriptEntry).getPlayerEntity().openWorkbench(null, true);
                } else // Otherwise, open inventory as usual
                {
                    Utilities.getEntryPlayer(scriptEntry).getPlayerEntity().openInventory(destination.getInventory());
                }
                break;
            // Make the attached player close any open inventory
            case CLOSE:
                Utilities.getEntryPlayer(scriptEntry).getPlayerEntity().closeInventory();
                break;
            // Turn destination's contents into a copy of origin's
            case COPY:
                if (origin == null) {
                    Debug.echoError(scriptEntry, "Missing origin argument!");
                    return;
                }
                replace(origin, destination);
                break;
            // Copy origin's contents to destination, then empty origin
            case MOVE:
                if (origin == null) {
                    Debug.echoError(scriptEntry, "Missing origin argument!");
                    return;
                }
                replace(origin, destination);
                origin.clear();
                break;
            // Swap the contents of the two inventories
            case SWAP:
                if (origin == null) {
                    Debug.echoError(scriptEntry, "Missing origin argument!");
                    return;
                }
                InventoryTag temp = new InventoryTag(destination.getInventory().getContents());
                replace(origin, destination);
                replace(temp, origin);
                break;
            // Add origin's contents to destination
            case ADD:
                Deprecations.oldInventoryCommands.warn(scriptEntry);
                if (origin == null) {
                    Debug.echoError(scriptEntry, "Missing origin argument!");
                    return;
                }
                destination.add(slotId, origin.getContents());
                break;
            // Remove origin's contents from destination
            case REMOVE:
                Deprecations.oldInventoryCommands.warn(scriptEntry);
                if (origin == null) {
                    Debug.echoError(scriptEntry, "Missing origin argument!");
                    return;
                }
                remove(destination.getInventory(), origin.getContents());
                break;
            // Set items by slot
            case SET:
                if (origin == null) {
                    Debug.echoError(scriptEntry, "Missing origin argument!");
                    return;
                }
                destination.setSlots(slotId, origin.getContents(), originentry.getKey());
                break;
            // Keep only items from the origin's contents in the destination
            case KEEP:
                {
                    if (origin == null) {
                        Debug.echoError(scriptEntry, "Missing origin argument!");
                        return;
                    }
                    ItemStack[] items = origin.getContents();
                    for (ItemStack invStack : destination.getInventory()) {
                        if (invStack != null) {
                            boolean keep = false;
                            // this inventory item
                            for (ItemStack item : items) {
                                if (invStack.isSimilar(item)) {
                                    keep = true;
                                    break;
                                }
                            }
                            // from the inventory
                            if (!keep) {
                                destination.getInventory().remove(invStack);
                            }
                        }
                    }
                    break;
                }
            // Exclude all items from the origin's contents in the destination
            case EXCLUDE:
                {
                    if (origin == null) {
                        Debug.echoError(scriptEntry, "Missing origin argument!");
                        return;
                    }
                    int oldCount = destination.count(null, false);
                    int newCount = -1;
                    while (oldCount != newCount) {
                        oldCount = newCount;
                        remove(destination.getInventory(), origin.getContents());
                        newCount = destination.count(null, false);
                    }
                    break;
                }
            // Add origin's contents over and over to destination until it is full
            case FILL:
                {
                    if (origin == null) {
                        Debug.echoError(scriptEntry, "Missing origin argument!");
                        return;
                    }
                    int oldCount = destination.count(null, false);
                    int newCount = -1;
                    while (oldCount != newCount) {
                        oldCount = newCount;
                        newCount = destination.add(0, origin.getContents()).count(null, false);
                    }
                    break;
                }
            // Clear the content of the destination inventory
            case CLEAR:
                destination.clear();
                break;
            // If this is a player inventory, update it
            case UPDATE:
                if (destination.idHolder instanceof PlayerTag) {
                    ((PlayerTag) destination.idHolder).getPlayerEntity().updateInventory();
                } else {
                    Debug.echoError("Only player inventories can be force-updated!");
                }
                break;
            case ADJUST:
                ItemTag toAdjust = new ItemTag(destination.getInventory().getItem(slotId));
                toAdjust.safeAdjust(new Mechanism(mechanism.asString(), mechanismValue, scriptEntry.getContext()));
                NMSHandler.getItemHelper().setInventoryItem(destination.getInventory(), toAdjust.getItemStack(), slotId);
                break;
            case FLAG:
                ItemTag toFlag = new ItemTag(destination.getInventory().getItem(slotId));
                FlagCommand.FlagActionProvider provider = (FlagCommand.FlagActionProvider) flagAction.provider;
                provider.expiration = expiration;
                provider.tracker = toFlag.getFlagTracker();
                flagAction.execute(scriptEntry.context);
                toFlag.reapplyTracker(provider.tracker);
                NMSHandler.getItemHelper().setInventoryItem(destination.getInventory(), toFlag.getItemStack(), slotId);
                break;
        }
    }
}
Also used : DataAction(com.denizenscript.denizencore.utilities.data.DataAction) AbstractMap(java.util.AbstractMap) FlagCommand(com.denizenscript.denizencore.scripts.commands.core.FlagCommand) List(java.util.List) ItemStack(org.bukkit.inventory.ItemStack)

Example 3 with DataAction

use of com.denizenscript.denizencore.utilities.data.DataAction in project Denizen-For-Bukkit by DenizenScript.

the class ItemFlags method getObjectAttribute.

@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
    if (attribute == null) {
        return null;
    }
    // -->
    if (attribute.startsWith("with_flag")) {
        ItemTag item = new ItemTag(this.item.getItemStack().clone());
        FlagCommand.FlagActionProvider provider = new FlagCommand.FlagActionProvider();
        provider.tracker = item.getFlagTracker();
        DataAction action = DataActionHelper.parse(provider, attribute.getParam(), attribute.context);
        // -->
        if (attribute.startsWith("duration", 2)) {
            provider.expiration = new TimeTag(TimeTag.now().millis() + attribute.getContextObject(2).asType(DurationTag.class, attribute.context).getMillis());
            attribute.fulfill(1);
        }
        action.execute(attribute.context);
        item.reapplyTracker(provider.tracker);
        return item.getObjectAttribute(attribute.fulfill(1));
    }
    return null;
}
Also used : FlagCommand(com.denizenscript.denizencore.scripts.commands.core.FlagCommand) DataAction(com.denizenscript.denizencore.utilities.data.DataAction) TimeTag(com.denizenscript.denizencore.objects.core.TimeTag) ItemTag(com.denizenscript.denizen.objects.ItemTag)

Aggregations

FlagCommand (com.denizenscript.denizencore.scripts.commands.core.FlagCommand)3 DataAction (com.denizenscript.denizencore.utilities.data.DataAction)3 ItemTag (com.denizenscript.denizen.objects.ItemTag)1 MapTagFlagTracker (com.denizenscript.denizencore.flags.MapTagFlagTracker)1 MapTag (com.denizenscript.denizencore.objects.core.MapTag)1 TimeTag (com.denizenscript.denizencore.objects.core.TimeTag)1 AbstractMap (java.util.AbstractMap)1 List (java.util.List)1 ItemStack (org.bukkit.inventory.ItemStack)1