Search in sources :

Example 11 with BukkitTagContext

use of net.aufdemrand.denizen.tags.BukkitTagContext in project Denizen-For-Bukkit by DenizenScript.

the class NPCTags method npcTags.

@TagManager.TagEvents
public void npcTags(ReplaceableTagEvent event) {
    if (!event.matches("npc") || event.replaced()) {
        return;
    }
    // Build a new attribute out of the raw_tag supplied in the script to be fulfilled
    Attribute attribute = event.getAttributes();
    // NPCTags require a... dNPC!
    dNPC n = ((BukkitTagContext) event.getContext()).npc;
    // Player tag may specify a new player in the <player[context]...> portion of the tag.
    if (attribute.hasContext(1)) // Check if this is a valid player and update the dPlayer object reference.
    {
        if (dNPC.matches(attribute.getContext(1))) {
            n = dNPC.valueOf(attribute.getContext(1));
        } else {
            if (!event.hasAlternative()) {
                dB.echoError("Could not match '" + attribute.getContext(1) + "' to a valid NPC!");
            }
            return;
        }
    }
    if (n == null || !n.isValid()) {
        if (!event.hasAlternative()) {
            dB.echoError("Invalid or missing NPC for tag <" + event.raw_tag + ">!");
        }
        return;
    }
    event.setReplaced(n.getAttribute(attribute.fulfill(1)));
}
Also used : net.aufdemrand.denizen.objects.dNPC(net.aufdemrand.denizen.objects.dNPC) BukkitTagContext(net.aufdemrand.denizen.tags.BukkitTagContext) Attribute(net.aufdemrand.denizencore.tags.Attribute)

Example 12 with BukkitTagContext

use of net.aufdemrand.denizen.tags.BukkitTagContext in project Denizen-For-Bukkit by DenizenScript.

the class PlayerTags method playerTags.

//////////
//  ReplaceableTagEvent handler
////////
@TagManager.TagEvents
public void playerTags(ReplaceableTagEvent event) {
    if (!event.matches("player", "pl") || event.replaced()) {
        return;
    }
    // Build a new attribute out of the raw_tag supplied in the script to be fulfilled
    Attribute attribute = event.getAttributes();
    // PlayerTags require a... dPlayer!
    dPlayer p = ((BukkitTagContext) event.getContext()).player;
    // Player tag may specify a new player in the <player[context]...> portion of the tag.
    if (attribute.hasContext(1)) {
        p = dPlayer.valueOf(attribute.getContext(1));
    }
    if (p == null || !p.isValid()) {
        if (!event.hasAlternative()) {
            dB.echoError("Invalid or missing player for tag <" + event.raw_tag + ">!");
        }
        return;
    }
    event.setReplaced(p.getAttribute(attribute.fulfill(1)));
}
Also used : BukkitTagContext(net.aufdemrand.denizen.tags.BukkitTagContext) Attribute(net.aufdemrand.denizencore.tags.Attribute) net.aufdemrand.denizen.objects.dPlayer(net.aufdemrand.denizen.objects.dPlayer)

Example 13 with BukkitTagContext

use of net.aufdemrand.denizen.tags.BukkitTagContext in project Denizen-For-Bukkit by DenizenScript.

the class ClickTrigger method clickTrigger.

// <--[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) {
    // ANY triggers enabled!
    if (!event.getNPC().hasTrait(TriggerTrait.class)) {
        return;
    }
    // The rest of the methods beyond this point require a dNPC object, which can easily be
    // obtained if a valid NPC object is available:
    dNPC npc = DenizenAPI.getDenizenNPC(event.getNPC());
    // super AbstractTrigger and contains the name of the trigger that was use in registration.
    if (!npc.getTriggerTrait().isEnabled(name)) {
        return;
    }
    // We'll get the player too, since it makes reading the next few methods a bit easier:
    dPlayer player = dPlayer.mirrorBukkitPlayer(event.getClicker());
    // Check availability based on the NPC's ENGAGED status and the trigger's COOLDOWN that is
    // provided (and adjustable) by the TriggerTrait. Just use .trigger(...)!
    // If unavailable (engaged or not cool), .trigger calls 'On Unavailable' action and returns false.
    // If available (not engaged, and cool), .trigger sets cool down and returns true.
    TriggerTrait.TriggerContext trigger = npc.getTriggerTrait().trigger(this, player);
    if (!trigger.wasTriggered()) {
        return;
    }
    if (trigger.hasDetermination() && trigger.getDetermination().equalsIgnoreCase("cancelled")) {
        event.setCancelled(true);
        return;
    }
    // Note: In some cases, the automatic actions that .trigger offers may not be
    // desired. In this case, it's recommended to at least use .triggerCooldownOnly which
    // only handles cooling down the trigger with the triggertrait if the 'available' criteria
    // is met. This handles the built-in cooldown that TriggerTrait implements.
    // Okay, now we need to know which interact script will be selected for the Player/NPC
    // based on requirements/npc's assignment script. To get that information, use:
    // InteractScriptHelper.getInteractScript(dNPC, Player, Trigger Class)
    // .getInteractScript will check the Assignment for possible scripts, and automatically
    // check requirements for each of them.
    InteractScriptContainer script = npc.getInteractScript(player, getClass());
    // In an Interact Script, Triggers can have multiple scripts to choose from depending on
    // some kind of 'criteria'. For the 'Click Trigger', that criteria is the item the Player
    // has in hand. Let's get the possible criteria to see which 'Click Trigger script', if any,
    // should trigger. For example:
    //
    // Script Name:
    //   type: interact
    //   steps:
    //     current step:
    //       click trigger:
    String id = null;
    if (script != null) {
        Map<String, String> idMap = script.getIdMapFor(this.getClass(), player);
        if (!idMap.isEmpty()) // Iterate through the different id entries in the step's click trigger
        {
            for (Map.Entry<String, String> entry : idMap.entrySet()) {
                // Tag the entry value to account for replaceables
                // TODO: script arg?
                String entry_value = TagManager.tag(entry.getValue(), new BukkitTagContext(player, npc, false, null, false, null));
                // Check if the item specified in the specified id's 'trigger:' key
                // matches the item that the player is holding.
                dItem item = dItem.valueOf(entry_value);
                if (item == null) {
                    dB.echoError("Invalid click trigger in script '" + script.getName() + "' (null trigger item)!");
                }
                if (item != null && item.comparesTo(player.getPlayerEntity().getItemInHand()) >= 0 && script.checkSpecificTriggerScriptRequirementsFor(this.getClass(), player, npc, entry.getKey())) {
                    id = entry.getKey();
                }
            }
        }
    }
    // we'll call the action 'on no click trigger'.
    if (!parse(npc, player, script, id)) {
        npc.action("no click trigger", player);
    }
}
Also used : TriggerTrait(net.aufdemrand.denizen.npc.traits.TriggerTrait) net.aufdemrand.denizen.objects.dNPC(net.aufdemrand.denizen.objects.dNPC) net.aufdemrand.denizen.objects.dItem(net.aufdemrand.denizen.objects.dItem) BukkitTagContext(net.aufdemrand.denizen.tags.BukkitTagContext) net.aufdemrand.denizen.objects.dPlayer(net.aufdemrand.denizen.objects.dPlayer) InteractScriptContainer(net.aufdemrand.denizen.scripts.containers.core.InteractScriptContainer) Map(java.util.Map) EventHandler(org.bukkit.event.EventHandler)

Example 14 with BukkitTagContext

use of net.aufdemrand.denizen.tags.BukkitTagContext in project Denizen-For-Bukkit by DenizenScript.

the class DamageTrigger method damageTrigger.

// <--[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) {
    if (event.getEntity() == null) {
        return;
    }
    dEntity damager = new dEntity(event.getDamager());
    if (damager.isProjectile() && damager.hasShooter()) {
        damager = damager.getShooter();
    }
    Map<String, dObject> context = new HashMap<String, dObject>();
    context.put("damage", new Element(event.getDamage()));
    dPlayer dplayer = null;
    if (CitizensAPI.getNPCRegistry().isNPC(event.getEntity())) {
        dNPC npc = DenizenAPI.getDenizenNPC(CitizensAPI.getNPCRegistry().getNPC(event.getEntity()));
        if (npc == null) {
            return;
        }
        if (npc.getCitizen() == null) {
            return;
        }
        context.put("damager", damager);
        npc.action("damaged", null, context);
        if (damager.isPlayer()) {
            dplayer = damager.getDenizenPlayer();
        } else {
            return;
        }
        if (!npc.getCitizen().hasTrait(TriggerTrait.class)) {
            return;
        }
        if (!npc.getTriggerTrait().isEnabled(name)) {
            return;
        }
        // Get the TriggerContext
        TriggerTrait.TriggerContext trigger = npc.getTriggerTrait().trigger(this, dplayer);
        // Return if the trigger wasn't triggered.
        if (!trigger.wasTriggered()) {
            return;
        }
        // ..or if the determination was cancelled.
        if (trigger.hasDetermination() && trigger.getDetermination().equalsIgnoreCase("cancelled")) {
            event.setCancelled(true);
            return;
        }
        // Build the interact script
        InteractScriptContainer script = InteractScriptHelper.getInteractScript(npc, dplayer, getClass());
        String id = null;
        if (script != null) {
            Map<String, String> idMap = script.getIdMapFor(this.getClass(), dplayer);
            if (!idMap.isEmpty()) // Iterate through the different id entries in the step's click trigger
            {
                for (Map.Entry<String, String> entry : idMap.entrySet()) {
                    // Tag the entry value to account for replaceables
                    // TODO: script arg?
                    String entry_value = TagManager.tag(entry.getValue(), new BukkitTagContext(dplayer, npc, false, null, false, null));
                    // matches the item that the player is holding.
                    if (dItem.valueOf(entry_value).comparesTo(dplayer.getPlayerEntity().getItemInHand()) >= 0 && script.checkSpecificTriggerScriptRequirementsFor(this.getClass(), dplayer, npc, entry.getKey())) {
                        id = entry.getKey();
                    }
                }
            }
        }
        if (!parse(npc, dplayer, script, id, context)) {
            npc.action("no damage trigger", dplayer);
        }
    }
}
Also used : TriggerTrait(net.aufdemrand.denizen.npc.traits.TriggerTrait) HashMap(java.util.HashMap) Element(net.aufdemrand.denizencore.objects.Element) net.aufdemrand.denizen.objects.dNPC(net.aufdemrand.denizen.objects.dNPC) BukkitTagContext(net.aufdemrand.denizen.tags.BukkitTagContext) net.aufdemrand.denizen.objects.dEntity(net.aufdemrand.denizen.objects.dEntity) net.aufdemrand.denizencore.objects.dObject(net.aufdemrand.denizencore.objects.dObject) net.aufdemrand.denizen.objects.dPlayer(net.aufdemrand.denizen.objects.dPlayer) InteractScriptContainer(net.aufdemrand.denizen.scripts.containers.core.InteractScriptContainer) HashMap(java.util.HashMap) Map(java.util.Map) EventHandler(org.bukkit.event.EventHandler)

Example 15 with BukkitTagContext

use of net.aufdemrand.denizen.tags.BukkitTagContext in project Denizen-For-Bukkit by DenizenScript.

the class AnchorTags method anchorTags.

@TagManager.TagEvents
public void anchorTags(ReplaceableTagEvent event) {
    if (!event.matches("ANCHOR")) {
        return;
    }
    dB.echoError(event.getAttributes().getScriptEntry().getResidingQueue(), "anchor: tags are deprecated! Use <npc.anchor[]>!");
    NPC npc = null;
    if (event.getType() != null && event.getType().matches("\\d+")) {
        npc = CitizensAPI.getNPCRegistry().getById(Integer.valueOf(event.getType()));
    } else if (((BukkitTagContext) event.getContext()).npc != null) {
        npc = ((BukkitTagContext) event.getContext()).npc.getCitizen();
    }
    if (npc == null) {
        return;
    }
    if (npc.getTrait(Anchors.class).getAnchor(event.getValue()) != null) {
        Location anchor = npc.getTrait(Anchors.class).getAnchor(event.getValue()).getLocation();
        event.setReplaced(anchor.getX() + "," + anchor.getY() + "," + anchor.getZ() + "," + anchor.getWorld().getName());
    }
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) BukkitTagContext(net.aufdemrand.denizen.tags.BukkitTagContext) Location(org.bukkit.Location)

Aggregations

BukkitTagContext (net.aufdemrand.denizen.tags.BukkitTagContext)23 net.aufdemrand.denizen.objects.dPlayer (net.aufdemrand.denizen.objects.dPlayer)9 Element (net.aufdemrand.denizencore.objects.Element)7 net.aufdemrand.denizencore.objects.dScript (net.aufdemrand.denizencore.objects.dScript)6 net.aufdemrand.denizen.objects.dEntity (net.aufdemrand.denizen.objects.dEntity)5 net.aufdemrand.denizen.objects.dNPC (net.aufdemrand.denizen.objects.dNPC)5 Map (java.util.Map)4 net.aufdemrand.denizen.objects.dItem (net.aufdemrand.denizen.objects.dItem)4 net.aufdemrand.denizencore.objects.dObject (net.aufdemrand.denizencore.objects.dObject)4 Attribute (net.aufdemrand.denizencore.tags.Attribute)4 EventHandler (org.bukkit.event.EventHandler)4 HashMap (java.util.HashMap)3 Matcher (java.util.regex.Matcher)3 TriggerTrait (net.aufdemrand.denizen.npc.traits.TriggerTrait)3 InteractScriptContainer (net.aufdemrand.denizen.scripts.containers.core.InteractScriptContainer)3 net.aufdemrand.denizencore.objects.dList (net.aufdemrand.denizencore.objects.dList)3 Player (org.bukkit.entity.Player)3 Iterator (java.util.Iterator)2 Pattern (java.util.regex.Pattern)2 BukkitScriptEntryData (net.aufdemrand.denizen.BukkitScriptEntryData)2