Search in sources :

Example 1 with ScriptEntryCreationException

use of net.aufdemrand.denizencore.exceptions.ScriptEntryCreationException in project Denizen-For-Bukkit by DenizenScript.

the class RepeatCommand method execute.

@SuppressWarnings("unchecked")
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    Element stop = scriptEntry.getElement("stop");
    Element next = scriptEntry.getElement("next");
    Element callback = scriptEntry.getElement("callback");
    Element quantity = scriptEntry.getElement("qty");
    if (stop != null && stop.asBoolean()) {
        // Report to dB
        dB.report(scriptEntry, getName(), stop.debug());
        boolean hasnext = false;
        for (int i = 0; i < scriptEntry.getResidingQueue().getQueueSize(); i++) {
            ScriptEntry entry = scriptEntry.getResidingQueue().getEntry(i);
            List<String> args = entry.getOriginalArguments();
            if (entry.getCommandName().equalsIgnoreCase("repeat") && args.size() > 0 && args.get(0).equalsIgnoreCase("\0CALLBACK")) {
                hasnext = true;
                break;
            }
        }
        if (hasnext) {
            while (scriptEntry.getResidingQueue().getQueueSize() > 0) {
                ScriptEntry entry = scriptEntry.getResidingQueue().getEntry(0);
                List<String> args = entry.getOriginalArguments();
                if (entry.getCommandName().equalsIgnoreCase("repeat") && args.size() > 0 && args.get(0).equalsIgnoreCase("\0CALLBACK")) {
                    scriptEntry.getResidingQueue().removeEntry(0);
                    break;
                }
                scriptEntry.getResidingQueue().removeEntry(0);
            }
        } else {
            dB.echoError("Cannot stop repeat: not in one!");
        }
        return;
    } else if (next != null && next.asBoolean()) {
        // Report to dB
        dB.report(scriptEntry, getName(), next.debug());
        boolean hasnext = false;
        for (int i = 0; i < scriptEntry.getResidingQueue().getQueueSize(); i++) {
            ScriptEntry entry = scriptEntry.getResidingQueue().getEntry(i);
            List<String> args = entry.getOriginalArguments();
            if (entry.getCommandName().equalsIgnoreCase("repeat") && args.size() > 0 && args.get(0).equalsIgnoreCase("\0CALLBACK")) {
                hasnext = true;
                break;
            }
        }
        if (hasnext) {
            while (scriptEntry.getResidingQueue().getQueueSize() > 0) {
                ScriptEntry entry = scriptEntry.getResidingQueue().getEntry(0);
                List<String> args = entry.getOriginalArguments();
                if (entry.getCommandName().equalsIgnoreCase("repeat") && args.size() > 0 && args.get(0).equalsIgnoreCase("\0CALLBACK")) {
                    break;
                }
                scriptEntry.getResidingQueue().removeEntry(0);
            }
        } else {
            dB.echoError("Cannot stop repeat: not in one!");
        }
        return;
    } else if (callback != null && callback.asBoolean()) {
        if (scriptEntry.getOwner() != null && (scriptEntry.getOwner().getCommandName().equalsIgnoreCase("repeat") || scriptEntry.getOwner().getBracedSet() == null || scriptEntry.getOwner().getBracedSet().size() == 0 || scriptEntry.getBracedSet().get(0).value.get(scriptEntry.getBracedSet().get(0).value.size() - 1) != scriptEntry)) {
            RepeatData data = (RepeatData) scriptEntry.getOwner().getData();
            data.index++;
            if (data.index <= data.target) {
                dB.echoDebug(scriptEntry, DebugElement.Header, "Repeat loop " + data.index);
                scriptEntry.getResidingQueue().addDefinition("value", String.valueOf(data.index));
                List<ScriptEntry> bracedCommands = BracedCommand.getBracedCommands(scriptEntry.getOwner()).get(0).value;
                ScriptEntry callbackEntry = null;
                try {
                    callbackEntry = new ScriptEntry("REPEAT", new String[] { "\0CALLBACK" }, (scriptEntry.getScript() != null ? scriptEntry.getScript().getContainer() : null));
                    callbackEntry.copyFrom(scriptEntry);
                } catch (ScriptEntryCreationException e) {
                    dB.echoError(e);
                }
                callbackEntry.setOwner(scriptEntry.getOwner());
                bracedCommands.add(callbackEntry);
                for (int i = 0; i < bracedCommands.size(); i++) {
                    bracedCommands.get(i).setInstant(true);
                    bracedCommands.get(i).addObject("reqId", scriptEntry.getObject("reqId"));
                }
                scriptEntry.getResidingQueue().injectEntries(bracedCommands, 0);
            }
        } else {
            dB.echoError("Repeat CALLBACK invalid: not a real callback!");
        }
    } else {
        // Get objects
        List<ScriptEntry> bracedCommandsList = ((List<BracedData>) scriptEntry.getObject("braces")).get(0).value;
        if (bracedCommandsList == null || bracedCommandsList.isEmpty()) {
            dB.echoError("Empty braces!");
            return;
        }
        // Report to dB
        dB.report(scriptEntry, getName(), quantity.debug());
        int target = quantity.asInt();
        if (target <= 0) {
            dB.echoDebug(scriptEntry, "Zero count, not looping...");
            return;
        }
        RepeatData datum = new RepeatData();
        datum.target = target;
        datum.index = 1;
        scriptEntry.setData(datum);
        ScriptEntry callbackEntry = null;
        try {
            callbackEntry = new ScriptEntry("REPEAT", new String[] { "\0CALLBACK" }, (scriptEntry.getScript() != null ? scriptEntry.getScript().getContainer() : null));
            callbackEntry.copyFrom(scriptEntry);
        } catch (ScriptEntryCreationException e) {
            dB.echoError(e);
        }
        callbackEntry.setOwner(scriptEntry);
        bracedCommandsList.add(callbackEntry);
        scriptEntry.getResidingQueue().addDefinition("value", "1");
        for (int i = 0; i < bracedCommandsList.size(); i++) {
            bracedCommandsList.get(i).setInstant(true);
            bracedCommandsList.get(i).addObject("reqId", scriptEntry.getObject("reqId"));
        }
        scriptEntry.getResidingQueue().injectEntries(bracedCommandsList, 0);
    }
}
Also used : Element(net.aufdemrand.denizencore.objects.Element) DebugElement(net.aufdemrand.denizencore.utilities.debugging.dB.DebugElement) ScriptEntry(net.aufdemrand.denizencore.scripts.ScriptEntry) List(java.util.List) ScriptEntryCreationException(net.aufdemrand.denizencore.exceptions.ScriptEntryCreationException)

Example 2 with ScriptEntryCreationException

use of net.aufdemrand.denizencore.exceptions.ScriptEntryCreationException in project Denizen-For-Bukkit by DenizenScript.

the class WhileCommand method execute.

@SuppressWarnings("unchecked")
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    Element stop = scriptEntry.getElement("stop");
    Element next = scriptEntry.getElement("next");
    Element callback = scriptEntry.getElement("callback");
    if (stop != null && stop.asBoolean()) {
        // Report to dB
        dB.report(scriptEntry, getName(), stop.debug());
        boolean hasnext = false;
        for (int i = 0; i < scriptEntry.getResidingQueue().getQueueSize(); i++) {
            ScriptEntry entry = scriptEntry.getResidingQueue().getEntry(i);
            List<String> args = entry.getOriginalArguments();
            if (entry.getCommandName().equalsIgnoreCase("while") && args.size() > 0 && args.get(0).equalsIgnoreCase("\0CALLBACK")) {
                hasnext = true;
                break;
            }
        }
        if (hasnext) {
            while (scriptEntry.getResidingQueue().getQueueSize() > 0) {
                ScriptEntry entry = scriptEntry.getResidingQueue().getEntry(0);
                List<String> args = entry.getOriginalArguments();
                if (entry.getCommandName().equalsIgnoreCase("while") && args.size() > 0 && args.get(0).equalsIgnoreCase("\0CALLBACK")) {
                    scriptEntry.getResidingQueue().removeEntry(0);
                    break;
                }
                scriptEntry.getResidingQueue().removeEntry(0);
            }
        } else {
            dB.echoError(scriptEntry.getResidingQueue(), "Cannot stop while: not in one!");
        }
        return;
    } else if (next != null && next.asBoolean()) {
        // Report to dB
        dB.report(scriptEntry, getName(), next.debug());
        boolean hasnext = false;
        for (int i = 0; i < scriptEntry.getResidingQueue().getQueueSize(); i++) {
            ScriptEntry entry = scriptEntry.getResidingQueue().getEntry(i);
            List<String> args = entry.getOriginalArguments();
            if (entry.getCommandName().equalsIgnoreCase("while") && args.size() > 0 && args.get(0).equalsIgnoreCase("\0CALLBACK")) {
                hasnext = true;
                break;
            }
        }
        if (hasnext) {
            while (scriptEntry.getResidingQueue().getQueueSize() > 0) {
                ScriptEntry entry = scriptEntry.getResidingQueue().getEntry(0);
                List<String> args = entry.getOriginalArguments();
                if (entry.getCommandName().equalsIgnoreCase("while") && args.size() > 0 && args.get(0).equalsIgnoreCase("\0CALLBACK")) {
                    break;
                }
                scriptEntry.getResidingQueue().removeEntry(0);
            }
        } else {
            dB.echoError(scriptEntry.getResidingQueue(), "Cannot stop while: not in one!");
        }
        return;
    } else if (callback != null && callback.asBoolean()) {
        if (scriptEntry.getOwner() != null && (scriptEntry.getOwner().getCommandName().equalsIgnoreCase("while") || scriptEntry.getOwner().getBracedSet() == null || scriptEntry.getOwner().getBracedSet().size() == 0 || scriptEntry.getBracedSet().get(0).value.get(scriptEntry.getBracedSet().get(0).value.size() - 1) != scriptEntry)) {
            WhileData data = (WhileData) scriptEntry.getOwner().getData();
            data.index++;
            if (System.currentTimeMillis() - data.LastChecked < 50) {
                data.instaTicks++;
                int max = Settings.whileMaxLoops();
                if (data.instaTicks > max && max != 0) {
                    return;
                }
            } else {
                data.instaTicks = 0;
            }
            data.LastChecked = System.currentTimeMillis();
            if (TagManager.tag(data.value, DenizenCore.getImplementation().getTagContextFor(scriptEntry, false)).equalsIgnoreCase("true")) {
                dB.echoDebug(scriptEntry, DebugElement.Header, "While loop " + data.index);
                scriptEntry.getResidingQueue().addDefinition("loop_index", String.valueOf(data.index));
                List<ScriptEntry> bracedCommands = BracedCommand.getBracedCommands(scriptEntry.getOwner()).get(0).value;
                ScriptEntry callbackEntry = null;
                try {
                    callbackEntry = new ScriptEntry("WHILE", new String[] { "\0CALLBACK" }, (scriptEntry.getScript() != null ? scriptEntry.getScript().getContainer() : null));
                    callbackEntry.copyFrom(scriptEntry);
                } catch (ScriptEntryCreationException e) {
                    dB.echoError(scriptEntry.getResidingQueue(), e);
                }
                callbackEntry.setOwner(scriptEntry.getOwner());
                bracedCommands.add(callbackEntry);
                for (int i = 0; i < bracedCommands.size(); i++) {
                    bracedCommands.get(i).setInstant(true);
                    bracedCommands.get(i).addObject("reqId", scriptEntry.getObject("reqId"));
                }
                scriptEntry.getResidingQueue().injectEntries(bracedCommands, 0);
            }
        } else {
            dB.echoError(scriptEntry.getResidingQueue(), "While CALLBACK invalid: not a real callback!");
        }
    } else {
        // Get objects
        Element value = scriptEntry.getElement("value");
        Element parsed_value = scriptEntry.getElement("parsed_value");
        List<ScriptEntry> bracedCommandsList = ((List<BracedData>) scriptEntry.getObject("braces")).get(0).value;
        if (bracedCommandsList == null || bracedCommandsList.isEmpty()) {
            dB.echoError(scriptEntry.getResidingQueue(), "Empty braces!");
            return;
        }
        // Report to dB
        dB.report(scriptEntry, getName(), value.debug());
        if (!parsed_value.asString().equalsIgnoreCase("true")) {
            return;
        }
        WhileData datum = new WhileData();
        datum.index = 1;
        datum.value = value.asString();
        datum.LastChecked = System.currentTimeMillis();
        datum.instaTicks = 1;
        scriptEntry.setData(datum);
        ScriptEntry callbackEntry = null;
        try {
            callbackEntry = new ScriptEntry("WHILE", new String[] { "\0CALLBACK" }, (scriptEntry.getScript() != null ? scriptEntry.getScript().getContainer() : null));
            callbackEntry.copyFrom(scriptEntry);
        } catch (ScriptEntryCreationException e) {
            dB.echoError(scriptEntry.getResidingQueue(), e);
        }
        callbackEntry.setOwner(scriptEntry);
        bracedCommandsList.add(callbackEntry);
        scriptEntry.getResidingQueue().addDefinition("loop_index", "1");
        for (int i = 0; i < bracedCommandsList.size(); i++) {
            bracedCommandsList.get(i).setInstant(true);
            bracedCommandsList.get(i).addObject("reqId", scriptEntry.getObject("reqId"));
        }
        scriptEntry.getResidingQueue().injectEntries(bracedCommandsList, 0);
    }
}
Also used : DebugElement(net.aufdemrand.denizencore.utilities.debugging.dB.DebugElement) Element(net.aufdemrand.denizencore.objects.Element) ScriptEntry(net.aufdemrand.denizencore.scripts.ScriptEntry) List(java.util.List) ScriptEntryCreationException(net.aufdemrand.denizencore.exceptions.ScriptEntryCreationException)

Aggregations

List (java.util.List)2 ScriptEntryCreationException (net.aufdemrand.denizencore.exceptions.ScriptEntryCreationException)2 Element (net.aufdemrand.denizencore.objects.Element)2 ScriptEntry (net.aufdemrand.denizencore.scripts.ScriptEntry)2 DebugElement (net.aufdemrand.denizencore.utilities.debugging.dB.DebugElement)2