use of net.aufdemrand.denizencore.objects.Element 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.
// Element to change the message.
//
// -->
public ChatContext process(Player player, String message) {
// Check if there is an NPC within range of a player to chat to.
dNPC npc = Utilities.getClosestNPC_ChatTrigger(player.getLocation(), 25);
dPlayer denizenPlayer = dPlayer.mirrorBukkitPlayer(player);
if (HyperDebug) {
dB.log("Processing chat trigger: valid npc? " + (npc != null));
}
// No NPC? Nothing else to do here.
if (npc == null) {
return new ChatContext(false);
}
if (HyperDebug) {
dB.log("Has trait? " + npc.getCitizen().hasTrait(TriggerTrait.class));
}
// just return false.
if (!npc.getCitizen().hasTrait(TriggerTrait.class)) {
return new ChatContext(false);
}
if (HyperDebug) {
dB.log("enabled? " + npc.getCitizen().getTrait(TriggerTrait.class).isEnabled(name));
}
if (!npc.getCitizen().getTrait(TriggerTrait.class).isEnabled(name)) {
return new ChatContext(false);
}
// Check range
if (npc.getTriggerTrait().getRadius(name) < npc.getLocation().distance(player.getLocation())) {
if (HyperDebug) {
dB.log("Not in range");
}
return new ChatContext(false);
}
if (Settings.chatMustSeeNPC()) {
if (!player.hasLineOfSight(npc.getEntity())) {
if (HyperDebug) {
dB.log("no LOS");
}
return new ChatContext(false);
}
}
if (Settings.chatMustLookAtNPC()) {
if (!NMSHandler.getInstance().getEntityHelper().isFacingEntity(player, npc.getEntity(), 45)) {
if (HyperDebug) {
dB.log("Not facing");
}
return new ChatContext(false);
}
}
Boolean ret = false;
// Denizen should be good to interact with. Let's get the script.
InteractScriptContainer script = npc.getInteractScript(denizenPlayer, ChatTrigger.class);
Map<String, dObject> context = new HashMap<String, dObject>();
context.put("message", new Element(message));
//
// Fire the Actions!
//
// If engaged or not cool, calls On Unavailable, if cool, calls On Chat
// If available (not engaged, and cool) sets cool down and returns true.
TriggerTrait.TriggerContext trigger = npc.getTriggerTrait().trigger(ChatTrigger.this, denizenPlayer, context);
// Return false if determine cancelled
if (trigger.hasDetermination()) {
if (trigger.getDetermination().equalsIgnoreCase("cancelled")) {
if (HyperDebug) {
dB.log("Cancelled");
}
// Mark as handled, the event will cancel.
return new ChatContext(true);
}
}
// Return false if trigger was unable to fire
if (!trigger.wasTriggered()) {
// through. Check the Settings if this is enabled.
if (Settings.chatGloballyIfUninteractable()) {
dB.echoDebug(script, ChatColor.YELLOW + "Resuming. " + ChatColor.WHITE + "The NPC is currently cooling down or engaged.");
return new ChatContext(false);
} else {
ret = true;
}
}
// Debugger
dB.report(script, name, aH.debugObj("Player", player.getName()) + aH.debugObj("NPC", npc.toString()) + aH.debugObj("Radius(Max)", npc.getLocation().distance(player.getLocation()) + "(" + npc.getTriggerTrait().getRadius(name) + ")") + aH.debugObj("Trigger text", message) + aH.debugObj("LOS", String.valueOf(player.hasLineOfSight(npc.getEntity()))) + aH.debugObj("Facing", String.valueOf(NMSHandler.getInstance().getEntityHelper().isFacingEntity(player, npc.getEntity(), 45))));
// Change the text if it's in the determination
if (trigger.hasDetermination()) {
message = trigger.getDetermination();
}
if (script == null) {
if (HyperDebug) {
dB.log("null script");
}
return new ChatContext(message, false);
}
// Check if the NPC has Chat Triggers for this step.
if (!script.containsTriggerInStep(InteractScriptHelper.getCurrentStep(denizenPlayer, script.getName()), ChatTrigger.class)) {
// it has no chat triggers for this step
if (npc.getCitizen().hasTrait(ChatbotTrait.class)) {
Utilities.talkToNPC(message, denizenPlayer, npc, Settings.chatToNpcOverhearingRange());
npc.getCitizen().getTrait(ChatbotTrait.class).chatTo(player, message);
if (HyperDebug) {
dB.log("chatbot");
}
return new ChatContext(false);
} else // No chat trigger for this step.. do we chat globally, or to the NPC?
if (!Settings.chatGloballyIfNoChatTriggers()) {
dB.echoDebug(script, player.getName() + " says to " + npc.getNicknameTrait().getNickname() + ", " + message);
return new ChatContext(false);
} else {
if (HyperDebug) {
dB.log("No trigger in step, chatting globally");
}
return new ChatContext(message, ret);
}
}
// Parse the script and match Triggers.. if found, cancel the text! The
// parser will take care of everything else.
String id = null;
boolean matched = false;
String replacementText = null;
String regexId = null;
String regexMessage = null;
// Use TreeMap to sort chat triggers alphabetically
TreeMap<String, String> idMap = new TreeMap<String, String>();
idMap.putAll(script.getIdMapFor(ChatTrigger.class, denizenPlayer));
if (!idMap.isEmpty()) {
// Iterate through the different id entries in the step's chat trigger
List<Map.Entry<String, String>> entries = new ArrayList<Map.Entry<String, String>>(idMap.entrySet());
Collections.sort(entries, new Comparator<Map.Entry<String, String>>() {
@Override
public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
if (o1 == null || o2 == null) {
return 0;
}
return o1.getKey().compareToIgnoreCase(o2.getKey());
}
});
for (Map.Entry<String, String> entry : entries) {
// Check if the chat trigger specified in the specified id's 'trigger:' key
// matches the text the player has said
// TODO: script arg?
String triggerText = TagManager.tag(entry.getValue(), new BukkitTagContext(denizenPlayer, npc, false, null, false, null));
Matcher matcher = triggerPattern.matcher(triggerText);
while (matcher.find()) {
if (!script.checkSpecificTriggerScriptRequirementsFor(ChatTrigger.class, denizenPlayer, npc, entry.getKey())) {
continue;
}
// TODO: script arg?
String keyword = TagManager.tag(matcher.group().replace("/", ""), new BukkitTagContext(denizenPlayer, npc, false, null, false, null));
String[] split = keyword.split("\\\\\\+REPLACE:", 2);
String replace = null;
if (split.length == 2) {
keyword = split[0];
replace = split[1];
}
// match already (thus using alphabetical priority for triggers)
if (regexId == null && isKeywordRegex(keyword)) {
Pattern pattern = Pattern.compile(keyword.substring(6));
Matcher m = pattern.matcher(message);
if (m.find()) {
// REGEX matches are left for last, so save it in case non-REGEX
// matches don't exist
regexId = entry.getKey();
regexMessage = triggerText.replace(matcher.group(), m.group());
dB.log("entry value: " + triggerText + " keyword: " + keyword + " m.group: " + m.group() + " matcher.group: " + matcher.group());
context.put("keyword", new Element(m.group()));
if (replace != null) {
regexMessage = replace;
}
}
} else if (isKeywordStrict(keyword)) {
if (message.toUpperCase().equalsIgnoreCase(keyword.toUpperCase())) {
// Trigger matches
id = entry.getKey();
replacementText = triggerText.replace("/", "");
matched = true;
if (replace != null) {
replacementText = replace;
}
}
} else if (message.toUpperCase().contains(keyword.toUpperCase())) {
// Trigger matches
id = entry.getKey();
replacementText = triggerText.replace("/", "");
matched = true;
if (replace != null) {
replacementText = replace;
}
}
}
if (matched) {
break;
}
}
}
if (!matched && regexId != null) {
id = regexId;
replacementText = regexMessage;
}
// If there was a match, the id of the match should have been returned.
if (id != null) {
Utilities.talkToNPC(replacementText, denizenPlayer, npc, Settings.chatToNpcOverhearingRange());
parse(npc, denizenPlayer, script, id, context);
if (HyperDebug) {
dB.log("chat to NPC");
}
return new ChatContext(true);
} else {
// none of its chat triggers worked
if (npc.getCitizen().hasTrait(ChatbotTrait.class)) {
Utilities.talkToNPC(message, denizenPlayer, npc, Settings.chatToNpcOverhearingRange());
npc.getCitizen().getTrait(ChatbotTrait.class).chatTo(player, message);
if (HyperDebug) {
dB.log("Chatbot");
}
return new ChatContext(true);
} else if (!Settings.chatGloballyIfFailedChatTriggers()) {
Utilities.talkToNPC(message, denizenPlayer, npc, Settings.chatToNpcOverhearingRange());
if (HyperDebug) {
dB.log("Chat globally");
}
return new ChatContext(true);
}
// No matching chat triggers, and the config.yml says we
// should just ignore the interaction...
}
if (HyperDebug) {
dB.log("Finished calculating");
}
return new ChatContext(message, ret);
}
use of net.aufdemrand.denizencore.objects.Element in project Denizen-For-Bukkit by DenizenScript.
the class dB method echoError.
public static void echoError(ScriptQueue source, Throwable ex) {
if (ThrowErrorEvent) {
ThrowErrorEvent = false;
Map<String, dObject> context = new HashMap<String, dObject>();
Throwable thrown = ex;
if (ex.getCause() != null) {
thrown = ex.getCause();
}
context.put("message", new Element(thrown.getMessage()));
context.put("type", new Element(thrown.getClass().getSimpleName()));
context.put("queue", source);
ScriptEntry entry = (source != null ? source.getLastEntryExecuted() : null);
List<String> Determinations = OldEventManager.doEvents(Arrays.asList("server generates exception"), entry == null ? new BukkitScriptEntryData(null, null) : entry.entryData, context);
ThrowErrorEvent = true;
for (String Determination : Determinations) {
if (Determination.equalsIgnoreCase("CANCELLED")) {
return;
}
}
}
if (!showDebug) {
return;
}
if (!showStackTraces) {
dB.echoError(source, "Exception! Enable '/denizen debug -s' for the nitty-gritty.");
} else {
dB.echoError(source, "Internal exception was thrown!");
ex.printStackTrace();
if (dB.record) {
String prefix = ConsoleSender.dateFormat.format(new Date()) + " [SEVERE] ";
boolean first = true;
while (ex != null) {
dB.Recording.append(URLEncoder.encode(prefix + (first ? "" : "Caused by: ") + ex.toString() + "\n"));
for (StackTraceElement ste : ex.getStackTrace()) {
dB.Recording.append(URLEncoder.encode(prefix + ste.toString() + "\n"));
}
if (ex.getCause() == ex) {
return;
}
ex = ex.getCause();
first = false;
}
}
}
}
use of net.aufdemrand.denizencore.objects.Element in project Denizen-For-Bukkit by DenizenScript.
the class BlockIgnitesScriptEvent method onBlockIgnites.
@EventHandler
public void onBlockIgnites(BlockIgniteEvent event) {
location = new dLocation(event.getBlock().getLocation());
material = dMaterial.getMaterialFrom(event.getBlock().getType(), event.getBlock().getData());
entity = null;
if (event.getIgnitingEntity() != null) {
entity = new dEntity(event.getIgnitingEntity());
}
origin_location = null;
if (event.getIgnitingBlock() != null) {
// TODO: Why would this be null?
origin_location = new dLocation(event.getIgnitingBlock().getLocation());
}
cause = new Element(event.getCause().toString());
cancelled = event.isCancelled();
this.event = event;
fire();
event.setCancelled(cancelled);
}
use of net.aufdemrand.denizencore.objects.Element in project Denizen-For-Bukkit by DenizenScript.
the class CommandSmartEvent method playerCommandPreprocess.
// <--[event]
// @Events
// command
// <command_name>|... command (in <area>)
//
// @Regex ^on( [^\s]+)? command( in ((notable (cuboid|ellipsoid))|([^\s]+)))?$
//
// @Triggers when a player or console runs a Bukkit command. This happens before
// any code of established commands allowing scripters to 'override' existing commands.
// @Context
// <context.command> returns the command name as an Element.
// <context.raw_args> returns any args used as an Element.
// <context.args> returns a dList of the arguments.
// <context.server> returns true if the command was run from the console.
// <context.cuboids> DEPRECATED.
//
// @Determine
// "FULFILLED" to tell Bukkit the command was handled.
//
// -->
@EventHandler
public void playerCommandPreprocess(PlayerCommandPreprocessEvent event) {
Map<String, dObject> context = new HashMap<String, dObject>();
String message = event.getMessage();
String command = message.split(" ")[0].replace("/", "").toUpperCase();
List<String> events = new ArrayList<String>();
events.add("command");
events.add(command + " command");
events.addAll(getAll(command));
// Look for cuboids that contain the block's location
List<dCuboid> cuboids = dCuboid.getNotableCuboidsContaining(event.getPlayer().getLocation());
dList cuboid_context = new dList();
List<String> cuboidEvents = new ArrayList<String>();
for (dCuboid cuboid : cuboids) {
for (String str : events) {
cuboidEvents.add(str + " in " + cuboid.identifySimple());
}
cuboid_context.add(cuboid.identifySimple());
}
events.addAll(cuboidEvents);
// Add in cuboids context, with either the cuboids or an empty list
context.put("cuboids", cuboid_context);
List<String> args = Arrays.asList(aH.buildArgs(message.split(" ").length > 1 ? message.split(" ", 2)[1] : ""));
// Fill context
context.put("args", new dList(args));
context.put("parsed_args", new dList(args));
context.put("command", new Element(command));
context.put("raw_args", new Element((message.split(" ").length > 1 ? message.split(" ", 2)[1] : "")));
context.put("server", Element.FALSE);
String determination;
// Run any event scripts and get the determination.
determination = BukkitWorldScriptHelper.doEvents(events, null, dEntity.getPlayerFrom(event.getPlayer()), context).toUpperCase();
// receive the default 'Invalid command' gibberish from bukkit.
if (determination.equals("FULFILLED") || determination.equals("CANCELLED")) {
event.setCancelled(true);
}
}
use of net.aufdemrand.denizencore.objects.Element in project Denizen-For-Bukkit by DenizenScript.
the class CreeperPoweredScriptEvent method onCreeperPowered.
@EventHandler
public void onCreeperPowered(CreeperPowerEvent event) {
lightning = new dEntity(event.getLightning());
entity = new dEntity(event.getEntity());
cause = new Element(event.getCause().name());
cancelled = event.isCancelled();
this.event = event;
fire();
event.setCancelled(cancelled);
}
Aggregations