/
ServerImpl.java
175 lines (158 loc) · 5.18 KB
/
ServerImpl.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
package net.ME1312.SubServers.Sync.Server;
import net.ME1312.Galaxi.Library.Util;
import net.ME1312.SubData.Client.DataSender;
import net.ME1312.SubData.Client.Library.ForwardedDataSender;
import net.ME1312.SubData.Client.SubDataClient;
import net.ME1312.SubData.Client.SubDataSender;
import net.ME1312.SubServers.Sync.SubAPI;
import net.md_5.bungee.BungeeServerInfo;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import java.lang.reflect.Field;
import java.net.InetSocketAddress;
import java.util.*;
/**
* Server Class
*/
public class ServerImpl extends BungeeServerInfo {
private HashMap<Integer, UUID> subdata = new HashMap<Integer, UUID>();
private List<UUID> whitelist = new ArrayList<UUID>();
private String nick = null;
private boolean hidden;
private final String signature;
public ServerImpl(String signature, String name, String display, InetSocketAddress address, Map<Integer, UUID> subdata, String motd, boolean hidden, boolean restricted, Collection<UUID> whitelist) {
super(name, address, motd, restricted);
if (Util.isNull(name, address, motd, hidden, restricted)) throw new NullPointerException();
this.signature = signature;
this.whitelist.addAll(whitelist);
this.hidden = hidden;
setDisplayName(display);
for (int channel : subdata.keySet())
setSubData(subdata.get(channel), channel);
}
/**
* Gets the SubData Client Channel IDs
*
* @return SubData Client Channel ID Array
*/
public DataSender[] getSubData() {
LinkedList<Integer> keys = new LinkedList<Integer>(subdata.keySet());
LinkedList<SubDataSender> channels = new LinkedList<SubDataSender>();
Collections.sort(keys);
for (Integer channel : keys) channels.add((subdata.getOrDefault(channel, null) == null)?null:new ForwardedDataSender((SubDataClient) SubAPI.getInstance().getSubDataNetwork()[0], subdata.get(channel)));
return channels.toArray(new SubDataSender[0]);
}
/**
* Link a SubData Client to this Object
*
* @param client Client to Link
* @param channel Channel ID
*/
public void setSubData(UUID client, int channel) {
if (channel < 0) throw new IllegalArgumentException("Subchannel ID cannot be less than zero");
if (client != null || channel == 0) {
if (!subdata.keySet().contains(channel) || (channel == 0 && (client == null || subdata.get(channel) == null))) {
subdata.put(channel, client);
}
} else {
subdata.remove(channel);
}
}
/**
* Get the Display Name of this Server
*
* @return Display Name
*/
public String getDisplayName() {
return (nick == null)?getName():nick;
}
/**
* Sets the Display Name for this Server
*
* @param value Value (or null to reset)
*/
public void setDisplayName(String value) {
if (value == null || value.length() == 0 || getName().equals(value)) {
this.nick = null;
} else {
this.nick = value;
}
}
/**
* See if a player is whitelisted
*
* @param player Player
* @return Whitelisted Status
*/
public boolean canAccess(CommandSender player) {
return (player instanceof ProxiedPlayer && whitelist.contains(((ProxiedPlayer) player).getUniqueId())) || super.canAccess(player);
}
/**
* Add a player to the whitelist (for use with restricted servers)
*
* @param player Player to add
*/
public void whitelist(UUID player) {
if (Util.isNull(player)) throw new NullPointerException();
whitelist.add(player);
}
/**
* Remove a player to the whitelist
*
* @param player Player to remove
*/
public void unwhitelist(UUID player) {
whitelist.remove(player);
}
/**
* If the server is hidden from players
*
* @return Hidden Status
*/
public boolean isHidden() {
return hidden;
}
/**
* Set if the server is hidden from players
*
* @param value Value
*/
public void setHidden(boolean value) {
if (Util.isNull(value)) throw new NullPointerException();
this.hidden = value;
}
/**
* Sets the MOTD of the Server
*
* @param value Value
*/
public void setMotd(String value) {
if (Util.isNull(value)) throw new NullPointerException();
try {
Util.reflect(BungeeServerInfo.class.getDeclaredField("motd"), this, value);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Sets if the Server is Restricted
*
* @param value Value
*/
public void setRestricted(boolean value) {
if (Util.isNull(value)) throw new NullPointerException();
try {
Util.reflect(BungeeServerInfo.class.getDeclaredField("restricted"), this, value);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Get the Signature of this Object
*
* @return Object Signature
*/
public final String getSignature() {
return signature;
}
}