/
PlayerReceivesMessageScriptEvent.java
134 lines (121 loc) · 4.76 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package com.denizenscript.denizen.events.player;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.utilities.FormattedTextHelper;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.utilities.packets.NetworkInterceptHelper;
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;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.chat.ComponentSerializer;
public class PlayerReceivesMessageScriptEvent extends BukkitScriptEvent {
// <--[event]
// @Events
// player receives message
//
// @Regex ^on player receives message$
//
// @Group Player
//
// @Cancellable true
//
// @Warning Using this will forcibly sync the chat thread.
//
// @Triggers when a player receives any chat message from the server. This does not normally include *player* chat, instead prefer <@link event player chats> for that.
//
// @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 boolean didModify;
public BaseComponent[] altMessageDetermination;
public ElementTag system;
public boolean modified;
public PlayerTag player;
public boolean loaded;
public void reset() {
player = null;
message = null;
rawJson = null;
system = null;
cancelled = false;
modified = false;
altMessageDetermination = null;
didModify = false;
}
@Override
public boolean couldMatch(ScriptPath path) {
return path.eventLower.startsWith("player receives message");
}
@Override
public void init() {
NetworkInterceptHelper.enable();
loaded = true;
}
@Override
public void destroy() {
loaded = false;
}
@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
if (determinationObj instanceof ElementTag) {
String determination = determinationObj.toString();
String lower = CoreUtilities.toLowerCase(determination);
if (lower.startsWith("message:")) {
message = new ElementTag(determination.substring("message:".length()), true);
altMessageDetermination = FormattedTextHelper.parse(message.asString(), ChatColor.WHITE);
modified = true;
return true;
}
if (lower.startsWith("raw_json:")) {
rawJson = new ElementTag(determination.substring("raw_json:".length()));
altMessageDetermination = null;
message = new ElementTag(FormattedTextHelper.stringify(ComponentSerializer.parse(rawJson.asString())), true);
modified = true;
return true;
}
}
return super.applyDetermination(path, determinationObj);
}
@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(player, null);
}
@Override
public ObjectTag getContext(String name) {
switch (name) {
case "message": return message;
case "system_message": return system;
case "raw_json":
if (altMessageDetermination != null) {
return new ElementTag(ComponentSerializer.toString(altMessageDetermination), true);
}
return rawJson;
}
return super.getContext(name);
}
public PlayerReceivesMessageScriptEvent triggerNow() {
PlayerReceivesMessageScriptEvent event = (PlayerReceivesMessageScriptEvent) fire();
if (event.modified && event.altMessageDetermination == null) {
event.altMessageDetermination = ComponentSerializer.parse(event.rawJson.asString());
}
return event;
}
}