/
ListPingScriptEvent.java
166 lines (151 loc) · 6.04 KB
/
ListPingScriptEvent.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package com.denizenscript.denizen.events.server;
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.server.ServerListPingEvent;
import org.bukkit.util.CachedServerIcon;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
public class ListPingScriptEvent extends BukkitScriptEvent implements Listener {
// <--[event]
// @Events
// server list ping
//
// @Regex ^on server list ping$
//
// @Group Server
//
// @Triggers when the server is pinged for a client's server list.
// @Context
// <context.motd> returns the MOTD that will show.
// <context.max_players> returns the number of max players that will show.
// <context.num_players> returns the number of online players that will show.
// <context.address> returns the IP address requesting the list.
// <context.protocol_version> returns the protocol ID of the server's version (only on Paper).
// <context.version_name> returns the name of the server's version (only on Paper).
// <context.client_protocol_version> returns the client's protocol version ID (only on Paper).
//
// @Determine
// ElementTag(Number) to change the max player amount that will show.
// "ICON:" + ElementTag of a file path to an icon image, to change the icon that will display.
// "PROTOCOL_VERSION:" + ElementTag(Number) to change the protocol ID number of the server's version (only on Paper).
// "VERSION_NAME:" + ElementTag to change the server's version name (only on Paper).
// "EXCLUDE_PLAYERS:" + ListTag(PlayerTag) to exclude a set of players from showing in the player count or preview of online players (only on Paper).
// ElementTag to change the MOTD that will show.
//
// -->
public ListPingScriptEvent() {
instance = this;
}
public static ListPingScriptEvent instance;
public ServerListPingEvent event;
@Override
public boolean couldMatch(ScriptPath path) {
if (!path.eventLower.startsWith("server list ping")) {
return false;
}
return true;
}
@Override
public String getName() {
return "ServerListPing";
}
// Despite the 'cached' class name, there's no actual internal cache.
public static HashMap<String, CachedServerIcon> iconCache = new HashMap<>();
@Override
public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
String determination = determinationObj.toString();
String determineLow = CoreUtilities.toLowerCase(determination);
if (determineLow.startsWith("icon:")) {
String iconFile = determination.substring("icon:".length());
CachedServerIcon icon = iconCache.get(iconFile);
if (icon != null) {
event.setServerIcon(icon);
return true;
}
File file = new File(iconFile);
if (!Utilities.canReadFile(file)) {
Debug.echoError("Cannot read icon file '" + iconFile + "'");
return false;
}
try {
icon = Bukkit.loadServerIcon(file);
}
catch (Exception ex) {
Debug.echoError(ex);
}
if (icon != null) {
iconCache.put(iconFile, icon);
event.setServerIcon(icon);
}
return true;
}
if (determination.length() > 0 && !determineLow.equalsIgnoreCase("none")) {
List<String> values = CoreUtilities.split(determination, '|', 2);
if (ArgumentHelper.matchesInteger(values.get(0))) {
event.setMaxPlayers(Integer.parseInt(values.get(0)));
if (values.size() == 2) {
event.setMotd(values.get(1));
}
}
else {
event.setMotd(determination);
}
return true;
}
else {
return super.applyDetermination(path, determinationObj);
}
}
@Override
public ObjectTag getContext(String name) {
if (name.equals("motd")) {
return new ElementTag(event.getMotd());
}
else if (name.equals("max_players")) {
return new ElementTag(event.getMaxPlayers());
}
else if (name.equals("num_players")) {
return new ElementTag(event.getNumPlayers());
}
else if (name.equals("address")) {
return new ElementTag(event.getAddress().toString());
}
return super.getContext(name);
}
public void syncFire(ServerListPingEvent event) {
this.event = event;
if (!Bukkit.isPrimaryThread()) {
BukkitScriptEvent altEvent = (BukkitScriptEvent) clone();
Future future = Bukkit.getScheduler().callSyncMethod(Denizen.getInstance(), () -> {
altEvent.fire();
return null;
});
try {
future.get(5, TimeUnit.SECONDS);
}
catch (Throwable ex) {
Debug.echoError(ex);
}
return;
}
fire(event);
}
public static class ListPingScriptEventSpigotImpl extends ListPingScriptEvent {
@EventHandler
public void onListPing(ServerListPingEvent event) {
syncFire(event);
}
}
}