Search in sources :

Example 86 with ElementTag

use of com.denizenscript.denizencore.objects.core.ElementTag in project Denizen-For-Bukkit by DenizenScript.

the class ExecuteCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    ElementTag cmd = scriptEntry.getElement("command");
    ElementTag type = scriptEntry.getElement("type");
    boolean silent = scriptEntry.argAsBoolean("silent");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), type, cmd, db("silent", silent));
    }
    String command = cmd.asString();
    switch(Type.valueOf(type.asString())) {
        case AS_PLAYER:
            try {
                PlayerCommandPreprocessEvent pcpe = new PlayerCommandPreprocessEvent(Utilities.getEntryPlayer(scriptEntry).getPlayerEntity(), "/" + command);
                Bukkit.getPluginManager().callEvent(pcpe);
                if (!pcpe.isCancelled()) {
                    Player player = Utilities.getEntryPlayer(scriptEntry).getPlayerEntity();
                    if (silent) {
                        NetworkInterceptHelper.enable();
                        silencedPlayers.add(player.getUniqueId());
                    }
                    player.performCommand(pcpe.getMessage().startsWith("/") ? pcpe.getMessage().substring(1) : pcpe.getMessage());
                    if (silent) {
                        silencedPlayers.remove(player.getUniqueId());
                    }
                }
            } catch (Throwable e) {
                Debug.echoError(scriptEntry, "Exception while executing command as player.");
                Debug.echoError(scriptEntry, e);
            }
            break;
        case AS_OP:
            if (CoreUtilities.equalsIgnoreCase(command, "stop")) {
                Debug.echoError("Please use as_server to execute 'stop'.");
                return;
            }
            Player player = Utilities.getEntryPlayer(scriptEntry).getPlayerEntity();
            PlayerHelper playerHelper = NMSHandler.getPlayerHelper();
            boolean isOp = player.isOp();
            if (!isOp) {
                playerHelper.setTemporaryOp(player, true);
            }
            try {
                PlayerCommandPreprocessEvent pcpe = new PlayerCommandPreprocessEvent(player, "/" + command);
                Bukkit.getPluginManager().callEvent(pcpe);
                if (!pcpe.isCancelled()) {
                    if (silent) {
                        NetworkInterceptHelper.enable();
                        silencedPlayers.add(player.getUniqueId());
                    }
                    player.performCommand(pcpe.getMessage().startsWith("/") ? pcpe.getMessage().substring(1) : pcpe.getMessage());
                    if (silent) {
                        silencedPlayers.remove(player.getUniqueId());
                    }
                }
            } catch (Throwable e) {
                Debug.echoError(scriptEntry, "Exception while executing command as OP.");
                Debug.echoError(scriptEntry, e);
            }
            if (!isOp) {
                playerHelper.setTemporaryOp(player, false);
            }
            break;
        case AS_NPC:
            if (!Utilities.getEntryNPC(scriptEntry).isSpawned()) {
                Debug.echoError(scriptEntry, "Cannot EXECUTE AS_NPC unless the NPC is Spawned.");
                return;
            }
            if (Utilities.getEntryNPC(scriptEntry).getEntity().getType() != EntityType.PLAYER) {
                Debug.echoError(scriptEntry, "Cannot EXECUTE AS_NPC unless the NPC is Player-Type.");
                return;
            }
            Utilities.getEntryNPC(scriptEntry).getEntity().setOp(true);
            try {
                ((Player) Utilities.getEntryNPC(scriptEntry).getEntity()).performCommand(command);
            } catch (Throwable e) {
                Debug.echoError(scriptEntry, "Exception while executing command as NPC-OP.");
                Debug.echoError(scriptEntry, e);
            }
            Utilities.getEntryNPC(scriptEntry).getEntity().setOp(false);
            break;
        case AS_SERVER:
            dcs.clearOutput();
            dcs.silent = silent;
            ServerCommandEvent sce = new ServerCommandEvent(dcs, command);
            Bukkit.getPluginManager().callEvent(sce);
            Denizen.getInstance().getServer().dispatchCommand(dcs, sce.getCommand());
            scriptEntry.addObject("output", new ListTag(dcs.getOutput()));
            break;
    }
}
Also used : PlayerCommandPreprocessEvent(org.bukkit.event.player.PlayerCommandPreprocessEvent) Player(org.bukkit.entity.Player) PlayerHelper(com.denizenscript.denizen.nms.interfaces.PlayerHelper) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ServerCommandEvent(org.bukkit.event.server.ServerCommandEvent) ListTag(com.denizenscript.denizencore.objects.core.ListTag)

Example 87 with ElementTag

use of com.denizenscript.denizencore.objects.core.ElementTag in project Denizen-For-Bukkit by DenizenScript.

the class ChunkLoadCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    ElementTag action = scriptEntry.getElement("action");
    ListTag chunklocs = scriptEntry.getObjectTag("location");
    DurationTag length = scriptEntry.getObjectTag("duration");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), action, chunklocs, length);
    }
    for (String chunkText : chunklocs) {
        Chunk chunk;
        if (ChunkTag.matches(chunkText)) {
            chunk = ChunkTag.valueOf(chunkText, scriptEntry.context).getChunk();
        } else if (LocationTag.matches(chunkText)) {
            chunk = LocationTag.valueOf(chunkText, scriptEntry.context).getChunk();
        } else {
            Debug.echoError("Chunk input '" + chunkText + "' is invalid.");
            return;
        }
        ChunkCoordinate coord = new ChunkCoordinate(chunk);
        switch(Action.valueOf(action.asString())) {
            case ADD:
                if (length.getSeconds() != 0) {
                    chunkDelays.put(coord, System.currentTimeMillis() + length.getMillis());
                } else {
                    chunkDelays.put(coord, (long) 0);
                }
                Debug.echoDebug(scriptEntry, "...added chunk " + chunk.getX() + ", " + chunk.getZ() + " with a delay of " + length.getSeconds() + " seconds.");
                if (!chunk.isLoaded()) {
                    chunk.load();
                }
                chunk.addPluginChunkTicket(Denizen.getInstance());
                if (length.getSeconds() > 0) {
                    Bukkit.getScheduler().scheduleSyncDelayedTask(Denizen.getInstance(), () -> {
                        if (chunkDelays.containsKey(coord) && chunkDelays.get(coord) <= System.currentTimeMillis()) {
                            chunk.removePluginChunkTicket(Denizen.getInstance());
                            chunkDelays.remove(coord);
                        }
                    }, length.getTicks() + 20);
                }
                break;
            case REMOVE:
                if (chunkDelays.containsKey(coord)) {
                    chunkDelays.remove(coord);
                    chunk.removePluginChunkTicket(Denizen.getInstance());
                    Debug.echoDebug(scriptEntry, "...allowing unloading of chunk " + chunk.getX() + ", " + chunk.getZ());
                } else {
                    Debug.echoDebug(scriptEntry, "Chunk '" + coord + "' was not on the load list, ignoring.");
                }
                break;
            case REMOVEALL:
                Debug.echoDebug(scriptEntry, "...allowing unloading of all stored chunks");
                for (ChunkCoordinate loopCoord : chunkDelays.keySet()) {
                    loopCoord.getChunk().getChunk().removePluginChunkTicket(Denizen.getInstance());
                }
                chunkDelays.clear();
                break;
        }
    }
}
Also used : ChunkCoordinate(com.denizenscript.denizen.utilities.blocks.ChunkCoordinate) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag) Chunk(org.bukkit.Chunk) ListTag(com.denizenscript.denizencore.objects.core.ListTag)

Example 88 with ElementTag

use of com.denizenscript.denizencore.objects.core.ElementTag in project Denizen-For-Bukkit by DenizenScript.

the class ExplodeCommand method parseArgs.

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
    for (Argument arg : scriptEntry) {
        if (!scriptEntry.hasObject("location") && arg.matchesArgumentType(LocationTag.class)) {
            scriptEntry.addObject("location", arg.asType(LocationTag.class));
        } else if (!scriptEntry.hasObject("power") && arg.matchesFloat() && arg.matchesPrefix("power", "p")) {
            scriptEntry.addObject("power", arg.asElement());
        } else if (!scriptEntry.hasObject("source") && arg.matchesArgumentType(EntityTag.class) && arg.matchesPrefix("source")) {
            scriptEntry.addObject("source", arg.asType(EntityTag.class));
        } else if (!scriptEntry.hasObject("breakblocks") && arg.matches("breakblocks")) {
            scriptEntry.addObject("breakblocks", new ElementTag(true));
        } else if (!scriptEntry.hasObject("fire") && arg.matches("fire")) {
            scriptEntry.addObject("fire", new ElementTag(true));
        } else {
            arg.reportUnhandled();
        }
    }
    scriptEntry.defaultObject("power", new ElementTag(1.0));
    scriptEntry.defaultObject("fire", new ElementTag(false));
    scriptEntry.defaultObject("breakblocks", new ElementTag(false));
    scriptEntry.defaultObject("location", Utilities.entryDefaultLocation(scriptEntry, false));
    if (!scriptEntry.hasObject("location")) {
        throw new InvalidArgumentsException("Missing location argument!");
    }
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) Argument(com.denizenscript.denizencore.objects.Argument) EntityTag(com.denizenscript.denizen.objects.EntityTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException)

Example 89 with ElementTag

use of com.denizenscript.denizencore.objects.core.ElementTag in project Denizen-For-Bukkit by DenizenScript.

the class PlayEffectCommand method parseArgs.

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
    ParticleHelper particleHelper = NMSHandler.getParticleHelper();
    for (Argument arg : scriptEntry) {
        if (!scriptEntry.hasObject("location") && arg.matchesArgumentList(LocationTag.class) && (arg.matchesPrefix("at") || !arg.hasPrefix())) {
            if (arg.matchesPrefix("at")) {
                scriptEntry.addObject("no_offset", new ElementTag(true));
            }
            scriptEntry.addObject("location", arg.asType(ListTag.class).filter(LocationTag.class, scriptEntry));
            continue;
        } else if (!scriptEntry.hasObject("effect") && !scriptEntry.hasObject("particleeffect") && !scriptEntry.hasObject("iconcrack")) {
            if (particleHelper.hasParticle(arg.getValue())) {
                scriptEntry.addObject("particleeffect", particleHelper.getParticle(arg.getValue()));
                continue;
            } else if (arg.matches("barrier") && NMSHandler.getVersion().isAtLeast(NMSVersion.v1_18)) {
                scriptEntry.addObject("particleeffect", particleHelper.getParticle("block_marker"));
                scriptEntry.addObject("special_data", new ElementTag("barrier"));
                continue;
            } else if (arg.matches("random")) {
                // Get another effect if "RANDOM" is used
                List<Particle> visible = particleHelper.getVisibleParticles();
                scriptEntry.addObject("particleeffect", visible.get(CoreUtilities.getRandom().nextInt(visible.size())));
                continue;
            } else if (arg.startsWith("iconcrack_")) {
                Deprecations.oldPlayEffectSpecials.warn(scriptEntry);
                // Allow iconcrack_[item] for item break effects (ex: iconcrack_stone)
                String shrunk = arg.getValue().substring("iconcrack_".length());
                ItemTag item = ItemTag.valueOf(shrunk, scriptEntry.context);
                if (item != null) {
                    scriptEntry.addObject("iconcrack", item);
                } else {
                    Debug.echoError("Invalid iconcrack_[item]. Must be a valid ItemTag!");
                }
                continue;
            } else if (arg.matchesEnum(Effect.class)) {
                scriptEntry.addObject("effect", Effect.valueOf(arg.getValue().toUpperCase()));
                continue;
            }
        }
        if (!scriptEntry.hasObject("radius") && arg.matchesFloat() && arg.matchesPrefix("visibility", "v", "radius", "r")) {
            scriptEntry.addObject("radius", arg.asElement());
        } else if (!scriptEntry.hasObject("data") && arg.matchesFloat() && arg.matchesPrefix("data", "d")) {
            scriptEntry.addObject("data", arg.asElement());
        } else if (!scriptEntry.hasObject("special_data") && arg.matchesPrefix("special_data")) {
            scriptEntry.addObject("special_data", arg.asElement());
        } else if (!scriptEntry.hasObject("quantity") && arg.matchesInteger() && arg.matchesPrefix("qty", "q", "quantity")) {
            if (arg.matchesPrefix("q", "qty")) {
                Deprecations.qtyTags.warn(scriptEntry);
            }
            scriptEntry.addObject("quantity", arg.asElement());
        } else if (!scriptEntry.hasObject("offset") && arg.matchesFloat() && arg.matchesPrefix("offset", "o")) {
            double offset = arg.asElement().asDouble();
            scriptEntry.addObject("offset", new LocationTag(null, offset, offset, offset));
        } else if (!scriptEntry.hasObject("offset") && arg.matchesArgumentType(LocationTag.class) && arg.matchesPrefix("offset", "o")) {
            scriptEntry.addObject("offset", arg.asType(LocationTag.class));
        } else if (!scriptEntry.hasObject("velocity") && arg.matchesArgumentType(LocationTag.class) && arg.matchesPrefix("velocity")) {
            scriptEntry.addObject("velocity", arg.asType(LocationTag.class));
        } else if (!scriptEntry.hasObject("targets") && arg.matchesArgumentList(PlayerTag.class) && arg.matchesPrefix("targets", "target", "t")) {
            scriptEntry.addObject("targets", arg.asType(ListTag.class).filter(PlayerTag.class, scriptEntry));
        } else {
            arg.reportUnhandled();
        }
    }
    scriptEntry.defaultObject("data", new ElementTag(0));
    scriptEntry.defaultObject("radius", new ElementTag(15));
    scriptEntry.defaultObject("quantity", new ElementTag(1));
    scriptEntry.defaultObject("offset", new LocationTag(null, 0.5, 0.5, 0.5));
    if (!scriptEntry.hasObject("effect") && !scriptEntry.hasObject("particleeffect") && !scriptEntry.hasObject("iconcrack")) {
        throw new InvalidArgumentsException("Missing effect argument!");
    }
    if (!scriptEntry.hasObject("location")) {
        throw new InvalidArgumentsException("Missing location argument!");
    }
}
Also used : Argument(com.denizenscript.denizencore.objects.Argument) Effect(org.bukkit.Effect) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException) ParticleHelper(com.denizenscript.denizen.nms.abstracts.ParticleHelper)

Example 90 with ElementTag

use of com.denizenscript.denizencore.objects.core.ElementTag in project Denizen-For-Bukkit by DenizenScript.

the class NarrateCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    if (scriptEntry.getResidingQueue().procedural) {
        Debug.echoError("'Narrate' should not be used in a procedure script. Consider the 'debug' command instead.");
    }
    List<PlayerTag> targets = (List<PlayerTag>) scriptEntry.getObject("targets");
    String text = scriptEntry.getElement("text").asString();
    ScriptTag formatObj = scriptEntry.getObjectTag("format");
    ElementTag perPlayerObj = scriptEntry.getElement("per_player");
    ElementTag from = scriptEntry.getElement("from");
    boolean perPlayer = perPlayerObj != null && perPlayerObj.asBoolean();
    BukkitTagContext context = (BukkitTagContext) scriptEntry.getContext();
    if (!perPlayer || targets == null) {
        text = TagManager.tag(text, context);
    }
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), db("Narrating", text), db("Targets", targets), formatObj, perPlayerObj, from);
    }
    UUID fromId = null;
    if (from != null) {
        if (from.asString().startsWith("p@")) {
            fromId = UUID.fromString(from.asString().substring("p@".length()));
        } else {
            fromId = UUID.fromString(from.asString());
        }
    }
    FormatScriptContainer format = formatObj == null ? null : (FormatScriptContainer) formatObj.getContainer();
    if (targets == null) {
        Bukkit.getServer().getConsoleSender().spigot().sendMessage(FormattedTextHelper.parse(format != null ? format.getFormattedText(text, scriptEntry) : text, ChatColor.WHITE));
        return;
    }
    for (PlayerTag player : targets) {
        if (player != null) {
            if (!player.isOnline()) {
                Debug.echoDebug(scriptEntry, "Player is offline, can't narrate to them. Skipping.");
                continue;
            }
            String personalText = text;
            if (perPlayer) {
                context.player = player;
                personalText = TagManager.tag(personalText, context);
            }
            BaseComponent[] component = FormattedTextHelper.parse(format != null ? format.getFormattedText(personalText, scriptEntry) : personalText, ChatColor.WHITE);
            if (fromId == null) {
                player.getPlayerEntity().spigot().sendMessage(component);
            } else {
                player.getPlayerEntity().spigot().sendMessage(ChatMessageType.CHAT, fromId, component);
            }
        } else {
            Debug.echoError("Narrated to non-existent player!?");
        }
    }
}
Also used : BaseComponent(net.md_5.bungee.api.chat.BaseComponent) BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) FormatScriptContainer(com.denizenscript.denizen.scripts.containers.core.FormatScriptContainer) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) List(java.util.List) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) UUID(java.util.UUID)

Aggregations

ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)237 ListTag (com.denizenscript.denizencore.objects.core.ListTag)86 EntityTag (com.denizenscript.denizen.objects.EntityTag)66 LocationTag (com.denizenscript.denizen.objects.LocationTag)49 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)48 InvalidArgumentsException (com.denizenscript.denizencore.exceptions.InvalidArgumentsException)43 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)40 List (java.util.List)40 Argument (com.denizenscript.denizencore.objects.Argument)28 Player (org.bukkit.entity.Player)28 MapTag (com.denizenscript.denizencore.objects.core.MapTag)27 EventHandler (org.bukkit.event.EventHandler)26 NPCTag (com.denizenscript.denizen.objects.NPCTag)24 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)22 ItemTag (com.denizenscript.denizen.objects.ItemTag)19 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)18 ArrayList (java.util.ArrayList)16 Entity (org.bukkit.entity.Entity)16 MaterialTag (com.denizenscript.denizen.objects.MaterialTag)12 ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)12