SubServers-2/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/Server.java

161 lines
4.3 KiB
Java
Raw Normal View History

2016-12-24 05:55:17 +01:00
package net.ME1312.SubServers.Bungee.Host;
2016-12-05 04:21:04 +01:00
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;
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;
2016-12-05 04:21:04 +01:00
import net.md_5.bungee.BungeeServerInfo;
import net.md_5.bungee.api.ChatColor;
2017-01-07 20:06:54 +01:00
import org.json.JSONObject;
2016-12-05 04:21:04 +01:00
import java.net.InetSocketAddress;
/**
* Server Class
*/
2017-01-08 03:30:03 +01:00
public class Server extends BungeeServerInfo implements 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-01-26 23:19:48 +01:00
private String nick = null;
2016-12-20 00:31:01 +01:00
private String motd;
private boolean restricted;
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 {
2016-12-05 04:21:04 +01:00
super(name, address, ChatColor.translateAlternateColorCodes('&', motd), restricted);
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);
2016-12-20 00:31:01 +01:00
this.motd = motd;
this.restricted = restricted;
this.hidden = hidden;
2016-12-05 04:21:04 +01:00
}
@Override
public Client getSubDataClient() {
return client;
}
@Override
public void linkSubDataClient(Client client) {
if (this.client == null) {
client.setHandler(this);
this.client = client;
} else if (client == null) {
this.client = null;
} else throw new IllegalStateException("A SubData Client is already linked to Server: " + getName());
}
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)) {
this.nick = null;
} else {
this.nick = value;
}
2017-01-26 23:19:48 +01:00
}
2016-12-20 00:31:01 +01:00
/**
* 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();
2016-12-20 00:31:01 +01:00
this.hidden = value;
}
/**
* Gets the MOTD of the Server
*
* @return Server MOTD
*/
@Override
public String getMotd() {
return motd;
}
/**
* Sets the MOTD of the Server
*
* @param value Value
*/
public void setMotd(String value) {
if (Util.isNull(value)) throw new NullPointerException();
2016-12-20 00:31:01 +01:00
this.motd = value;
}
/**
* Gets if the Server is Restricted
*
* @return Restricted Status
*/
@Override
public boolean isRestricted() {
return restricted;
}
/**
* Sets if the Server is Restricted
*
* @param value Value
*/
public void setRestricted(boolean value) {
if (Util.isNull(value)) throw new NullPointerException();
2016-12-20 00:31:01 +01:00
this.restricted = value;
}
2017-01-07 20:06:54 +01:00
2017-01-08 03:30:03 +01:00
@Override
public void addExtra(String handle, Object value) {
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) {
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) {
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) {
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
}
2016-12-05 04:21:04 +01:00
}