Search in sources :

Example 51 with ListTag

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

the class Conversion method getInventory.

public static AbstractMap.SimpleEntry<Integer, InventoryTag> getInventory(Argument arg, TagContext context) {
    boolean isElement = arg.object instanceof ElementTag;
    if (arg.object instanceof InventoryTag || (isElement && InventoryTag.matches(arg.getValue()))) {
        InventoryTag inv = arg.object instanceof InventoryTag ? (InventoryTag) arg.object : InventoryTag.valueOf(arg.getValue(), context);
        if (inv != null) {
            return new AbstractMap.SimpleEntry<>(inv.getContents().length, inv);
        }
    } else if (arg.object instanceof MapTag || (isElement && arg.getValue().startsWith("map@"))) {
        MapTag map = arg.object instanceof MapTag ? (MapTag) arg.object : MapTag.valueOf(arg.getValue(), context);
        int maxSlot = 0;
        for (Map.Entry<StringHolder, ObjectTag> entry : map.map.entrySet()) {
            if (!ArgumentHelper.matchesInteger(entry.getKey().str)) {
                return null;
            }
            int slot = new ElementTag(entry.getKey().str).asInt();
            if (slot > maxSlot) {
                maxSlot = slot;
            }
        }
        InventoryTag inventory = new InventoryTag(Math.min(InventoryTag.maxSlots, (maxSlot / 9) * 9 + 9));
        for (Map.Entry<StringHolder, ObjectTag> entry : map.map.entrySet()) {
            int slot = new ElementTag(entry.getKey().str).asInt();
            ItemTag item = ItemTag.getItemFor(entry.getValue(), context);
            if (item == null) {
                if (context == null || context.debug) {
                    Debug.echoError("Not a valid item: '" + entry.getValue() + "'");
                }
                continue;
            }
            inventory.getInventory().setItem(slot - 1, item.getItemStack());
        }
        return new AbstractMap.SimpleEntry<>(maxSlot, inventory);
    } else if (arg.object instanceof LocationTag || (isElement && LocationTag.matches(arg.getValue()))) {
        InventoryTag inv = (arg.object instanceof LocationTag ? (LocationTag) arg.object : LocationTag.valueOf(arg.getValue(), context)).getInventory();
        if (inv != null) {
            return new AbstractMap.SimpleEntry<>(inv.getContents().length, inv);
        }
    } else if (arg.object instanceof EntityTag || arg.object instanceof PlayerTag || arg.object instanceof NPCTag || (isElement && EntityTag.matches(arg.getValue()))) {
        InventoryTag inv = EntityTag.valueOf(arg.getValue(), context).getInventory();
        if (inv != null) {
            return new AbstractMap.SimpleEntry<>(inv.getContents().length, inv);
        }
    }
    ListTag asList = ListTag.getListFor(arg.object, context);
    if (asList.containsObjectsFrom(ItemTag.class)) {
        List<ItemTag> list = asList.filter(ItemTag.class, context);
        ItemStack[] items = convertItems(list).toArray(new ItemStack[list.size()]);
        InventoryTag inventory = new InventoryTag(Math.min(InventoryTag.maxSlots, (items.length / 9) * 9 + 9));
        inventory.setContents(items);
        return new AbstractMap.SimpleEntry<>(items.length, inventory);
    }
    return null;
}
Also used : ListTag(com.denizenscript.denizencore.objects.core.ListTag) MapTag(com.denizenscript.denizencore.objects.core.MapTag) AbstractMap(java.util.AbstractMap) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ItemStack(org.bukkit.inventory.ItemStack)

Example 52 with ListTag

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

the class SchematicCommand method schematicTags.

public void schematicTags(ReplaceableTagEvent event) {
    if (!event.matches("schematic")) {
        return;
    }
    Attribute attribute = event.getAttributes();
    String id = attribute.hasParam() ? attribute.getParam().toUpperCase() : null;
    attribute = attribute.fulfill(1);
    // -->
    if (attribute.startsWith("list")) {
        event.setReplaced(new ListTag(schematics.keySet()).getAttribute(attribute.fulfill(1)));
    }
    if (id == null) {
        return;
    }
    if (!schematics.containsKey(id)) {
        // Meta below
        if (attribute.startsWith("exists")) {
            event.setReplaced(new ElementTag(false).getAttribute(attribute.fulfill(1)));
            return;
        }
        Debug.echoError(attribute.getScriptEntry(), "Schematic file " + id + " is not loaded.");
        return;
    }
    CuboidBlockSet set = schematics.get(id);
    // -->
    if (attribute.startsWith("exists")) {
        event.setReplaced(new ElementTag(true).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("height")) {
        event.setReplaced(new ElementTag(set.y_length).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("length")) {
        event.setReplaced(new ElementTag(set.z_height).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("width")) {
        event.setReplaced(new ElementTag(set.x_width).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("block")) {
        if (attribute.hasParam() && LocationTag.matches(attribute.getParam())) {
            LocationTag location = attribute.paramAsType(LocationTag.class);
            FullBlockData block = set.blockAt(location.getX(), location.getY(), location.getZ());
            event.setReplaced(new MaterialTag(block.data).getAttribute(attribute.fulfill(1)));
            return;
        }
    }
    // -->
    if (attribute.startsWith("origin")) {
        event.setReplaced(new LocationTag(null, set.center_x, set.center_y, set.center_z).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("blocks")) {
        event.setReplaced(new ElementTag(set.blocks.length).getAttribute(attribute.fulfill(1)));
        return;
    }
    // -->
    if (attribute.startsWith("cuboid") && attribute.hasParam()) {
        LocationTag origin = attribute.paramAsType(LocationTag.class);
        event.setReplaced(set.getCuboid(origin).getAttribute(attribute.fulfill(1)));
        return;
    }
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) MaterialTag(com.denizenscript.denizen.objects.MaterialTag) Attribute(com.denizenscript.denizencore.tags.Attribute) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag)

Example 53 with ListTag

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

the class DamageTrigger method damageTrigger.

// <--[language]
// @name Damage Triggers
// @group NPC Interact Scripts
// @description
// Damage Triggers are triggered when when a player left clicks the NPC.
// Despite the name, these do not actually require the NPC take any damage, only that the player left clicks the NPC.
// 
// In scripts, use <context.damage> to measure how much damage was done to the NPC
// (though note that invincible NPCs don't necessarily take any damage even when this is non-zero).
// 
// These are very basic with no extraneous complexity.
// 
// -->
// <--[action]
// @Actions
// no damage trigger
// 
// @Triggers when the NPC is damaged by a player but no damage trigger fires.
// 
// @Context
// None
// 
// -->
// Technically defined in TriggerTrait, but placing here instead.
// <--[action]
// @Actions
// damage
// 
// @Triggers when the NPC is damaged by a player.
// 
// @Context
// <context.damage> returns how much damage was done.
// 
// @Determine
// "cancelled" to cancel the damage event.
// 
// -->
// <--[action]
// @Actions
// damaged
// 
// @Triggers when the NPC is damaged by an entity.
// 
// @Context
// <context.damage> returns how much damage was done.
// <context.damager> returns the entity that did the damage.
// 
// @Determine
// "cancelled" to cancel the damage event.
// 
// -->
@EventHandler
public void damageTrigger(EntityDamageByEntityEvent event) {
    Map<String, ObjectTag> context = new HashMap<>();
    context.put("damage", new ElementTag(event.getDamage()));
    if (CitizensAPI.getNPCRegistry().isNPC(event.getEntity())) {
        NPCTag npc = new NPCTag(CitizensAPI.getNPCRegistry().getNPC(event.getEntity()));
        if (npc == null) {
            return;
        }
        if (npc.getCitizen() == null) {
            return;
        }
        EntityTag damager = new EntityTag(event.getDamager());
        if (damager.isProjectile() && damager.hasShooter()) {
            damager = damager.getShooter();
        }
        context.put("damager", damager.getDenizenObject());
        ListTag determ = npc.action("damaged", null, context);
        if (determ != null && determ.containsCaseInsensitive("cancelled")) {
            event.setCancelled(true);
            return;
        }
        if (!damager.isPlayer()) {
            return;
        }
        PlayerTag dplayer = damager.getDenizenPlayer();
        if (!npc.getCitizen().hasTrait(TriggerTrait.class)) {
            return;
        }
        if (!npc.getTriggerTrait().isEnabled(name)) {
            return;
        }
        TriggerTrait.TriggerContext trigger = npc.getTriggerTrait().trigger(this, dplayer);
        if (!trigger.wasTriggered()) {
            return;
        }
        if (trigger.hasDetermination() && trigger.getDeterminations().containsCaseInsensitive("cancelled")) {
            event.setCancelled(true);
            return;
        }
        List<InteractScriptContainer> scripts = InteractScriptHelper.getInteractScripts(npc, dplayer, true, ClickTrigger.class);
        boolean any = false;
        if (scripts != null) {
            for (InteractScriptContainer script : scripts) {
                String id = null;
                Map<String, String> idMap = script.getIdMapFor(ClickTrigger.class, dplayer);
                if (!idMap.isEmpty()) {
                    for (Map.Entry<String, String> entry : idMap.entrySet()) {
                        String entry_value = TagManager.tag(entry.getValue(), new BukkitTagContext(dplayer, npc, null, false, new ScriptTag(script)));
                        if (ItemTag.valueOf(entry_value, script).comparesTo(dplayer.getPlayerEntity().getEquipment().getItemInMainHand()) >= 0) {
                            id = entry.getKey();
                        }
                    }
                }
                if (parse(npc, dplayer, script, id, context)) {
                    any = true;
                }
            }
        }
        if (!any) {
            npc.action("no damage trigger", dplayer);
        }
    }
}
Also used : TriggerTrait(com.denizenscript.denizen.npc.traits.TriggerTrait) HashMap(java.util.HashMap) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) NPCTag(com.denizenscript.denizen.objects.NPCTag) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) EntityTag(com.denizenscript.denizen.objects.EntityTag) InteractScriptContainer(com.denizenscript.denizen.scripts.containers.core.InteractScriptContainer) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) HashMap(java.util.HashMap) Map(java.util.Map) EventHandler(org.bukkit.event.EventHandler)

Example 54 with ListTag

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

the class CuboidBlockSet method buildEntities.

public void buildEntities(CuboidTag cuboid, Location center) {
    entities = new ListTag();
    for (Entity ent : cuboid.getWorld().getEntities()) {
        if (cuboid.isInsideCuboid(ent.getLocation())) {
            if (copyTypes.contains(ent.getType())) {
                EntityTag entTag = new EntityTag(ent);
                if (entTag.isPlayer() || entTag.isNPC()) {
                    continue;
                }
                MapTag data = new MapTag();
                data.putObject("entity", entTag.describe(null));
                data.putObject("rotation", new ElementTag(0));
                Vector offset = ent.getLocation().toVector().subtract(center.toVector());
                data.putObject("offset", new LocationTag((String) null, offset.getX(), offset.getY(), offset.getZ(), ent.getLocation().getYaw(), ent.getLocation().getPitch()));
                entities.addObject(data);
            }
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag) Vector(org.bukkit.util.Vector) MapTag(com.denizenscript.denizencore.objects.core.MapTag)

Example 55 with ListTag

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

the class CuboidBlockSet method rotateEntitiesOne.

public void rotateEntitiesOne() {
    if (entities == null) {
        return;
    }
    ListTag outEntities = new ListTag();
    for (MapTag data : entities.filter(MapTag.class, CoreUtilities.noDebugContext)) {
        LocationTag offset = data.getObject("offset").asType(LocationTag.class, CoreUtilities.noDebugContext);
        int rotation = data.getObject("rotation").asElement().asInt();
        offset = new LocationTag((String) null, offset.getZ(), offset.getY(), -offset.getX() + 1, offset.getYaw(), offset.getPitch());
        rotation += 90;
        while (rotation >= 360) {
            rotation -= 360;
        }
        data = data.duplicate();
        data.putObject("offset", offset);
        data.putObject("rotation", new ElementTag(rotation));
        outEntities.addObject(data);
    }
    entities = outEntities;
}
Also used : ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag) MapTag(com.denizenscript.denizencore.objects.core.MapTag)

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