2016-12-24 05:55:17 +01:00
|
|
|
package net.ME1312.SubServers.Bungee.Host;
|
2016-12-05 04:21:04 +01:00
|
|
|
|
2017-08-22 15:02:23 +02:00
|
|
|
import net.ME1312.SubServers.Bungee.Event.SubEditServerEvent;
|
2017-01-07 20:06:54 +01:00
|
|
|
import net.ME1312.SubServers.Bungee.Library.Config.YAMLSection;
|
|
|
|
import net.ME1312.SubServers.Bungee.Library.Config.YAMLValue;
|
2016-12-24 05:55:17 +01:00
|
|
|
import net.ME1312.SubServers.Bungee.Library.Exception.InvalidServerException;
|
2017-01-08 03:30:03 +01:00
|
|
|
import net.ME1312.SubServers.Bungee.Library.ExtraDataHandler;
|
2017-08-22 15:02:23 +02:00
|
|
|
import net.ME1312.SubServers.Bungee.Library.NamedContainer;
|
2017-01-30 21:22:36 +01:00
|
|
|
import net.ME1312.SubServers.Bungee.Library.Util;
|
2016-12-24 05:55:17 +01:00
|
|
|
import net.ME1312.SubServers.Bungee.Network.Client;
|
|
|
|
import net.ME1312.SubServers.Bungee.Network.ClientHandler;
|
2017-08-22 15:02:23 +02:00
|
|
|
import net.ME1312.SubServers.Bungee.Network.SubDataServer;
|
2017-08-26 07:19:59 +02:00
|
|
|
import net.ME1312.SubServers.Bungee.SubAPI;
|
2017-08-27 05:56:15 +02:00
|
|
|
import net.md_5.bungee.api.Callback;
|
2016-12-05 04:21:04 +01:00
|
|
|
import net.md_5.bungee.api.ChatColor;
|
2017-08-27 05:56:15 +02:00
|
|
|
import net.md_5.bungee.api.CommandSender;
|
|
|
|
import net.md_5.bungee.api.ServerPing;
|
|
|
|
import net.md_5.bungee.api.config.ServerInfo;
|
2017-07-19 01:28:59 +02:00
|
|
|
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
2017-01-07 20:06:54 +01:00
|
|
|
import org.json.JSONObject;
|
2016-12-05 04:21:04 +01:00
|
|
|
|
|
|
|
import java.net.InetSocketAddress;
|
2017-08-27 05:56:15 +02:00
|
|
|
import java.util.*;
|
2016-12-05 04:21:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Server Class
|
|
|
|
*/
|
2017-08-27 05:56:15 +02:00
|
|
|
public class Server implements ServerInfo, ClientHandler, ExtraDataHandler {
|
2017-01-07 20:06:54 +01:00
|
|
|
private YAMLSection extra = new YAMLSection();
|
2016-12-05 04:21:04 +01:00
|
|
|
private Client client = null;
|
2017-08-26 07:19:59 +02:00
|
|
|
private List<String> groups = new ArrayList<String>();
|
2017-01-26 23:19:48 +01:00
|
|
|
private String nick = null;
|
2017-08-27 05:56:15 +02:00
|
|
|
private ServerInfo info;
|
2016-12-20 00:31:01 +01:00
|
|
|
private boolean hidden;
|
2016-12-05 04:21:04 +01:00
|
|
|
|
2016-12-19 01:38:02 +01:00
|
|
|
public Server(String name, InetSocketAddress address, String motd, boolean hidden, boolean restricted) throws InvalidServerException {
|
2017-01-30 21:22:36 +01:00
|
|
|
if (Util.isNull(name, address, motd, hidden, restricted)) throw new NullPointerException();
|
2016-12-05 04:21:04 +01:00
|
|
|
if (name.contains(" ")) throw new InvalidServerException("Server names cannot have spaces: " + name);
|
2017-08-22 15:02:23 +02:00
|
|
|
SubDataServer.allowConnection(getAddress().getAddress());
|
2017-08-27 05:56:15 +02:00
|
|
|
this.info = new ServerInfo(name, address, motd, restricted);
|
2016-12-20 00:31:01 +01:00
|
|
|
this.hidden = hidden;
|
2016-12-05 04:21:04 +01:00
|
|
|
}
|
|
|
|
|
2017-08-27 05:56:15 +02:00
|
|
|
private static final class ServerInfo extends net.md_5.bungee.BungeeServerInfo {
|
|
|
|
private String motd;
|
|
|
|
private boolean restricted;
|
|
|
|
|
|
|
|
public ServerInfo(String name, InetSocketAddress address, String motd, boolean restricted) {
|
|
|
|
super(name, address, ChatColor.translateAlternateColorCodes('&', motd), restricted);
|
|
|
|
this.motd = motd;
|
|
|
|
this.restricted = restricted;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getMotd() {
|
|
|
|
return motd;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setMotd(String value) {
|
|
|
|
this.motd = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isRestricted() {
|
|
|
|
return restricted;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setRestricted(boolean value) {
|
|
|
|
this.restricted = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-05 04:21:04 +01:00
|
|
|
@Override
|
2017-06-30 15:36:16 +02:00
|
|
|
public Client getSubData() {
|
2016-12-05 04:21:04 +01:00
|
|
|
return client;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-06-30 15:36:16 +02:00
|
|
|
public void setSubData(Client client) {
|
|
|
|
this.client = client;
|
2017-07-21 22:45:41 +02:00
|
|
|
if (client != null && (client.getHandler() == null || !equals(client.getHandler()))) client.setHandler(this);
|
2016-12-05 04:21:04 +01:00
|
|
|
}
|
2016-12-20 00:31:01 +01:00
|
|
|
|
2017-01-26 23:19:48 +01:00
|
|
|
/**
|
|
|
|
* 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) {
|
2017-04-24 18:28:16 +02:00
|
|
|
if (value == null || value.length() == 0 || getName().equals(value)) {
|
2017-08-22 15:02:23 +02:00
|
|
|
new SubEditServerEvent(null, this, new NamedContainer<String, Object>("display", getName()), false);
|
2017-04-24 18:28:16 +02:00
|
|
|
this.nick = null;
|
|
|
|
} else {
|
2017-08-22 15:02:23 +02:00
|
|
|
new SubEditServerEvent(null, this, new NamedContainer<String, Object>("display", value), false);
|
2017-04-24 18:28:16 +02:00
|
|
|
this.nick = value;
|
|
|
|
}
|
2017-01-26 23:19:48 +01:00
|
|
|
}
|
|
|
|
|
2016-12-20 00:31:01 +01:00
|
|
|
/**
|
2017-08-27 05:56:15 +02:00
|
|
|
* Get this Server's Groups
|
|
|
|
*
|
|
|
|
* @return Group names
|
|
|
|
*/
|
|
|
|
public List<String> getGroups() {
|
|
|
|
return groups;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add this Server to a Group
|
|
|
|
*
|
|
|
|
* @param value Group name
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("deprecation")
|
|
|
|
public void addGroup(String value) {
|
|
|
|
if (Util.isNull(value)) throw new NullPointerException();
|
|
|
|
if (value.length() > 0 && !groups.contains(value)) {
|
|
|
|
List<Server> list = (SubAPI.getInstance().getInternals().groups.keySet().contains(value))?SubAPI.getInstance().getInternals().groups.get(value):new ArrayList<Server>();
|
|
|
|
list.add(this);
|
|
|
|
SubAPI.getInstance().getInternals().groups.put(value, list);
|
|
|
|
groups.add(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove this Server from a Group
|
|
|
|
*
|
|
|
|
* @param value value Group name
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("deprecation")
|
|
|
|
public void removeGroup(String value) {
|
|
|
|
if (Util.isNull(value)) throw new NullPointerException();
|
|
|
|
List<Server> list = SubAPI.getInstance().getInternals().groups.get(value);
|
|
|
|
list.remove(this);
|
|
|
|
SubAPI.getInstance().getInternals().groups.put(value, list);
|
|
|
|
groups.remove(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the Server is hidden from players
|
2016-12-20 00:31:01 +01:00
|
|
|
*
|
|
|
|
* @return Hidden Status
|
|
|
|
*/
|
|
|
|
public boolean isHidden() {
|
|
|
|
return hidden;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-27 05:56:15 +02:00
|
|
|
* Set if the Server is hidden from players
|
2016-12-20 00:31:01 +01:00
|
|
|
*
|
|
|
|
* @param value Value
|
|
|
|
*/
|
|
|
|
public void setHidden(boolean value) {
|
2017-01-30 21:22:36 +01:00
|
|
|
if (Util.isNull(value)) throw new NullPointerException();
|
2017-08-22 15:02:23 +02:00
|
|
|
new SubEditServerEvent(null, this, new NamedContainer<String, Object>("hidden", value), false);
|
2016-12-20 00:31:01 +01:00
|
|
|
this.hidden = value;
|
|
|
|
}
|
|
|
|
|
2017-08-27 05:56:15 +02:00
|
|
|
// Methods unrelated to SubServers
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get this Server's Name
|
|
|
|
*
|
|
|
|
* @return Server Name
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public String getName() {
|
|
|
|
return info.getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get this Server's Address
|
|
|
|
*
|
|
|
|
* @return Server Address
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public InetSocketAddress getAddress() {
|
|
|
|
return info.getAddress();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Players connected to this Server
|
|
|
|
*
|
|
|
|
* @return Player list
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public Collection<ProxiedPlayer> getPlayers() {
|
|
|
|
return info.getPlayers();
|
|
|
|
}
|
|
|
|
|
2016-12-20 00:31:01 +01:00
|
|
|
/**
|
2017-08-27 05:56:15 +02:00
|
|
|
* Get this Server's MOTD
|
2016-12-20 00:31:01 +01:00
|
|
|
*
|
|
|
|
* @return Server MOTD
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public String getMotd() {
|
2017-08-27 05:56:15 +02:00
|
|
|
return info.getMotd();
|
2016-12-20 00:31:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-27 05:56:15 +02:00
|
|
|
* Set this Server's MOTD
|
2016-12-20 00:31:01 +01:00
|
|
|
*
|
|
|
|
* @param value Value
|
|
|
|
*/
|
|
|
|
public void setMotd(String value) {
|
2017-01-30 21:22:36 +01:00
|
|
|
if (Util.isNull(value)) throw new NullPointerException();
|
2017-08-22 15:02:23 +02:00
|
|
|
new SubEditServerEvent(null, this, new NamedContainer<String, Object>("motd", value), false);
|
2017-08-27 05:56:15 +02:00
|
|
|
info.setMotd(value);
|
2016-12-20 00:31:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-27 05:56:15 +02:00
|
|
|
* Whether the Player can access this Server
|
2016-12-20 00:31:01 +01:00
|
|
|
*
|
2017-08-27 05:56:15 +02:00
|
|
|
* @param sender Player
|
|
|
|
* @return Player Access Status
|
2016-12-20 00:31:01 +01:00
|
|
|
*/
|
|
|
|
@Override
|
2017-08-27 05:56:15 +02:00
|
|
|
public boolean canAccess(CommandSender sender) {
|
|
|
|
return info.canAccess(sender);
|
2016-12-20 00:31:01 +01:00
|
|
|
}
|
|
|
|
|
2017-08-26 07:19:59 +02:00
|
|
|
/**
|
2017-08-27 05:56:15 +02:00
|
|
|
* Send PluginMessageChannel data to the Server
|
2017-08-26 07:19:59 +02:00
|
|
|
*
|
2017-08-27 05:56:15 +02:00
|
|
|
* @param channel Channel name
|
|
|
|
* @param data Data to send
|
2017-08-26 07:19:59 +02:00
|
|
|
*/
|
2017-08-27 05:56:15 +02:00
|
|
|
@Override
|
|
|
|
public void sendData(String channel, byte[] data) {
|
|
|
|
info.sendData(channel, data);
|
2017-08-26 07:19:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-27 05:56:15 +02:00
|
|
|
* Send PluginMessageChannel data to the Server
|
2017-08-26 07:19:59 +02:00
|
|
|
*
|
2017-08-27 05:56:15 +02:00
|
|
|
* @param channel Channel name
|
|
|
|
* @param data Data to send
|
|
|
|
* @param queue Queue message for later if cannot be sent immediately
|
|
|
|
* @return If the message was sent immediately
|
2017-08-26 07:19:59 +02:00
|
|
|
*/
|
2017-08-27 05:56:15 +02:00
|
|
|
@Override
|
|
|
|
public boolean sendData(String channel, byte[] data, boolean queue) {
|
|
|
|
return info.sendData(channel, data, queue);
|
2017-08-26 07:19:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-27 05:56:15 +02:00
|
|
|
* Ping the Server
|
2017-08-26 07:19:59 +02:00
|
|
|
*
|
2017-08-27 05:56:15 +02:00
|
|
|
* @param callback Ping Callback
|
2017-08-26 07:19:59 +02:00
|
|
|
*/
|
2017-08-27 05:56:15 +02:00
|
|
|
@Override
|
|
|
|
public void ping(Callback<ServerPing> callback) {
|
|
|
|
info.ping(callback);
|
2017-08-26 07:19:59 +02:00
|
|
|
}
|
|
|
|
|
2016-12-20 00:31:01 +01:00
|
|
|
/**
|
2017-08-27 05:56:15 +02:00
|
|
|
* Get the Server's Restricted Status
|
2016-12-20 00:31:01 +01:00
|
|
|
*
|
2017-08-27 05:56:15 +02:00
|
|
|
* @return Restricted Status
|
2016-12-20 00:31:01 +01:00
|
|
|
*/
|
2017-08-27 05:56:15 +02:00
|
|
|
public boolean isRestricted() {
|
|
|
|
return info.isRestricted();
|
|
|
|
}
|
|
|
|
|
2016-12-20 00:31:01 +01:00
|
|
|
public void setRestricted(boolean value) {
|
2017-01-30 21:22:36 +01:00
|
|
|
if (Util.isNull(value)) throw new NullPointerException();
|
2017-08-22 15:02:23 +02:00
|
|
|
new SubEditServerEvent(null, this, new NamedContainer<String, Object>("restricted", value), false);
|
2017-08-27 05:56:15 +02:00
|
|
|
info.setRestricted(value);
|
2016-12-20 00:31:01 +01:00
|
|
|
}
|
2017-01-07 20:06:54 +01:00
|
|
|
|
2017-01-08 03:30:03 +01:00
|
|
|
@Override
|
|
|
|
public void addExtra(String handle, Object value) {
|
2017-01-30 21:22:36 +01:00
|
|
|
if (Util.isNull(handle, value)) throw new NullPointerException();
|
2017-01-08 03:30:03 +01:00
|
|
|
extra.set(handle, value);
|
2017-01-07 20:06:54 +01:00
|
|
|
}
|
|
|
|
|
2017-01-08 03:30:03 +01:00
|
|
|
@Override
|
|
|
|
public boolean hasExtra(String handle) {
|
2017-01-30 21:22:36 +01:00
|
|
|
if (Util.isNull(handle)) throw new NullPointerException();
|
2017-01-08 03:30:03 +01:00
|
|
|
return extra.getKeys().contains(handle);
|
2017-01-07 20:06:54 +01:00
|
|
|
}
|
|
|
|
|
2017-01-08 03:30:03 +01:00
|
|
|
@Override
|
|
|
|
public YAMLValue getExtra(String handle) {
|
2017-01-30 21:22:36 +01:00
|
|
|
if (Util.isNull(handle)) throw new NullPointerException();
|
2017-01-08 03:30:03 +01:00
|
|
|
return extra.get(handle);
|
2017-01-07 20:06:54 +01:00
|
|
|
}
|
|
|
|
|
2017-01-08 03:30:03 +01:00
|
|
|
@Override
|
|
|
|
public YAMLSection getExtra() {
|
|
|
|
return extra.clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void removeExtra(String handle) {
|
2017-01-30 21:22:36 +01:00
|
|
|
if (Util.isNull(handle)) throw new NullPointerException();
|
2017-01-08 03:30:03 +01:00
|
|
|
extra.remove(handle);
|
2017-01-07 20:06:54 +01:00
|
|
|
}
|
2017-07-19 01:28:59 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
JSONObject info = new JSONObject();
|
|
|
|
info.put("type", "Server");
|
|
|
|
info.put("name", getName());
|
2017-08-26 07:19:59 +02:00
|
|
|
info.put("group", getGroups());
|
2017-07-19 01:28:59 +02:00
|
|
|
info.put("display", getDisplayName());
|
2017-08-22 15:02:23 +02:00
|
|
|
info.put("address", getAddress().getAddress().getHostAddress() + ':' + getAddress().getPort());
|
|
|
|
info.put("motd", getMotd());
|
|
|
|
info.put("restricted", isRestricted());
|
|
|
|
info.put("hidden", isHidden());
|
2017-07-19 01:28:59 +02:00
|
|
|
JSONObject players = new JSONObject();
|
|
|
|
for (ProxiedPlayer player : getPlayers()) {
|
|
|
|
JSONObject pinfo = new JSONObject();
|
|
|
|
pinfo.put("name", player.getName());
|
|
|
|
pinfo.put("nick", player.getDisplayName());
|
|
|
|
players.put(player.getUniqueId().toString(), pinfo);
|
|
|
|
}
|
|
|
|
info.put("players", players);
|
|
|
|
if (getSubData() != null) info.put("subdata", getSubData().getAddress().toString());
|
|
|
|
info.put("extra", getExtra().toJSON());
|
|
|
|
return info.toString();
|
|
|
|
}
|
2016-12-05 04:21:04 +01:00
|
|
|
}
|