Skip to content

Commit

Permalink
Add INJECT command to handle last missing part of deprecating runtask…
Browse files Browse the repository at this point in the history
…. INJECT 'runs' a script in the same queue.
  • Loading branch information
aufdemrand committed Sep 8, 2013
1 parent d18cccd commit 2052a0c
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 6 deletions.
Expand Up @@ -1008,7 +1008,26 @@ public void registerCoreMembers() {
// -->
registerCoreMember(InventoryCommand.class,
"INVENTORY", "inventory [open/copy/move/swap/add/remove/keep/exclude/fill/clear] [destination:<inventory>] (origin:<inventory>)", 2);


// <--[command]
// @Name Inject
// @Usage inject (locally) [<script>] (path:<name>) (instantly)
// @Required 1
// @Stable 1.0
// @Short Runs a script in the current ScriptQueue.
// @Author aufdemrand
// @Description
// Todo
// @Tags
// Todo
// @Usage
// Todo
// @Example
// Todo
// -->
registerCoreMember(InjectCommand.class,
"INJECT", "inject (locally) [<script>] (path:<name>) (instantly)", 1);

// <--[command]
// @Name Invisible
// @Usage invisible [player/npc] [state:true/false/toggle]
Expand Down Expand Up @@ -1467,11 +1486,11 @@ public void registerCoreMembers() {

// <--[command]
// @Name Run
// @Usage run [<script>] (path:<name>) (as:<player>/<npc>) (define:<element>|...) (id:<name>) (delay:<value>) (loop) (qty:<#>)
// @Usage run (locally) [<script>] (path:<name>) (as:<player>/<npc>) (def:<element>|...) (id:<name>) (instantly) (delay:<value>)
// @Required 1
// @Stable Todo
// @Short Todo
// @Author Todo
// @Stable 1.0
// @Short Runs a script in a new ScriptQueue.
// @Author aufdemrand
// @Description
// Todo
// @Tags
Expand All @@ -1482,7 +1501,7 @@ public void registerCoreMembers() {
// Todo
// -->
registerCoreMember(RunCommand.class,
"RUN", "run [<script>] (path:<name>) (as:<player>/<npc>) (define:<element>|...) (id:<name>) (delay:<value>) (loop) (qty:<#>)", 1);
"RUN", "run (locally) [<script>] (path:<name>) (as:<player>/<npc>) (def:<element>|...) (id:<name>) (instantly) (delay:<value>)", 1);

// <--[command]
// @Name RunTask
Expand Down
@@ -0,0 +1,95 @@
package net.aufdemrand.denizen.scripts.commands.core;

import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.scripts.queues.ScriptQueue;
import net.aufdemrand.denizen.scripts.queues.core.InstantQueue;
import net.aufdemrand.denizen.scripts.queues.core.TimedQueue;
import net.aufdemrand.denizen.utilities.debugging.dB;

import java.util.List;

/**
* Injects a task script in the current ScriptQueue.
* This replaces the now-deprecated runtask command without the queue argument.
*
* @author Jeremy Schroeder
*
*/
public class InjectCommand extends AbstractCommand {

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

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

if (arg.matches("instant, instantly"))
scriptEntry.addObject("instant", new Element(true));

else if (arg.matches("local, locally"))
scriptEntry.addObject("local", new Element(true));

else if (!scriptEntry.hasObject("script")
&& arg.matchesArgumentType(dScript.class))
scriptEntry.addObject("script", arg.asType(dScript.class));

else if (!scriptEntry.hasObject("path"))
scriptEntry.addObject("path", arg.asElement());

}

if (!scriptEntry.hasObject("script") && !scriptEntry.hasObject("local"))
throw new InvalidArgumentsException("Must define a SCRIPT to be injected.");

if (!scriptEntry.hasObject("path") && scriptEntry.hasObject("local"))
throw new InvalidArgumentsException("Must specify a PATH.");

}

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {

dB.report(getName(),
(scriptEntry.hasObject("script") ? scriptEntry.getdObject("script").debug() : scriptEntry.getScript().debug())
+ (scriptEntry.hasObject("instant") ? scriptEntry.getdObject("instant").debug() : "")
+ (scriptEntry.hasObject("path") ? scriptEntry.getElement("path").debug() : "")
+ (scriptEntry.hasObject("local") ? scriptEntry.getElement("local").debug() : ""));

// Get the script
dScript script = (dScript) scriptEntry.getObject("script");

// Get the entries
List<ScriptEntry> entries;
// If it's local
if (scriptEntry.hasObject("local"))
entries = scriptEntry.getScript().getContainer().getEntries(
scriptEntry.getPlayer(),
scriptEntry.getNPC(),
scriptEntry.getElement("path").asString());

// If it has a path
else if (scriptEntry.hasObject("path"))
entries = script.getContainer().getEntries(
scriptEntry.getPlayer(),
scriptEntry.getNPC(),
scriptEntry.getElement("path").asString());

// Else, assume standard path
else entries = script.getContainer().getBaseEntries(
scriptEntry.getPlayer(),
scriptEntry.getNPC());

// If 'instantly' was specified, make each entry 'instant'.
if (scriptEntry.hasObject("instant"))
for (ScriptEntry entry : entries)
entry.setInstant(true);

// Inject the entries into the current scriptqueue
scriptEntry.getResidingQueue().injectEntries(entries, 0);

}

}

0 comments on commit 2052a0c

Please sign in to comment.