/
SyncChatSmartEvent.java
116 lines (93 loc) · 3.82 KB
/
SyncChatSmartEvent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package net.aufdemrand.denizen.events.core;
import net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizen.scripts.containers.core.BukkitWorldScriptHelper;
import net.aufdemrand.denizencore.events.OldSmartEvent;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizencore.scripts.ScriptRegistry;
import net.aufdemrand.denizen.scripts.containers.core.FormatScriptContainer;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerChatEvent;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SyncChatSmartEvent implements OldSmartEvent, Listener {
///////////////////
// SMARTEVENT METHODS
///////////////
@Override
public boolean shouldInitialize(Set<String> events) {
// Fail if "Use asynchronous event" is true in config file
if (Settings.worldScriptChatEventAsynchronous()) return false;
// Loop through event names from loaded world script events
for (String event : events) {
// Use a regex pattern to narrow down matches
Matcher m = Pattern.compile("on player chats", Pattern.CASE_INSENSITIVE)
.matcher(event);
if (m.matches()) {
return true;
}
}
// No matches at all, so return false.
return false;
}
@Override
public void _initialize() {
// Yay! Your event is in use! Register it here.
DenizenAPI.getCurrentInstance().getServer().getPluginManager()
.registerEvents(this, DenizenAPI.getCurrentInstance());
// Record that you loaded in the debug.
dB.log("Loaded Sync Chat SmartEvent.");
}
@Override
public void breakDown() {
// Unregister events or any other temporary links your event created in _intialize()
PlayerChatEvent.getHandlerList().unregister(this);
}
//////////////
// MECHANICS
///////////
// <--[event]
// @Events
// player chats
//
// @Warning Using this will forcibly sync the chat thread.
//
// @Triggers when a player chats.
// @Context
// <context.message> returns the player's message as an Element.
// <context.format> returns the chat message's format.
//
// @Determine
// "CANCELLED" to stop the player from chatting.
// Element(String) to change the message.
// "FORMAT:" to set the format script the message should use.
//
// -->
@EventHandler
public void playerChat(final PlayerChatEvent event) {
final Map<String, dObject> context = new HashMap<String, dObject>();
context.put("message", new Element(event.getMessage()));
context.put("format", new Element(event.getMessage()));
dPlayer player = new dPlayer(event.getPlayer());
String determination = BukkitWorldScriptHelper.doEvents(Arrays.asList("player chats"), null, player, context);
if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);
else if (determination.toUpperCase().startsWith("FORMAT:")) {
String name = determination.substring(7);
FormatScriptContainer format = ScriptRegistry.getScriptContainer(name);
if (format == null) dB.echoError("Could not find format script matching '" + name + '\'');
else event.setFormat(format.getFormattedText(event.getMessage(), null, player));
}
else if (!determination.equals("none")) {
event.setMessage(determination);
}
}
}