Search in sources :

Example 1 with TriggerTrait

use of com.denizenscript.denizen.npc.traits.TriggerTrait in project Denizen-For-Bukkit by DenizenScript.

the class TriggerCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    ElementTag toggle = scriptEntry.getElement("toggle");
    ElementTag trigger = scriptEntry.getElement("trigger");
    ElementTag radius = scriptEntry.getElement("radius");
    DurationTag cooldown = scriptEntry.getObjectTag("cooldown");
    NPCTag npc = scriptEntry.hasObject("npc") ? (NPCTag) scriptEntry.getObject("npc") : Utilities.getEntryNPC(scriptEntry);
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), trigger, toggle, radius, cooldown, npc);
    }
    // Add trigger trait
    if (!npc.getCitizen().hasTrait(TriggerTrait.class)) {
        npc.getCitizen().addTrait(TriggerTrait.class);
    }
    TriggerTrait trait = npc.getCitizen().getOrAddTrait(TriggerTrait.class);
    switch(Toggle.valueOf(toggle.asString().toUpperCase())) {
        case TOGGLE:
            trait.toggleTrigger(trigger.asString());
            break;
        case TRUE:
            trait.toggleTrigger(trigger.asString(), true);
            break;
        case FALSE:
            trait.toggleTrigger(trigger.asString(), false);
            break;
    }
    if (radius != null) {
        trait.setLocalRadius(trigger.asString(), radius.asInt());
    }
    if (cooldown != null) {
        trait.setLocalCooldown(trigger.asString(), cooldown.getSeconds());
    }
}
Also used : TriggerTrait(com.denizenscript.denizen.npc.traits.TriggerTrait) NPCTag(com.denizenscript.denizen.objects.NPCTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag)

Example 2 with TriggerTrait

use of com.denizenscript.denizen.npc.traits.TriggerTrait 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 3 with TriggerTrait

use of com.denizenscript.denizen.npc.traits.TriggerTrait 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 4 with TriggerTrait

use of com.denizenscript.denizen.npc.traits.TriggerTrait in project Denizen-For-Bukkit by DenizenScript.

the class ChatTrigger method process.

// Technically defined in TriggerTrait, but placing here instead.
// <--[action]
// @Actions
// chat
// 
// @Triggers when a player chats to the NPC.
// 
// @Context
// <context.message> returns the triggering message
// <context.keyword> returns the keyword matched by a RegEx trigger
// 
// @Determine
// "CANCELLED" to stop the player from chatting.
// ElementTag to change the message.
// 
// -->
public ChatContext process(Player player, String message) {
    NPCTag npc = Utilities.getClosestNPC_ChatTrigger(player.getLocation(), 25);
    if (Debug.verbose) {
        Debug.log("Processing chat trigger: valid npc? " + (npc != null));
    }
    if (npc == null) {
        return new ChatContext(false);
    }
    if (Debug.verbose) {
        Debug.log("Has trait?  " + npc.getCitizen().hasTrait(TriggerTrait.class));
    }
    if (!npc.getCitizen().hasTrait(TriggerTrait.class)) {
        return new ChatContext(false);
    }
    if (Debug.verbose) {
        Debug.log("enabled? " + npc.getCitizen().getOrAddTrait(TriggerTrait.class).isEnabled(name));
    }
    if (!npc.getCitizen().getOrAddTrait(TriggerTrait.class).isEnabled(name)) {
        return new ChatContext(false);
    }
    if (npc.getTriggerTrait().getRadius(name) < npc.getLocation().distance(player.getLocation())) {
        if (Debug.verbose) {
            Debug.log("Not in range");
        }
        return new ChatContext(false);
    }
    if (Settings.chatMustSeeNPC()) {
        if (!player.hasLineOfSight(npc.getEntity())) {
            if (Debug.verbose) {
                Debug.log("no LOS");
            }
            return new ChatContext(false);
        }
    }
    if (Settings.chatMustLookAtNPC()) {
        if (!NMSHandler.getEntityHelper().isFacingEntity(player, npc.getEntity(), 45)) {
            if (Debug.verbose) {
                Debug.log("Not facing");
            }
            return new ChatContext(false);
        }
    }
    boolean ret = false;
    Map<String, ObjectTag> context = new HashMap<>();
    context.put("message", new ElementTag(message));
    TriggerTrait.TriggerContext trigger = npc.getTriggerTrait().trigger(ChatTrigger.this, new PlayerTag(player), context);
    if (trigger.hasDetermination()) {
        if (trigger.getDeterminations().containsCaseInsensitive("cancelled")) {
            if (Debug.verbose) {
                Debug.log("Cancelled");
            }
            return new ChatContext(true);
        }
    }
    if (!trigger.wasTriggered()) {
        if (Settings.chatGloballyIfUninteractable()) {
            if (Debug.verbose) {
                Debug.log(ChatColor.YELLOW + "Resuming. " + ChatColor.WHITE + "The NPC is currently cooling down or engaged.");
            }
            return new ChatContext(false);
        } else {
            ret = true;
        }
    }
    if (trigger.hasDetermination()) {
        message = trigger.getDeterminations().get(0);
    }
    List<InteractScriptContainer> scripts = npc.getInteractScripts(new PlayerTag(player), ChatTrigger.class);
    if (scripts == null) {
        if (Debug.verbose) {
            Debug.log("null scripts");
        }
        return new ChatContext(message, false);
    }
    ChatContext returnable = new ChatContext(ret);
    for (InteractScriptContainer script : scripts) {
        processSingle(message, player, npc, context, script, returnable);
    }
    return returnable;
}
Also used : TriggerTrait(com.denizenscript.denizen.npc.traits.TriggerTrait) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) NPCTag(com.denizenscript.denizen.objects.NPCTag) InteractScriptContainer(com.denizenscript.denizen.scripts.containers.core.InteractScriptContainer) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag)

Example 5 with TriggerTrait

use of com.denizenscript.denizen.npc.traits.TriggerTrait in project Denizen-For-Bukkit by DenizenScript.

the class ProximityTrigger method onEnable.

@Override
public void onEnable() {
    Bukkit.getServer().getPluginManager().registerEvents(this, Denizen.getInstance());
    final ProximityTrigger trigger = this;
    taskID = Bukkit.getScheduler().scheduleSyncRepeatingTask(Denizen.getInstance(), () -> {
        Collection<? extends Player> allPlayers = Bukkit.getOnlinePlayers();
        for (NPC citizensNPC : CitizensAPI.getNPCRegistry()) {
            if (citizensNPC == null || !citizensNPC.isSpawned()) {
                continue;
            }
            if (!citizensNPC.hasTrait(TriggerTrait.class) || !citizensNPC.getOrAddTrait(TriggerTrait.class).isEnabled(name)) {
                continue;
            }
            NPCTag npc = new NPCTag(citizensNPC);
            TriggerTrait triggerTrait = npc.getTriggerTrait();
            for (Player bukkitPlayer : allPlayers) {
                if (!npc.getWorld().equals(bukkitPlayer.getWorld()) && hasExitedProximityOf(bukkitPlayer, npc)) {
                    continue;
                }
                if (!isCloseEnough(bukkitPlayer, npc) && hasExitedProximityOf(bukkitPlayer, npc)) {
                    continue;
                }
                PlayerTag player = PlayerTag.mirrorBukkitPlayer(bukkitPlayer);
                double entryRadius = triggerTrait.getRadius(name);
                double exitRadius = triggerTrait.getRadius(name);
                double moveRadius = triggerTrait.getRadius(name);
                Location npcLocation = npc.getLocation();
                boolean playerChangedWorlds = false;
                if (npcLocation.getWorld() != player.getWorld()) {
                    playerChangedWorlds = true;
                }
                boolean exitedProximity = hasExitedProximityOf(bukkitPlayer, npc);
                double distance = 0;
                if (!playerChangedWorlds) {
                    distance = npcLocation.distance(player.getLocation());
                }
                if (!exitedProximity && (playerChangedWorlds || distance >= exitRadius)) {
                    if (!triggerTrait.triggerCooldownOnly(trigger, player)) {
                        continue;
                    }
                    exitProximityOf(bukkitPlayer, npc);
                    npc.action("exit proximity", player);
                    parseAll(npc, player, "EXIT");
                } else if (exitedProximity && distance <= entryRadius) {
                    if (!triggerTrait.triggerCooldownOnly(trigger, player)) {
                        continue;
                    }
                    enterProximityOf(bukkitPlayer, npc);
                    npc.action("enter proximity", player);
                    parseAll(npc, player, "ENTRY");
                } else if (!exitedProximity && distance <= moveRadius) {
                    npc.action("move proximity", player);
                    parseAll(npc, player, "MOVE");
                }
            }
        }
    }, 5, 5);
}
Also used : NPC(net.citizensnpcs.api.npc.NPC) Player(org.bukkit.entity.Player) TriggerTrait(com.denizenscript.denizen.npc.traits.TriggerTrait) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) NPCTag(com.denizenscript.denizen.objects.NPCTag) Location(org.bukkit.Location)

Aggregations

TriggerTrait (com.denizenscript.denizen.npc.traits.TriggerTrait)5 NPCTag (com.denizenscript.denizen.objects.NPCTag)5 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)4 InteractScriptContainer (com.denizenscript.denizen.scripts.containers.core.InteractScriptContainer)3 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)3 BukkitTagContext (com.denizenscript.denizen.tags.BukkitTagContext)2 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)2 Map (java.util.Map)2 EventHandler (org.bukkit.event.EventHandler)2 EntityTag (com.denizenscript.denizen.objects.EntityTag)1 ItemTag (com.denizenscript.denizen.objects.ItemTag)1 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)1 ListTag (com.denizenscript.denizencore.objects.core.ListTag)1 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)1 HashMap (java.util.HashMap)1 NPC (net.citizensnpcs.api.npc.NPC)1 Location (org.bukkit.Location)1 Player (org.bukkit.entity.Player)1