Skip to content

Commit

Permalink
Make looping work in the run command
Browse files Browse the repository at this point in the history
There's a bit of a bug, where if you change a flag while using the loop,
the script won't recognize it until all loops are done.
  • Loading branch information
Morphan1 committed Jul 19, 2013
1 parent c4eb965 commit 909088c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
45 changes: 38 additions & 7 deletions src/main/java/net/aufdemrand/denizen/scripts/ScriptQueue.java
Expand Up @@ -108,8 +108,11 @@ public static boolean _queueExists(String id) {
// Keep track of Bukit's Scheduler taskId for the engine, for when it times out.
protected int taskId;

//The speed of the engine, the # of ticks between each revolution.
// The speed of the engine, the # of ticks between each revolution.
protected int ticks;

// How many times the queue should loop
protected int loopQty = 1;

// List of ScriptEntries in the queue
List<ScriptEntry> scriptEntries = new ArrayList<ScriptEntry>();
Expand Down Expand Up @@ -168,6 +171,10 @@ public boolean isPaused() {
public void setSpeed(int ticks) {
this.ticks = ticks;
}

public void setLoop(int loopQty) {
this.loopQty = loopQty;
}


public void delayFor(long delayTicks) {
Expand All @@ -182,6 +189,7 @@ public void delayUntil(long delayTime) {
boolean is_stopping = false;

public void stop() {
loopTimes++;
if (!is_stopping) {
is_stopping = true;
List<ScriptEntry> entries = lastEntryExecuted.getScript()
Expand All @@ -199,17 +207,28 @@ public void stop() {
_queues.remove(id);
dB.echoDebug("Completing queue " + id + "...");
Bukkit.getServer().getScheduler().cancelTask(taskId);
isStarted = false;
}
}


private boolean isStarted = false;
private int loopTimes = 0;

List<ScriptEntry> scriptEntriesClone = new ArrayList<ScriptEntry>();

public void start() {
// If already started, no need to restart.
if (isStarted) return;

if (isStarted)
return;
if (loopQty > 1) {
if (loopTimes == 0)
scriptEntriesClone.addAll(scriptEntries);
else {
scriptEntries.clear();
scriptEntries.addAll(scriptEntriesClone);
}
}
// Start the queue
dB.echoDebug("Starting queue " + id + ". (Speed=" + ticks + "tpr)");
isStarted = true;
Expand Down Expand Up @@ -245,10 +264,16 @@ public void run() {

private void revolve() {
// If entries queued up are empty, deconstruct the queue.
if (scriptEntries.isEmpty())
{
if (scriptEntries.isEmpty()) {
stop();
is_stopping = false;
isStarted = false;
if (loopTimes < loopQty)
Bukkit.getScheduler().scheduleSyncDelayedTask(DenizenAPI.getCurrentInstance(), new Runnable() {
public void run() {
start();
}
}, 10);
return;
}
// Check if this Queue isn't paused
Expand All @@ -258,10 +283,16 @@ private void revolve() {

// Criteria met for a successful 'revolution' of this queue...
DenizenAPI.getCurrentInstance().getScriptEngine().revolve(this);
if (scriptEntries.isEmpty())
{
if (scriptEntries.isEmpty()) {
stop();
is_stopping = false;
isStarted = false;
if (loopTimes < loopQty)
Bukkit.getScheduler().scheduleSyncDelayedTask(DenizenAPI.getCurrentInstance(), new Runnable() {
public void run() {
start();
}
}, delayTicks + 1);
}
}

Expand Down
Expand Up @@ -74,7 +74,7 @@ else if (arg.matchesPrefix("delay")
else if (arg.matches("loop"))
scriptEntry.addObject("loop", Element.TRUE);

else if (arg.matchesPrefix("q, quantity")
else if (arg.matchesPrefix("q, qty, quantity")
&& arg.matchesPrimitive(aH.PrimitiveType.Integer))
scriptEntry.addObject("quantity", arg.asElement());

Expand Down Expand Up @@ -146,6 +146,14 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
x++;
}
}

// Run a loop on the script
if (scriptEntry.hasObject("loop")) {
int qty = 1;
if (scriptEntry.hasObject("quantity"))
qty = ((Element) scriptEntry.getObject("quantity")).asInt();
queue.setLoop(qty);
}

// OK, GO!
queue.start();
Expand Down

0 comments on commit 909088c

Please sign in to comment.