Package net.aufdemrand.denizen.scripts.commands.item

Source Code of net.aufdemrand.denizen.scripts.commands.item.InventoryCommand

package net.aufdemrand.denizen.scripts.commands.item;

import java.util.List;

import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.Conversion;
import net.aufdemrand.denizen.utilities.debugging.dB;

/**
* Lets you store and edit inventories.
*
* @author David Cernat
*/

public class InventoryCommand extends AbstractCommand {

    private enum Action { OPEN, CLOSE, COPY, MOVE, SWAP, ADD, REMOVE, SET, KEEP, EXCLUDE, FILL, CLEAR, UPDATE }

    @SuppressWarnings("unchecked")
    @Override
    public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {

        for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {

            // Check for a dList of actions
            if (arg.matchesEnumList(Action.values())) {
                scriptEntry.addObject("actions", arg.asType(dList.class).filter(Action.values()));
            }

            // Check for an origin, which can be a dInventory, dEntity, dLocation
            // or a dList of dItems
            else if (!scriptEntry.hasObject("origin")
                     && arg.matchesPrefix("origin", "o", "source", "items", "item", "i", "from", "f")
                     && (arg.matchesArgumentTypes(dInventory.class, dEntity.class, dLocation.class)
                         || arg.matchesArgumentList(dItem.class))) {
                scriptEntry.addObject("origin", Conversion.getInventory(arg));
            }

            // Check for a destination, which can be a dInventory, dEntity
            // or dLocation
            else if (!scriptEntry.hasObject("destination")
                     && arg.matchesPrefix("destination", "dest", "d", "target", "to", "t")
                     && arg.matchesArgumentTypes(dInventory.class, dEntity.class, dLocation.class)) {
                scriptEntry.addObject("destination", Conversion.getInventory(arg));
            }

            // Check for specified slot number
            else if (!scriptEntry.hasObject("slot")
                    && arg.matchesPrefix("slot, s")
                    && arg.matchesPrimitive(aH.PrimitiveType.Integer)) {
                scriptEntry.addObject("slot", arg.asElement());
            }

            else arg.reportUnhandled();
        }

        // Check to make sure required arguments have been filled
        if (!scriptEntry.hasObject("actions"))
            throw new InvalidArgumentsException("Must specify an Inventory action!");

        scriptEntry.defaultObject("slot", new Element(1)).defaultObject("destination",
                        ((BukkitScriptEntryData)scriptEntry.entryData).hasPlayer() ? ((BukkitScriptEntryData)scriptEntry.entryData).getPlayer().getDenizenEntity().getInventory() : null);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {

        // Get objects
        List<String> actions = (List<String>) scriptEntry.getObject("actions");
        dInventory origin = (dInventory) scriptEntry.getObject("origin");
        dInventory destination = (dInventory) scriptEntry.getObject("destination");
        Element slot = scriptEntry.getElement("slot");

        dB.report(scriptEntry, getName(),
                aH.debugObj("actions", actions.toString())
                + destination.debug()
                + (origin != null ? origin.debug() : "")
                + slot.debug());

        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().equalsIgnoreCase("workbench")) {
                        ((BukkitScriptEntryData)scriptEntry.entryData).getPlayer().getPlayerEntity()
                            .openWorkbench(null, true);
                    }
                    // Otherwise, open inventory as usual
                    else ((BukkitScriptEntryData)scriptEntry.entryData).getPlayer().getPlayerEntity().openInventory(destination.getInventory());
                    break;

                // Make the attached player close any open inventory
                case CLOSE:
                    ((BukkitScriptEntryData)scriptEntry.entryData).getPlayer().getPlayerEntity().closeInventory();
                    break;

                // Turn destination's contents into a copy of origin's
                case COPY:
                    if (origin == null) {
                        dB.echoError(scriptEntry.getResidingQueue(), "Missing origin argument!");
                        return;
                    }
                    origin.replace(destination);
                    break;

                // Copy origin's contents to destination, then empty origin
                case MOVE:
                    if (origin == null) {
                        dB.echoError(scriptEntry.getResidingQueue(), "Missing origin argument!");
                        return;
                    }
                    origin.replace(destination);
                    origin.clear();
                    break;

                // Swap the contents of the two inventories
                case SWAP:
                    if (origin == null) {
                        dB.echoError(scriptEntry.getResidingQueue(), "Missing origin argument!");
                        return;
                    }
                    dInventory temp = new dInventory(destination.getInventory());
                    origin.replace(destination);
                    temp.replace(origin);
                    break;

                // Add origin's contents to destination
                case ADD:
                    if (origin == null) {
                        dB.echoError(scriptEntry.getResidingQueue(), "Missing origin argument!");
                        return;
                    }
                    destination.add(slot.asInt()-1, origin.getContents());
                    break;

                // Remove origin's contents from destination
                case REMOVE:
                    if (origin == null) {
                        dB.echoError(scriptEntry.getResidingQueue(), "Missing origin argument!");
                        return;
                    }
                    destination.remove(origin.getContents());
                    break;

                // Set items by slot
                case SET:
                    if (origin == null) {
                        dB.echoError(scriptEntry.getResidingQueue(), "Missing origin argument!");
                        return;
                    }
                    destination.setSlots(slot.asInt()-1, origin.getContents());
                    break;

                // Keep only items from the origin's contents in the
                // destination
                case KEEP:
                    if (origin == null) {
                        dB.echoError(scriptEntry.getResidingQueue(), "Missing origin argument!");
                        return;
                    }
                    destination.keep(origin.getContents());
                    break;

                // Exclude all items from the origin's contents in the
                // destination
                case EXCLUDE:
                    if (origin == null) {
                        dB.echoError(scriptEntry.getResidingQueue(), "Missing origin argument!");
                        return;
                    }
                    destination.exclude(origin.getContents());
                    break;

                // Add origin's contents over and over to destination
                // until it is full
                case FILL:
                    if (origin == null) {
                        dB.echoError(scriptEntry.getResidingQueue(), "Missing origin argument!");
                        return;
                    }
                    destination.fill(origin.getContents());
                    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.update())
                        dB.echoError("Only player inventories can be force-updated!");
                    break;

            }
        }
    }
}
TOP

Related Classes of net.aufdemrand.denizen.scripts.commands.item.InventoryCommand

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.