Search in sources :

Example 1 with BukkitScriptEntryData

use of net.aufdemrand.denizen.BukkitScriptEntryData in project Denizen-For-Bukkit by DenizenScript.

the class GiveCommand method parseArgs.

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
    /* Match arguments to expected variables */
    for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
        if (!scriptEntry.hasObject("qty") && arg.matchesPrefix("q", "qty", "quantity") && arg.matchesPrimitive(aH.PrimitiveType.Double)) {
            scriptEntry.addObject("qty", arg.asElement());
            scriptEntry.addObject("set_quantity", new Element(true));
        } else if (!scriptEntry.hasObject("type") && arg.matches("money", "coins")) {
            scriptEntry.addObject("type", Type.MONEY);
        } else if (!scriptEntry.hasObject("type") && arg.matches("xp", "exp", "experience")) {
            scriptEntry.addObject("type", Type.EXP);
        } else if (!scriptEntry.hasObject("engrave") && arg.matches("engrave")) {
            scriptEntry.addObject("engrave", new Element(true));
        } else if (!scriptEntry.hasObject("unlimit_stack_size") && arg.matches("unlimit_stack_size")) {
            scriptEntry.addObject("unlimit_stack_size", new Element(true));
        } else if (!scriptEntry.hasObject("items") && !scriptEntry.hasObject("type")) {
            scriptEntry.addObject("items", dList.valueOf(arg.raw_value.startsWith("item:") ? arg.raw_value.substring("item:".length()) : arg.raw_value).filter(dItem.class, scriptEntry));
        } else if (!scriptEntry.hasObject("inventory") && arg.matchesPrefix("t", "to") && arg.matchesArgumentType(dInventory.class)) {
            scriptEntry.addObject("inventory", arg.asType(dInventory.class));
        } else if (!scriptEntry.hasObject("slot") && arg.matchesPrefix("slot") && arg.matchesPrimitive(aH.PrimitiveType.Integer)) {
            scriptEntry.addObject("slot", arg.asElement());
        } else {
            arg.reportUnhandled();
        }
    }
    scriptEntry.defaultObject("type", Type.ITEM).defaultObject("engrave", new Element(false)).defaultObject("unlimit_stack_size", new Element(false)).defaultObject("qty", new Element(1)).defaultObject("slot", new Element(1));
    Type type = (Type) scriptEntry.getObject("type");
    if (type != Type.MONEY && scriptEntry.getObject("inventory") == null) {
        scriptEntry.addObject("inventory", ((BukkitScriptEntryData) scriptEntry.entryData).hasPlayer() ? ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getInventory() : null);
    }
    if (!scriptEntry.hasObject("inventory") && type != Type.MONEY) {
        throw new InvalidArgumentsException("Must specify an inventory to give to!");
    }
    if (type == Type.ITEM && scriptEntry.getObject("items") == null) {
        throw new InvalidArgumentsException("Must specify item/items!");
    }
}
Also used : BukkitScriptEntryData(net.aufdemrand.denizen.BukkitScriptEntryData) net.aufdemrand.denizencore.objects.aH(net.aufdemrand.denizencore.objects.aH) Element(net.aufdemrand.denizencore.objects.Element) net.aufdemrand.denizen.objects.dInventory(net.aufdemrand.denizen.objects.dInventory) InvalidArgumentsException(net.aufdemrand.denizencore.exceptions.InvalidArgumentsException)

Example 2 with BukkitScriptEntryData

use of net.aufdemrand.denizen.BukkitScriptEntryData in project Denizen-For-Bukkit by DenizenScript.

the class InventoryCommand method execute.

@SuppressWarnings("unchecked")
@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
    // Get objects
    List<String> actions = (List<String>) scriptEntry.getObject("actions");
    AbstractMap.SimpleEntry<Integer, dInventory> originentry = (AbstractMap.SimpleEntry<Integer, dInventory>) scriptEntry.getObject("origin");
    dInventory origin = originentry != null ? originentry.getValue() : null;
    AbstractMap.SimpleEntry<Integer, dInventory> destinationentry = (AbstractMap.SimpleEntry<Integer, dInventory>) scriptEntry.getObject("destination");
    dInventory destination = destinationentry.getValue();
    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);
                } else // Otherwise, open inventory as usual
                {
                    ((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(), originentry.getKey());
                break;
            // destination
            case KEEP:
                if (origin == null) {
                    dB.echoError(scriptEntry.getResidingQueue(), "Missing origin argument!");
                    return;
                }
                destination.keep(origin.getContents());
                break;
            // destination
            case EXCLUDE:
                if (origin == null) {
                    dB.echoError(scriptEntry.getResidingQueue(), "Missing origin argument!");
                    return;
                }
                destination.exclude(origin.getContents());
                break;
            // 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;
        }
    }
}
Also used : AbstractMap(java.util.AbstractMap) BukkitScriptEntryData(net.aufdemrand.denizen.BukkitScriptEntryData) Element(net.aufdemrand.denizencore.objects.Element) List(java.util.List) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) net.aufdemrand.denizen.objects.dInventory(net.aufdemrand.denizen.objects.dInventory)

Example 3 with BukkitScriptEntryData

use of net.aufdemrand.denizen.BukkitScriptEntryData in project Denizen-For-Bukkit by DenizenScript.

the class PushCommand method parseArgs.

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
    for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
        if (!scriptEntry.hasObject("origin") && arg.matchesPrefix("origin", "o", "source", "shooter", "s")) {
            if (arg.matchesArgumentType(dEntity.class)) {
                scriptEntry.addObject("originEntity", arg.asType(dEntity.class));
            } else if (arg.matchesArgumentType(dLocation.class)) {
                scriptEntry.addObject("originLocation", arg.asType(dLocation.class));
            } else {
                dB.echoError("Ignoring unrecognized argument: " + arg.raw_value);
            }
        } else if (!scriptEntry.hasObject("destination") && arg.matchesArgumentType(dLocation.class) && arg.matchesPrefix("destination", "d")) {
            scriptEntry.addObject("destination", arg.asType(dLocation.class));
        } else if (!scriptEntry.hasObject("duration") && arg.matchesArgumentType(Duration.class) && arg.matchesPrefix("duration", "d")) {
            scriptEntry.addObject("duration", arg.asType(Duration.class));
        } else if (!scriptEntry.hasObject("speed") && arg.matchesPrimitive(aH.PrimitiveType.Double) && arg.matchesPrefix("speed", "s")) {
            scriptEntry.addObject("speed", arg.asElement());
        } else if (!scriptEntry.hasObject("script") && (arg.matchesArgumentType(dScript.class) || arg.matchesPrefix("script"))) {
            scriptEntry.addObject("script", arg.asType(dScript.class));
        } else if (!scriptEntry.hasObject("entities") && arg.matchesArgumentList(dEntity.class)) {
            scriptEntry.addObject("entities", arg.asType(dList.class).filter(dEntity.class));
        } else if (!scriptEntry.hasObject("force_along") && arg.matches("force_along")) {
            scriptEntry.addObject("force_along", new Element(true));
        } else if (!scriptEntry.hasObject("no_rotate") && arg.matches("no_rotate")) {
            scriptEntry.addObject("no_rotate", new Element(true));
        } else if (!scriptEntry.hasObject("precision") && arg.matchesPrefix("precision")) {
            scriptEntry.addObject("precision", arg.asElement());
        } else if (!scriptEntry.hasObject("no_damage") && arg.matches("no_damage")) {
            scriptEntry.addObject("no_damage", new Element(true));
        } else if (arg.matchesPrefix("def", "define", "context")) {
            scriptEntry.addObject("definitions", arg.asType(dList.class));
        } else {
            arg.reportUnhandled();
        }
    }
    if (!scriptEntry.hasObject("originLocation")) {
        scriptEntry.defaultObject("originEntity", ((BukkitScriptEntryData) scriptEntry.entryData).hasNPC() ? ((BukkitScriptEntryData) scriptEntry.entryData).getNPC().getDenizenEntity() : null, ((BukkitScriptEntryData) scriptEntry.entryData).hasPlayer() ? ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getDenizenEntity() : null);
    }
    scriptEntry.defaultObject("speed", new Element(1.5));
    scriptEntry.defaultObject("duration", new Duration(20));
    scriptEntry.defaultObject("force_along", new Element(false));
    scriptEntry.defaultObject("precision", new Element(2));
    if (!scriptEntry.hasObject("entities")) {
        throw new InvalidArgumentsException("Must specify entity/entities!");
    }
    if (!scriptEntry.hasObject("originEntity") && !scriptEntry.hasObject("originLocation")) {
        throw new InvalidArgumentsException("Must specify an origin location!");
    }
}
Also used : BukkitScriptEntryData(net.aufdemrand.denizen.BukkitScriptEntryData) net.aufdemrand.denizen.objects.dEntity(net.aufdemrand.denizen.objects.dEntity) net.aufdemrand.denizencore.objects.dScript(net.aufdemrand.denizencore.objects.dScript) net.aufdemrand.denizencore.objects.aH(net.aufdemrand.denizencore.objects.aH) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) Element(net.aufdemrand.denizencore.objects.Element) Duration(net.aufdemrand.denizencore.objects.Duration) net.aufdemrand.denizen.objects.dLocation(net.aufdemrand.denizen.objects.dLocation) InvalidArgumentsException(net.aufdemrand.denizencore.exceptions.InvalidArgumentsException)

Example 4 with BukkitScriptEntryData

use of net.aufdemrand.denizen.BukkitScriptEntryData in project Denizen-For-Bukkit by DenizenScript.

the class ZapCommand method execute.

@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
    final dScript script = (dScript) scriptEntry.getObject("script");
    Duration duration = (Duration) scriptEntry.getObject("duration");
    dB.report(scriptEntry, getName(), ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().debug() + script.debug() + (scriptEntry.hasObject("step") ? scriptEntry.getElement("step").debug() : aH.debugObj("step", "++ (inc)")) + (duration != null ? duration.debug() : ""));
    String step = scriptEntry.hasObject("step") ? scriptEntry.getElement("step").asString() : null;
    // Let's get the current step for reference.
    String currentStep = InteractScriptHelper.getCurrentStep(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer(), script.getName());
    // Special-case for backwards compatibility: ability to use ZAP to count up steps.
    if (step == null) {
        // to '1' so it can be incremented next time.
        if (aH.matchesInteger(currentStep)) {
            step = String.valueOf(aH.getIntegerFrom(currentStep) + 1);
        } else {
            step = "1";
        }
    }
    if (step.equalsIgnoreCase(currentStep)) {
        dB.echoError(scriptEntry.getResidingQueue(), "Zapping to own current step!");
        return;
    }
    // ZAP for this script is taking place.
    if (durations.containsKey(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getSaveName() + "," + script.getName())) {
        try {
            DenizenAPI.getCurrentInstance().getServer().getScheduler().cancelTask(durations.get(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getSaveName() + "," + script.getName()));
        } catch (Exception e) {
        }
    }
    // One last thing... check for duration.
    if (duration != null && duration.getSeconds() > 0) {
        // If a DURATION is specified, the currentStep should be remembered and
        // restored after the duration.
        scriptEntry.addObject("step", new Element(currentStep));
        // And let's take away the duration that was set to avoid a re-duration
        // inception-ion-ion-ion-ion... ;)
        scriptEntry.addObject("duration", Duration.ZERO);
        // Now let's add a delayed task to set it back after the duration
        // Delays are in ticks, so let's multiply our duration (which is in seconds) by 20.
        // 20 ticks per second.
        long delay = (long) (duration.getSeconds() * 20);
        // Set delayed task and put id in a map
        dB.log("Setting delayed task 'RESET ZAP' for '" + script.identify() + "'");
        durations.put(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getSaveName() + "," + script.getName(), DenizenAPI.getCurrentInstance().getServer().getScheduler().scheduleSyncDelayedTask(DenizenAPI.getCurrentInstance(), new Runnable() {

            @Override
            public void run() {
                dB.log("Running delayed task 'RESET ZAP' for '" + script.identify() + "'");
                try {
                    durations.remove(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getSaveName() + "," + script.getName().toUpperCase());
                    execute(scriptEntry);
                } catch (CommandExecutionException e) {
                    dB.echoError(scriptEntry.getResidingQueue(), "Could not run delayed task!");
                    dB.echoError(scriptEntry.getResidingQueue(), e);
                }
            }
        }, delay));
    }
    //
    // FINALLY! ZAP! Change the step in Saves... your step is now ZAPPED!
    // Fun fact: ZAP is named in homage of ZZT-OOPs ZAP command. Google it.
    //
    DenizenAPI.getCurrentInstance().getSaves().set("Players." + ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getSaveName() + ".Scripts." + script.getName().toUpperCase() + "." + "Current Step", step);
}
Also used : BukkitScriptEntryData(net.aufdemrand.denizen.BukkitScriptEntryData) net.aufdemrand.denizencore.objects.dScript(net.aufdemrand.denizencore.objects.dScript) Element(net.aufdemrand.denizencore.objects.Element) Duration(net.aufdemrand.denizencore.objects.Duration) CommandExecutionException(net.aufdemrand.denizencore.exceptions.CommandExecutionException) InvalidArgumentsException(net.aufdemrand.denizencore.exceptions.InvalidArgumentsException) CommandExecutionException(net.aufdemrand.denizencore.exceptions.CommandExecutionException)

Example 5 with BukkitScriptEntryData

use of net.aufdemrand.denizen.BukkitScriptEntryData in project Denizen-For-Bukkit by DenizenScript.

the class CooldownCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    // Fetch objects
    dScript script = (dScript) scriptEntry.getObject("script");
    Duration duration = (Duration) scriptEntry.getObject("duration");
    Type type = (scriptEntry.hasObject("type") ? (Type) scriptEntry.getObject("type") : Type.PLAYER);
    // Report to dB
    dB.report(scriptEntry, getName(), aH.debugObj("Type", type.name()) + script.debug() + (type.name().equalsIgnoreCase("player") ? ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().debug() : "") + duration.debug());
    // Perform cooldown
    switch(type) {
        case PLAYER:
            setCooldown(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer(), duration, script.getName(), false);
            break;
        case GLOBAL:
            setCooldown(null, duration, script.getName(), true);
            break;
    }
}
Also used : BukkitScriptEntryData(net.aufdemrand.denizen.BukkitScriptEntryData) net.aufdemrand.denizencore.objects.dScript(net.aufdemrand.denizencore.objects.dScript) Duration(net.aufdemrand.denizencore.objects.Duration)

Aggregations

BukkitScriptEntryData (net.aufdemrand.denizen.BukkitScriptEntryData)61 Element (net.aufdemrand.denizencore.objects.Element)38 InvalidArgumentsException (net.aufdemrand.denizencore.exceptions.InvalidArgumentsException)19 net.aufdemrand.denizencore.objects.aH (net.aufdemrand.denizencore.objects.aH)18 net.aufdemrand.denizencore.objects.dList (net.aufdemrand.denizencore.objects.dList)18 net.aufdemrand.denizen.objects.dLocation (net.aufdemrand.denizen.objects.dLocation)13 net.aufdemrand.denizen.objects.dNPC (net.aufdemrand.denizen.objects.dNPC)13 Duration (net.aufdemrand.denizencore.objects.Duration)9 net.aufdemrand.denizen.objects.dEntity (net.aufdemrand.denizen.objects.dEntity)8 net.aufdemrand.denizencore.objects.dScript (net.aufdemrand.denizencore.objects.dScript)8 net.aufdemrand.denizen.objects.dPlayer (net.aufdemrand.denizen.objects.dPlayer)6 ScriptEntry (net.aufdemrand.denizencore.scripts.ScriptEntry)6 Player (org.bukkit.entity.Player)6 ArrayList (java.util.ArrayList)5 AssignmentTrait (net.aufdemrand.denizen.npc.traits.AssignmentTrait)5 net.aufdemrand.denizen.objects.dInventory (net.aufdemrand.denizen.objects.dInventory)5 net.aufdemrand.denizen.objects.dItem (net.aufdemrand.denizen.objects.dItem)5 CommandExecutionException (net.aufdemrand.denizencore.exceptions.CommandExecutionException)5 net.aufdemrand.denizencore.objects.dObject (net.aufdemrand.denizencore.objects.dObject)5 EventHandler (org.bukkit.event.EventHandler)5