Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add INJECT command to handle last missing part of deprecating runtask…
…. INJECT 'runs' a script in the same queue.
- Loading branch information
1 parent
d18cccd
commit 2052a0c
Showing
2 changed files
with
120 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/main/java/net/aufdemrand/denizen/scripts/commands/core/InjectCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
} | ||
|
||
} |