2016-12-24 05:55:17 +01:00
|
|
|
package net.ME1312.SubServers.Bungee.Host;
|
2016-12-05 04:21:04 +01:00
|
|
|
|
2018-04-15 03:53:51 +02:00
|
|
|
import com.google.gson.Gson;
|
2017-01-08 03:30:03 +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.InvalidHostException;
|
|
|
|
import net.ME1312.SubServers.Bungee.Library.Exception.InvalidServerException;
|
2017-01-08 03:30:03 +01:00
|
|
|
import net.ME1312.SubServers.Bungee.Library.ExtraDataHandler;
|
2017-01-30 21:22:36 +01:00
|
|
|
import net.ME1312.SubServers.Bungee.Library.Util;
|
2017-07-19 01:28:59 +02:00
|
|
|
import net.ME1312.SubServers.Bungee.Network.ClientHandler;
|
2017-08-22 15:02:23 +02:00
|
|
|
import net.ME1312.SubServers.Bungee.Network.SubDataServer;
|
2016-12-24 05:55:17 +01:00
|
|
|
import net.ME1312.SubServers.Bungee.SubPlugin;
|
2016-12-05 04:21:04 +01:00
|
|
|
|
|
|
|
import java.net.InetAddress;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Host Layout Class
|
|
|
|
*/
|
2017-01-08 03:30:03 +01:00
|
|
|
public abstract class Host implements ExtraDataHandler {
|
|
|
|
private YAMLSection extra = new YAMLSection();
|
2017-12-10 01:30:06 +01:00
|
|
|
private final String signature;
|
2017-01-26 23:19:48 +01:00
|
|
|
private String nick = null;
|
2016-12-05 04:21:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This constructor is required to launch your host from the drivers list. Do not add or remove any arguments.
|
|
|
|
*
|
|
|
|
* @param plugin SubServers Internals
|
2016-12-15 22:04:39 +01:00
|
|
|
* @param name The Name of your Host
|
|
|
|
* @param enabled If your host is Enabled
|
|
|
|
* @param address The address of your Host
|
|
|
|
* @param directory The runtime directory of your Host
|
|
|
|
* @param gitBash The Git Bash directory
|
|
|
|
*/
|
|
|
|
public Host(SubPlugin plugin, String name, Boolean enabled, InetAddress address, String directory, String gitBash) {
|
|
|
|
if (name.contains(" ")) throw new InvalidHostException("Host names cannot have spaces: " + name);
|
2017-12-13 18:29:50 +01:00
|
|
|
signature = plugin.api.signAnonymousObject();
|
2017-12-07 18:22:36 +01:00
|
|
|
SubDataServer.allowConnection(address.getHostAddress());
|
2016-12-15 22:04:39 +01:00
|
|
|
}
|
2016-12-05 04:21:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Is this Host Enabled?
|
|
|
|
*
|
|
|
|
* @return Enabled Status
|
|
|
|
*/
|
|
|
|
public abstract boolean isEnabled();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set if this Host is Enabled
|
|
|
|
*
|
|
|
|
* @param value Value
|
|
|
|
*/
|
|
|
|
public abstract void setEnabled(boolean value);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Address of this Host
|
|
|
|
*
|
|
|
|
* @return Host Address
|
|
|
|
*/
|
|
|
|
public abstract InetAddress getAddress();
|
|
|
|
|
2016-12-15 22:04:39 +01:00
|
|
|
/**
|
2017-04-24 18:28:16 +02:00
|
|
|
* Get the host Directory Path
|
2016-12-15 22:04:39 +01:00
|
|
|
*
|
2017-04-24 18:28:16 +02:00
|
|
|
* @return Host Directory Path
|
2016-12-15 22:04:39 +01:00
|
|
|
*/
|
2017-04-24 18:28:16 +02:00
|
|
|
public abstract String getPath();
|
2016-12-15 22:04:39 +01:00
|
|
|
|
2016-12-05 04:21:04 +01:00
|
|
|
/**
|
|
|
|
* Get the Name of this Host
|
|
|
|
*
|
|
|
|
* @return Host Name
|
|
|
|
*/
|
|
|
|
public abstract String getName();
|
|
|
|
|
2017-01-26 23:19:48 +01:00
|
|
|
/**
|
|
|
|
* Get the Display Name of this Host
|
|
|
|
*
|
|
|
|
* @return Display Name
|
|
|
|
*/
|
|
|
|
public String getDisplayName() {
|
|
|
|
return (nick == null)?getName():nick;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the Display Name for this Host
|
|
|
|
*
|
|
|
|
* @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-05 04:21:04 +01:00
|
|
|
/**
|
|
|
|
* Starts the Servers Specified
|
|
|
|
*
|
|
|
|
* @param servers Servers
|
2016-12-20 00:31:01 +01:00
|
|
|
* @return Success Status
|
2016-12-05 04:21:04 +01:00
|
|
|
*/
|
2016-12-19 01:38:02 +01:00
|
|
|
public int start(String... servers) {
|
|
|
|
return start(null, servers);
|
2016-12-05 04:21:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts the Servers Specified
|
|
|
|
*
|
|
|
|
* @param player Player who started
|
|
|
|
* @param servers Servers
|
2016-12-20 00:31:01 +01:00
|
|
|
* @return Success Status
|
2016-12-05 04:21:04 +01:00
|
|
|
*/
|
2017-01-30 21:22:36 +01:00
|
|
|
public int start(UUID player, String... servers) {
|
|
|
|
int i = 0;
|
|
|
|
for (String server : servers) {
|
|
|
|
if (getSubServer(server.toLowerCase()).start(player)) i++;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
2016-12-05 04:21:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops the Servers Specified
|
|
|
|
*
|
|
|
|
* @param servers Servers
|
2016-12-20 00:31:01 +01:00
|
|
|
* @return Success Status
|
2016-12-05 04:21:04 +01:00
|
|
|
*/
|
2016-12-19 01:38:02 +01:00
|
|
|
public int stop(String... servers) {
|
|
|
|
return stop(null, servers);
|
2016-12-05 04:21:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops the Servers Specified
|
|
|
|
*
|
|
|
|
* @param player Player who started
|
|
|
|
* @param servers Servers
|
2016-12-20 00:31:01 +01:00
|
|
|
* @return Success Status
|
2016-12-05 04:21:04 +01:00
|
|
|
*/
|
2017-01-30 21:22:36 +01:00
|
|
|
public int stop(UUID player, String... servers) {
|
|
|
|
int i = 0;
|
|
|
|
for (String server : servers) {
|
|
|
|
if (getSubServer(server.toLowerCase()).stop(player)) i++;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
2016-12-05 04:21:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Terminates the Servers Specified
|
|
|
|
*
|
|
|
|
* @param servers Servers
|
2016-12-20 00:31:01 +01:00
|
|
|
* @return Success Status
|
2016-12-05 04:21:04 +01:00
|
|
|
*/
|
2016-12-19 01:38:02 +01:00
|
|
|
public int terminate(String... servers) {
|
|
|
|
return terminate(null, servers);
|
2016-12-05 04:21:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Terminates the Servers Specified
|
|
|
|
*
|
|
|
|
* @param player Player who started
|
|
|
|
* @param servers Servers
|
2016-12-20 00:31:01 +01:00
|
|
|
* @return Success Status
|
2016-12-05 04:21:04 +01:00
|
|
|
*/
|
2017-01-30 21:22:36 +01:00
|
|
|
public int terminate(UUID player, String... servers) {
|
|
|
|
int i = 0;
|
|
|
|
for (String server : servers) {
|
|
|
|
if (getSubServer(server.toLowerCase()).terminate(player)) i++;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
2016-12-05 04:21:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Commands the Servers Specified
|
|
|
|
*
|
|
|
|
* @param command Command to send
|
|
|
|
* @param servers Servers
|
2016-12-20 00:31:01 +01:00
|
|
|
* @return Success Status
|
2016-12-05 04:21:04 +01:00
|
|
|
*/
|
2016-12-19 01:38:02 +01:00
|
|
|
public int command(String command, String... servers) {
|
|
|
|
return command(null, command, servers);
|
2016-12-05 04:21:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Commands the Servers Specified
|
|
|
|
*
|
|
|
|
* @param player Player who started
|
|
|
|
* @param command Command to send
|
|
|
|
* @param servers Servers
|
2016-12-20 00:31:01 +01:00
|
|
|
* @return Success Status
|
2016-12-05 04:21:04 +01:00
|
|
|
*/
|
2017-01-30 21:22:36 +01:00
|
|
|
public int command(UUID player, String command, String... servers) {
|
|
|
|
int i = 0;
|
|
|
|
for (String server : servers) {
|
|
|
|
if (getSubServer(server.toLowerCase()).command(player, command)) i++;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
2016-12-05 04:21:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the SubCreator Instance for this Host
|
|
|
|
*
|
|
|
|
* @return SubCreator
|
|
|
|
*/
|
|
|
|
public abstract SubCreator getCreator();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the SubServers on this Host
|
|
|
|
*
|
|
|
|
* @return SubServer Map
|
|
|
|
*/
|
|
|
|
public abstract Map<String, ? extends SubServer> getSubServers();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a SubServer
|
|
|
|
*
|
|
|
|
* @param name SubServer Name
|
|
|
|
* @return a SubServer
|
|
|
|
*/
|
|
|
|
public abstract SubServer getSubServer(String name);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a SubServer
|
|
|
|
*
|
|
|
|
* @param player Player who Added
|
|
|
|
* @param name Name of Server
|
|
|
|
* @param enabled Enabled Status
|
|
|
|
* @param port Port Number
|
|
|
|
* @param motd Motd of the Server
|
|
|
|
* @param log Logging Status
|
|
|
|
* @param directory Directory
|
|
|
|
* @param executable Executable
|
|
|
|
* @param stopcmd Command to Stop the Server
|
2016-12-19 01:38:02 +01:00
|
|
|
* @param hidden if the server should be hidden from players
|
2016-12-15 22:04:39 +01:00
|
|
|
* @param restricted Players will need a permission to join if true
|
2016-12-05 04:21:04 +01:00
|
|
|
* @param temporary Temporary Status
|
|
|
|
* @return The SubServer
|
|
|
|
* @throws InvalidServerException
|
|
|
|
*/
|
2018-01-29 07:04:36 +01:00
|
|
|
public abstract SubServer addSubServer(UUID player, String name, boolean enabled, int port, String motd, boolean log, String directory, Executable executable, String stopcmd, boolean hidden, boolean restricted, boolean temporary) throws InvalidServerException;
|
2016-12-05 04:21:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a SubServer
|
|
|
|
*
|
|
|
|
* @param name Name of Server
|
|
|
|
* @param enabled Enabled Status
|
|
|
|
* @param port Port Number
|
|
|
|
* @param motd Motd of the Server
|
|
|
|
* @param log Logging Status
|
|
|
|
* @param directory Directory
|
|
|
|
* @param executable Executable
|
|
|
|
* @param stopcmd Command to Stop the Server
|
2016-12-19 01:38:02 +01:00
|
|
|
* @param hidden if the server should be hidden from players
|
2016-12-15 22:04:39 +01:00
|
|
|
* @param restricted Players will need a permission to join if true
|
2016-12-05 04:21:04 +01:00
|
|
|
* @param temporary Temporary Status
|
|
|
|
* @return The SubServer
|
|
|
|
* @throws InvalidServerException
|
|
|
|
*/
|
2018-01-29 07:04:36 +01:00
|
|
|
public SubServer addSubServer(String name, boolean enabled, int port, String motd, boolean log, String directory, Executable executable, String stopcmd, boolean hidden, boolean restricted, boolean temporary) throws InvalidServerException {
|
|
|
|
return addSubServer(null, name, enabled, port, motd, log, directory, executable, stopcmd, hidden, restricted, temporary);
|
2016-12-05 04:21:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a SubServer
|
|
|
|
*
|
|
|
|
* @param name SubServer Name
|
|
|
|
* @throws InterruptedException
|
2016-12-20 00:31:01 +01:00
|
|
|
* @return Success Status
|
|
|
|
*/
|
|
|
|
public boolean removeSubServer(String name) throws InterruptedException {
|
|
|
|
return removeSubServer(null, name);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a SubServer
|
|
|
|
*
|
|
|
|
* @param player Player Removing
|
|
|
|
* @param name SubServer Name
|
|
|
|
* @throws InterruptedException
|
|
|
|
* @return Success Status
|
|
|
|
*/
|
|
|
|
public abstract boolean removeSubServer(UUID player, String name) throws InterruptedException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Forces the Removal of a SubServer
|
|
|
|
*
|
|
|
|
* @param name SubServer Name
|
2016-12-05 04:21:04 +01:00
|
|
|
*/
|
2017-04-01 18:50:09 +02:00
|
|
|
public boolean forceRemoveSubServer(String name) throws InterruptedException {
|
2016-12-20 00:31:01 +01:00
|
|
|
return forceRemoveSubServer(null, name);
|
|
|
|
}
|
2016-12-05 04:21:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Forces the Removal of a SubServer
|
|
|
|
*
|
2016-12-20 00:31:01 +01:00
|
|
|
* @param player Player Removing
|
2016-12-05 04:21:04 +01:00
|
|
|
* @param name SubServer Name
|
|
|
|
*/
|
2017-04-01 18:50:09 +02:00
|
|
|
public abstract boolean forceRemoveSubServer(UUID player, String name) throws InterruptedException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a SubServer
|
|
|
|
*
|
|
|
|
* @param name SubServer Name
|
|
|
|
* @return Success Status
|
|
|
|
*/
|
|
|
|
public boolean deleteSubServer(String name) throws InterruptedException {
|
|
|
|
return deleteSubServer(null, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a SubServer
|
|
|
|
*
|
|
|
|
* @param player Player Deleting
|
|
|
|
* @param name SubServer Name
|
|
|
|
* @return Success Status
|
|
|
|
*/
|
|
|
|
public abstract boolean deleteSubServer(UUID player, String name) throws InterruptedException;
|
2016-12-05 04:21:04 +01:00
|
|
|
|
2017-04-24 18:28:16 +02:00
|
|
|
/**
|
|
|
|
* Forced the Deletion of a SubServer
|
|
|
|
*
|
|
|
|
* @param name SubServer Name
|
|
|
|
* @return Success Status
|
|
|
|
*/
|
|
|
|
public boolean forceDeleteSubServer(String name) throws InterruptedException {
|
|
|
|
return deleteSubServer(null, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Forces the Deletion of a SubServer
|
|
|
|
*
|
|
|
|
* @param player Player Deleting
|
|
|
|
* @param name SubServer Name
|
|
|
|
* @return Success Status
|
|
|
|
*/
|
|
|
|
public abstract boolean forceDeleteSubServer(UUID player, String name) throws InterruptedException;
|
|
|
|
|
2017-12-10 01:30:06 +01:00
|
|
|
/**
|
|
|
|
* Get the Signature of this Object
|
|
|
|
*
|
|
|
|
* @return Object Signature
|
|
|
|
*/
|
|
|
|
public final String getSignature() {
|
|
|
|
return signature;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
|
|
|
}
|
|
|
|
|
|
|
|
@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-07-19 01:28:59 +02:00
|
|
|
|
|
|
|
@Override
|
2018-04-15 03:53:51 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
2017-07-19 01:28:59 +02:00
|
|
|
public String toString() {
|
2018-04-15 03:53:51 +02:00
|
|
|
YAMLSection hinfo = new YAMLSection();
|
|
|
|
hinfo.set("type", "Host");
|
|
|
|
hinfo.set("name", getName());
|
|
|
|
hinfo.set("display", getDisplayName());
|
|
|
|
hinfo.set("enabled", isEnabled());
|
|
|
|
hinfo.set("address", getAddress().getHostAddress());
|
|
|
|
hinfo.set("dir", getPath());
|
|
|
|
|
|
|
|
YAMLSection cinfo = new YAMLSection();
|
|
|
|
YAMLSection templates = new YAMLSection();
|
2018-01-21 21:45:27 +01:00
|
|
|
for (SubCreator.ServerTemplate template : getCreator().getTemplates().values())
|
2018-04-15 03:53:51 +02:00
|
|
|
templates.set(template.getName(), new YAMLSection(new Gson().fromJson(template.toString(), Map.class)));
|
|
|
|
cinfo.set("templates", templates);
|
|
|
|
hinfo.set("creator", cinfo);
|
2017-11-22 22:58:33 +01:00
|
|
|
|
2018-04-15 03:53:51 +02:00
|
|
|
YAMLSection servers = new YAMLSection();
|
2017-07-19 01:28:59 +02:00
|
|
|
for (SubServer server : getSubServers().values()) {
|
2018-04-15 03:53:51 +02:00
|
|
|
servers.set(server.getName(), new YAMLSection(new Gson().fromJson(server.toString(), Map.class)));
|
2017-07-19 01:28:59 +02:00
|
|
|
}
|
2018-04-15 03:53:51 +02:00
|
|
|
hinfo.set("servers", servers);
|
|
|
|
if (this instanceof ClientHandler && ((ClientHandler) this).getSubData() != null) hinfo.set("subdata", ((ClientHandler) this).getSubData().getAddress().toString());
|
|
|
|
hinfo.set("signature", signature);
|
2018-07-01 05:46:58 +02:00
|
|
|
hinfo.set("extra", getExtra());
|
2018-04-15 03:53:51 +02:00
|
|
|
return hinfo.toJSON();
|
2017-07-19 01:28:59 +02:00
|
|
|
}
|
2016-12-05 04:21:04 +01:00
|
|
|
}
|