mirror of
https://github.com/ME1312/SubServers-2.git
synced 2025-01-25 09:01:23 +01:00
Add a post-start event
This commit is contained in:
parent
ecdc25b520
commit
30dd28547f
@ -0,0 +1,34 @@
|
||||
package net.ME1312.SubServers.Bungee.Event;
|
||||
|
||||
import net.ME1312.Galaxi.Library.Util;
|
||||
import net.ME1312.SubServers.Bungee.Host.SubServer;
|
||||
import net.ME1312.SubServers.Bungee.Library.SubEvent;
|
||||
import net.md_5.bungee.api.plugin.Cancellable;
|
||||
import net.md_5.bungee.api.plugin.Event;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Server Started Event
|
||||
*/
|
||||
public class SubStartedEvent extends Event implements SubEvent {
|
||||
private SubServer server;
|
||||
|
||||
/**
|
||||
* Server Started Event<br>
|
||||
* <b>This event can only be called when a SubData connection is made!</b>
|
||||
*
|
||||
* @param server Server Starting
|
||||
*/
|
||||
public SubStartedEvent(SubServer server) {
|
||||
if (Util.isNull(server)) throw new NullPointerException();
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the Server Effected
|
||||
*
|
||||
* @return The Server Effected
|
||||
*/
|
||||
public SubServer getServer() { return server; }
|
||||
}
|
@ -195,7 +195,7 @@ public class ExternalSubCreator extends SubCreator {
|
||||
|
||||
String name = server.getName();
|
||||
String prefix = name + File.separator + "Updater";
|
||||
Util.isException(() -> Util.reflect(SubServerContainer.class.getDeclaredField("lock"), server, true));
|
||||
Util.isException(() -> Util.reflect(SubServerContainer.class.getDeclaredField("updating"), server, true));
|
||||
ExternalSubLogger logger = new ExternalSubLogger(this, prefix, log, null);
|
||||
thread.put(name.toLowerCase(), new NamedContainer<>(server.getAddress().getPort(), logger));
|
||||
|
||||
@ -204,7 +204,7 @@ public class ExternalSubCreator extends SubCreator {
|
||||
if (!event.isCancelled()) {
|
||||
logger.start();
|
||||
host.queue(new PacketExCreateServer(server, version, logger.getExternalAddress(), data -> {
|
||||
Util.isException(() -> Util.reflect(SubServerContainer.class.getDeclaredField("lock"), server, false));
|
||||
Util.isException(() -> Util.reflect(SubServerContainer.class.getDeclaredField("updating"), server, false));
|
||||
try {
|
||||
if (data.getInt(0x0001) == 0) {
|
||||
Logger.get(prefix).info("Saving...");
|
||||
|
@ -79,6 +79,7 @@ public class ExternalSubServer extends SubServerContainer {
|
||||
lock = false;
|
||||
if (!event.isCancelled()) {
|
||||
Logger.get("SubServers").info("Now starting " + getName());
|
||||
started = false;
|
||||
running = true;
|
||||
logger.start();
|
||||
host.queue(new PacketExEditServer(this, PacketExEditServer.UpdateType.START, logger.getExternalAddress().toString()));
|
||||
@ -105,12 +106,12 @@ public class ExternalSubServer extends SubServerContainer {
|
||||
} else return false;
|
||||
}
|
||||
private void stopped(Boolean allowrestart) {
|
||||
SubStoppedEvent event = new SubStoppedEvent(this);
|
||||
host.plugin.getPluginManager().callEvent(event);
|
||||
Logger.get("SubServers").info(getName() + " has stopped");
|
||||
logger.stop();
|
||||
history.clear();
|
||||
running = false;
|
||||
SubStoppedEvent event = new SubStoppedEvent(this);
|
||||
host.plugin.getPluginManager().callEvent(event);
|
||||
Logger.get("SubServers").info(getName() + " has stopped");
|
||||
|
||||
if (stopaction == StopAction.REMOVE_SERVER || stopaction == StopAction.RECYCLE_SERVER || stopaction == StopAction.DELETE_SERVER) {
|
||||
try {
|
||||
|
@ -380,10 +380,10 @@ public class InternalSubCreator extends SubCreator {
|
||||
if (host.isAvailable() && host.isEnabled() && host == server.getHost() && server.isAvailable() && !server.isRunning() && server.getTemplate() != null && server.getTemplate().isEnabled() && server.getTemplate().canUpdate() && (version != null || !server.getTemplate().requiresVersion())) {
|
||||
StackTraceElement[] origin = new Exception().getStackTrace();
|
||||
|
||||
Util.isException(() -> Util.reflect(SubServerContainer.class.getDeclaredField("lock"), server, true));
|
||||
Util.isException(() -> Util.reflect(SubServerContainer.class.getDeclaredField("updating"), server, true));
|
||||
|
||||
CreatorTask task = new CreatorTask(player, server, version, x -> {
|
||||
Util.isException(() -> Util.reflect(SubServerContainer.class.getDeclaredField("lock"), server, false));
|
||||
Util.isException(() -> Util.reflect(SubServerContainer.class.getDeclaredField("updating"), server, false));
|
||||
if (callback != null && x != null) try {
|
||||
callback.run(x);
|
||||
} catch (Throwable e) {
|
||||
|
@ -119,6 +119,7 @@ public class InternalSubServer extends SubServerContainer {
|
||||
|
||||
private void run() {
|
||||
allowrestart = true;
|
||||
started = false;
|
||||
try {
|
||||
ProcessBuilder pb = new ProcessBuilder().command(Executable.parse(host.getCreator().getBashDirectory(), executable)).directory(directory);
|
||||
pb.environment().put("name", getName());
|
||||
@ -142,13 +143,14 @@ public class InternalSubServer extends SubServerContainer {
|
||||
allowrestart = false;
|
||||
}
|
||||
|
||||
SubStoppedEvent event = new SubStoppedEvent(this);
|
||||
host.plugin.getPluginManager().callEvent(event);
|
||||
Logger.get("SubServers").info(getName() + " has stopped");
|
||||
process = null;
|
||||
command = null;
|
||||
history.clear();
|
||||
|
||||
SubStoppedEvent event = new SubStoppedEvent(this);
|
||||
host.plugin.getPluginManager().callEvent(event);
|
||||
|
||||
if (stopaction == StopAction.REMOVE_SERVER || stopaction == StopAction.RECYCLE_SERVER || stopaction == StopAction.DELETE_SERVER) {
|
||||
try {
|
||||
if (stopaction == StopAction.RECYCLE_SERVER) {
|
||||
|
@ -16,7 +16,8 @@ import java.util.*;
|
||||
public abstract class SubServerContainer extends ServerContainer implements SubServer {
|
||||
private List<NamedContainer<String, String>> incompatibilities = new ArrayList<NamedContainer<String, String>>();
|
||||
private String template = null;
|
||||
private boolean lock;
|
||||
protected boolean started;
|
||||
private boolean updating;
|
||||
|
||||
/**
|
||||
* Creates a SubServer
|
||||
@ -59,7 +60,7 @@ public abstract class SubServerContainer extends ServerContainer implements SubS
|
||||
|
||||
@Override
|
||||
public boolean isAvailable() {
|
||||
return !lock && getHost().isAvailable();
|
||||
return !updating && getHost().isAvailable();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -25,12 +25,18 @@ public abstract class SubServerController {
|
||||
control = new SubServerContainer(host, name, port, motd, hidden, restricted) {
|
||||
@Override
|
||||
public boolean start() {
|
||||
return SubServerController.this.start();
|
||||
if (SubServerController.this.start()) {
|
||||
started = false;
|
||||
return true;
|
||||
} else return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean start(UUID player) {
|
||||
return SubServerController.this.start(player);
|
||||
if (SubServerController.this.start(player)) {
|
||||
started = false;
|
||||
return true;
|
||||
} else return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -3,6 +3,7 @@ package net.ME1312.SubServers.Bungee.Network.Packet;
|
||||
import net.ME1312.SubData.Server.DataClient;
|
||||
import net.ME1312.SubData.Server.Protocol.Initial.InitialPacket;
|
||||
import net.ME1312.SubData.Server.SubDataClient;
|
||||
import net.ME1312.SubServers.Bungee.Event.SubStartedEvent;
|
||||
import net.ME1312.SubServers.Bungee.Host.Server;
|
||||
import net.ME1312.SubServers.Bungee.Host.ServerContainer;
|
||||
import net.ME1312.SubServers.Bungee.Host.SubServer;
|
||||
@ -10,8 +11,10 @@ import net.ME1312.Galaxi.Library.Map.ObjectMap;
|
||||
import net.ME1312.Galaxi.Library.Util;
|
||||
import net.ME1312.SubData.Server.Protocol.PacketObjectIn;
|
||||
import net.ME1312.SubData.Server.Protocol.PacketObjectOut;
|
||||
import net.ME1312.SubServers.Bungee.Host.SubServerContainer;
|
||||
import net.ME1312.SubServers.Bungee.Library.Compatibility.Logger;
|
||||
import net.ME1312.SubServers.Bungee.SubPlugin;
|
||||
import net.md_5.bungee.api.ProxyServer;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.HashMap;
|
||||
@ -106,6 +109,11 @@ public class PacketLinkServer implements InitialPacket, PacketObjectIn<Integer>,
|
||||
Logger.get("SubServers").info("Sending shutdown signal to rogue SubServer: " + server.getName());
|
||||
client.sendPacket(new PacketOutExReset("Rogue SubServer Detected"));
|
||||
} else {
|
||||
if (server instanceof SubServer && !Util.getDespiteException(() -> Util.reflect(SubServerContainer.class.getDeclaredField("started"), server), true)) {
|
||||
Util.isException(() -> Util.reflect(SubServerContainer.class.getDeclaredField("started"), server, true));
|
||||
SubStartedEvent event = new SubStartedEvent((SubServer) server);
|
||||
ProxyServer.getInstance().getPluginManager().callEvent(event);
|
||||
}
|
||||
client.sendPacket(new PacketLinkServer(server.getName(), 0, null));
|
||||
}
|
||||
setReady(client, true);
|
||||
|
@ -114,6 +114,7 @@ public class PacketOutExRunEvent implements Listener, PacketObjectOut<Integer> {
|
||||
broadcast(new PacketOutExRunEvent(event.getClass(), args));
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(priority = Byte.MAX_VALUE)
|
||||
public void event(SubSendCommandEvent event) {
|
||||
if (!event.isCancelled()) {
|
||||
@ -147,6 +148,14 @@ public class PacketOutExRunEvent implements Listener, PacketObjectOut<Integer> {
|
||||
broadcast(new PacketOutExRunEvent(event.getClass(), args));
|
||||
}
|
||||
}
|
||||
@EventHandler(priority = Byte.MAX_VALUE)
|
||||
public void event(SubStartedEvent event) {
|
||||
ObjectMap<String> args = new ObjectMap<String>();
|
||||
args.set("server", event.getServer().getName());
|
||||
broadcast(event.getServer(), new PacketOutExRunEvent(event.getClass(), args));
|
||||
|
||||
}
|
||||
|
||||
@EventHandler(priority = Byte.MAX_VALUE)
|
||||
public void event(SubStopEvent event) {
|
||||
if (!event.isCancelled()) {
|
||||
@ -162,9 +171,10 @@ public class PacketOutExRunEvent implements Listener, PacketObjectOut<Integer> {
|
||||
public void event(SubStoppedEvent event) {
|
||||
ObjectMap<String> args = new ObjectMap<String>();
|
||||
args.set("server", event.getServer().getName());
|
||||
broadcast(new PacketOutExRunEvent(event.getClass(), args));
|
||||
broadcast(event.getServer(), new PacketOutExRunEvent(event.getClass(), args));
|
||||
|
||||
}
|
||||
|
||||
@EventHandler(priority = Byte.MAX_VALUE)
|
||||
public void event(SubRemoveServerEvent event) {
|
||||
if (!event.isCancelled()) {
|
||||
|
@ -0,0 +1,40 @@
|
||||
package net.ME1312.SubServers.Client.Bukkit.Event;
|
||||
|
||||
import net.ME1312.Galaxi.Library.Util;
|
||||
import net.ME1312.SubServers.Client.Bukkit.Library.SubEvent;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
|
||||
/**
|
||||
* Server Started Event
|
||||
*/
|
||||
public class SubStartedEvent extends Event implements SubEvent {
|
||||
private String server;
|
||||
|
||||
/**
|
||||
* Server Started Event<br>
|
||||
* <b>This event can only be called when a SubData connection is made!</b>
|
||||
*
|
||||
* @param server Server that Started
|
||||
*/
|
||||
public SubStartedEvent(String server) {
|
||||
if (Util.isNull(server)) throw new NullPointerException();
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the Server Effected
|
||||
*
|
||||
* @return The Server Effected
|
||||
*/
|
||||
public String getServer() { return server; }
|
||||
|
||||
@Override
|
||||
public HandlerList getHandlers() {
|
||||
return handlers;
|
||||
}
|
||||
public static HandlerList getHandlerList() {
|
||||
return handlers;
|
||||
}
|
||||
private static HandlerList handlers = new HandlerList();
|
||||
}
|
@ -89,6 +89,15 @@ public class PacketInExRunEvent implements PacketObjectIn<Integer> {
|
||||
}
|
||||
}
|
||||
});
|
||||
callback("SubStartedEvent", new Callback<ObjectMap<String>>() {
|
||||
@Override
|
||||
public void run(ObjectMap<String> data) {
|
||||
if (plugin.isEnabled()) {
|
||||
Bukkit.getPluginManager().callEvent(new SubStartedEvent(data.getString("server")));
|
||||
callback("SubStartedEvent", this);
|
||||
}
|
||||
}
|
||||
});
|
||||
callback("SubStopEvent", new Callback<ObjectMap<String>>() {
|
||||
@Override
|
||||
public void run(ObjectMap<String> data) {
|
||||
|
@ -0,0 +1,43 @@
|
||||
package net.ME1312.SubServers.Client.Sponge.Event;
|
||||
|
||||
import net.ME1312.Galaxi.Library.Util;
|
||||
import net.ME1312.SubServers.Client.Sponge.Library.SubEvent;
|
||||
import org.spongepowered.api.event.cause.Cause;
|
||||
import org.spongepowered.api.event.impl.AbstractEvent;
|
||||
|
||||
/**
|
||||
* Server Started Event
|
||||
*/
|
||||
public class SubStartedEvent extends AbstractEvent implements SubEvent {
|
||||
private String server;
|
||||
|
||||
/**
|
||||
* Server Started Event<br>
|
||||
* <b>This event can only be called when a SubData connection is made!</b>
|
||||
*
|
||||
* @param server Server that Started
|
||||
*/
|
||||
public SubStartedEvent(String server) {
|
||||
if (Util.isNull(server)) throw new NullPointerException();
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the Server Effected
|
||||
*
|
||||
* @return The Server Effected
|
||||
*/
|
||||
public String getServer() { return server; }
|
||||
|
||||
/**
|
||||
* Gets the cause of this Event
|
||||
*
|
||||
* @deprecated Use simplified methods where available
|
||||
* @return An empty cause list
|
||||
*/
|
||||
@Override
|
||||
@Deprecated
|
||||
public Cause getCause() {
|
||||
return Cause.builder().build(getContext());
|
||||
}
|
||||
}
|
@ -75,6 +75,13 @@ public class PacketInExRunEvent implements PacketObjectIn<Integer> {
|
||||
callback("SubStartEvent", this);
|
||||
}
|
||||
});
|
||||
callback("SubStartedEvent", new Callback<ObjectMap<String>>() {
|
||||
@Override
|
||||
public void run(ObjectMap<String> data) {
|
||||
Sponge.getEventManager().post(new SubStartedEvent(data.getString("server")));
|
||||
callback("SubStartedEvent", this);
|
||||
}
|
||||
});
|
||||
callback("SubStopEvent", new Callback<ObjectMap<String>>() {
|
||||
@Override
|
||||
public void run(ObjectMap<String> data) {
|
||||
|
@ -0,0 +1,31 @@
|
||||
package net.ME1312.SubServers.Host.Event;
|
||||
|
||||
import net.ME1312.Galaxi.Library.Event.Event;
|
||||
import net.ME1312.Galaxi.Library.Util;
|
||||
|
||||
/**
|
||||
* Server Started Event
|
||||
*/
|
||||
public class SubStartedEvent extends Event {
|
||||
private String server;
|
||||
|
||||
/**
|
||||
* Server Started Event<br>
|
||||
* <b>This event can only be called when a SubData connection is made!</b>
|
||||
*
|
||||
* @param server Server that Started
|
||||
*/
|
||||
public SubStartedEvent(String server) {
|
||||
if (Util.isNull(server)) throw new NullPointerException();
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the Server Effected
|
||||
*
|
||||
* @return The Server Effected
|
||||
*/
|
||||
public String getServer() {
|
||||
return server;
|
||||
}
|
||||
}
|
@ -73,6 +73,13 @@ public class PacketInExRunEvent implements PacketObjectIn<Integer> {
|
||||
callback("SubStartEvent", this);
|
||||
}
|
||||
});
|
||||
callback("SubStartedEvent", new Callback<ObjectMap<String>>() {
|
||||
@Override
|
||||
public void run(ObjectMap<String> data) {
|
||||
GalaxiEngine.getInstance().getPluginManager().executeEvent(new SubStartedEvent(data.getRawString("server")));
|
||||
callback("SubStartedEvent", this);
|
||||
}
|
||||
});
|
||||
callback("SubStopEvent", new Callback<ObjectMap<String>>() {
|
||||
@Override
|
||||
public void run(ObjectMap<String> data) {
|
||||
|
@ -0,0 +1,32 @@
|
||||
package net.ME1312.SubServers.Sync.Event;
|
||||
|
||||
import net.ME1312.Galaxi.Library.Util;
|
||||
import net.ME1312.SubServers.Sync.Library.SubEvent;
|
||||
import net.md_5.bungee.api.plugin.Event;
|
||||
|
||||
/**
|
||||
* Server Started Event
|
||||
*/
|
||||
public class SubStartedEvent extends Event implements SubEvent {
|
||||
private String server;
|
||||
|
||||
/**
|
||||
* Server Started Event<br>
|
||||
* <b>This event can only be called when a SubData connection is made!</b>
|
||||
*
|
||||
* @param server Server that Started
|
||||
*/
|
||||
public SubStartedEvent(String server) {
|
||||
if (Util.isNull(server)) throw new NullPointerException();
|
||||
this.server = server;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the Server Effected
|
||||
*
|
||||
* @return The Server Effected
|
||||
*/
|
||||
public String getServer() {
|
||||
return server;
|
||||
}
|
||||
}
|
@ -75,6 +75,13 @@ public class PacketInExRunEvent implements PacketObjectIn<Integer> {
|
||||
callback("SubStartEvent", this);
|
||||
}
|
||||
});
|
||||
callback("SubStartedEvent", new Callback<ObjectMap<String>>() {
|
||||
@Override
|
||||
public void run(ObjectMap<String> data) {
|
||||
ProxyServer.getInstance().getPluginManager().callEvent(new SubStartedEvent(data.getRawString("server")));
|
||||
callback("SubStartedEvent", this);
|
||||
}
|
||||
});
|
||||
callback("SubNetworkConnectEvent", new Callback<ObjectMap<String>>() {
|
||||
@Override
|
||||
public void run(ObjectMap<String> data) {
|
||||
|
Loading…
Reference in New Issue
Block a user