Search in sources :

Example 1 with TagContext

use of net.aufdemrand.denizencore.tags.TagContext in project Denizen-For-Bukkit by DenizenScript.

the class SidebarCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    Element action = scriptEntry.getElement("action");
    Element elTitle = scriptEntry.getElement("title");
    Element elLines = scriptEntry.getElement("lines");
    Element elValue = scriptEntry.getElement("value");
    Element elIncrement = scriptEntry.getElement("increment");
    Element elStart = scriptEntry.getElement("start");
    Element elPlayers = scriptEntry.getElement("players");
    Element elPerPlayer = scriptEntry.getElement("per_player");
    dList players = dList.valueOf(TagManager.tag(elPlayers.asString(), new BukkitTagContext(scriptEntry, false)));
    boolean per_player = elPerPlayer.asBoolean();
    String perTitle = null;
    String perLines = null;
    String perValue = null;
    String perIncrement = null;
    String perStart = null;
    Element title = null;
    dList lines = null;
    dList value = null;
    Element increment = null;
    Element start = null;
    String debug;
    if (per_player) {
        if (elTitle != null) {
            perTitle = elTitle.asString();
        }
        if (elLines != null) {
            perLines = elLines.asString();
        }
        if (elValue != null) {
            perValue = elValue.asString();
        }
        if (elIncrement != null) {
            perIncrement = elIncrement.asString();
        }
        if (elStart != null) {
            perStart = elStart.asString();
        }
        debug = action.debug() + (elTitle != null ? elTitle.debug() : "") + (elLines != null ? elLines.debug() : "") + (elValue != null ? elValue.debug() : "") + (elIncrement != null ? elIncrement.debug() : "") + (elStart != null ? elStart.debug() : "") + players.debug();
    } else {
        BukkitTagContext context = (BukkitTagContext) DenizenAPI.getCurrentInstance().getTagContextFor(scriptEntry, false);
        if (elTitle != null) {
            title = new Element(TagManager.tag(elTitle.asString(), context));
        }
        if (elLines != null) {
            lines = dList.valueOf(TagManager.tag(elLines.asString(), context));
        }
        if (elValue != null) {
            value = dList.valueOf(TagManager.tag(elValue.asString(), context));
        }
        if (elIncrement != null) {
            increment = new Element(TagManager.tag(elIncrement.asString(), context));
        }
        if (elStart != null) {
            start = new Element(TagManager.tag(elStart.asString(), context));
        }
        debug = action.debug() + (title != null ? title.debug() : "") + (lines != null ? lines.debug() : "") + (value != null ? value.debug() : "") + (increment != null ? increment.debug() : "") + (start != null ? start.debug() : "") + players.debug();
    }
    dB.report(scriptEntry, getName(), debug);
    switch(Action.valueOf(action.asString())) {
        case ADD:
            for (dPlayer player : players.filter(dPlayer.class)) {
                if (player == null || !player.isValid()) {
                    dB.echoError("Invalid player!");
                    continue;
                }
                Sidebar sidebar = createSidebar(player);
                if (sidebar == null) {
                    continue;
                }
                List<String> current = sidebar.getLines();
                if (per_player) {
                    TagContext context = new BukkitTagContext(player, ((BukkitScriptEntryData) scriptEntry.entryData).getNPC(), false, scriptEntry, scriptEntry.shouldDebug(), scriptEntry.getScript());
                    value = dList.valueOf(TagManager.tag(perValue, context));
                    if (perLines != null) {
                        lines = dList.valueOf(TagManager.tag(perLines, context));
                    }
                }
                if (lines != null) {
                    try {
                        for (int i = 0; i < lines.size(); i++) {
                            int index = Integer.valueOf(lines.get(i)) - 1;
                            String line = value.get(i);
                            current.add(index, line);
                        }
                    } catch (Exception e) {
                        dB.echoError(e);
                        continue;
                    }
                } else {
                    current.addAll(value);
                }
                sidebar.setLines(current);
                sidebar.sendUpdate();
            }
            break;
        case REMOVE:
            for (dPlayer player : players.filter(dPlayer.class)) {
                if (player == null || !player.isValid()) {
                    dB.echoError("Invalid player!");
                    continue;
                }
                Sidebar sidebar = createSidebar(player);
                if (sidebar == null) {
                    continue;
                }
                List<String> current = sidebar.getLines();
                if (per_player) {
                    TagContext context = new BukkitTagContext(player, ((BukkitScriptEntryData) scriptEntry.entryData).getNPC(), false, scriptEntry, scriptEntry.shouldDebug(), scriptEntry.getScript());
                    if (perValue != null) {
                        value = dList.valueOf(TagManager.tag(perValue, context));
                    }
                    if (perLines != null) {
                        lines = dList.valueOf(TagManager.tag(perLines, context));
                    }
                }
                if (lines != null) {
                    try {
                        int offset = 0;
                        for (String line : lines) {
                            int index = Integer.valueOf(line) - 1 - offset;
                            current.remove(index);
                            offset++;
                        }
                    } catch (Exception e) {
                        dB.echoError(e);
                        continue;
                    }
                    sidebar.setLines(current);
                    sidebar.sendUpdate();
                } else if (value != null) {
                    try {
                        Iterator<String> it = current.iterator();
                        while (it.hasNext()) {
                            String next = it.next();
                            for (String line : value) {
                                if (next.equalsIgnoreCase(line)) {
                                    it.remove();
                                }
                            }
                        }
                        for (String line : value) {
                            for (int i = 0; i < current.size(); i++) {
                                if (current.get(i).equalsIgnoreCase(line)) {
                                    current.remove(i);
                                }
                            }
                        }
                    } catch (Exception e) {
                        dB.echoError(e);
                        continue;
                    }
                    sidebar.setLines(current);
                    sidebar.sendUpdate();
                } else {
                    sidebar.remove();
                    sidebars.remove(player.getPlayerEntity().getUniqueId());
                }
            }
            break;
        case SET:
            for (dPlayer player : players.filter(dPlayer.class)) {
                if (player == null || !player.isValid()) {
                    dB.echoError("Invalid player!");
                    continue;
                }
                Sidebar sidebar = createSidebar(player);
                if (sidebar == null) {
                    continue;
                }
                List<String> current = sidebar.getLines();
                boolean currEdited = false;
                if (per_player) {
                    TagContext context = new BukkitTagContext(player, ((BukkitScriptEntryData) scriptEntry.entryData).getNPC(), false, scriptEntry, scriptEntry.shouldDebug(), scriptEntry.getScript());
                    if (perValue != null) {
                        value = dList.valueOf(TagManager.tag(perValue, context));
                    }
                    if (perLines != null) {
                        lines = dList.valueOf(TagManager.tag(perLines, context));
                    }
                    if (perStart != null) {
                        start = new Element(TagManager.tag(perStart, context));
                    }
                    if (perIncrement != null) {
                        increment = new Element(TagManager.tag(perIncrement, context));
                    }
                    if (perTitle != null) {
                        title = new Element(TagManager.tag(perTitle, context));
                    }
                }
                if (lines != null) {
                    try {
                        for (int i = 0; i < lines.size(); i++) {
                            int index = Integer.valueOf(lines.get(i)) - 1;
                            String line = value.get(i);
                            if (index > current.size()) {
                                current.add(line);
                            } else {
                                current.set(index, line);
                            }
                        }
                    } catch (Exception e) {
                        dB.echoError(e);
                        continue;
                    }
                    currEdited = true;
                } else if (value != null) {
                    current = value;
                    currEdited = true;
                }
                if (start != null) {
                    sidebar.setStart(start.asInt());
                    currEdited = true;
                }
                if (increment != null) {
                    sidebar.setIncrement(increment.asInt());
                    currEdited = true;
                }
                if (title != null) {
                    sidebar.setTitle(title.asString());
                }
                if (currEdited) {
                    sidebar.setLines(current);
                }
                sidebar.sendUpdate();
            }
            break;
    }
}
Also used : BukkitTagContext(net.aufdemrand.denizen.tags.BukkitTagContext) BukkitTagContext(net.aufdemrand.denizen.tags.BukkitTagContext) TagContext(net.aufdemrand.denizencore.tags.TagContext) Element(net.aufdemrand.denizencore.objects.Element) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) net.aufdemrand.denizen.objects.dPlayer(net.aufdemrand.denizen.objects.dPlayer) Iterator(java.util.Iterator) Sidebar(net.aufdemrand.denizen.nms.abstracts.Sidebar) InvalidArgumentsException(net.aufdemrand.denizencore.exceptions.InvalidArgumentsException) CommandExecutionException(net.aufdemrand.denizencore.exceptions.CommandExecutionException)

Aggregations

Iterator (java.util.Iterator)1 Sidebar (net.aufdemrand.denizen.nms.abstracts.Sidebar)1 net.aufdemrand.denizen.objects.dPlayer (net.aufdemrand.denizen.objects.dPlayer)1 BukkitTagContext (net.aufdemrand.denizen.tags.BukkitTagContext)1 CommandExecutionException (net.aufdemrand.denizencore.exceptions.CommandExecutionException)1 InvalidArgumentsException (net.aufdemrand.denizencore.exceptions.InvalidArgumentsException)1 Element (net.aufdemrand.denizencore.objects.Element)1 net.aufdemrand.denizencore.objects.dList (net.aufdemrand.denizencore.objects.dList)1 TagContext (net.aufdemrand.denizencore.tags.TagContext)1