-
Notifications
You must be signed in to change notification settings - Fork 23
/
RemotePlayer.java
215 lines (191 loc) · 7.08 KB
/
RemotePlayer.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
package net.ME1312.SubServers.Bungee.Host;
import net.ME1312.Galaxi.Library.Callback.Callback;
import net.ME1312.Galaxi.Library.Container.ContainedPair;
import net.ME1312.Galaxi.Library.Map.ObjectMap;
import net.ME1312.Galaxi.Library.Util;
import net.ME1312.SubData.Server.SubDataClient;
import net.ME1312.SubData.Server.SubDataSerializable;
import net.ME1312.SubServers.Bungee.Library.Compatibility.RPSI;
import net.ME1312.SubServers.Bungee.Network.Packet.*;
import net.ME1312.SubServers.Bungee.SubAPI;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import java.lang.reflect.InvocationTargetException;
import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.UUID;
/**
* Remote Player Class
*/
public class RemotePlayer implements net.ME1312.SubServers.Bungee.Library.Compatibility.RemotePlayer, SubDataSerializable {
private ProxiedPlayer local;
private UUID id;
private String name;
private InetSocketAddress ip;
private Proxy proxy;
private Server server;
/**
* Translate a Local Player to a Remote Player
*
* @param player Local Player
*/
public RemotePlayer(ProxiedPlayer player) {
this(player, player.getServer().getInfo());
}
/**
* Translate a Local Player to a Remote Player
*
* @param player Local Player
* @param server Server the player is on
*/
public RemotePlayer(ProxiedPlayer player, ServerInfo server) {
if (Util.isNull(player)) throw new NullPointerException();
this.local = player;
this.id = player.getUniqueId();
this.server = (server instanceof Server)? (Server) server : null;
}
/**
* Search for a Remote Player using their ID
*
* @param name Player Name
* @param id Player UUID
* @param proxy Proxy the player is on
* @param server Server the player is on
* @param ip Player IP Address
*/
public RemotePlayer(String name, UUID id, Proxy proxy, ServerInfo server, InetSocketAddress ip) {
if (Util.isNull(name, id, proxy, ip)) throw new NullPointerException();
this.id = id;
this.name = name;
this.ip = ip;
this.proxy = proxy;
this.server = (server instanceof Server)? (Server) server : null;
}
@Override
public ProxiedPlayer get() {
return local;
}
private static ProxiedPlayer get(UUID player) {
return ProxyServer.getInstance().getPlayer(player);
}
@Override
public UUID getUniqueId() {
if (local != null) {
return local.getUniqueId();
} else return id;
}
@Override
public String getName() {
if (local != null) {
return local.getName();
} else return name;
}
@SuppressWarnings("deprecation")
@Override
public InetSocketAddress getAddress() {
if (local != null) {
return local.getAddress();
} else return ip;
}
/**
* Gets the proxy this player is connected to.
*
* @return the proxy this player is connected to
*/
public Proxy getProxy() {
if (local != null) {
return SubAPI.getInstance().getMasterProxy();
} else return proxy;
}
@Override
public String getProxyName() {
Proxy proxy = getProxy();
return (proxy == null)? null : proxy.getName();
}
private SubDataClient getProxyConnection() {
Proxy proxy = getProxy();
return (proxy == null)? null : (SubDataClient) proxy.getSubData()[0];
}
@Override
public Server getServer() {
return server;
}
@Override
public String getServerName() {
Server server = getServer();
return (server == null)? null : server.getName();
}
static {
// These overrides provide for the static methods in BungeeCommon
new RPSI() {
@Override
protected void sendMessage(UUID[] players, String[] messages, Callback<Integer> response) {
StackTraceElement[] origin = new Exception().getStackTrace();
PacketMessagePlayer.run(Arrays.asList(players), new ContainedPair<>(messages, null), null, i -> {
try {
response.run(i);
} catch (Throwable e) {
Throwable ew = new InvocationTargetException(e);
ew.setStackTrace(origin);
ew.printStackTrace();
}
});
}
@Override
protected void sendMessage(UUID[] players, BaseComponent[][] messages, Callback<Integer> response) {
StackTraceElement[] origin = new Exception().getStackTrace();
PacketMessagePlayer.run(Arrays.asList(players), new ContainedPair<>(null, messages), null, i -> {
try {
response.run(i);
} catch (Throwable e) {
Throwable ew = new InvocationTargetException(e);
ew.setStackTrace(origin);
ew.printStackTrace();
}
});
}
@Override
protected void transfer(UUID[] players, String server, Callback<Integer> response) {
StackTraceElement[] origin = new Exception().getStackTrace();
PacketTransferPlayer.run(Arrays.asList(players), server, i -> {
try {
response.run(i);
} catch (Throwable e) {
Throwable ew = new InvocationTargetException(e);
ew.setStackTrace(origin);
ew.printStackTrace();
}
});
}
@Override
protected void disconnect(UUID[] players, String reason, Callback<Integer> response) {
StackTraceElement[] origin = new Exception().getStackTrace();
PacketDisconnectPlayer.run(Arrays.asList(players), reason, i -> {
try {
response.run(i);
} catch (Throwable e) {
Throwable ew = new InvocationTargetException(e);
ew.setStackTrace(origin);
ew.printStackTrace();
}
});
}
};
}
@Override
public boolean equals(Object obj) {
return obj instanceof RemotePlayer && getUniqueId().equals(((RemotePlayer) obj).getUniqueId());
}
@Override
public ObjectMap<String> forSubData() {
ObjectMap<String> pinfo = new ObjectMap<String>();
pinfo.set("name", getName());
pinfo.set("id", getUniqueId());
pinfo.set("address", getAddress().getAddress().getHostAddress() + ':' + getAddress().getPort());
if (getServer() != null) pinfo.set("server", getServer().getName());
if (getProxy() != null) pinfo.set("proxy", getProxy().getName());
return pinfo;
}
}