Search in sources :

Example 1 with CommandExecutionException

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

the class CreateWorldCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    Element worldName = scriptEntry.getElement("world_name");
    Element generator = scriptEntry.getElement("generator");
    Element worldType = scriptEntry.getElement("worldtype");
    Element environment = scriptEntry.getElement("environment");
    Element copy_from = scriptEntry.getElement("copy_from");
    Element seed = scriptEntry.getElement("seed");
    dB.report(scriptEntry, getName(), worldName.debug() + (generator != null ? generator.debug() : "") + environment.debug() + (copy_from != null ? copy_from.debug() : "") + worldType.debug() + (seed != null ? seed.debug() : ""));
    if (copy_from != null) {
        try {
            if (copy_from.asString().contains("..")) {
                dB.echoError(scriptEntry.getResidingQueue(), "Invalid copy from world name!");
                return;
            }
            File newFolder = new File(worldName.asString());
            File folder = new File(copy_from.asString().replace("w@", ""));
            if (!folder.exists() || !folder.isDirectory()) {
                dB.echoError(scriptEntry.getResidingQueue(), "Invalid copy from world folder - does not exist!");
                return;
            }
            FileUtils.copyDirectory(folder, newFolder);
            File file = new File(worldName.asString() + "/uid.dat");
            if (file.exists()) {
                file.delete();
            }
            File file2 = new File(worldName.asString() + "/session.lock");
            if (file2.exists()) {
                file2.delete();
            }
        } catch (Exception ex) {
            dB.echoError(ex);
            return;
        }
    }
    World world;
    WorldCreator worldCreator = WorldCreator.name(worldName.asString()).environment(World.Environment.valueOf(environment.asString().toUpperCase())).type(WorldType.valueOf(worldType.asString().toUpperCase()));
    if (generator != null) {
        worldCreator.generator(generator.asString());
    }
    if (seed != null) {
        worldCreator.seed(seed.asLong());
    }
    world = Bukkit.getServer().createWorld(worldCreator);
    if (world == null) {
        dB.echoDebug(scriptEntry, "World is null, something went wrong in creation!");
    }
}
Also used : WorldCreator(org.bukkit.WorldCreator) Element(net.aufdemrand.denizencore.objects.Element) World(org.bukkit.World) File(java.io.File) InvalidArgumentsException(net.aufdemrand.denizencore.exceptions.InvalidArgumentsException) CommandExecutionException(net.aufdemrand.denizencore.exceptions.CommandExecutionException)

Example 2 with CommandExecutionException

use of net.aufdemrand.denizencore.exceptions.CommandExecutionException 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)

Example 3 with CommandExecutionException

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

the class ListenCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    Element action = scriptEntry.getElement("action");
    Element type = scriptEntry.getElement("type");
    Element id = scriptEntry.getElement("id");
    dScript finish_script = (dScript) scriptEntry.getObject("finish_script");
    dB.report(scriptEntry, getName(), action.debug() + (type != null ? type.debug() : "") + id.debug() + (finish_script != null ? finish_script.debug() : ""));
    dB.echoError(scriptEntry.getResidingQueue(), "Warning: Listen is outdated and may become unsupported in the future.");
    List<aH.Argument> arguments = (ArrayList<aH.Argument>) scriptEntry.getObject("args");
    switch(Action.valueOf(action.asString().toUpperCase())) {
        case NEW:
            // First make sure there isn't already a 'player listener' for this player with the specified ID.
            if (DenizenAPI.getCurrentInstance().getListenerRegistry().getListenersFor(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer()) != null && DenizenAPI.getCurrentInstance().getListenerRegistry().getListenersFor(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer()).containsKey(CoreUtilities.toLowerCase(id.asString()))) {
                dB.echoError(scriptEntry.getResidingQueue(), "Cancelled creation of NEW listener! Listener ID '" + id.asString() + "' already exists!");
                break;
            }
            // Also make sure there is a valid script input
            if (finish_script == null) {
                dB.echoError("Must specify a valid script!");
                break;
            }
            try {
                DenizenAPI.getCurrentInstance().getListenerRegistry().get(type.asString()).createInstance(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer(), id.asString()).build(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer(), id.asString(), type.asString(), arguments, finish_script, ((BukkitScriptEntryData) scriptEntry.entryData).getNPC());
            } catch (Exception e) {
                dB.echoDebug(scriptEntry, "Cancelled creation of NEW listener!");
                // Why? Maybe a wrong listener type...
                if (DenizenAPI.getCurrentInstance().getListenerRegistry().get(type.asString()) == null) {
                    dB.echoError(scriptEntry.getResidingQueue(), "Invalid listener type!");
                } else // Just print the stacktrace if anything else, so we can debug other possible
                // problems.
                {
                    dB.echoError(scriptEntry.getResidingQueue(), e);
                }
                // Deconstruct the listener in case it was partially created while erroring out.
                try {
                    DenizenAPI.getCurrentInstance().getListenerRegistry().getListenerFor(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer(), id.asString()).cancel();
                } catch (Exception ex) {
                }
            }
            break;
        case FINISH:
            if (DenizenAPI.getCurrentInstance().getListenerRegistry().getListenerFor(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer(), id.asString()) != null) {
                DenizenAPI.getCurrentInstance().getListenerRegistry().getListenerFor(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer(), id.asString()).finish();
            }
            break;
        case CANCEL:
            if (((BukkitScriptEntryData) scriptEntry.entryData).getPlayer() != null) {
                if (id != null) {
                    if (DenizenAPI.getCurrentInstance().getListenerRegistry().getListenerFor(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer(), id.asString()) != null) {
                        DenizenAPI.getCurrentInstance().getListenerRegistry().getListenerFor(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer(), id.asString()).cancel();
                    } else {
                        for (AbstractListener listener : DenizenAPI.getCurrentInstance().getListenerRegistry().getListenersFor(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer()).values()) {
                            listener.cancel();
                        }
                    }
                }
            } else {
                DenizenAPI.getCurrentInstance().getSaves().set("Listeners." + ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getName() + "." + id, null);
            }
            break;
    }
}
Also used : BukkitScriptEntryData(net.aufdemrand.denizen.BukkitScriptEntryData) net.aufdemrand.denizencore.objects.dScript(net.aufdemrand.denizencore.objects.dScript) Element(net.aufdemrand.denizencore.objects.Element) net.aufdemrand.denizencore.objects.aH(net.aufdemrand.denizencore.objects.aH) ArrayList(java.util.ArrayList) AbstractListener(net.aufdemrand.denizen.listeners.AbstractListener) InvalidArgumentsException(net.aufdemrand.denizencore.exceptions.InvalidArgumentsException) CommandExecutionException(net.aufdemrand.denizencore.exceptions.CommandExecutionException)

Example 4 with CommandExecutionException

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

the class PushableCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    dNPC denizenNPC = ((BukkitScriptEntryData) scriptEntry.entryData).getNPC();
    if (denizenNPC == null) {
        throw new CommandExecutionException("No valid NPC attached to this queue!");
    }
    PushableTrait trait = denizenNPC.getPushableTrait();
    Element state = scriptEntry.getElement("state");
    Duration delay = scriptEntry.getdObject("delay");
    Element returnable = scriptEntry.getElement("return");
    if (state == null && delay == null && returnable == null) {
        state = new Element("TOGGLE");
    }
    dB.report(scriptEntry, getName(), (state != null ? state.debug() : "") + (delay != null ? delay.debug() : "") + (returnable != null ? returnable.debug() : ""));
    if (delay != null) {
        trait.setDelay(delay.getSecondsAsInt());
    }
    if (returnable != null) {
        trait.setReturnable(returnable.asBoolean());
    }
    if (state != null) {
        switch(Toggle.valueOf(state.asString().toUpperCase())) {
            case TRUE:
            case ON:
                trait.setPushable(true);
                break;
            case FALSE:
            case OFF:
                trait.setPushable(false);
                break;
            case TOGGLE:
                trait.setPushable(!trait.isPushable());
                break;
        }
    }
}
Also used : net.aufdemrand.denizen.objects.dNPC(net.aufdemrand.denizen.objects.dNPC) BukkitScriptEntryData(net.aufdemrand.denizen.BukkitScriptEntryData) Element(net.aufdemrand.denizencore.objects.Element) CommandExecutionException(net.aufdemrand.denizencore.exceptions.CommandExecutionException) Duration(net.aufdemrand.denizencore.objects.Duration) PushableTrait(net.aufdemrand.denizen.npc.traits.PushableTrait)

Example 5 with CommandExecutionException

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

the class MapCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    Element id = scriptEntry.getElement("map-id");
    dWorld create = scriptEntry.getdObject("new");
    Element reset = scriptEntry.getElement("reset");
    dLocation resetLoc = scriptEntry.getdObject("reset-loc");
    Element image = scriptEntry.getElement("image");
    dScript script = scriptEntry.getdObject("script");
    Element resize = scriptEntry.getElement("resize");
    Element width = scriptEntry.getElement("width");
    Element height = scriptEntry.getElement("height");
    Element x = scriptEntry.getElement("x-value");
    Element y = scriptEntry.getElement("y-value");
    dB.report(scriptEntry, getName(), (id != null ? id.debug() : "") + (create != null ? create.debug() : "") + reset.debug() + (resetLoc != null ? resetLoc.debug() : "") + (image != null ? image.debug() : "") + (script != null ? script.debug() : "") + resize.debug() + (width != null ? width.debug() : "") + (height != null ? height.debug() : "") + x.debug() + y.debug());
    MapView map = null;
    if (create != null) {
        map = Bukkit.getServer().createMap(create.getWorld());
        scriptEntry.addObject("created_map", new Element(map.getId()));
    } else if (id != null) {
        map = Bukkit.getServer().getMap((short) id.asInt());
        if (map == null) {
            throw new CommandExecutionException("No map found for ID '" + id.asInt() + "'!");
        }
    } else {
        throw new CommandExecutionException("The map command failed somehow! Report this to a developer!");
    }
    if (reset.asBoolean()) {
        List<MapRenderer> oldRenderers = DenizenMapManager.removeDenizenRenderers(map);
        for (MapRenderer renderer : oldRenderers) {
            map.addRenderer(renderer);
        }
        if (resetLoc != null) {
            map.setCenterX(resetLoc.getBlockX());
            map.setCenterZ(resetLoc.getBlockZ());
            map.setWorld(resetLoc.getWorld());
        }
    } else if (script != null) {
        DenizenMapManager.removeDenizenRenderers(map);
        ((MapScriptContainer) script.getContainer()).applyTo(map);
    } else {
        DenizenMapRenderer dmr = DenizenMapManager.getDenizenRenderer(map);
        if (image != null) {
            int wide = width != null ? width.asInt() : resize.asBoolean() ? 128 : 0;
            int high = height != null ? height.asInt() : resize.asBoolean() ? 128 : 0;
            if (CoreUtilities.toLowerCase(image.asString()).endsWith(".gif")) {
                dmr.addObject(new MapAnimatedImage(x.asString(), y.asString(), "true", false, image.asString(), wide, high));
            } else {
                dmr.addObject(new MapImage(x.asString(), y.asString(), "true", false, image.asString(), wide, high));
            }
        }
    }
}
Also used : MapImage(net.aufdemrand.denizen.utilities.maps.MapImage) MapAnimatedImage(net.aufdemrand.denizen.utilities.maps.MapAnimatedImage) net.aufdemrand.denizencore.objects.dScript(net.aufdemrand.denizencore.objects.dScript) DenizenMapRenderer(net.aufdemrand.denizen.utilities.maps.DenizenMapRenderer) MapRenderer(org.bukkit.map.MapRenderer) Element(net.aufdemrand.denizencore.objects.Element) MapView(org.bukkit.map.MapView) CommandExecutionException(net.aufdemrand.denizencore.exceptions.CommandExecutionException) DenizenMapRenderer(net.aufdemrand.denizen.utilities.maps.DenizenMapRenderer) net.aufdemrand.denizen.objects.dWorld(net.aufdemrand.denizen.objects.dWorld) net.aufdemrand.denizen.objects.dLocation(net.aufdemrand.denizen.objects.dLocation)

Aggregations

CommandExecutionException (net.aufdemrand.denizencore.exceptions.CommandExecutionException)21 InvalidArgumentsException (net.aufdemrand.denizencore.exceptions.InvalidArgumentsException)17 Element (net.aufdemrand.denizencore.objects.Element)16 net.aufdemrand.denizen.objects.dLocation (net.aufdemrand.denizen.objects.dLocation)8 net.aufdemrand.denizencore.objects.dList (net.aufdemrand.denizencore.objects.dList)8 Duration (net.aufdemrand.denizencore.objects.Duration)6 BukkitScriptEntryData (net.aufdemrand.denizen.BukkitScriptEntryData)5 net.aufdemrand.denizencore.objects.dScript (net.aufdemrand.denizencore.objects.dScript)5 List (java.util.List)4 net.aufdemrand.denizen.objects.dEntity (net.aufdemrand.denizen.objects.dEntity)4 ScriptEntry (net.aufdemrand.denizencore.scripts.ScriptEntry)4 Player (org.bukkit.entity.Player)4 File (java.io.File)3 net.aufdemrand.denizen.objects.dNPC (net.aufdemrand.denizen.objects.dNPC)3 net.aufdemrand.denizen.objects.dPlayer (net.aufdemrand.denizen.objects.dPlayer)3 net.aufdemrand.denizencore.objects.dObject (net.aufdemrand.denizencore.objects.dObject)3 ScriptQueue (net.aufdemrand.denizencore.scripts.queues.ScriptQueue)3 Location (org.bukkit.Location)2 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)2 Vector (org.bukkit.util.Vector)2