From 279b4a9f2d04f36227c247205339e24bbcf7ccfd Mon Sep 17 00:00:00 2001 From: NycuRO Date: Fri, 9 Aug 2019 18:48:26 +0300 Subject: [PATCH 1/3] add basic ServerStopEvent --- src/main/java/cn/nukkit/Server.java | 70 ++++++++++--------- .../nukkit/event/server/ServerStopEvent.java | 17 +++++ 2 files changed, 55 insertions(+), 32 deletions(-) create mode 100644 src/main/java/cn/nukkit/event/server/ServerStopEvent.java diff --git a/src/main/java/cn/nukkit/Server.java b/src/main/java/cn/nukkit/Server.java index 0a726b824c5..0ffb79c1f8b 100644 --- a/src/main/java/cn/nukkit/Server.java +++ b/src/main/java/cn/nukkit/Server.java @@ -18,6 +18,7 @@ import cn.nukkit.event.server.BatchPacketsEvent; import cn.nukkit.event.server.PlayerDataSerializeEvent; import cn.nukkit.event.server.QueryRegenerateEvent; +import cn.nukkit.event.server.ServerStopEvent; import cn.nukkit.inventory.CraftingManager; import cn.nukkit.inventory.Recipe; import cn.nukkit.item.Item; @@ -813,48 +814,53 @@ public void forceShutdown() { this.hasStopped = true; - if (this.rcon != null) { - this.rcon.close(); - } + ServerStopEvent serverStopEvent = new ServerStopEvent(); + getPluginManager().callEvent(serverStopEvent); + if (!serverStopEvent.isCancelled()) { - for (Player player : new ArrayList<>(this.players.values())) { - player.close(player.getLeaveMessage(), this.getConfig("settings.shutdown-message", "Server closed")); - } + if (this.rcon != null) { + this.rcon.close(); + } - this.getLogger().debug("Disabling all plugins"); - this.pluginManager.disablePlugins(); + for (Player player : new ArrayList<>(this.players.values())) { + player.close(player.getLeaveMessage(), this.getConfig("settings.shutdown-message", "Server closed")); + } - this.getLogger().debug("Removing event handlers"); - HandlerList.unregisterAll(); + this.getLogger().debug("Disabling all plugins"); + this.pluginManager.disablePlugins(); - this.getLogger().debug("Stopping all tasks"); - this.scheduler.cancelAllTasks(); - this.scheduler.mainThreadHeartbeat(Integer.MAX_VALUE); + this.getLogger().debug("Removing event handlers"); + HandlerList.unregisterAll(); - this.getLogger().debug("Unloading all levels"); - for (Level level : this.levelArray) { - this.unloadLevel(level, true); - } + this.getLogger().debug("Stopping all tasks"); + this.scheduler.cancelAllTasks(); + this.scheduler.mainThreadHeartbeat(Integer.MAX_VALUE); - this.getLogger().debug("Closing console"); - this.consoleThread.interrupt(); + this.getLogger().debug("Unloading all levels"); + for (Level level : this.levelArray) { + this.unloadLevel(level, true); + } - this.getLogger().debug("Stopping network interfaces"); - for (SourceInterface interfaz : this.network.getInterfaces()) { - interfaz.shutdown(); - this.network.unregisterInterface(interfaz); - } + this.getLogger().debug("Closing console"); + this.consoleThread.interrupt(); - if (nameLookup != null) { - nameLookup.close(); - } + this.getLogger().debug("Stopping network interfaces"); + for (SourceInterface interfaz : this.network.getInterfaces()) { + interfaz.shutdown(); + this.network.unregisterInterface(interfaz); + } - this.getLogger().debug("Disabling timings"); - Timings.stopServer(); - if (this.watchdog != null) { - this.watchdog.kill(); + if (nameLookup != null) { + nameLookup.close(); + } + + this.getLogger().debug("Disabling timings"); + Timings.stopServer(); + if (this.watchdog != null) { + this.watchdog.kill(); + } + //todo other things } - //todo other things } catch (Exception e) { log.fatal("Exception happened while shutting down, exiting the process", e); System.exit(1); diff --git a/src/main/java/cn/nukkit/event/server/ServerStopEvent.java b/src/main/java/cn/nukkit/event/server/ServerStopEvent.java new file mode 100644 index 00000000000..810bea2fb2a --- /dev/null +++ b/src/main/java/cn/nukkit/event/server/ServerStopEvent.java @@ -0,0 +1,17 @@ +package cn.nukkit.event.server; + +import cn.nukkit.event.Cancellable; +import cn.nukkit.event.HandlerList; + +/** + * author: NycuRO + * NukkitX Project + */ +public class ServerStopEvent extends ServerEvent implements Cancellable { + + private static final HandlerList handlers = new HandlerList(); + + public static HandlerList getHandlers() { + return handlers; + } +} From 442de4dd86e141f09423cc8b4a08838eeac7ce71 Mon Sep 17 00:00:00 2001 From: NycuRO Date: Mon, 6 Jan 2020 16:13:21 +0200 Subject: [PATCH 2/3] Update Server.java --- src/main/java/cn/nukkit/Server.java | 66 ++++++++++++++--------------- 1 file changed, 32 insertions(+), 34 deletions(-) diff --git a/src/main/java/cn/nukkit/Server.java b/src/main/java/cn/nukkit/Server.java index 0ffb79c1f8b..6637ae40ba3 100644 --- a/src/main/java/cn/nukkit/Server.java +++ b/src/main/java/cn/nukkit/Server.java @@ -816,51 +816,49 @@ public void forceShutdown() { ServerStopEvent serverStopEvent = new ServerStopEvent(); getPluginManager().callEvent(serverStopEvent); - if (!serverStopEvent.isCancelled()) { - if (this.rcon != null) { - this.rcon.close(); - } + if (this.rcon != null) { + this.rcon.close(); + } - for (Player player : new ArrayList<>(this.players.values())) { - player.close(player.getLeaveMessage(), this.getConfig("settings.shutdown-message", "Server closed")); - } + for (Player player : new ArrayList<>(this.players.values())) { + player.close(player.getLeaveMessage(), this.getConfig("settings.shutdown-message", "Server closed")); + } - this.getLogger().debug("Disabling all plugins"); - this.pluginManager.disablePlugins(); + this.getLogger().debug("Disabling all plugins"); + this.pluginManager.disablePlugins(); - this.getLogger().debug("Removing event handlers"); - HandlerList.unregisterAll(); + this.getLogger().debug("Removing event handlers"); + HandlerList.unregisterAll(); - this.getLogger().debug("Stopping all tasks"); - this.scheduler.cancelAllTasks(); - this.scheduler.mainThreadHeartbeat(Integer.MAX_VALUE); + this.getLogger().debug("Stopping all tasks"); + this.scheduler.cancelAllTasks(); + this.scheduler.mainThreadHeartbeat(Integer.MAX_VALUE); - this.getLogger().debug("Unloading all levels"); - for (Level level : this.levelArray) { - this.unloadLevel(level, true); - } + this.getLogger().debug("Unloading all levels"); + for (Level level : this.levelArray) { + this.unloadLevel(level, true); + } - this.getLogger().debug("Closing console"); - this.consoleThread.interrupt(); + this.getLogger().debug("Closing console"); + this.consoleThread.interrupt(); - this.getLogger().debug("Stopping network interfaces"); - for (SourceInterface interfaz : this.network.getInterfaces()) { - interfaz.shutdown(); - this.network.unregisterInterface(interfaz); - } + this.getLogger().debug("Stopping network interfaces"); + for (SourceInterface interfaz : this.network.getInterfaces()) { + interfaz.shutdown(); + this.network.unregisterInterface(interfaz); + } - if (nameLookup != null) { - nameLookup.close(); - } + if (nameLookup != null) { + nameLookup.close(); + } - this.getLogger().debug("Disabling timings"); - Timings.stopServer(); - if (this.watchdog != null) { - this.watchdog.kill(); - } - //todo other things + this.getLogger().debug("Disabling timings"); + Timings.stopServer(); + if (this.watchdog != null) { + this.watchdog.kill(); } + //todo other things } catch (Exception e) { log.fatal("Exception happened while shutting down, exiting the process", e); System.exit(1); From c21b714cabf35a298237c2145385462c441e9fbc Mon Sep 17 00:00:00 2001 From: NycuRO Date: Thu, 14 May 2020 23:32:40 +0300 Subject: [PATCH 3/3] Update ServerStopEvent.java --- src/main/java/cn/nukkit/event/server/ServerStopEvent.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/cn/nukkit/event/server/ServerStopEvent.java b/src/main/java/cn/nukkit/event/server/ServerStopEvent.java index 810bea2fb2a..f0938dada84 100644 --- a/src/main/java/cn/nukkit/event/server/ServerStopEvent.java +++ b/src/main/java/cn/nukkit/event/server/ServerStopEvent.java @@ -7,7 +7,7 @@ * author: NycuRO * NukkitX Project */ -public class ServerStopEvent extends ServerEvent implements Cancellable { +public class ServerStopEvent extends ServerEvent { private static final HandlerList handlers = new HandlerList();