Search in sources :

Example 46 with ListTag

use of com.denizenscript.denizencore.objects.core.ListTag 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 47 with ListTag

use of com.denizenscript.denizencore.objects.core.ListTag 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 48 with ListTag

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

the class TeamCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    ElementTag id = scriptEntry.getElement("id");
    ElementTag name = scriptEntry.getElement("name");
    ListTag add = scriptEntry.getObjectTag("add");
    ListTag remove = scriptEntry.getObjectTag("remove");
    ElementTag prefix = scriptEntry.getElement("prefix");
    ElementTag suffix = scriptEntry.getElement("suffix");
    ElementTag option = scriptEntry.getElement("option");
    ElementTag status = scriptEntry.getElement("status");
    ElementTag color = scriptEntry.getElement("color");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), id, name, add, remove, prefix, suffix, color, option, status);
    }
    Scoreboard board;
    if (id.asString().equalsIgnoreCase("main")) {
        board = ScoreboardHelper.getMain();
    } else {
        if (ScoreboardHelper.hasScoreboard(id.asString())) {
            board = ScoreboardHelper.getScoreboard(id.asString());
        } else {
            board = ScoreboardHelper.createScoreboard(id.asString());
        }
    }
    Team team = board.getTeam(name.asString());
    if (team == null) {
        String low = CoreUtilities.toLowerCase(name.asString());
        team = board.getTeams().stream().filter(t -> CoreUtilities.toLowerCase(t.getName()).equals(low)).findFirst().orElse(null);
        if (team == null) {
            team = board.registerNewTeam(name.asString());
        }
    }
    if (add != null) {
        for (String string : add) {
            string = translateEntry(string, scriptEntry.context);
            if (!team.hasEntry(string)) {
                team.addEntry(string);
            }
        }
    }
    if (remove != null) {
        for (String string : remove) {
            string = translateEntry(string, scriptEntry.context);
            if (team.hasEntry(string)) {
                team.removeEntry(string);
            }
        }
    }
    if (option != null) {
        String optName = CoreUtilities.toLowerCase(option.asString());
        String statusName = CoreUtilities.toLowerCase(status.asString());
        if (optName.equals("friendly_fire")) {
            team.setAllowFriendlyFire(statusName.equals("always"));
        } else if (optName.equals("see_invisible")) {
            team.setCanSeeFriendlyInvisibles(statusName.equals("always"));
        } else {
            team.setOption(Team.Option.valueOf(optName.toUpperCase()), Team.OptionStatus.valueOf(statusName.toUpperCase()));
        }
    }
    if (prefix != null) {
        team.setPrefix(prefix.asString());
    }
    if (suffix != null) {
        team.setSuffix(suffix.asString());
    }
    if (color != null) {
        team.setColor(ChatColor.valueOf(color.asString().toUpperCase()));
    }
    if (team.getEntries().isEmpty()) {
        team.unregister();
    }
}
Also used : ListTag(com.denizenscript.denizencore.objects.core.ListTag) Team(org.bukkit.scoreboard.Team) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException) Argument(com.denizenscript.denizencore.objects.Argument) Scoreboard(org.bukkit.scoreboard.Scoreboard) ScoreboardHelper(com.denizenscript.denizen.utilities.ScoreboardHelper) TagContext(com.denizenscript.denizencore.tags.TagContext) EntityTag(com.denizenscript.denizen.objects.EntityTag) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) ChatColor(org.bukkit.ChatColor) Debug(com.denizenscript.denizen.utilities.debugging.Debug) AbstractCommand(com.denizenscript.denizencore.scripts.commands.AbstractCommand) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) CoreUtilities(com.denizenscript.denizencore.utilities.CoreUtilities) Scoreboard(org.bukkit.scoreboard.Scoreboard) Team(org.bukkit.scoreboard.Team) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag)

Example 49 with ListTag

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

the class ModifyBlockCommand method execute.

@Override
public void execute(final ScriptEntry scriptEntry) {
    final ListTag materials = scriptEntry.getObjectTag("materials");
    final List<LocationTag> locations = (List<LocationTag>) scriptEntry.getObject("locations");
    final ListTag location_list = scriptEntry.getObjectTag("location_list");
    final ElementTag physics = scriptEntry.getElement("physics");
    final ItemTag natural = scriptEntry.getObjectTag("natural");
    final ElementTag delayed = scriptEntry.getElement("delayed");
    final ElementTag maxDelayMs = scriptEntry.getElement("max_delay_ms");
    final ElementTag radiusElement = scriptEntry.getElement("radius");
    final ElementTag heightElement = scriptEntry.getElement("height");
    final ElementTag depthElement = scriptEntry.getElement("depth");
    final ScriptTag script = scriptEntry.getObjectTag("script");
    final PlayerTag source = scriptEntry.getObjectTag("source");
    ListTag percents = scriptEntry.getObjectTag("percents");
    if (percents != null && percents.size() != materials.size()) {
        Debug.echoError(scriptEntry, "Percents length != materials length");
        percents = null;
    }
    final List<MaterialTag> materialList = materials.filter(MaterialTag.class, scriptEntry);
    for (MaterialTag mat : materialList) {
        if (!mat.getMaterial().isBlock()) {
            Debug.echoError("Material '" + mat.getMaterial().name() + "' is not a block material");
            return;
        }
    }
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), materials, physics, radiusElement, heightElement, depthElement, natural, delayed, maxDelayMs, script, percents, source, (locations == null ? location_list : db("locations", locations)));
    }
    Player sourcePlayer = source == null ? null : source.getPlayerEntity();
    final boolean doPhysics = physics.asBoolean();
    final int radius = radiusElement.asInt();
    final int height = heightElement.asInt();
    final int depth = depthElement.asInt();
    List<Float> percentages = null;
    if (percents != null) {
        percentages = new ArrayList<>();
        for (String str : percents) {
            percentages.add(new ElementTag(str).asFloat());
        }
    }
    final List<Float> percs = percentages;
    if (locations == null && location_list == null) {
        Debug.echoError("Must specify a valid location!");
        return;
    }
    if ((location_list != null && location_list.isEmpty()) || (locations != null && locations.isEmpty())) {
        return;
    }
    if (materialList.isEmpty()) {
        Debug.echoError("Must specify a valid material!");
        return;
    }
    no_physics = !doPhysics;
    if (delayed.asBoolean()) {
        final long maxDelay = maxDelayMs.asLong();
        new BukkitRunnable() {

            int index = 0;

            @Override
            public void run() {
                try {
                    long start = System.currentTimeMillis();
                    LocationTag loc;
                    if (locations != null) {
                        loc = locations.get(0);
                    } else {
                        loc = getLocAt(location_list, 0, scriptEntry);
                    }
                    if (isLocationBad(scriptEntry, loc)) {
                        scriptEntry.setFinished(true);
                        cancel();
                        return;
                    }
                    boolean was_static = preSetup(loc);
                    while ((locations != null && locations.size() > index) || (location_list != null && location_list.size() > index)) {
                        LocationTag nLoc;
                        if (locations != null) {
                            nLoc = locations.get(index);
                        } else {
                            nLoc = getLocAt(location_list, index, scriptEntry);
                        }
                        if (isLocationBad(scriptEntry, nLoc)) {
                            scriptEntry.setFinished(true);
                            cancel();
                            return;
                        }
                        handleLocation(nLoc, index, materialList, doPhysics, natural, radius, height, depth, percs, sourcePlayer, scriptEntry);
                        index++;
                        if (System.currentTimeMillis() - start > maxDelay) {
                            break;
                        }
                    }
                    postComplete(loc, was_static);
                    if ((locations != null && locations.size() == index) || (location_list != null && location_list.size() == index)) {
                        if (script != null) {
                            ScriptUtilities.createAndStartQueue(script.getContainer(), null, scriptEntry.entryData, null, null, null, null, null, scriptEntry);
                        }
                        scriptEntry.setFinished(true);
                        cancel();
                    }
                } catch (Throwable ex) {
                    Debug.echoError(ex);
                }
            }
        }.runTaskTimer(Denizen.getInstance(), 1, 1);
    } else {
        LocationTag loc;
        if (locations != null) {
            loc = locations.get(0);
        } else {
            loc = getLocAt(location_list, 0, scriptEntry);
        }
        if (isLocationBad(scriptEntry, loc)) {
            return;
        }
        boolean was_static = preSetup(loc);
        int index = 0;
        if (locations != null) {
            for (LocationTag obj : locations) {
                if (isLocationBad(scriptEntry, obj)) {
                    return;
                }
                handleLocation(obj, index, materialList, doPhysics, natural, radius, height, depth, percentages, sourcePlayer, scriptEntry);
                index++;
            }
        } else {
            for (int i = 0; i < location_list.size(); i++) {
                LocationTag obj = getLocAt(location_list, i, scriptEntry);
                if (isLocationBad(scriptEntry, obj)) {
                    return;
                }
                handleLocation(obj, index, materialList, doPhysics, natural, radius, height, depth, percentages, sourcePlayer, scriptEntry);
                index++;
            }
        }
        postComplete(loc, was_static);
        scriptEntry.setFinished(true);
    }
}
Also used : Player(org.bukkit.entity.Player) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) ListTag(com.denizenscript.denizencore.objects.core.ListTag) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) ArrayList(java.util.ArrayList) List(java.util.List) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag)

Example 50 with ListTag

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

the class BossBarCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    ElementTag id = scriptEntry.getElement("id");
    ElementTag action = scriptEntry.getElement("action");
    ListTag players = scriptEntry.getObjectTag("players");
    ElementTag title = scriptEntry.getElement("title");
    ElementTag progress = scriptEntry.getElement("progress");
    ElementTag color = scriptEntry.getElement("color");
    ElementTag style = scriptEntry.getElement("style");
    ListTag options = scriptEntry.getObjectTag("options");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), id, action, players, title, progress, color, style, options);
    }
    String idString = CoreUtilities.toLowerCase(id.asString());
    switch(Action.valueOf(action.asString().toUpperCase())) {
        case CREATE:
            {
                if (bossBarMap.containsKey(idString)) {
                    Debug.echoError("BossBar '" + idString + "' already exists!");
                    return;
                }
                String barTitle = title != null ? title.asString() : "";
                List<PlayerTag> barPlayers = players.filter(PlayerTag.class, scriptEntry);
                double barProgress = progress != null ? progress.asDouble() : 1D;
                BarColor barColor = color != null ? BarColor.valueOf(color.asString().toUpperCase()) : BarColor.WHITE;
                BarStyle barStyle = style != null ? BarStyle.valueOf(style.asString().toUpperCase()) : BarStyle.SOLID;
                BarFlag[] barFlags = new BarFlag[options != null ? options.size() : 0];
                if (options != null) {
                    for (int i = 0; i < options.size(); i++) {
                        barFlags[i] = (BarFlag.valueOf(options.get(i).toUpperCase()));
                    }
                }
                BossBar bossBar = Bukkit.createBossBar(barTitle, barColor, barStyle, barFlags);
                NMSHandler.getPlayerHelper().setBossBarTitle(bossBar, barTitle);
                bossBar.setProgress(barProgress);
                for (PlayerTag player : barPlayers) {
                    if (!player.isOnline()) {
                        Debug.echoError("Player must be online to show a BossBar to them!");
                        continue;
                    }
                    bossBar.addPlayer(player.getPlayerEntity());
                }
                bossBar.setVisible(true);
                bossBarMap.put(idString, bossBar);
                break;
            }
        case UPDATE:
            {
                if (!bossBarMap.containsKey(idString)) {
                    Debug.echoError("BossBar '" + idString + "' does not exist!");
                    return;
                }
                BossBar bossBar1 = bossBarMap.get(idString);
                if (title != null) {
                    NMSHandler.getPlayerHelper().setBossBarTitle(bossBar1, title.asString());
                }
                if (progress != null) {
                    bossBar1.setProgress(progress.asDouble());
                }
                if (color != null) {
                    bossBar1.setColor(BarColor.valueOf(color.asString().toUpperCase()));
                }
                if (style != null) {
                    bossBar1.setStyle(BarStyle.valueOf(style.asString().toUpperCase()));
                }
                if (options != null) {
                    HashSet<BarFlag> oldFlags = new HashSet<>(Arrays.asList(BarFlag.values()));
                    HashSet<BarFlag> newFlags = new HashSet<>(options.size());
                    for (String flagName : options) {
                        BarFlag flag = BarFlag.valueOf(flagName.toUpperCase());
                        newFlags.add(flag);
                        oldFlags.remove(flag);
                    }
                    for (BarFlag flag : oldFlags) {
                        bossBar1.removeFlag(flag);
                    }
                    for (BarFlag flag : newFlags) {
                        bossBar1.addFlag(flag);
                    }
                }
                if (players != null) {
                    for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
                        bossBar1.addPlayer(player.getPlayerEntity());
                    }
                }
                break;
            }
        case REMOVE:
            {
                if (!bossBarMap.containsKey(idString)) {
                    Debug.echoError("BossBar '" + idString + "' does not exist!");
                    return;
                }
                if (players != null) {
                    BossBar bar = bossBarMap.get(idString);
                    for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
                        bar.removePlayer(player.getPlayerEntity());
                    }
                    break;
                }
                bossBarMap.get(idString).setVisible(false);
                bossBarMap.remove(idString);
                break;
            }
    }
}
Also used : BarStyle(org.bukkit.boss.BarStyle) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) BarColor(org.bukkit.boss.BarColor) BarFlag(org.bukkit.boss.BarFlag) BossBar(org.bukkit.boss.BossBar) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag)

Aggregations

ListTag (com.denizenscript.denizencore.objects.core.ListTag)122 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)71 MapTag (com.denizenscript.denizencore.objects.core.MapTag)21 ItemStack (org.bukkit.inventory.ItemStack)18 EntityTag (com.denizenscript.denizen.objects.EntityTag)16 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)14 List (java.util.List)14 ItemTag (com.denizenscript.denizen.objects.ItemTag)13 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)13 Player (org.bukkit.entity.Player)13 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)12 LocationTag (com.denizenscript.denizen.objects.LocationTag)11 NPCTag (com.denizenscript.denizen.objects.NPCTag)9 InvalidArgumentsException (com.denizenscript.denizencore.exceptions.InvalidArgumentsException)9 ArrayList (java.util.ArrayList)9 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)8 MaterialTag (com.denizenscript.denizen.objects.MaterialTag)7 ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)7 NPC (net.citizensnpcs.api.npc.NPC)7 Entity (org.bukkit.entity.Entity)7