Search in sources :

Example 96 with ElementTag

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

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

the class TitleCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    String title = scriptEntry.getElement("title").asString();
    String subtitle = scriptEntry.getElement("subtitle").asString();
    DurationTag fade_in = scriptEntry.getObjectTag("fade_in");
    DurationTag stay = scriptEntry.getObjectTag("stay");
    DurationTag fade_out = scriptEntry.getObjectTag("fade_out");
    List<PlayerTag> targets = (List<PlayerTag>) scriptEntry.getObject("targets");
    ElementTag perPlayerObj = scriptEntry.getElement("per_player");
    boolean perPlayer = perPlayerObj != null && perPlayerObj.asBoolean();
    BukkitTagContext context = (BukkitTagContext) scriptEntry.getContext();
    if (!perPlayer) {
        title = TagManager.tag(title, context);
        subtitle = TagManager.tag(subtitle, context);
    }
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), db("title", title), db("subtitle", subtitle), fade_in, stay, fade_out, db("targets", targets), perPlayerObj);
    }
    for (PlayerTag player : targets) {
        if (player != null) {
            if (!player.isOnline()) {
                Debug.echoDebug(scriptEntry, "Player is offline, can't send title to them. Skipping.");
                continue;
            }
            String personalTitle = title;
            String personalSubtitle = subtitle;
            if (perPlayer) {
                context.player = player;
                personalTitle = TagManager.tag(personalTitle, context);
                personalSubtitle = TagManager.tag(personalSubtitle, context);
            }
            NMSHandler.getPacketHelper().showTitle(player.getPlayerEntity(), personalTitle, personalSubtitle, fade_in.getTicksAsInt(), stay.getTicksAsInt(), fade_out.getTicksAsInt());
        } else {
            Debug.echoError("Sent title to non-existent player!?");
        }
    }
}
Also used : BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) List(java.util.List) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag)

Example 98 with ElementTag

use of com.denizenscript.denizencore.objects.core.ElementTag 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)

Example 99 with ElementTag

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

the class ScoreboardCommand method parseArgs.

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
    for (Argument arg : scriptEntry) {
        if (!scriptEntry.hasObject("action") && arg.matchesEnum(Action.class)) {
            scriptEntry.addObject("action", arg.asElement());
        } else if (!scriptEntry.hasObject("lines") && arg.matchesPrefix("lines", "l")) {
            scriptEntry.addObject("lines", arg.asElement());
        } else if (!scriptEntry.hasObject("id") && arg.matchesPrefix("id")) {
            scriptEntry.addObject("id", arg.asElement());
        } else if (!scriptEntry.hasObject("objective") && arg.matchesPrefix("objective", "obj", "o")) {
            scriptEntry.addObject("objective", arg.asElement());
        } else if (!scriptEntry.hasObject("criteria") && arg.matchesPrefix("criteria", "c")) {
            scriptEntry.addObject("criteria", arg.asElement());
        } else if (!scriptEntry.hasObject("score") && arg.matchesInteger()) {
            scriptEntry.addObject("score", arg.asElement());
        } else if (!scriptEntry.hasObject("displayslot") && (arg.matchesEnum(DisplaySlot.class) || arg.matches("none"))) {
            scriptEntry.addObject("displayslot", arg.asElement());
        } else if (!scriptEntry.hasObject("displayslot") && arg.matchesPrefix("displayname")) {
            scriptEntry.addObject("displayname", arg.asElement());
        } else if (!scriptEntry.hasObject("rendertype") && arg.matchesPrefix("rendertype") && arg.matchesEnum(RenderType.class)) {
            scriptEntry.addObject("rendertype", arg.asElement());
        } else if (!scriptEntry.hasObject("viewers") && arg.matchesArgumentList(PlayerTag.class)) {
            scriptEntry.addObject("viewers", arg.asType(ListTag.class).filter(PlayerTag.class, scriptEntry));
        } else {
            arg.reportUnhandled();
        }
    }
    scriptEntry.defaultObject("action", new ElementTag("add"));
    scriptEntry.defaultObject("id", new ElementTag("main"));
}
Also used : Argument(com.denizenscript.denizencore.objects.Argument) DisplaySlot(org.bukkit.scoreboard.DisplaySlot) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag) RenderType(org.bukkit.scoreboard.RenderType)

Example 100 with ElementTag

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

the class DebugBlockCommand method parseArgs.

// <--[command]
// @Name DebugBlock
// @Syntax debugblock [<location>|.../clear] (color:<color>) (alpha:<#.#>) (name:<name>) (players:<player>|...) (d:<duration>{10s})
// @Required 1
// @Maximum 6
// @Short Shows or clears minecraft debug blocks.
// @Synonyms GameTestMarker
// @Group player
// 
// @Description
// Shows or clears minecraft debug blocks, AKA "Game Test Markers".
// These are block-grid-aligned markers that are a perfect cube of a single (specifiable) transparent color, and stay for a specified duration of time or until cleared.
// Markers can optionally also have simple text names.
// 
// If arguments are unspecified, the default color is white (in practice: green), the default alpha is 1.0 (most opaque, but not completely opaque),
// the default player is the linked player, the default name is none, and the default duration is 10 seconds.
// 
// The underlying color input is a full color value, however the current minecraft client can only render shades between green and gray (ie the red and blue color channels are ignored).
// 
// @Tags
// None
// 
// @Usage
// Use to show a debug block where the player is looking.
// - debugblock <player.cursor_on>
// 
// @Usage
// Use to show a transparent green debug block in front of the player for five seconds.
// - debugblock <player.eye_location.forward[2]> color:green alpha:0.5 d:5s
// 
// @Usage
// Use to remove all debug blocks,
// - debugblock clear
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
    for (Argument arg : scriptEntry) {
        if (!scriptEntry.hasObject("players") && arg.matchesPrefix("to", "players")) {
            scriptEntry.addObject("players", arg.asType(ListTag.class).filter(PlayerTag.class, scriptEntry));
        } else if (arg.matchesPrefix("d", "duration") && arg.matchesArgumentType(DurationTag.class)) {
            scriptEntry.addObject("duration", arg.asType(DurationTag.class));
        } else if (arg.matchesPrefix("color") && arg.matchesArgumentType(ColorTag.class)) {
            scriptEntry.addObject("color", arg.asType(ColorTag.class));
        } else if (arg.matchesPrefix("alpha") && arg.matchesFloat()) {
            scriptEntry.addObject("alpha", arg.asElement());
        } else if (arg.matchesPrefix("name")) {
            scriptEntry.addObject("name", arg.asElement());
        } else if (arg.matches("clear")) {
            scriptEntry.addObject("clear", new ElementTag(true));
        } else if (!scriptEntry.hasObject("locations") && arg.matchesArgumentList(LocationTag.class)) {
            scriptEntry.addObject("locations", arg.asType(ListTag.class).filter(LocationTag.class, scriptEntry));
        } else {
            arg.reportUnhandled();
        }
    }
    if (!scriptEntry.hasObject("players") && Utilities.entryHasPlayer(scriptEntry)) {
        scriptEntry.defaultObject("players", Collections.singletonList(Utilities.getEntryPlayer(scriptEntry)));
    }
    if (!scriptEntry.hasObject("locations") && !scriptEntry.hasObject("clear")) {
        throw new InvalidArgumentsException("Must specify at least one valid location!");
    }
    if (!scriptEntry.hasObject("players")) {
        throw new InvalidArgumentsException("Must have a valid, online player attached!");
    }
    scriptEntry.defaultObject("duration", new DurationTag(10));
    scriptEntry.defaultObject("color", new ColorTag(255, 255, 255));
    scriptEntry.defaultObject("alpha", new ElementTag("1"));
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) Argument(com.denizenscript.denizencore.objects.Argument) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) ColorTag(com.denizenscript.denizen.objects.ColorTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException)

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