2018-07-01 05:46:58 +02:00
|
|
|
package net.ME1312.SubServers.Host.Network.API;
|
|
|
|
|
2019-05-13 05:28:27 +02:00
|
|
|
import net.ME1312.Galaxi.Library.Map.ObjectMap;
|
|
|
|
import net.ME1312.Galaxi.Library.Map.ObjectMapValue;
|
2018-09-09 00:06:21 +02:00
|
|
|
import net.ME1312.Galaxi.Library.NamedContainer;
|
|
|
|
import net.ME1312.Galaxi.Library.Util;
|
2019-05-13 05:28:27 +02:00
|
|
|
import net.ME1312.SubData.Client.SubDataClient;
|
2018-08-09 20:54:56 +02:00
|
|
|
import net.ME1312.SubServers.Host.Network.Packet.PacketDownloadProxyInfo;
|
2018-07-01 05:46:58 +02:00
|
|
|
import net.ME1312.SubServers.Host.SubAPI;
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
public class Proxy {
|
2019-05-13 05:28:27 +02:00
|
|
|
ObjectMap<String> raw;
|
2018-07-01 05:46:58 +02:00
|
|
|
long timestamp;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an API representation of a Proxy
|
|
|
|
*
|
|
|
|
* @param raw Raw representation of the Proxy
|
|
|
|
*/
|
2019-05-13 05:28:27 +02:00
|
|
|
public Proxy(ObjectMap<String> raw) {
|
2018-07-01 05:46:58 +02:00
|
|
|
load(raw);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
return obj instanceof Proxy && getSignature().equals(((Proxy) obj).getSignature());
|
|
|
|
}
|
|
|
|
|
2019-05-13 05:28:27 +02:00
|
|
|
private void load(ObjectMap<String> raw) {
|
2018-07-01 05:46:58 +02:00
|
|
|
this.raw = raw;
|
|
|
|
this.timestamp = Calendar.getInstance().getTime().getTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Download a new copy of the data from SubData
|
|
|
|
*/
|
|
|
|
public void refresh() {
|
|
|
|
String name = getName();
|
2019-05-13 05:28:27 +02:00
|
|
|
((SubDataClient) SubAPI.getInstance().getSubDataNetwork()[0]).sendPacket(new PacketDownloadProxyInfo(name, data -> load(data.getMap(name))));
|
2018-07-01 05:46:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-05-13 05:28:27 +02:00
|
|
|
* Gets the SubData Client Channel IDs
|
2018-07-01 05:46:58 +02:00
|
|
|
*
|
2019-05-13 05:28:27 +02:00
|
|
|
* @return SubData Client Channel ID Array
|
2018-07-01 05:46:58 +02:00
|
|
|
*/
|
2019-05-13 05:28:27 +02:00
|
|
|
public UUID[] getSubData() {
|
|
|
|
return raw.getUUIDList("subdata", Collections.emptyList()).toArray(new UUID[0]);
|
2018-07-01 05:46:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Name of this Proxy
|
|
|
|
*
|
|
|
|
* @return Name
|
|
|
|
*/
|
|
|
|
public String getName() {
|
|
|
|
return raw.getRawString("name");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Display Name of this Proxy
|
|
|
|
*
|
|
|
|
* @return Display Name
|
|
|
|
*/
|
|
|
|
public String getDisplayName() {
|
|
|
|
return raw.getRawString("display");
|
|
|
|
}
|
|
|
|
|
2018-08-06 04:07:18 +02:00
|
|
|
/**
|
|
|
|
* Test if the proxy is connected to RedisBungee's server
|
|
|
|
*
|
|
|
|
* @return Redis Status
|
|
|
|
*/
|
|
|
|
public boolean isRedis() {
|
|
|
|
return raw.getBoolean("redis");
|
|
|
|
}
|
|
|
|
|
2018-07-01 05:46:58 +02:00
|
|
|
/**
|
|
|
|
* Get the players on this proxy (via RedisBungee)
|
|
|
|
*
|
|
|
|
* @return Player Collection
|
|
|
|
*/
|
|
|
|
public Collection<NamedContainer<String, UUID>> getPlayers() {
|
|
|
|
List<NamedContainer<String, UUID>> players = new ArrayList<NamedContainer<String, UUID>>();
|
2019-05-13 05:28:27 +02:00
|
|
|
for (String id : raw.getMap("players").getKeys()) {
|
|
|
|
players.add(new NamedContainer<String, UUID>(raw.getMap("players").getMap(id).getRawString("name"), UUID.fromString(id)));
|
2018-07-01 05:46:58 +02:00
|
|
|
}
|
|
|
|
return players;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Signature of this Object
|
|
|
|
*
|
|
|
|
* @return Object Signature
|
|
|
|
*/
|
|
|
|
public final String getSignature() {
|
|
|
|
return raw.getRawString("signature");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Timestamp for when the data was last refreshed
|
|
|
|
*
|
|
|
|
* @return Data Timestamp
|
|
|
|
*/
|
|
|
|
public long getTimestamp() {
|
|
|
|
return timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if an extra value exists
|
|
|
|
*
|
|
|
|
* @param handle Handle
|
|
|
|
* @return Value Status
|
|
|
|
*/
|
|
|
|
public boolean hasExtra(String handle) {
|
|
|
|
if (Util.isNull(handle)) throw new NullPointerException();
|
2019-05-13 05:28:27 +02:00
|
|
|
return raw.getMap("extra").getKeys().contains(handle);
|
2018-07-01 05:46:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an extra value
|
|
|
|
*
|
|
|
|
* @param handle Handle
|
|
|
|
* @return Value
|
|
|
|
*/
|
2019-05-13 05:28:27 +02:00
|
|
|
public ObjectMapValue<String> getExtra(String handle) {
|
2018-07-01 05:46:58 +02:00
|
|
|
if (Util.isNull(handle)) throw new NullPointerException();
|
2019-05-13 05:28:27 +02:00
|
|
|
return raw.getMap("extra").get(handle);
|
2018-07-01 05:46:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the extra value section
|
|
|
|
*
|
|
|
|
* @return Extra Value Section
|
|
|
|
*/
|
2019-05-13 05:28:27 +02:00
|
|
|
public ObjectMap<String> getExtra() {
|
|
|
|
return raw.getMap("extra").clone();
|
2018-07-01 05:46:58 +02:00
|
|
|
}
|
|
|
|
|
2018-08-10 06:50:10 +02:00
|
|
|
/**
|
|
|
|
* Get the raw representation of the Proxy
|
|
|
|
*
|
|
|
|
* @return Raw Proxy
|
|
|
|
*/
|
2019-05-13 05:28:27 +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
|
|
|
}
|