Search in sources :

Example 41 with ItemTag

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

the class ClickTrigger method clickTrigger.

// <--[language]
// @name Click Triggers
// @group NPC Interact Scripts
// @description
// Click Triggers are triggered when a player right clicks the NPC.
// 
// These are very basic with no extraneous complexity.
// 
// <code>
// click trigger:
// script:
// - narrate "hi <player.name>"
// </code>
// 
// They can optionally have an item matcher with multiple triggers, for the item in the player's hand. For example:
// <code>
// click trigger:
// 1:
// trigger: my_item_script
// script:
// - narrate "Nice item script"
// 2:
// trigger: stone
// script:
// - narrate "Nice vanilla item"
// 3:
// script:
// - narrate "I don't recognize your held item"
// </code>
// 
// -->
// <--[action]
// @Actions
// no click trigger
// 
// @Triggers when the NPC is clicked but no click trigger fires.
// 
// @Context
// None
// 
// -->
// Technically defined in TriggerTrait, but placing here instead.
// <--[action]
// @Actions
// click
// 
// @Triggers when the NPC is clicked by a player.
// 
// @Context
// None
// 
// @Determine
// "cancelled" to cancel the click event completely.
// 
// -->
@EventHandler
public void clickTrigger(NPCRightClickEvent event) {
    if (!event.getNPC().hasTrait(TriggerTrait.class)) {
        return;
    }
    NPCTag npc = new NPCTag(event.getNPC());
    if (!npc.getTriggerTrait().isEnabled(name)) {
        return;
    }
    PlayerTag player = PlayerTag.mirrorBukkitPlayer(event.getClicker());
    TriggerTrait.TriggerContext trigger = npc.getTriggerTrait().trigger(this, player);
    if (!trigger.wasTriggered()) {
        return;
    }
    if (trigger.hasDetermination() && trigger.getDeterminations().containsCaseInsensitive("cancelled")) {
        event.setCancelled(true);
        return;
    }
    List<InteractScriptContainer> scripts = npc.getInteractScripts(player, ClickTrigger.class);
    boolean any = false;
    if (scripts != null) {
        for (InteractScriptContainer script : scripts) {
            String id = null;
            Map<String, String> idMap = script.getIdMapFor(ClickTrigger.class, player);
            if (!idMap.isEmpty()) {
                ItemTag heldItem = new ItemTag(player.getPlayerEntity().getEquipment().getItemInMainHand());
                for (Map.Entry<String, String> entry : idMap.entrySet()) {
                    String entry_value = TagManager.tag(entry.getValue(), new BukkitTagContext(player, npc, null, false, null));
                    boolean isMatch = entry_value.isEmpty() || BukkitScriptEvent.tryItem(heldItem, entry_value);
                    if (script.shouldDebug()) {
                        Debug.echoDebug(script, "Comparing click trigger '<A>" + entry_value + "<W>' with item '<A>" + heldItem.debuggable() + "<W>': " + (isMatch ? "<GR>Match!" : "<Y>Not a match"));
                    }
                    if (isMatch) {
                        id = entry.getKey();
                        break;
                    }
                }
            }
            if (parse(npc, player, script, id)) {
                any = true;
            }
        }
    }
    if (!any) {
        npc.action("no click trigger", player);
    }
}
Also used : TriggerTrait(com.denizenscript.denizen.npc.traits.TriggerTrait) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) NPCTag(com.denizenscript.denizen.objects.NPCTag) InteractScriptContainer(com.denizenscript.denizen.scripts.containers.core.InteractScriptContainer) ItemTag(com.denizenscript.denizen.objects.ItemTag) Map(java.util.Map) EventHandler(org.bukkit.event.EventHandler)

Example 42 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 43 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)

Example 44 with ItemTag

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

the class ItemRawNBT method setFullNBT.

public void setFullNBT(ItemTag item, MapTag input, TagContext context, boolean retainOld) {
    CompoundTag compoundTag = retainOld ? NMSHandler.getItemHelper().getNbtData(item.getItemStack()) : null;
    Map<String, Tag> result = compoundTag == null ? new LinkedHashMap<>() : new LinkedHashMap<>(compoundTag.getValue());
    for (Map.Entry<StringHolder, ObjectTag> entry : input.map.entrySet()) {
        try {
            Tag tag = convertObjectToNbt(entry.getValue().toString(), context, "(item).");
            if (tag != null) {
                result.put(entry.getKey().str, tag);
            }
        } catch (Exception ex) {
            Debug.echoError("Raw_Nbt input failed for root key '" + entry.getKey().str + "'.");
            Debug.echoError(ex);
            return;
        }
    }
    compoundTag = NMSHandler.getInstance().createCompoundTag(result);
    item.setItemStack(NMSHandler.getItemHelper().setNbtData(item.getItemStack(), compoundTag));
}
Also used : StringHolder(com.denizenscript.denizencore.utilities.text.StringHolder) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) ItemTag(com.denizenscript.denizen.objects.ItemTag) MapTag(com.denizenscript.denizencore.objects.core.MapTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag)

Example 45 with ItemTag

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

the class LingeringPotionSplashScriptEvent method onLingeringPotionSplash.

@EventHandler
public void onLingeringPotionSplash(LingeringPotionSplashEvent event) {
    item = new ItemTag(event.getEntity().getItem());
    location = new LocationTag(event.getAreaEffectCloud().getLocation());
    this.event = event;
    fire(event);
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) ItemTag(com.denizenscript.denizen.objects.ItemTag) EventHandler(org.bukkit.event.EventHandler)

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