-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
RunCommandsPacketIn.java
116 lines (109 loc) · 4.93 KB
/
RunCommandsPacketIn.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 com.denizenscript.depenizen.bukkit.bungee.packets.in;
import com.denizenscript.depenizen.bukkit.Depenizen;
import com.denizenscript.depenizen.bukkit.bungee.BungeeBridge;
import com.denizenscript.depenizen.bukkit.bungee.PacketIn;
import io.netty.buffer.ByteBuf;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizencore.scripts.ScriptBuilder;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.queues.ScriptQueue;
import com.denizenscript.denizencore.scripts.queues.core.InstantQueue;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import java.util.*;
public class RunCommandsPacketIn extends PacketIn {
@Override
public String getName() {
return "RunCommands";
}
@Override
public void process(ByteBuf data) {
if (data.readableBytes() < 12) {
BungeeBridge.instance.handler.fail("Invalid RunCommandsPacket (bytes available: " + data.readableBytes() + ")");
return;
}
int commamndsLength = data.readInt();
if (data.readableBytes() < commamndsLength || commamndsLength < 0) {
BungeeBridge.instance.handler.fail("Invalid RunCommandsPacket (command bytes requested: " + commamndsLength + ")");
return;
}
String commands = readString(data, commamndsLength);
int defsLength = data.readInt();
if (data.readableBytes() < defsLength || defsLength < 0) {
BungeeBridge.instance.handler.fail("Invalid RunCommandsPacket (def bytes requested: " + defsLength + ")");
return;
}
String defs = readString(data, defsLength);
boolean shouldDebug = data.readByte() != 0;
long uuidMost = data.readLong();
long uuidLeast = data.readLong();
Bukkit.getScheduler().scheduleSyncDelayedTask(Depenizen.instance, new Runnable() {
@Override
public void run() {
PlayerTag linkedPlayer = null;
if (uuidMost != 0 || uuidLeast != 0) {
UUID uuid = new UUID(uuidMost, uuidLeast);
try {
OfflinePlayer player = Bukkit.getOfflinePlayer(uuid);
if (player != null) {
linkedPlayer = new PlayerTag(player);
}
}
catch (Exception ex) {
// Ignore
}
}
List<String> commandsSeparated = CoreUtilities.split(commands, '\n');
List<Object> rawEntries = new ArrayList<>();
for (String cmd : commandsSeparated) {
if (cmd.length() > 0) {
rawEntries.add(parseCommandsBack(unescape(cmd)));
}
}
List<ScriptEntry> entries = ScriptBuilder.buildScriptEntries(rawEntries, null, new BukkitScriptEntryData(linkedPlayer, null));
if (entries.isEmpty()) {
return;
}
if (!shouldDebug) {
for (ScriptEntry entry : entries) {
entry.shouldDebugBool = false;
}
}
ScriptQueue queue = new InstantQueue("BUNGEE_").addEntries(entries);
List<String> defSets = CoreUtilities.split(defs, '\r');
List<String> defNames = CoreUtilities.split(defSets.get(0), '\n');
List<String> defValues = CoreUtilities.split(defSets.get(1), '\n');
for (int i = 0; i < defNames.size(); i++) {
String name = unescape(defNames.get(i));
if (name.length() > 0) {
String value = unescape(defValues.get(i));
queue.addDefinition(name, value);
Debug.echoDebug(entries.get(0), "Adding definition '" + name + "' as " + value);
}
}
queue.start();
}
});
}
public static String unescape(String text) {
return text.replace("\\n", "\n").replace("\\r", "\r").replace("\\\\", "\\");
}
public static Object parseCommandsBack(String value) {
List<String> cmds = CoreUtilities.split(value, '\r');
if (cmds.size() == 1) {
return cmds.get(0);
}
List<Object> resultSubList = new ArrayList<>();
for (int i = 1; i < cmds.size(); i++) {
if (cmds.get(i).length() > 0) {
resultSubList.add(parseCommandsBack(unescape(cmds.get(i))));
}
}
Map<String, Object> resultMap = new HashMap<>();
resultMap.put(cmds.get(0), resultSubList);
return resultMap;
}
}