2016-12-24 05:55:17 +01:00
|
|
|
package net.ME1312.SubServers.Bungee.Host.Internal;
|
2016-12-05 04:21:04 +01:00
|
|
|
|
2016-12-24 05:55:17 +01:00
|
|
|
import net.ME1312.SubServers.Bungee.Event.*;
|
|
|
|
import net.ME1312.SubServers.Bungee.Host.Executable;
|
|
|
|
import net.ME1312.SubServers.Bungee.Library.Container;
|
|
|
|
import net.ME1312.SubServers.Bungee.Library.Exception.InvalidServerException;
|
|
|
|
import net.ME1312.SubServers.Bungee.Host.Host;
|
|
|
|
import net.ME1312.SubServers.Bungee.Host.SubServer;
|
|
|
|
import net.ME1312.SubServers.Bungee.Library.NamedContainer;
|
2016-12-05 04:21:04 +01:00
|
|
|
|
|
|
|
import java.io.BufferedWriter;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.OutputStreamWriter;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
public class InternalSubServer extends SubServer {
|
|
|
|
private InternalHost host;
|
|
|
|
private boolean enabled;
|
|
|
|
private Container<Boolean> log;
|
2017-01-01 20:34:46 +01:00
|
|
|
private String dir;
|
2016-12-05 04:21:04 +01:00
|
|
|
private File directory;
|
|
|
|
private Executable executable;
|
|
|
|
private String stopcmd;
|
|
|
|
private Process process;
|
2016-12-20 00:31:01 +01:00
|
|
|
private Thread thread;
|
2016-12-15 22:04:39 +01:00
|
|
|
private BufferedWriter command;
|
2016-12-05 04:21:04 +01:00
|
|
|
private boolean restart;
|
2016-12-15 22:04:39 +01:00
|
|
|
private boolean allowrestart;
|
2016-12-05 04:21:04 +01:00
|
|
|
private boolean temporary;
|
|
|
|
|
2016-12-19 01:38:02 +01:00
|
|
|
public InternalSubServer(Host host, String name, boolean enabled, int port, String motd, boolean log, String directory, Executable executable, String stopcmd, boolean start, boolean restart, boolean hidden, boolean restricted, boolean temporary) throws InvalidServerException {
|
|
|
|
super(host, name, port, motd, hidden, restricted);
|
2016-12-05 04:21:04 +01:00
|
|
|
this.host = (InternalHost) host;
|
|
|
|
this.enabled = enabled;
|
|
|
|
this.log = new Container<Boolean>(log);
|
2017-01-01 20:34:46 +01:00
|
|
|
this.dir = directory;
|
2016-12-15 22:04:39 +01:00
|
|
|
this.directory = new File(host.getDirectory(), directory);
|
2016-12-05 04:21:04 +01:00
|
|
|
this.executable = executable;
|
|
|
|
this.stopcmd = stopcmd;
|
|
|
|
this.process = null;
|
2016-12-20 00:31:01 +01:00
|
|
|
this.thread = null;
|
2016-12-15 22:04:39 +01:00
|
|
|
this.command = null;
|
2016-12-05 04:21:04 +01:00
|
|
|
this.restart = restart;
|
|
|
|
this.temporary = temporary;
|
|
|
|
|
|
|
|
if (start || temporary) start();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void run() {
|
2016-12-20 00:31:01 +01:00
|
|
|
(thread = new Thread(() -> {
|
2016-12-15 22:04:39 +01:00
|
|
|
allowrestart = true;
|
|
|
|
try {
|
|
|
|
process = Runtime.getRuntime().exec(executable.toString(), null, directory);
|
2016-12-19 01:38:02 +01:00
|
|
|
System.out.println("SubServers > Now starting " + getName());
|
2016-12-29 08:29:22 +01:00
|
|
|
final InternalSubLogger read = new InternalSubLogger(process, getName(), log, null);
|
2016-12-15 22:04:39 +01:00
|
|
|
read.start();
|
|
|
|
command = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
|
|
|
|
|
2016-12-05 04:21:04 +01:00
|
|
|
try {
|
2016-12-15 22:04:39 +01:00
|
|
|
process.waitFor();
|
|
|
|
} catch (InterruptedException e) {
|
2016-12-05 04:21:04 +01:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2016-12-15 22:04:39 +01:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
allowrestart = false;
|
|
|
|
}
|
2016-12-05 04:21:04 +01:00
|
|
|
|
2016-12-19 01:38:02 +01:00
|
|
|
SubStoppedEvent event = new SubStoppedEvent(this);
|
2016-12-15 22:04:39 +01:00
|
|
|
host.plugin.getPluginManager().callEvent(event);
|
2016-12-19 01:38:02 +01:00
|
|
|
System.out.println("SubServers > " + getName() + " has stopped");
|
2016-12-15 22:04:39 +01:00
|
|
|
process = null;
|
|
|
|
command = null;
|
2016-12-05 04:21:04 +01:00
|
|
|
|
2016-12-15 22:04:39 +01:00
|
|
|
if (temporary) {
|
|
|
|
try {
|
2016-12-19 01:38:02 +01:00
|
|
|
host.removeSubServer(getName());
|
2016-12-15 22:04:39 +01:00
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (restart && allowrestart) {
|
2016-12-05 04:21:04 +01:00
|
|
|
try {
|
2016-12-15 22:04:39 +01:00
|
|
|
Thread.sleep(2500);
|
|
|
|
start();
|
2016-12-05 04:21:04 +01:00
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-20 00:31:01 +01:00
|
|
|
})).start();
|
2016-12-05 04:21:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-12-19 01:38:02 +01:00
|
|
|
public boolean start(UUID player) {
|
2016-12-05 04:21:04 +01:00
|
|
|
if (enabled && !isRunning()) {
|
2016-12-15 22:04:39 +01:00
|
|
|
SubStartEvent event = new SubStartEvent(player, this);
|
2016-12-05 04:21:04 +01:00
|
|
|
host.plugin.getPluginManager().callEvent(event);
|
|
|
|
if (!event.isCancelled()) {
|
|
|
|
run();
|
2016-12-19 01:38:02 +01:00
|
|
|
return true;
|
|
|
|
} else return false;
|
|
|
|
} else return false;
|
2016-12-05 04:21:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-12-19 01:38:02 +01:00
|
|
|
public boolean stop(UUID player) {
|
2016-12-05 04:21:04 +01:00
|
|
|
if (isRunning()) {
|
2016-12-15 22:04:39 +01:00
|
|
|
SubStopEvent event = new SubStopEvent(player, this, false);
|
2016-12-05 04:21:04 +01:00
|
|
|
host.plugin.getPluginManager().callEvent(event);
|
|
|
|
if (!event.isCancelled()) {
|
2016-12-15 22:04:39 +01:00
|
|
|
try {
|
2016-12-19 01:38:02 +01:00
|
|
|
allowrestart = false;
|
2016-12-15 22:04:39 +01:00
|
|
|
command.write(stopcmd);
|
|
|
|
command.newLine();
|
|
|
|
command.flush();
|
2016-12-19 01:38:02 +01:00
|
|
|
return true;
|
2016-12-15 22:04:39 +01:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
2016-12-19 01:38:02 +01:00
|
|
|
return false;
|
2016-12-15 22:04:39 +01:00
|
|
|
}
|
2016-12-19 01:38:02 +01:00
|
|
|
} else return false;
|
|
|
|
} else return false;
|
2016-12-05 04:21:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-12-19 01:38:02 +01:00
|
|
|
public boolean terminate(UUID player) {
|
2016-12-05 04:21:04 +01:00
|
|
|
if (isRunning()) {
|
2016-12-15 22:04:39 +01:00
|
|
|
SubStopEvent event = new SubStopEvent(player, this, true);
|
2016-12-05 04:21:04 +01:00
|
|
|
host.plugin.getPluginManager().callEvent(event);
|
|
|
|
if (!event.isCancelled()) {
|
2016-12-19 01:38:02 +01:00
|
|
|
allowrestart = false;
|
2016-12-05 04:21:04 +01:00
|
|
|
process.destroyForcibly();
|
2016-12-19 01:38:02 +01:00
|
|
|
return true;
|
|
|
|
} else return false;
|
|
|
|
} else return false;
|
2016-12-05 04:21:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-12-19 01:38:02 +01:00
|
|
|
public boolean command(UUID player, String command) {
|
2016-12-05 04:21:04 +01:00
|
|
|
if (isRunning()) {
|
2016-12-15 22:04:39 +01:00
|
|
|
SubSendCommandEvent event = new SubSendCommandEvent(player, this, command);
|
2016-12-05 04:21:04 +01:00
|
|
|
host.plugin.getPluginManager().callEvent(event);
|
|
|
|
if (!event.isCancelled()) {
|
2016-12-15 22:04:39 +01:00
|
|
|
try {
|
2016-12-19 01:38:02 +01:00
|
|
|
if (event.getCommand().equalsIgnoreCase(stopcmd)) allowrestart = false;
|
2016-12-15 22:04:39 +01:00
|
|
|
this.command.write(event.getCommand());
|
|
|
|
this.command.newLine();
|
|
|
|
this.command.flush();
|
2016-12-19 01:38:02 +01:00
|
|
|
return true;
|
2016-12-15 22:04:39 +01:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
2016-12-19 01:38:02 +01:00
|
|
|
return false;
|
2016-12-15 22:04:39 +01:00
|
|
|
}
|
2016-12-19 01:38:02 +01:00
|
|
|
} else return false;
|
|
|
|
} else return false;
|
2016-12-15 22:04:39 +01:00
|
|
|
}
|
|
|
|
|
2016-12-05 04:21:04 +01:00
|
|
|
@Override
|
|
|
|
public void waitFor() throws InterruptedException {
|
2016-12-20 00:31:01 +01:00
|
|
|
while (thread != null && thread.isAlive()) {
|
|
|
|
Thread.sleep(250);
|
2016-12-05 04:21:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isRunning() {
|
|
|
|
return process != null && process.isAlive();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Host getHost() {
|
|
|
|
return host;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isEnabled() {
|
|
|
|
return enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setEnabled(boolean value) {
|
|
|
|
enabled = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isLogging() {
|
|
|
|
return log.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setLogging(boolean value) {
|
|
|
|
log.set(value);
|
|
|
|
}
|
|
|
|
|
2017-01-01 20:34:46 +01:00
|
|
|
@Override
|
|
|
|
public String getDirectory() {
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
2016-12-05 04:21:04 +01:00
|
|
|
@Override
|
|
|
|
public String getStopCommand() {
|
|
|
|
return stopcmd;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setStopCommand(String value) {
|
|
|
|
stopcmd = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean willAutoRestart() {
|
|
|
|
return restart;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setAutoRestart(boolean value) {
|
|
|
|
restart = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isTemporary() {
|
|
|
|
return temporary;
|
|
|
|
}
|
|
|
|
}
|