From 909088c6a85ce96e6f9138e6ca3f80aed15d7fa3 Mon Sep 17 00:00:00 2001 From: MorphanOne Date: Fri, 19 Jul 2013 16:27:27 -0400 Subject: [PATCH] Make looping work in the run command 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. --- .../denizen/scripts/ScriptQueue.java | 45 ++++++++++++++++--- .../scripts/commands/core/RunCommand.java | 10 ++++- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/src/main/java/net/aufdemrand/denizen/scripts/ScriptQueue.java b/src/main/java/net/aufdemrand/denizen/scripts/ScriptQueue.java index 8e07b4f19a..8e5b8d9d5c 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/ScriptQueue.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/ScriptQueue.java @@ -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 scriptEntries = new ArrayList(); @@ -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) { @@ -182,6 +189,7 @@ public void delayUntil(long delayTime) { boolean is_stopping = false; public void stop() { + loopTimes++; if (!is_stopping) { is_stopping = true; List entries = lastEntryExecuted.getScript() @@ -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 scriptEntriesClone = new ArrayList(); 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; @@ -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 @@ -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); } } diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/RunCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/RunCommand.java index b229e9028c..c1c82c5b4c 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/RunCommand.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/RunCommand.java @@ -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()); @@ -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();