-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
BungeeCommand.java
146 lines (137 loc) · 6 KB
/
BungeeCommand.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
package com.denizenscript.depenizen.bukkit.commands.bungee;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.depenizen.bukkit.bungee.BungeeBridge;
import com.denizenscript.depenizen.bukkit.bungee.packets.out.KeepAlivePacketOut;
import com.denizenscript.depenizen.bukkit.bungee.packets.out.RedirectPacketOut;
import com.denizenscript.depenizen.bukkit.bungee.packets.out.redirectable.RunCommandsPacketOut;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.dB;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.dList;
import com.denizenscript.denizencore.objects.dObject;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.BracedCommand;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public class BungeeCommand extends BracedCommand {
// <--[command]
// @Name Bungee
// @Syntax bungee [<server>|...] [<commands>]
// @Group Depenizen
// @Plugin Depenizen, BungeeCord
// @Required 1
// @Short Runs a set of commands on another server.
//
// @Description
// This command runs a set of commands on another server on the Bungee network.
// Tags will be parsed on the remote server, but definitions from the originating queue will be used.
// The linked player will be available on the remote server if that server has ever seen the player.
// Generally, prefer <@link command BungeeRun>.
//
// @Tags
// None
//
// @Usage
// Use to run a simple announce command on another server;
// - bungee lobby:
// - announce "Wow! Stuff is happening!"
//
// -->
@Override
public void onEnable() {
setBraced();
}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : ArgumentHelper.interpretArguments(scriptEntry.aHArgs)) {
if (!scriptEntry.hasObject("servers")) {
scriptEntry.addObject("servers", arg.asType(dList.class));
}
else if (arg.matchesOne("{")) {
break;
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("servers")) {
throw new InvalidArgumentsException("Must define servers to run the script on.");
}
scriptEntry.addObject("braces", getBracedCommands(scriptEntry));
}
@Override
public void execute(ScriptEntry scriptEntry) {
dList servers = scriptEntry.getdObject("servers");
if (scriptEntry.dbCallShouldDebug()) {
dB.report(scriptEntry, getName(), servers.debug());
}
if (!BungeeBridge.instance.connected) {
dB.echoError("Cannot Bungee command: bungee is not connected!");
return;
}
List<BracedCommand.BracedData> bdlist = (List<BracedData>) scriptEntry.getObject("braces");
if (bdlist == null || bdlist.isEmpty()) {
Debug.echoError(scriptEntry.getResidingQueue(), "Empty braces (internal)!");
return;
}
List<ScriptEntry> bracedCommandsList = bdlist.get(0).value;
if (bracedCommandsList == null || bracedCommandsList.isEmpty()) {
Debug.echoError(scriptEntry.getResidingQueue(), "Empty braces!");
return;
}
StringBuilder toSend = new StringBuilder();
for (ScriptEntry entry : bracedCommandsList) {
toSend.append(stringify(entry)).append("\n");
}
StringBuilder defNames = new StringBuilder();
StringBuilder defValues = new StringBuilder();
for (Map.Entry<String, dObject> def : scriptEntry.getResidingQueue().getAllDefinitions().entrySet()) {
defNames.append(escape(def.getKey())).append("\n");
defValues.append(escape(def.getValue().toString())).append("\n");
}
RunCommandsPacketOut packetScript = new RunCommandsPacketOut(toSend.toString(),
defNames.toString() + "\r" + defValues.toString(), scriptEntry.shouldDebug(),
Utilities.entryHasPlayer(scriptEntry) ?
Utilities.getEntryPlayer(scriptEntry).getOfflinePlayer().getUniqueId()
: new UUID(0, 0));
for (String server : servers) {
RedirectPacketOut packet = new RedirectPacketOut(server, packetScript);
BungeeBridge.instance.sendPacket(packet);
BungeeBridge.instance.sendPacket(new KeepAlivePacketOut());
}
}
public static String escape(String text) {
return text.replace("\\", "\\\\").replace("\n", "\\n").replace("\r", "\\r");
}
public static String stringify(Object obj) {
if (obj instanceof ScriptEntry) {
ScriptEntry entry = (ScriptEntry) obj;
if (entry.internal.insideList == null) {
return escape(entry.internal.originalLine);
}
StringBuilder result = new StringBuilder();
result.append(escape(entry.internal.originalLine)).append("\r");
for (Object subCommand : entry.internal.insideList) {
result.append(stringify(subCommand)).append("\r");
}
return result.toString();
}
else if (obj instanceof Map) {
Map<Object, Object> valueMap = (Map<Object, Object>) obj;
StringBuilder result = new StringBuilder();
Object cmdLine = valueMap.keySet().toArray()[0];
result.append(escape(cmdLine.toString())).append("\r");
List<Object> inside = (List<Object>) valueMap.get(cmdLine);
for (Object subCommand : inside) {
result.append(stringify(subCommand)).append("\r");
}
return result.toString();
}
else {
return escape(obj.toString());
}
}
}