2020-11-14 06:29:29 +01:00
|
|
|
package net.ME1312.SubServers.Client.Common.Network.API;
|
2018-07-01 05:46:58 +02:00
|
|
|
|
2020-06-12 07:45:49 +02:00
|
|
|
import net.ME1312.Galaxi.Library.Callback.Callback;
|
2020-11-14 08:07:25 +01:00
|
|
|
import net.ME1312.Galaxi.Library.Container.ContainedPair;
|
|
|
|
import net.ME1312.Galaxi.Library.Container.Pair;
|
2019-05-14 04:02:38 +02:00
|
|
|
import net.ME1312.Galaxi.Library.Map.ObjectMap;
|
2020-06-12 07:45:49 +02:00
|
|
|
import net.ME1312.Galaxi.Library.Util;
|
2020-08-12 02:28:34 +02:00
|
|
|
import net.ME1312.SubData.Client.DataClient;
|
2019-10-19 00:24:58 +02:00
|
|
|
import net.ME1312.SubData.Client.DataSender;
|
|
|
|
import net.ME1312.SubData.Client.Library.ForwardedDataSender;
|
2019-05-14 04:02:38 +02:00
|
|
|
import net.ME1312.SubData.Client.SubDataClient;
|
2019-10-19 00:24:58 +02:00
|
|
|
import net.ME1312.SubData.Client.SubDataSender;
|
2020-11-16 21:34:59 +01:00
|
|
|
import net.ME1312.SubServers.Client.Common.ClientAPI;
|
2020-11-14 06:29:29 +01:00
|
|
|
import net.ME1312.SubServers.Client.Common.Network.Packet.PacketDownloadPlayerInfo;
|
|
|
|
import net.ME1312.SubServers.Client.Common.Network.Packet.PacketDownloadServerInfo;
|
2018-07-01 05:46:58 +02:00
|
|
|
|
2020-06-12 07:45:49 +02:00
|
|
|
import java.lang.reflect.InvocationTargetException;
|
2018-07-01 05:46:58 +02:00
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
import java.util.*;
|
|
|
|
|
2020-08-12 02:28:34 +02:00
|
|
|
/**
|
|
|
|
* Simplified Server Data Class
|
|
|
|
*/
|
2018-07-01 05:46:58 +02:00
|
|
|
public class Server {
|
2019-05-14 04:02:38 +02:00
|
|
|
ObjectMap<String> raw;
|
2020-06-12 07:45:49 +02:00
|
|
|
private List<RemotePlayer> players = null;
|
2020-08-12 02:28:34 +02:00
|
|
|
DataClient client;
|
2018-07-01 05:46:58 +02:00
|
|
|
long timestamp;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an API representation of a Server
|
|
|
|
*
|
|
|
|
* @param raw Raw representation of the Server
|
|
|
|
*/
|
2019-05-14 04:02:38 +02:00
|
|
|
public Server(ObjectMap<String> raw) {
|
2020-08-12 02:28:34 +02:00
|
|
|
this(null, raw);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an API representation of a Server
|
|
|
|
*
|
|
|
|
* @param client SubData connection
|
|
|
|
* @param raw Raw representation of the Server
|
|
|
|
*/
|
|
|
|
Server(DataClient client, ObjectMap<String> raw) {
|
|
|
|
this.client = (SubDataClient) client;
|
2018-07-01 05:46:58 +02:00
|
|
|
load(raw);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
return obj instanceof Server && getSignature().equals(((Server) obj).getSignature());
|
|
|
|
}
|
|
|
|
|
2019-05-14 04:02:38 +02:00
|
|
|
void load(ObjectMap<String> raw) {
|
2018-07-01 05:46:58 +02:00
|
|
|
this.raw = raw;
|
2020-06-12 07:45:49 +02:00
|
|
|
this.players = null;
|
2018-07-01 05:46:58 +02:00
|
|
|
this.timestamp = Calendar.getInstance().getTime().getTime();
|
|
|
|
}
|
|
|
|
|
2020-08-12 02:28:34 +02:00
|
|
|
SubDataClient client() {
|
|
|
|
return SimplifiedData.client(client);
|
|
|
|
}
|
|
|
|
|
2018-07-01 05:46:58 +02:00
|
|
|
/**
|
|
|
|
* Download a new copy of the data from SubData
|
|
|
|
*/
|
|
|
|
public void refresh() {
|
|
|
|
String name = getName();
|
2020-08-12 02:28:34 +02:00
|
|
|
client().sendPacket(new PacketDownloadServerInfo(Collections.singletonList(name), data -> load(data.getMap(name))));
|
2018-07-01 05:46:58 +02:00
|
|
|
}
|
|
|
|
|
2018-07-06 05:01:38 +02:00
|
|
|
/**
|
2019-05-14 04:02:38 +02:00
|
|
|
* Gets the SubData Client Channel IDs
|
2018-07-06 05:01:38 +02:00
|
|
|
*
|
2019-05-14 04:02:38 +02:00
|
|
|
* @return SubData Client Channel ID Array
|
2018-07-06 05:01:38 +02:00
|
|
|
*/
|
2019-05-14 04:02:38 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
2019-10-19 00:24:58 +02:00
|
|
|
public DataSender[] getSubData() {
|
2019-05-14 04:02:38 +02:00
|
|
|
ObjectMap<Integer> subdata = new ObjectMap<Integer>((Map<Integer, ?>) raw.getObject("subdata"));
|
|
|
|
LinkedList<Integer> keys = new LinkedList<Integer>(subdata.getKeys());
|
2019-10-19 00:24:58 +02:00
|
|
|
LinkedList<SubDataSender> channels = new LinkedList<SubDataSender>();
|
2019-05-14 04:02:38 +02:00
|
|
|
Collections.sort(keys);
|
2020-11-14 06:29:29 +01:00
|
|
|
for (Integer channel : keys) channels.add((subdata.isNull(channel))?null:new ForwardedDataSender((SubDataClient) ClientAPI.getInstance().getSubDataNetwork()[0], subdata.getUUID(channel)));
|
2019-10-19 00:24:58 +02:00
|
|
|
return channels.toArray(new SubDataSender[0]);
|
2018-07-06 05:01:38 +02:00
|
|
|
}
|
|
|
|
|
2018-07-01 05:46:58 +02:00
|
|
|
/**
|
|
|
|
* Get the Name of this Server
|
|
|
|
*
|
|
|
|
* @return Server Name
|
|
|
|
*/
|
|
|
|
public String getName() {
|
|
|
|
return raw.getRawString("name");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Display Name of this Server
|
|
|
|
*
|
|
|
|
* @return Display Name
|
|
|
|
*/
|
|
|
|
public String getDisplayName() {
|
|
|
|
return raw.getRawString("display");
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Get the Address of this Server
|
|
|
|
*
|
|
|
|
* @return Server Address
|
|
|
|
*/
|
|
|
|
public InetSocketAddress getAddress() {
|
|
|
|
return new InetSocketAddress(raw.getRawString("address").split(":")[0], Integer.parseInt(raw.getRawString("address").split(":")[1]));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get this Server's Groups
|
|
|
|
*
|
|
|
|
* @return Group names
|
|
|
|
*/
|
|
|
|
public List<String> getGroups() {
|
|
|
|
return new LinkedList<String>(raw.getRawStringList("group"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-06-12 07:45:49 +02:00
|
|
|
* Get players on this server across all known proxies
|
2018-07-01 05:46:58 +02:00
|
|
|
*
|
2020-06-12 07:45:49 +02:00
|
|
|
* @return Remote Player Collection
|
2018-07-01 05:46:58 +02:00
|
|
|
*/
|
2020-11-14 19:12:08 +01:00
|
|
|
public Collection<Pair<String, UUID>> getRemotePlayers() {
|
2020-11-14 08:07:25 +01:00
|
|
|
List<Pair<String, UUID>> players = new ArrayList<Pair<String, UUID>>();
|
2019-05-14 04:02:38 +02:00
|
|
|
for (String id : raw.getMap("players").getKeys()) {
|
2020-11-14 08:07:25 +01:00
|
|
|
players.add(new ContainedPair<String, UUID>(raw.getMap("players").getRawString(id), UUID.fromString(id)));
|
2018-07-01 05:46:58 +02:00
|
|
|
}
|
|
|
|
return players;
|
|
|
|
}
|
|
|
|
|
2020-06-12 07:45:49 +02:00
|
|
|
/**
|
|
|
|
* Get players on this server across all known proxies
|
|
|
|
*
|
|
|
|
* @param callback Remote Player Collection
|
|
|
|
*/
|
2020-11-14 19:12:08 +01:00
|
|
|
public void getRemotePlayers(Callback<Collection<RemotePlayer>> callback) {
|
2020-06-12 07:45:49 +02:00
|
|
|
if (Util.isNull(callback)) throw new NullPointerException();
|
|
|
|
StackTraceElement[] origin = new Exception().getStackTrace();
|
|
|
|
Runnable run = () -> {
|
|
|
|
try {
|
|
|
|
callback.run(players);
|
|
|
|
} catch (Throwable e) {
|
|
|
|
Throwable ew = new InvocationTargetException(e);
|
|
|
|
ew.setStackTrace(origin);
|
|
|
|
ew.printStackTrace();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (players == null) {
|
|
|
|
LinkedList<UUID> ids = new LinkedList<UUID>();
|
|
|
|
for (String id : raw.getMap("players").getKeys()) ids.add(UUID.fromString(id));
|
2020-08-12 02:28:34 +02:00
|
|
|
client().sendPacket(new PacketDownloadPlayerInfo(ids, data -> {
|
2020-06-12 07:45:49 +02:00
|
|
|
LinkedList<RemotePlayer> players = new LinkedList<RemotePlayer>();
|
|
|
|
for (String player : data.getKeys()) {
|
|
|
|
players.add(new RemotePlayer(data.getMap(player)));
|
|
|
|
}
|
|
|
|
|
|
|
|
this.players = players;
|
|
|
|
run.run();
|
|
|
|
}));
|
|
|
|
} else {
|
|
|
|
run.run();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-01 05:46:58 +02:00
|
|
|
/**
|
|
|
|
* If the server is hidden from players
|
|
|
|
*
|
|
|
|
* @return Hidden Status
|
|
|
|
*/
|
|
|
|
public boolean isHidden() {
|
|
|
|
return raw.getBoolean("hidden");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the MOTD of the Server
|
|
|
|
*
|
|
|
|
* @return Server MOTD
|
|
|
|
*/
|
|
|
|
public String getMotd() {
|
|
|
|
return raw.getRawString("motd");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets if the Server is Restricted
|
|
|
|
*
|
|
|
|
* @return Restricted Status
|
|
|
|
*/
|
|
|
|
public boolean isRestricted() {
|
|
|
|
return raw.getBoolean("restricted");
|
|
|
|
}
|
|
|
|
|
2019-01-10 21:52:38 +01:00
|
|
|
/**
|
|
|
|
* Get a copy of the current whitelist
|
|
|
|
*
|
|
|
|
* @return Player Whitelist
|
|
|
|
*/
|
|
|
|
public Collection<UUID> getWhitelist() {
|
|
|
|
return raw.getUUIDList("whitelist");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* See if a player is whitelisted
|
|
|
|
*
|
|
|
|
* @param player Player to check
|
|
|
|
* @return Whitelisted Status
|
|
|
|
*/
|
|
|
|
public boolean isWhitelisted(UUID player) {
|
|
|
|
return getWhitelist().contains(player);
|
|
|
|
}
|
|
|
|
|
2018-07-01 05:46:58 +02:00
|
|
|
/**
|
|
|
|
* Get the Signature of this Object
|
|
|
|
*
|
|
|
|
* @return Object Signature
|
|
|
|
*/
|
|
|
|
public String getSignature() {
|
|
|
|
return raw.getRawString("signature");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Timestamp for when the data was last refreshed
|
|
|
|
*
|
|
|
|
* @return Data Timestamp
|
|
|
|
*/
|
|
|
|
public long getTimestamp() {
|
|
|
|
return timestamp;
|
|
|
|
}
|
|
|
|
|
2018-08-10 06:50:10 +02:00
|
|
|
/**
|
|
|
|
* Get the raw representation of the Server
|
|
|
|
*
|
|
|
|
* @return Raw Server
|
|
|
|
*/
|
2019-05-14 04:02:38 +02:00
|
|
|
public ObjectMap<String> getRaw() {
|
2018-08-10 06:50:10 +02:00
|
|
|
return raw.clone();
|
|
|
|
}
|
2018-07-01 05:46:58 +02:00
|
|
|
}
|