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;
}
Aggregations