Search in sources :

Example 26 with NPCTag

use of com.denizenscript.denizen.objects.NPCTag 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 27 with NPCTag

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

the class NPCTagBase method navComplete.

// <--[action]
// @Actions
// complete navigation
// 
// @Triggers when the NPC has finished a 'walk' command,
// or has reached a path point.
// 
// @Context
// None
// 
// -->
@EventHandler
public void navComplete(NavigationCompleteEvent event) {
    // Do the assignment script action
    if (!event.getNPC().hasTrait(AssignmentTrait.class)) {
        return;
    }
    NPCTag npc = new NPCTag(event.getNPC());
    npc.action("complete navigation", null);
}
Also used : AssignmentTrait(com.denizenscript.denizen.npc.traits.AssignmentTrait) NPCTag(com.denizenscript.denizen.objects.NPCTag) EventHandler(org.bukkit.event.EventHandler)

Example 28 with NPCTag

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

the class NPCTagBase method onHit.

// <--[action]
// @Actions
// hit
// hit on <entity>
// 
// @Triggers when the NPC hits an enemy.
// 
// @Context
// None
// 
// -->
// <--[action]
// @Actions
// kill
// kill of <entity>
// 
// @Triggers when the NPC kills an enemy.
// 
// @Context
// None
// 
// -->
@EventHandler(priority = EventPriority.MONITOR)
public void onHit(EntityDamageByEntityEvent event) {
    NPC citizen = CitizensAPI.getNPCRegistry().getNPC(event.getEntity());
    if (citizen == null) {
        if (event.getDamager() instanceof Projectile) {
            if (((Projectile) event.getDamager()).getShooter() instanceof Entity) {
                citizen = CitizensAPI.getNPCRegistry().getNPC((Entity) ((Projectile) event.getDamager()).getShooter());
            }
        }
    }
    if (citizen == null || !citizen.hasTrait(AssignmentTrait.class)) {
        return;
    }
    NPCTag npc = new NPCTag(citizen);
    PlayerTag player = null;
    if (event.getEntity() instanceof Player) {
        player = PlayerTag.mirrorBukkitPlayer((Player) event.getEntity());
    }
    // TODO: Context containing the entity hit
    npc.action("hit", player);
    npc.action("hit on " + event.getEntityType().name(), player);
    if (event.getEntity() instanceof LivingEntity) {
        if (((LivingEntity) event.getEntity()).getHealth() - event.getFinalDamage() <= 0) {
            npc.action("kill", player);
            npc.action("kill of " + event.getEntityType().name(), player);
        }
    }
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) LivingEntity(org.bukkit.entity.LivingEntity) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) NPCTag(com.denizenscript.denizen.objects.NPCTag) Projectile(org.bukkit.entity.Projectile) EventHandler(org.bukkit.event.EventHandler)

Example 29 with NPCTag

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

the class NPCTagBase method navBegin.

// <--[action]
// @Actions
// begin navigation
// 
// @Triggers when the NPC has received a 'walk' command,
// or is about to follow a path.
// 
// @Context
// None
// 
// -->
@EventHandler
public void navBegin(NavigationBeginEvent event) {
    if (!event.getNPC().hasTrait(AssignmentTrait.class)) {
        return;
    }
    NPCTag npc = new NPCTag(event.getNPC());
    npc.action("begin navigation", null);
    if (event.getNPC().getNavigator().getTargetType() == TargetType.ENTITY) {
        LivingEntity entity = (LivingEntity) event.getNPC().getNavigator().getEntityTarget().getTarget();
        // and that entity is not dead, trigger "on attack" command
        if (event.getNPC().getNavigator().getEntityTarget().isAggressive() && !entity.isDead()) {
            PlayerTag player = null;
            // Check if the entity attacked by this NPC is a player
            if (entity instanceof Player) {
                player = PlayerTag.mirrorBukkitPlayer((Player) entity);
            }
            // <--[action]
            // @Actions
            // attack
            // attack on <entity>
            // 
            // @Triggers when the NPC is about to attack an enemy.
            // 
            // @Context
            // None
            // 
            // -->
            npc.action("attack", player);
            npc.action("attack on " + entity.getType().toString(), player);
        }
        previousLocations.put(event.getNPC().getId(), npc.getLocation());
    }
}
Also used : AssignmentTrait(com.denizenscript.denizen.npc.traits.AssignmentTrait) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) NPCTag(com.denizenscript.denizen.objects.NPCTag) EventHandler(org.bukkit.event.EventHandler)

Example 30 with NPCTag

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

the class NPCTagBase method navCancel.

// <--[action]
// @Actions
// cancel navigation
// cancel navigation due to <reason>
// 
// @Triggers when a plugin or script cancels an NPC's navigation.
// 
// @Context
// None
// 
// -->
@EventHandler
public void navCancel(NavigationCancelEvent event) {
    if (!event.getNPC().hasTrait(AssignmentTrait.class)) {
        return;
    }
    NPCTag npc = new NPCTag(event.getNPC());
    npc.action("cancel navigation", null);
    npc.action("cancel navigation due to " + event.getCancelReason().toString(), null);
}
Also used : AssignmentTrait(com.denizenscript.denizen.npc.traits.AssignmentTrait) NPCTag(com.denizenscript.denizen.objects.NPCTag) EventHandler(org.bukkit.event.EventHandler)

Aggregations

NPCTag (com.denizenscript.denizen.objects.NPCTag)72 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)24 EventHandler (org.bukkit.event.EventHandler)22 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)21 Player (org.bukkit.entity.Player)15 LocationTag (com.denizenscript.denizen.objects.LocationTag)12 EntityTag (com.denizenscript.denizen.objects.EntityTag)10 ListTag (com.denizenscript.denizencore.objects.core.ListTag)9 ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)8 HashMap (java.util.HashMap)8 NPC (net.citizensnpcs.api.npc.NPC)8 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)7 AssignmentTrait (com.denizenscript.denizen.npc.traits.AssignmentTrait)6 Location (org.bukkit.Location)6 TriggerTrait (com.denizenscript.denizen.npc.traits.TriggerTrait)5 BukkitScriptEntryData (com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData)5 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Entity (org.bukkit.entity.Entity)5