Search in sources :

Example 66 with ItemTag

use of com.denizenscript.denizen.objects.ItemTag in project Denizen-For-Bukkit by DenizenScript.

the class DropCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    LocationTag location = scriptEntry.getObjectTag("location");
    ElementTag quantity = scriptEntry.getElement("quantity");
    ElementTag action = scriptEntry.getElement("action");
    ElementTag speed = scriptEntry.getElement("speed");
    List<ItemTag> items = (List<ItemTag>) scriptEntry.getObject("item");
    EntityTag entity = scriptEntry.getObjectTag("entity");
    DurationTag delay = scriptEntry.getObjectTag("delay");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), action, location, quantity, db("items", items), entity, speed, delay);
    }
    ListTag entityList = new ListTag();
    switch(Action.valueOf(action.asString())) {
        case DROP_EXP:
            EntityTag orb = new EntityTag(location.getWorld().spawnEntity(location, EntityType.EXPERIENCE_ORB));
            ((ExperienceOrb) orb.getBukkitEntity()).setExperience(quantity.asInt());
            entityList.addObject(orb);
            break;
        case DROP_ITEM:
            for (ItemTag item : items) {
                if (item.getMaterial().getMaterial() == Material.AIR) {
                    continue;
                }
                if (quantity.asInt() > 1 && item.isUnique()) {
                    Debug.echoDebug(scriptEntry, "Cannot drop multiples of this item because it is Unique!");
                }
                for (int x = 0; x < quantity.asInt(); x++) {
                    EntityTag e = new EntityTag(location.getWorld().dropItem(location, item.getItemStack()));
                    if (e.isValid()) {
                        e.setVelocity(e.getVelocity().multiply(speed != null ? speed.asDouble() : 1d));
                        if (delay != null) {
                            ((Item) e.getBukkitEntity()).setPickupDelay(delay.getTicksAsInt());
                        }
                    }
                    entityList.addObject(e);
                }
            }
            break;
        case DROP_ENTITY:
            if (quantity.asInt() > 1 && entity.isUnique()) {
                Debug.echoDebug(scriptEntry, "Cannot drop multiples of this entity because it is Unique!");
                entity.spawnAt(location);
                entityList.addObject(entity);
                break;
            }
            for (int x = 0; x < quantity.asInt(); x++) {
                ArrayList<Mechanism> mechanisms = new ArrayList<>();
                for (Mechanism mechanism : entity.getWaitingMechanisms()) {
                    mechanisms.add(new Mechanism(mechanism.getName(), mechanism.value, scriptEntry.context));
                }
                EntityTag ent = new EntityTag(entity.getEntityType(), mechanisms);
                ent.spawnAt(location);
                entityList.addObject(ent);
            }
            break;
    }
    scriptEntry.addObject("dropped_entities", entityList);
    if (entityList.size() == 1) {
        scriptEntry.addObject("dropped_entity", entityList.getObject(0));
    }
}
Also used : ArrayList(java.util.ArrayList) ExperienceOrb(org.bukkit.entity.ExperienceOrb) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag) LocationTag(com.denizenscript.denizen.objects.LocationTag) Item(org.bukkit.entity.Item) ArrayList(java.util.ArrayList) List(java.util.List) EntityTag(com.denizenscript.denizen.objects.EntityTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ItemTag(com.denizenscript.denizen.objects.ItemTag)

Example 67 with ItemTag

use of com.denizenscript.denizen.objects.ItemTag in project Denizen-For-Bukkit by DenizenScript.

the class EntityBreedScriptEvent method onEntityBreeds.

@EventHandler
public void onEntityBreeds(EntityBreedEvent event) {
    Entity entity = event.getEntity();
    this.entity = new EntityTag(entity);
    breeder = event.getBreeder() == null ? null : new EntityTag(event.getBreeder());
    father = new EntityTag(event.getFather());
    mother = new EntityTag(event.getMother());
    item = event.getBredWith() == null ? null : new ItemTag(event.getBredWith());
    experience = event.getExperience();
    this.event = event;
    EntityTag.rememberEntity(entity);
    fire(event);
    EntityTag.forgetEntity(entity);
}
Also used : Entity(org.bukkit.entity.Entity) EntityTag(com.denizenscript.denizen.objects.EntityTag) ItemTag(com.denizenscript.denizen.objects.ItemTag) EventHandler(org.bukkit.event.EventHandler)

Example 68 with ItemTag

use of com.denizenscript.denizen.objects.ItemTag in project Denizen-For-Bukkit by DenizenScript.

the class EntityDrinkingPotion method adjust.

@Override
public void adjust(Mechanism mechanism) {
    // -->
    if (mechanism.matches("drinking_potion") && mechanism.requireObject(ItemTag.class)) {
        ItemTag potion = mechanism.valueAsType(ItemTag.class);
        if (potion.getBukkitMaterial() != Material.POTION) {
            mechanism.echoError("Invalid item input '" + potion + "': item must be a potion");
            return;
        }
        getWitch().setDrinkingPotion(potion.getItemStack());
    }
    // -->
    if (mechanism.matches("potion_drink_duration") && mechanism.requireObject(DurationTag.class)) {
        getWitch().setPotionUseTimeLeft(mechanism.valueAsType(DurationTag.class).getTicksAsInt());
    }
}
Also used : ItemTag(com.denizenscript.denizen.objects.ItemTag) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag)

Example 69 with ItemTag

use of com.denizenscript.denizen.objects.ItemTag in project Denizen-For-Bukkit by DenizenScript.

the class DisplayItemCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    ItemTag item = scriptEntry.getObjectTag("item");
    DurationTag duration = scriptEntry.getObjectTag("duration");
    LocationTag location = scriptEntry.getObjectTag("location");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), item, duration, location);
    }
    // Drop the item
    final Item dropped = location.getWorld().dropItem(location.getBlockLocation().clone().add(0.5, 1.5, 0.5), item.getItemStack());
    dropped.setVelocity(new Vector(0, 0, 0));
    dropped.setGravity(false);
    dropped.setPickupDelay(32767);
    int ticks = duration.getTicksAsInt();
    NMSHandler.getEntityHelper().setTicksLived(dropped, ticks <= 0 ? -32768 : -ticks);
    if (!dropped.isValid()) {
        Debug.echoDebug(scriptEntry, "Item failed to spawned (likely blocked by some plugin).");
        return;
    }
    final UUID itemUUID = dropped.getUniqueId();
    protectedEntities.add(itemUUID);
    scriptEntry.addObject("dropped", new EntityTag(dropped));
    if (ticks > 0) {
        Bukkit.getScheduler().scheduleSyncDelayedTask(Denizen.getInstance(), () -> {
            protectedEntities.remove(itemUUID);
            if (dropped.isValid() && !dropped.isDead()) {
                dropped.remove();
            }
        }, ticks);
    }
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) Item(org.bukkit.entity.Item) EntityTag(com.denizenscript.denizen.objects.EntityTag) ItemTag(com.denizenscript.denizen.objects.ItemTag) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag) UUID(java.util.UUID) Vector(org.bukkit.util.Vector)

Example 70 with ItemTag

use of com.denizenscript.denizen.objects.ItemTag in project Denizen-For-Bukkit by DenizenScript.

the class EquipCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    Map<String, ItemTag> equipment = (Map<String, ItemTag>) scriptEntry.getObject("equipment");
    List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), db("entities", entities), db("equipment", equipment));
    }
    for (EntityTag entity : entities) {
        if (entity.isGeneric()) {
            Debug.echoError(scriptEntry, "Cannot equip generic entity " + entity.identify() + "!");
        } else if (entity.isCitizensNPC()) {
            NPCTag npc = entity.getDenizenNPC();
            if (npc != null) {
                Equipment trait = npc.getEquipmentTrait();
                if (equipment.get("hand") != null) {
                    trait.set(0, equipment.get("hand").getItemStack());
                }
                if (equipment.get("head") != null) {
                    trait.set(1, equipment.get("head").getItemStack());
                }
                if (equipment.get("chest") != null) {
                    trait.set(2, equipment.get("chest").getItemStack());
                }
                if (equipment.get("legs") != null) {
                    trait.set(3, equipment.get("legs").getItemStack());
                }
                if (equipment.get("boots") != null) {
                    trait.set(4, equipment.get("boots").getItemStack());
                }
                if (equipment.get("offhand") != null) {
                    trait.set(5, equipment.get("offhand").getItemStack());
                }
                if (npc.isSpawned()) {
                    LivingEntity livingEntity = npc.getLivingEntity();
                    // TODO: Citizens API for this blob?
                    if (livingEntity instanceof AbstractHorse) {
                        if (equipment.get("saddle") != null) {
                            ((AbstractHorse) livingEntity).getInventory().setSaddle(equipment.get("saddle").getItemStack());
                        }
                        if (equipment.get("horse_armor") != null) {
                            if (((AbstractHorse) livingEntity).getInventory() instanceof HorseInventory) {
                                ((HorseInventory) ((AbstractHorse) livingEntity).getInventory()).setArmor(equipment.get("horse_armor").getItemStack());
                            }
                        }
                    } else if (livingEntity instanceof Steerable) {
                        if (equipment.get("saddle") != null) {
                            ItemTag saddle = equipment.get("saddle");
                            if (saddle.getBukkitMaterial() == Material.SADDLE) {
                                ((Steerable) livingEntity).setSaddle(true);
                            } else {
                                ((Steerable) livingEntity).setSaddle(false);
                            }
                        }
                    }
                }
            }
        } else {
            LivingEntity livingEntity = entity.getLivingEntity();
            if (livingEntity != null) {
                if (livingEntity instanceof AbstractHorse) {
                    if (equipment.get("saddle") != null) {
                        ((AbstractHorse) livingEntity).getInventory().setSaddle(equipment.get("saddle").getItemStack());
                    }
                    if (equipment.get("horse_armor") != null) {
                        if (((AbstractHorse) livingEntity).getInventory() instanceof HorseInventory) {
                            ((HorseInventory) ((AbstractHorse) livingEntity).getInventory()).setArmor(equipment.get("horse_armor").getItemStack());
                        }
                    }
                } else if (livingEntity instanceof Steerable) {
                    if (equipment.get("saddle") != null) {
                        ItemTag saddle = equipment.get("saddle");
                        if (saddle.getBukkitMaterial() == Material.SADDLE) {
                            ((Steerable) livingEntity).setSaddle(true);
                        } else {
                            ((Steerable) livingEntity).setSaddle(false);
                        }
                    }
                } else {
                    if (equipment.get("hand") != null) {
                        livingEntity.getEquipment().setItemInMainHand(equipment.get("hand").getItemStack());
                    }
                    if (equipment.get("head") != null) {
                        livingEntity.getEquipment().setHelmet(equipment.get("head").getItemStack());
                    }
                    if (equipment.get("chest") != null) {
                        livingEntity.getEquipment().setChestplate(equipment.get("chest").getItemStack());
                    }
                    if (equipment.get("legs") != null) {
                        livingEntity.getEquipment().setLeggings(equipment.get("legs").getItemStack());
                    }
                    if (equipment.get("boots") != null) {
                        livingEntity.getEquipment().setBoots(equipment.get("boots").getItemStack());
                    }
                    if (equipment.get("offhand") != null) {
                        livingEntity.getEquipment().setItemInOffHand(equipment.get("offhand").getItemStack());
                    }
                }
            }
        }
    }
}
Also used : AbstractHorse(org.bukkit.entity.AbstractHorse) LivingEntity(org.bukkit.entity.LivingEntity) Steerable(org.bukkit.entity.Steerable) Equipment(net.citizensnpcs.api.trait.trait.Equipment) NPCTag(com.denizenscript.denizen.objects.NPCTag) EntityTag(com.denizenscript.denizen.objects.EntityTag) HorseInventory(org.bukkit.inventory.HorseInventory) ItemTag(com.denizenscript.denizen.objects.ItemTag)

Aggregations

ItemTag (com.denizenscript.denizen.objects.ItemTag)77 EventHandler (org.bukkit.event.EventHandler)35 ItemStack (org.bukkit.inventory.ItemStack)20 LocationTag (com.denizenscript.denizen.objects.LocationTag)19 EntityTag (com.denizenscript.denizen.objects.EntityTag)14 ListTag (com.denizenscript.denizencore.objects.core.ListTag)14 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)13 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)9 Item (org.bukkit.entity.Item)7 Argument (com.denizenscript.denizencore.objects.Argument)6 List (java.util.List)6 InvalidArgumentsException (com.denizenscript.denizencore.exceptions.InvalidArgumentsException)5 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)5 HumanEntity (org.bukkit.entity.HumanEntity)5 Player (org.bukkit.entity.Player)5 InventoryTag (com.denizenscript.denizen.objects.InventoryTag)4 BukkitTagContext (com.denizenscript.denizen.tags.BukkitTagContext)4 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)4 ArrayList (java.util.ArrayList)4 MaterialTag (com.denizenscript.denizen.objects.MaterialTag)3