added startup and shutdown events. fixes #586

This commit is contained in:
gnembon committed 2020-12-07 01:11:55 -04:00
1 parent e8a6b8936f
commit 522d971688
4 files changed
+35 -3

No files matched your search

+7
View File
@@ -32,6 +32,13 @@ if it accepts required number of parameters.
Global events will be handled once per app that is with `'global'` scope. With `player` scoped apps, each player instance Global events will be handled once per app that is with `'global'` scope. With `player` scoped apps, each player instance
is responsible independently from handling their events, so a global event may be executed multiple times for each player. is responsible independently from handling their events, so a global event may be executed multiple times for each player.
### `__on_server_starts()`
Event triggers after world is loaded and after all startup apps have started. It won't be triggered with `/reload`.
### `__on_server_shuts_down()`
Event triggers when the server started the shutdown process, before `__on_close()` is executed. Unlike `__on_close()`, it doesn't
trigger with `/reload`.
### `__on_tick()` ### `__on_tick()`
Event triggers at the beginning of each tick, located in the overworld. You can use `in_dimension()` Event triggers at the beginning of each tick, located in the overworld. You can use `in_dimension()`
to access other dimensions from there. to access other dimensions from there.
+2 -2
View File
@@ -102,9 +102,9 @@ public class CarpetServer implements ClientModInitializer,DedicatedServerModInit
public static void onServerLoadedWorlds(MinecraftServer minecraftServer) public static void onServerLoadedWorlds(MinecraftServer minecraftServer)
{ {
HopperCounter.resetAll(minecraftServer);
extensions.forEach(e -> e.onServerLoadedWorlds(minecraftServer)); extensions.forEach(e -> e.onServerLoadedWorlds(minecraftServer));
scriptServer.initializeForWorld(); scriptServer.initializeForWorld();
HopperCounter.resetAll(minecraftServer);
} }
public static void tick(MinecraftServer server) public static void tick(MinecraftServer server)
@@ -169,9 +169,9 @@ public class CarpetServer implements ClientModInitializer,DedicatedServerModInit
public static void onServerClosed(MinecraftServer server) public static void onServerClosed(MinecraftServer server)
{ {
if (scriptServer != null) scriptServer.onClose();
ServerNetworkHandler.close(); ServerNetworkHandler.close();
currentCommandDispatcher = null; currentCommandDispatcher = null;
if (scriptServer != null) scriptServer.onClose();
LoggerRegistry.stopLoggers(); LoggerRegistry.stopLoggers();
extensions.forEach(e -> e.onServerClosed(server)); extensions.forEach(e -> e.onServerClosed(server));
@@ -309,6 +309,30 @@ public class CarpetEventServer
return events; return events;
} }
public static final Event START = new Event("server_starts", 0, true)
{
@Override
public void onTick()
{
handler.call(Collections::emptyList, () ->
CarpetServer.minecraft_server.getCommandSource().
withWorld(CarpetServer.minecraft_server.getWorld(World.OVERWORLD))
);
}
};
public static final Event SHUTDOWN = new Event("server_shuts_down", 0, true)
{
@Override
public void onTick()
{
handler.call(Collections::emptyList, () ->
CarpetServer.minecraft_server.getCommandSource().
withWorld(CarpetServer.minecraft_server.getWorld(World.OVERWORLD))
);
}
};
public static final Event TICK = new Event("tick", 0, true) public static final Event TICK = new Event("tick", 0, true)
{ {
@Override @Override
@@ -127,7 +127,7 @@ public class CarpetScriptServer
addScriptHost(server.getCommandSource(), moduleName, null, true, true, false); addScriptHost(server.getCommandSource(), moduleName, null, true, true, false);
} }
} }
CarpetEventServer.Event.START.onTick();
} }
public Module getModule(String name, boolean allowLibraries) public Module getModule(String name, boolean allowLibraries)
@@ -489,6 +489,7 @@ public class CarpetScriptServer
public void onClose() public void onClose()
{ {
CarpetEventServer.Event.SHUTDOWN.onTick();
for (ScriptHost host : modules.values()) for (ScriptHost host : modules.values())
{ {
host.onClose(); host.onClose();