2018-01-22 16:01:33 +01:00
|
|
|
package net.ME1312.SubServers.Bungee.Host;
|
|
|
|
|
2019-04-18 16:02:09 +02:00
|
|
|
import net.ME1312.SubData.Server.DataClient;
|
|
|
|
import net.ME1312.SubData.Server.SubDataClient;
|
2018-08-05 23:41:17 +02:00
|
|
|
import net.ME1312.SubServers.Bungee.Event.SubRemoveProxyEvent;
|
2019-04-18 16:02:09 +02:00
|
|
|
import net.ME1312.Galaxi.Library.Map.ObjectMap;
|
|
|
|
import net.ME1312.Galaxi.Library.Map.ObjectMapValue;
|
|
|
|
import net.ME1312.Galaxi.Library.ExtraDataHandler;
|
2020-06-12 07:45:49 +02:00
|
|
|
import net.ME1312.Galaxi.Library.Container.NamedContainer;
|
2019-04-18 16:02:09 +02:00
|
|
|
import net.ME1312.Galaxi.Library.Util;
|
|
|
|
import net.ME1312.SubData.Server.ClientHandler;
|
2018-01-22 16:01:33 +01:00
|
|
|
import net.ME1312.SubServers.Bungee.SubAPI;
|
2019-08-30 21:44:54 +02:00
|
|
|
import net.ME1312.SubServers.Bungee.SubProxy;
|
2018-08-05 23:41:17 +02:00
|
|
|
import net.md_5.bungee.api.ProxyServer;
|
2020-06-12 07:45:49 +02:00
|
|
|
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
2018-01-22 16:01:33 +01:00
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Proxy Class
|
|
|
|
*/
|
2019-05-10 04:43:34 +02:00
|
|
|
public class Proxy implements ClientHandler, ExtraDataHandler {
|
|
|
|
private HashMap<Integer, SubDataClient> subdata = new HashMap<Integer, SubDataClient>();
|
2019-04-18 16:02:09 +02:00
|
|
|
private ObjectMap<String> extra = new ObjectMap<String>();
|
2018-01-22 16:01:33 +01:00
|
|
|
private final String signature;
|
2018-08-05 23:41:17 +02:00
|
|
|
private boolean persistent = true;
|
2018-01-22 16:01:33 +01:00
|
|
|
private String nick = null;
|
|
|
|
private final String name;
|
|
|
|
|
2018-08-05 23:41:17 +02:00
|
|
|
@SuppressWarnings("deprecation")
|
2018-01-22 16:01:33 +01:00
|
|
|
public Proxy(String name) throws IllegalArgumentException {
|
2018-08-05 23:41:17 +02:00
|
|
|
if (name == null) {
|
|
|
|
name = Util.getNew(SubAPI.getInstance().getInternals().proxies.keySet(), () -> UUID.randomUUID().toString());
|
|
|
|
persistent = false;
|
|
|
|
}
|
2018-01-22 16:01:33 +01:00
|
|
|
if (name.contains(" ")) throw new IllegalArgumentException("Proxy names cannot have spaces: " + name);
|
|
|
|
this.name = name;
|
|
|
|
this.signature = SubAPI.getInstance().signAnonymousObject();
|
2019-05-10 04:43:34 +02:00
|
|
|
|
2019-05-14 04:02:38 +02:00
|
|
|
subdata.put(0, null);
|
2018-01-22 16:01:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-10 04:43:34 +02:00
|
|
|
public DataClient[] getSubData() {
|
|
|
|
LinkedList<Integer> keys = new LinkedList<Integer>(subdata.keySet());
|
|
|
|
LinkedList<SubDataClient> channels = new LinkedList<SubDataClient>();
|
|
|
|
Collections.sort(keys);
|
|
|
|
for (Integer channel : keys) channels.add(subdata.get(channel));
|
|
|
|
return channels.toArray(new DataClient[0]);
|
2018-01-22 16:01:33 +01:00
|
|
|
}
|
|
|
|
|
2019-05-14 04:02:38 +02:00
|
|
|
public void setSubData(SubDataClient client, int channel) {
|
2019-05-10 06:04:10 +02:00
|
|
|
boolean update = false;
|
2019-05-10 04:43:34 +02:00
|
|
|
if (channel < 0) throw new IllegalArgumentException("Subchannel ID cannot be less than zero");
|
2019-05-10 06:04:10 +02:00
|
|
|
if (client != null || channel == 0) {
|
|
|
|
if (!subdata.keySet().contains(channel) || (channel == 0 && (client == null || subdata.get(channel) == null))) {
|
|
|
|
update = true;
|
2019-05-10 04:43:34 +02:00
|
|
|
subdata.put(channel, (SubDataClient) client);
|
|
|
|
if (client != null && (client.getHandler() == null || !equals(client.getHandler()))) ((SubDataClient) client).setHandler(this);
|
|
|
|
}
|
2019-05-10 06:04:10 +02:00
|
|
|
} else {
|
|
|
|
update = true;
|
|
|
|
subdata.remove(channel);
|
|
|
|
}
|
2019-05-10 04:43:34 +02:00
|
|
|
|
2019-05-10 06:04:10 +02:00
|
|
|
if (update) {
|
2019-05-10 04:43:34 +02:00
|
|
|
DataClient[] subdata = getSubData();
|
|
|
|
if (subdata[0] == null && subdata.length <= 1 && !persistent) {
|
|
|
|
ProxyServer.getInstance().getPluginManager().callEvent(new SubRemoveProxyEvent(this));
|
|
|
|
SubAPI.getInstance().getInternals().proxies.remove(getName().toLowerCase());
|
|
|
|
}
|
2018-08-05 23:41:17 +02:00
|
|
|
}
|
2019-05-10 04:43:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void removeSubData(DataClient client) {
|
|
|
|
for (Integer channel : Util.getBackwards(subdata, (SubDataClient) client)) setSubData(null, channel);
|
2018-01-22 16:01:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Name of this Proxy
|
|
|
|
*
|
|
|
|
* @return Name
|
|
|
|
*/
|
|
|
|
public String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Display Name of this Proxy
|
|
|
|
*
|
|
|
|
* @return Display Name
|
|
|
|
*/
|
|
|
|
public String getDisplayName() {
|
|
|
|
return (nick == null)?getName():nick;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the Display Name for this Proxy
|
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-06 04:07:18 +02:00
|
|
|
/**
|
2020-06-12 07:45:49 +02:00
|
|
|
* Determine if the proxy is connected to RedisBungee's server
|
2018-08-06 04:07:18 +02:00
|
|
|
*
|
|
|
|
* @return Redis Status
|
|
|
|
*/
|
|
|
|
@SuppressWarnings({"deprecation", "unchecked"})
|
|
|
|
public boolean isRedis() {
|
2019-08-30 21:44:54 +02:00
|
|
|
SubProxy plugin = SubAPI.getInstance().getInternals();
|
2018-08-09 20:54:56 +02:00
|
|
|
return plugin.redis != null && Util.getDespiteException(() -> plugin.redis("getPlayersOnProxy", new NamedContainer<>(String.class, getName())) != null, false);
|
2018-08-06 04:07:18 +02:00
|
|
|
}
|
|
|
|
|
2020-06-12 07:45:49 +02:00
|
|
|
/**
|
|
|
|
* Determine if the proxy is the Master Proxy
|
|
|
|
*
|
|
|
|
* @return Master Proxy Status
|
|
|
|
*/
|
|
|
|
public boolean isMaster() {
|
|
|
|
return SubAPI.getInstance().getMasterProxy() == this;
|
|
|
|
}
|
|
|
|
|
2018-01-22 16:01:33 +01:00
|
|
|
/**
|
|
|
|
* Get the players on this proxy (via RedisBungee)
|
|
|
|
*
|
2020-06-12 07:45:49 +02:00
|
|
|
* @return Remote Player Collection
|
2018-01-22 16:01:33 +01:00
|
|
|
*/
|
|
|
|
@SuppressWarnings({"deprecation", "unchecked"})
|
2020-06-12 07:45:49 +02:00
|
|
|
public Collection<RemotePlayer> getPlayers() {
|
|
|
|
List<RemotePlayer> players = new LinkedList<RemotePlayer>();
|
|
|
|
//List<UUID> used = new ArrayList<UUID>();
|
2019-08-30 21:44:54 +02:00
|
|
|
SubProxy plugin = SubAPI.getInstance().getInternals();
|
2018-08-09 20:54:56 +02:00
|
|
|
if (plugin.redis != null) {
|
2018-01-22 16:01:33 +01:00
|
|
|
try {
|
2020-06-12 07:45:49 +02:00
|
|
|
for (UUID id : (Set<UUID>) plugin.redis("getPlayersOnProxy", new NamedContainer<>(String.class, getName()))) {
|
|
|
|
//if (!used.contains(id)) {
|
|
|
|
players.add(new RemotePlayer(id));
|
|
|
|
// used.add(id);
|
|
|
|
//}
|
|
|
|
}
|
2018-08-05 23:41:17 +02:00
|
|
|
} catch (Exception e) {}
|
2018-01-22 16:01:33 +01:00
|
|
|
}
|
|
|
|
return players;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Signature of this Object
|
|
|
|
*
|
|
|
|
* @return Object Signature
|
|
|
|
*/
|
|
|
|
public final String getSignature() {
|
|
|
|
return signature;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void addExtra(String handle, Object value) {
|
|
|
|
if (Util.isNull(handle, value)) throw new NullPointerException();
|
|
|
|
extra.set(handle, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasExtra(String handle) {
|
|
|
|
if (Util.isNull(handle)) throw new NullPointerException();
|
|
|
|
return extra.getKeys().contains(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-04-18 16:02:09 +02:00
|
|
|
public ObjectMapValue getExtra(String handle) {
|
2018-01-22 16:01:33 +01:00
|
|
|
if (Util.isNull(handle)) throw new NullPointerException();
|
|
|
|
return extra.get(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-04-18 16:02:09 +02:00
|
|
|
public ObjectMap<String> getExtra() {
|
2018-01-22 16:01:33 +01:00
|
|
|
return extra.clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void removeExtra(String handle) {
|
|
|
|
if (Util.isNull(handle)) throw new NullPointerException();
|
|
|
|
extra.remove(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-04-18 16:02:09 +02:00
|
|
|
public ObjectMap<String> forSubData() {
|
|
|
|
ObjectMap<String> info = new ObjectMap<String>();
|
2018-04-15 03:53:51 +02:00
|
|
|
info.set("type", "Proxy");
|
|
|
|
info.set("name", getName());
|
|
|
|
info.set("display", getDisplayName());
|
2019-04-18 16:02:09 +02:00
|
|
|
ObjectMap<String> players = new ObjectMap<String>();
|
2020-06-12 07:45:49 +02:00
|
|
|
for (RemotePlayer player : getPlayers())
|
|
|
|
players.set(player.getUniqueId().toString(), player.getName());
|
2018-04-15 03:53:51 +02:00
|
|
|
info.set("players", players);
|
2018-08-06 04:07:18 +02:00
|
|
|
info.set("redis", isRedis());
|
2020-06-12 07:45:49 +02:00
|
|
|
info.set("master", isMaster());
|
2019-05-14 04:02:38 +02:00
|
|
|
ObjectMap<Integer> subdata = new ObjectMap<Integer>();
|
|
|
|
for (int channel : this.subdata.keySet()) subdata.set(channel, (this.subdata.get(channel) == null)?null:this.subdata.get(channel).getID());
|
2019-05-10 04:43:34 +02:00
|
|
|
info.set("subdata", subdata);
|
2018-04-15 03:53:51 +02:00
|
|
|
info.set("signature", signature);
|
2018-07-01 05:46:58 +02:00
|
|
|
info.set("extra", getExtra());
|
2019-04-18 16:02:09 +02:00
|
|
|
return info;
|
2018-01-22 16:01:33 +01:00
|
|
|
}
|
|
|
|
}
|