Search in sources :

Example 31 with Argument

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

the class MountCommand method parseArgs.

// <--[command]
// @Name Mount
// @Syntax mount (cancel) [<entity>|...] (<location>)
// @Required 0
// @Maximum 3
// @Short Mounts one entity onto another.
// @Group entity
// 
// @Description
// Mounts an entity onto another as though in a vehicle. Can be used to force a player into a vehicle or to
// mount an entity onto another entity. e.g. a player onto an npc. If the entity(s) don't exist they will be
// spawned. Accepts a location, which the entities will be teleported to on mounting.
// 
// @Tags
// <EntityTag.vehicle>
// <EntityTag.is_inside_vehicle>
// <entry[saveName].mounted_entities> returns a list of entities that were mounted.
// 
// @Usage
// Use to mount an NPC on top of a player.
// - mount <npc>|<player>
// 
// @Usage
// Use to spawn a mutant pile of mobs.
// - mount cow|pig|sheep|chicken
// 
// @Usage
// Use to place a diamond block above a player's head.
// - mount falling_block,diamond_block|<player>
// 
// @Usage
// Use to force an entity in a vehicle.
// - mount <player>|boat
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
    List<EntityTag> entities = null;
    for (Argument arg : scriptEntry) {
        if (!scriptEntry.hasObject("cancel") && arg.matches("cancel")) {
            scriptEntry.addObject("cancel", "");
        } else if (!scriptEntry.hasObject("location") && arg.matchesArgumentType(LocationTag.class)) {
            scriptEntry.addObject("location", arg.asType(LocationTag.class));
            scriptEntry.addObject("custom_location", new ElementTag(true));
        } else if (!scriptEntry.hasObject("entities") && arg.matchesArgumentList(EntityTag.class)) {
            entities = arg.asType(ListTag.class).filter(EntityTag.class, scriptEntry);
            scriptEntry.addObject("entities", entities);
        } else {
            arg.reportUnhandled();
        }
    }
    if (!scriptEntry.hasObject("entities")) {
        throw new InvalidArgumentsException("Must specify entity/entities!");
    }
    if (!scriptEntry.hasObject("location")) {
        if (entities != null) {
            for (int i = entities.size() - 1; i >= 0; i--) {
                if (entities.get(i).isSpawned()) {
                    scriptEntry.defaultObject("location", entities.get(i).getLocation());
                    break;
                }
            }
        }
        scriptEntry.defaultObject("location", Utilities.entryDefaultLocation(scriptEntry, true));
    }
    if (!scriptEntry.hasObject("location")) {
        throw new InvalidArgumentsException("Must specify a location!");
    }
}
Also used : 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 32 with Argument

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

the class GiveCommand method parseArgs.

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
    for (Argument arg : scriptEntry) {
        if (!scriptEntry.hasObject("quantity") && arg.matchesPrefix("q", "qty", "quantity") && arg.matchesFloat()) {
            if (arg.matchesPrefix("q", "qty")) {
                Deprecations.qtyTags.warn(scriptEntry);
            }
            scriptEntry.addObject("quantity", arg.asElement());
            scriptEntry.addObject("set_quantity", new ElementTag(true));
        } else if (!scriptEntry.hasObject("type") && arg.matches("money", "coins")) {
            Deprecations.giveTakeMoney.warn(scriptEntry);
            scriptEntry.addObject("type", Type.MONEY);
        } else if (!scriptEntry.hasObject("type") && arg.matches("xp", "exp", "experience")) {
            scriptEntry.addObject("type", Type.EXP);
        } else if (!scriptEntry.hasObject("unlimit_stack_size") && arg.matches("unlimit_stack_size")) {
            scriptEntry.addObject("unlimit_stack_size", new ElementTag(true));
        } else if (!scriptEntry.hasObject("items") && !scriptEntry.hasObject("type") && (arg.matchesArgumentList(ItemTag.class))) {
            scriptEntry.addObject("items", arg.asType(ListTag.class).filter(ItemTag.class, scriptEntry));
        } else if (!scriptEntry.hasObject("inventory") && arg.matchesPrefix("t", "to") && arg.matchesArgumentType(InventoryTag.class)) {
            scriptEntry.addObject("inventory", arg.asType(InventoryTag.class));
        } else if (!scriptEntry.hasObject("slot") && arg.matchesPrefix("slot")) {
            scriptEntry.addObject("slot", arg.asElement());
        } else {
            arg.reportUnhandled();
        }
    }
    scriptEntry.defaultObject("type", Type.ITEM).defaultObject("unlimit_stack_size", new ElementTag(false)).defaultObject("quantity", new ElementTag(1)).defaultObject("slot", new ElementTag(1));
    Type type = (Type) scriptEntry.getObject("type");
    if (type == Type.ITEM) {
        if (!scriptEntry.hasObject("items")) {
            throw new InvalidArgumentsException("Must specify item/items!");
        }
        if (!scriptEntry.hasObject("inventory")) {
            if (!Utilities.entryHasPlayer(scriptEntry)) {
                throw new InvalidArgumentsException("Must specify an inventory to give to!");
            }
            scriptEntry.addObject("inventory", Utilities.getEntryPlayer(scriptEntry).getInventory());
        }
    } else {
        if (!Utilities.entryHasPlayer(scriptEntry)) {
            throw new InvalidArgumentsException("Must link a player to give money or XP!");
        }
    }
}
Also used : Argument(com.denizenscript.denizencore.objects.Argument) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ItemTag(com.denizenscript.denizen.objects.ItemTag) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException)

Example 33 with Argument

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

the class NarrateCommand method parseArgs.

// <--[command]
// @Name Narrate
// @Syntax narrate [<text>] (targets:<player>|...) (format:<script>) (per_player) (from:<uuid>)
// @Required 1
// @Maximum 5
// @Short Shows some text to the player.
// @Group player
// 
// @Description
// Prints some text into the target's chat area. If no target is specified it will default to the attached player or the console.
// 
// Accepts the 'format:<script>' argument, which will reformat the text according to the specified format script. See <@link language Format Script Containers>.
// 
// Optionally use 'per_player' with a list of player targets, to have the tags in the text input be reparsed for each and every player.
// So, for example, "- narrate 'hello <player.name>' targets:<server.online_players>"
// would normally say "hello bob" to every player (every player sees the exact same name in the text, ie bob sees "hello bob", steve also sees "hello bob", etc)
// but if you use "per_player", each player online would see their own name (so bob sees "hello bob", steve sees "hello steve", etc).
// 
// Optionally, specify 'from:<uuid>' to indicate that message came from a specific UUID (used for things like the vanilla client social interaction block option).
// 
// @Tags
// None
// 
// @Usage
// Use to narrate text to the player.
// - narrate "Hello World!"
// 
// @Usage
// Use to narrate text to a list of players.
// - narrate "Hello there." targets:<[player]>|<[someplayer]>|<[thatplayer]>
// 
// @Usage
// Use to narrate text to a unique message to every player on the server.
// - narrate "Hello <player.name>, your secret code is <util.random.duuid>." targets:<server.online_players> per_player
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
    for (Argument arg : ArgumentHelper.interpret(scriptEntry, scriptEntry.getOriginalArguments())) {
        if (!scriptEntry.hasObject("format") && arg.matchesPrefix("format", "f")) {
            String formatStr = TagManager.tag(arg.getValue(), scriptEntry.getContext());
            FormatScriptContainer format = ScriptRegistry.getScriptContainer(formatStr);
            if (format == null) {
                Debug.echoError("Could not find format script matching '" + formatStr + "'");
            }
            scriptEntry.addObject("format", new ScriptTag(format));
        } else if (!scriptEntry.hasObject("targets") && arg.matchesPrefix("target", "targets", "t")) {
            scriptEntry.addObject("targets", ListTag.getListFor(TagManager.tagObject(arg.getValue(), scriptEntry.getContext()), scriptEntry.getContext()).filter(PlayerTag.class, scriptEntry));
        } else if (!scriptEntry.hasObject("from") && arg.matchesPrefix("from")) {
            scriptEntry.addObject("from", TagManager.tagObject(arg.getValue(), scriptEntry.getContext()));
        } else if (!scriptEntry.hasObject("per_player") && arg.matches("per_player")) {
            scriptEntry.addObject("per_player", new ElementTag(true));
        } else if (!scriptEntry.hasObject("text")) {
            scriptEntry.addObject("text", arg.getRawElement());
        } else {
            arg.reportUnhandled();
        }
    }
    if (!scriptEntry.hasObject("targets")) {
        scriptEntry.addObject("targets", (Utilities.entryHasPlayer(scriptEntry) ? Collections.singletonList(Utilities.getEntryPlayer(scriptEntry)) : null));
    }
    if (!scriptEntry.hasObject("text")) {
        throw new InvalidArgumentsException("Missing any text!");
    }
}
Also used : Argument(com.denizenscript.denizencore.objects.Argument) FormatScriptContainer(com.denizenscript.denizen.scripts.containers.core.FormatScriptContainer) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException)

Example 34 with Argument

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

the class ClickableCommand method parseArgs.

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
    MapTag defMap = new MapTag();
    for (Argument arg : scriptEntry) {
        if (arg.matchesPrefix("defmap") && arg.matchesArgumentType(MapTag.class)) {
            defMap.map.putAll(arg.asType(MapTag.class).map);
        } else if (arg.hasPrefix() && arg.getPrefix().getRawValue().startsWith("def.")) {
            defMap.putObject(arg.getPrefix().getRawValue().substring("def.".length()), arg.object);
        } else if (!scriptEntry.hasObject("script")) {
            String scriptName = arg.getRawValue();
            int dotIndex = scriptName.indexOf('.');
            if (dotIndex > 0) {
                scriptEntry.addObject("path", new ElementTag(scriptName.substring(dotIndex + 1)));
                scriptName = scriptName.substring(0, dotIndex);
            }
            ScriptTag script = new ScriptTag(scriptName);
            if (!script.isValid()) {
                arg.reportUnhandled();
            } else {
                scriptEntry.addObject("script", script);
            }
        } else {
            arg.reportUnhandled();
        }
    }
    if (!defMap.map.isEmpty()) {
        scriptEntry.addObject("def_map", defMap);
    }
}
Also used : Argument(com.denizenscript.denizencore.objects.Argument)

Example 35 with Argument

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

the class ItemInventory method adjust.

@Override
public void adjust(Mechanism mechanism) {
    // -->
    if (mechanism.matches("inventory_contents") && mechanism.hasValue()) {
        List<ItemStack> items = new ArrayList<>();
        for (ItemTag item : mechanism.valueAsType(ListTag.class).filter(ItemTag.class, mechanism.context)) {
            items.add(item.getItemStack());
        }
        if (item.getItemMeta() instanceof BlockStateMeta) {
            BlockStateMeta bsm = ((BlockStateMeta) item.getItemMeta());
            InventoryHolder invHolder = (InventoryHolder) bsm.getBlockState();
            if (items.size() > getInventoryFor(invHolder).getSize()) {
                mechanism.echoError("Invalid inventory_contents input size; expected " + getInventoryFor(invHolder).getSize() + " or less.");
                return;
            }
            getInventoryFor(invHolder).setContents(items.toArray(new ItemStack[0]));
            bsm.setBlockState((BlockState) invHolder);
            item.setItemMeta(bsm);
        } else if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_17)) {
            BundleMeta bundle = (BundleMeta) item.getItemMeta();
            bundle.setItems(items);
            item.setItemMeta(bundle);
        }
    }
    if (mechanism.matches("inventory") && mechanism.hasValue()) {
        Deprecations.itemInventoryTag.warn(mechanism.context);
        Argument argument = new Argument("");
        argument.unsetValue();
        argument.object = mechanism.getValue();
        Map.Entry<Integer, InventoryTag> inventoryPair = Conversion.getInventory(argument, mechanism.context);
        if (inventoryPair == null || inventoryPair.getValue().getInventory() == null) {
            return;
        }
        ListTag items = InventoryContents.getFrom(inventoryPair.getValue()).getContents(false);
        ItemStack[] itemArray = new ItemStack[items.size()];
        for (int i = 0; i < itemArray.length; i++) {
            itemArray[i] = ((ItemTag) items.objectForms.get(i)).getItemStack().clone();
        }
        if (item.getItemMeta() instanceof BlockStateMeta) {
            BlockStateMeta bsm = ((BlockStateMeta) item.getItemMeta());
            InventoryHolder invHolder = (InventoryHolder) bsm.getBlockState();
            if (items.size() > getInventoryFor(invHolder).getSize()) {
                mechanism.echoError("Invalid inventory mechanism input size; expected " + getInventoryFor(invHolder).getSize() + " or less.");
                return;
            }
            getInventoryFor(invHolder).setContents(itemArray);
            bsm.setBlockState((BlockState) invHolder);
            item.setItemMeta(bsm);
        } else if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_17)) {
            BundleMeta bundle = (BundleMeta) item.getItemMeta();
            bundle.setItems(Arrays.asList(itemArray));
            item.setItemMeta(bundle);
        }
    }
}
Also used : BundleMeta(org.bukkit.inventory.meta.BundleMeta) Argument(com.denizenscript.denizencore.objects.Argument) ArrayList(java.util.ArrayList) ListTag(com.denizenscript.denizencore.objects.core.ListTag) BlockStateMeta(org.bukkit.inventory.meta.BlockStateMeta) ItemStack(org.bukkit.inventory.ItemStack) ItemTag(com.denizenscript.denizen.objects.ItemTag) InventoryHolder(org.bukkit.inventory.InventoryHolder) Map(java.util.Map) InventoryTag(com.denizenscript.denizen.objects.InventoryTag)

Aggregations

Argument (com.denizenscript.denizencore.objects.Argument)35 InvalidArgumentsException (com.denizenscript.denizencore.exceptions.InvalidArgumentsException)30 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)23 EntityTag (com.denizenscript.denizen.objects.EntityTag)12 ListTag (com.denizenscript.denizencore.objects.core.ListTag)12 ItemTag (com.denizenscript.denizen.objects.ItemTag)8 LocationTag (com.denizenscript.denizen.objects.LocationTag)6 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)5 FormatScriptContainer (com.denizenscript.denizen.scripts.containers.core.FormatScriptContainer)3 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)3 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)3 ArrayList (java.util.ArrayList)3 MaterialTag (com.denizenscript.denizen.objects.MaterialTag)2 ParticleHelper (com.denizenscript.denizen.nms.abstracts.ParticleHelper)1 ColorTag (com.denizenscript.denizen.objects.ColorTag)1 InventoryTag (com.denizenscript.denizen.objects.InventoryTag)1 Map (java.util.Map)1 Effect (org.bukkit.Effect)1 Statistic (org.bukkit.Statistic)1 WeatherType (org.bukkit.WeatherType)1