/
PlayerReceivesMessageScriptEvent.java
115 lines (100 loc) · 3.51 KB
/
PlayerReceivesMessageScriptEvent.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
package com.denizenscript.denizen.events.player;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import com.denizenscript.denizencore.utilities.CoreUtilities;
public class PlayerReceivesMessageScriptEvent extends BukkitScriptEvent {
// <--[event]
// @Events
// player receives message
//
// @Regex ^on player receives message$
//
// @Cancellable true
//
// @Warning Using this will forcibly sync the chat thread.
//
// @Triggers when a player receives any chat message from the server.
//
// @Context
// <context.message> returns an ElementTag of the message.
// <context.raw_json> returns an ElementTag of the raw JSON used for the message.
// <context.system_message> returns true if the message is a system message (not player chat).
//
// @Determine
// "MESSAGE:" + ElementTag to change the message.
// "RAW_JSON:" + ElementTag to change the JSON used for the message.
//
// @Player Always.
//
// -->
public PlayerReceivesMessageScriptEvent() {
instance = this;
}
public static PlayerReceivesMessageScriptEvent instance;
public ElementTag message;
public ElementTag rawJson;
public ElementTag system;
public PlayerTag player;
public boolean messageModified;
public boolean rawJsonModified;
public boolean loaded;
@Override
public boolean couldMatch(ScriptPath path) {
return path.eventLower.startsWith("player receives message");
}
@Override
public boolean matches(ScriptPath path) {
return true;
}
@Override
public String getName() {
return "PlayerReceivesMessage";
}
@Override
public void init() {
loaded = true;
}
@Override
public void destroy() {
loaded = false;
}
@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
if (determinationObj instanceof ElementTag && !isDefaultDetermination(determinationObj)) {
String determination = determinationObj.toString();
String lower = CoreUtilities.toLowerCase(determination);
if (lower.startsWith("message:")) {
message = new ElementTag(determination.substring("message:".length()));
messageModified = true;
return true;
}
if (lower.startsWith("raw_json:")) {
rawJson = new ElementTag(determination.substring("raw_json:".length()));
rawJsonModified = true;
return true;
}
}
return super.applyDetermination(path, determinationObj);
}
@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(player, null);
}
@Override
public ObjectTag getContext(String name) {
if (name.equals("message")) {
return message;
}
if (name.equals("raw_json")) {
return rawJson;
}
if (name.equals("system_message")) {
return system;
}
return super.getContext(name);
}
}