/
BungeeBridge.java
263 lines (227 loc) · 9.23 KB
/
BungeeBridge.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package com.denizenscript.depenizen.bukkit.bungee;
import com.denizenscript.denizencore.DenizenCore;
import com.denizenscript.depenizen.bukkit.Depenizen;
import com.denizenscript.depenizen.bukkit.bungee.packets.in.*;
import com.denizenscript.depenizen.bukkit.bungee.packets.out.ControlsProxyCommandPacketOut;
import com.denizenscript.depenizen.bukkit.bungee.packets.out.ControlsProxyPingPacketOut;
import com.denizenscript.depenizen.bukkit.bungee.packets.out.KeepAlivePacketOut;
import com.denizenscript.depenizen.bukkit.commands.bungee.BungeeCommand;
import com.denizenscript.depenizen.bukkit.commands.bungee.BungeeExecuteCommand;
import com.denizenscript.depenizen.bukkit.commands.bungee.BungeeRunCommand;
import com.denizenscript.depenizen.bukkit.commands.bungee.BungeeTagCommand;
import com.denizenscript.depenizen.bukkit.events.bungee.*;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.tags.TagRunnable;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.ReplaceableTagEvent;
import com.denizenscript.denizencore.tags.TagManager;
import org.bukkit.Bukkit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import static com.denizenscript.denizencore.events.ScriptEvent.*;
public class BungeeBridge {
public static BungeeBridge instance;
public Channel channel;
public NioEventLoopGroup workerGroup;
public BungeeClientHandler handler;
public String address;
public int port;
public String serverName = "<Unknown>";
public List<String> knownServers = new ArrayList<>();
public HashMap<Integer, PacketIn> packets = new HashMap<>();
public boolean connected = false;
public boolean controlsProxyPing = false;
public boolean controlsProxyCommand = false;
public int keepAliveTickRate = 10;
public int ticksTilKeepalive = 10;
public long lastPacketReceived = 0;
public void checkBroadcastProxyPing() {
if (connected) {
sendPacket(new ControlsProxyPingPacketOut(controlsProxyPing));
}
}
public void checkBroadcastProxyCommand() {
if (connected) {
sendPacket(new ControlsProxyCommandPacketOut(controlsProxyCommand));
}
}
public void registerPackets() {
packets.put(1, new KeepAlivePacketIn());
packets.put(50, new YourInfoPacketIn());
packets.put(51, new AddServerPacketIn());
packets.put(52, new RemoveServerPacketIn());
packets.put(53, new PlayerJoinPacketIn());
packets.put(54, new PlayerQuitPacketIn());
packets.put(55, new PlayerSwitchServerPacketIn());
packets.put(56, new ProxyPingPacketIn());
packets.put(57, new RunScriptPacketIn());
packets.put(58, new RunCommandsPacketIn());
packets.put(59, new ReadTagPacketIn());
packets.put(60, new TagResponsePacketIn());
packets.put(61, new ProxyCommandPacketIn());
}
public void sendPacket(PacketOut packet) {
if (!connected && !packet.canBeFirstPacket) {
Debug.echoError("BungeeBridge tried to send packet while not connected.");
return;
}
ByteBuf buf = channel.alloc().buffer();
packet.writeTo(buf);
ByteBuf header = channel.alloc().buffer();
header.writeInt(buf.writerIndex());
header.writeInt(packet.getPacketId());
channel.writeAndFlush(header);
channel.writeAndFlush(buf);
if (connected) {
ticksTilKeepalive = 0;
}
}
public void init(String address, int port) {
this.address = address;
this.port = port;
workerGroup = new NioEventLoopGroup();
registerPackets();
connect();
successInit();
}
public boolean reconnectPending = false;
public void reconnect() {
if (reconnectPending || shuttingDown) {
return;
}
connected = false;
Bukkit.getScheduler().scheduleSyncDelayedTask(Depenizen.instance, () -> {
reconnectPending = false;
connect();
}, 20 * 5);
}
public boolean shuttingDown = false;
public void onShutdown() {
shuttingDown = true;
if (connected) {
try {
handler.channel.close().await();
connected = false;
}
catch (Throwable ex) {
Debug.echoError(ex);
}
}
}
private boolean showedLastError = false;
private boolean hasConnectionLoading = false;
public void connect() {
try {
ticksTilKeepalive = 100;
hasConnectionLoading = false;
knownServers.clear();
Bootstrap b = new Bootstrap();
b.group(workerGroup);
b.channel(NioSocketChannel.class);
b.option(ChannelOption.SO_KEEPALIVE, true);
b.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) {
hasConnectionLoading = true;
handler = new BungeeClientHandler();
handler.channel = ch;
ch.pipeline().addLast(handler).addLast(new NettyExceptionHandler());
channel = ch;
}
});
b.connect(address, port).addListener(future -> Bukkit.getScheduler().scheduleSyncDelayedTask(Depenizen.instance, () -> {
if (!connected && !hasConnectionLoading) {
reconnect();
}
}, 10));
showedLastError = false;
}
catch (Throwable ex) {
if (!showedLastError) {
showedLastError = true;
Debug.echoError(ex);
}
reconnect();
}
}
public void successInit() {
instance = this;
registerScriptEvent(new BungeePlayerJoinsScriptEvent());
registerScriptEvent(new BungeePlayerQuitsScriptEvent());
registerScriptEvent(new BungeePlayerServerSwitchScriptEvent());
registerScriptEvent(new BungeeProxyServerCommandScriptEvent());
registerScriptEvent(new BungeeProxyServerListPingScriptEvent());
registerScriptEvent(new BungeeServerConnectScriptEvent());
registerScriptEvent(new BungeeServerDisconnectScriptEvent());
DenizenCore.commandRegistry.registerCommand(BungeeRunCommand.class);
DenizenCore.commandRegistry.registerCommand(BungeeExecuteCommand.class);
DenizenCore.commandRegistry.registerCommand(BungeeCommand.class);
DenizenCore.commandRegistry.registerCommand(BungeeTagCommand.class);
TagManager.registerTagHandler(new TagRunnable.RootForm() {
@Override
public void run(ReplaceableTagEvent event) {
tagEvent(event);
}
}, "bungee");
Bukkit.getScheduler().scheduleSyncRepeatingTask(Depenizen.instance, () -> {
if (!connected) {
return;
}
if (System.currentTimeMillis() > lastPacketReceived + 20 * 1000) {
// 20 seconds without a packet = connection lost!
handler.fail("Connection time out.");
return;
}
ticksTilKeepalive--;
if (ticksTilKeepalive <= 0) {
sendPacket(new KeepAlivePacketOut());
ticksTilKeepalive = keepAliveTickRate;
}
}, 1, 1);
}
public void tagEvent(ReplaceableTagEvent event) {
Attribute attribute = event.getAttributes().fulfill(1);
// <--[tag]
// @attribute <bungee.server>
// @returns ElementTag
// @plugin Depenizen, DepenizenBungee, BungeeCord
// @description
// Returns the name of the current server (according to the Bungee proxy config).
// -->
if (attribute.startsWith("server")) {
event.setReplacedObject(new ElementTag(serverName)
.getObjectAttribute(attribute.fulfill(1)));
}
// <--[tag]
// @attribute <bungee.list_servers>
// @returns ListTag
// @plugin Depenizen, DepenizenBungee, BungeeCord
// @description
// Returns a list of known bungee server names.
// -->
if (attribute.startsWith("list_servers")) {
event.setReplacedObject(new ListTag(knownServers)
.getObjectAttribute(attribute.fulfill(1)));
}
// <--[tag]
// @attribute <bungee.connected>
// @returns ElementTag(Boolean)
// @plugin Depenizen, DepenizenBungee, BungeeCord
// @description
// Returns whether this server is currently connected to the BungeeCord proxy server.
// -->
if (attribute.startsWith("connected")) {
event.setReplacedObject(new ElementTag(connected)
.getObjectAttribute(attribute.fulfill(1)));
}
}
}