Search in sources :

Example 6 with InteractScriptContainer

use of com.denizenscript.denizen.scripts.containers.core.InteractScriptContainer 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)

Aggregations

InteractScriptContainer (com.denizenscript.denizen.scripts.containers.core.InteractScriptContainer)6 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)5 NPCTag (com.denizenscript.denizen.objects.NPCTag)4 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)4 TriggerTrait (com.denizenscript.denizen.npc.traits.TriggerTrait)3 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)3 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)3 BukkitTagContext (com.denizenscript.denizen.tags.BukkitTagContext)2 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)2 TimeTag (com.denizenscript.denizencore.objects.core.TimeTag)2 Map (java.util.Map)2 EventHandler (org.bukkit.event.EventHandler)2 AssignmentTrait (com.denizenscript.denizen.npc.traits.AssignmentTrait)1 EntityTag (com.denizenscript.denizen.objects.EntityTag)1 ItemTag (com.denizenscript.denizen.objects.ItemTag)1 AssignmentScriptContainer (com.denizenscript.denizen.scripts.containers.core.AssignmentScriptContainer)1 BukkitScriptEntryData (com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData)1 InvalidArgumentsException (com.denizenscript.denizencore.exceptions.InvalidArgumentsException)1 ListTag (com.denizenscript.denizencore.objects.core.ListTag)1 HashMap (java.util.HashMap)1