Skip to content

Commit

Permalink
Add Save Events to Server Lifecycle Events (FabricMC#3533)
Browse files Browse the repository at this point in the history
* Add Server Begin Save and Server End Save event

* rename events

* add tests

* pass more params through
  • Loading branch information
MrNavaStar authored Feb 9, 2024
1 parent 32782cf commit a67ffb5
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,24 @@ private ServerLifecycleEvents() {
}
});

/**
* Called before a Minecraft server begins saving data.
*/
public static final Event<BeforeSave> BEFORE_SAVE = EventFactory.createArrayBacked(BeforeSave.class, callbacks -> (server, flush, force) -> {
for (BeforeSave callback : callbacks) {
callback.onBeforeSave(server, flush, force);
}
});

/**
* Called after a Minecraft server finishes saving data.
*/
public static final Event<AfterSave> AFTER_SAVE = EventFactory.createArrayBacked(AfterSave.class, callbacks -> (server, flush, force) -> {
for (AfterSave callback : callbacks) {
callback.onAfterSave(server, flush, force);
}
});

@FunctionalInterface
public interface ServerStarting {
void onServerStarting(MinecraftServer server);
Expand Down Expand Up @@ -160,4 +178,28 @@ public interface EndDataPackReload {
*/
void endDataPackReload(MinecraftServer server, LifecycledResourceManager resourceManager, boolean success);
}

@FunctionalInterface
public interface BeforeSave {
/**
* Called before a Minecraft server begins saving data.
*
* @param server the server
* @param flush is true when all chunks are being written to disk, server will likely freeze during this time
* @param force whether servers that have save-off set should save
*/
void onBeforeSave(MinecraftServer server, boolean flush, boolean force);
}

@FunctionalInterface
public interface AfterSave {
/**
* Called before a Minecraft server begins saving data.
*
* @param server the server
* @param flush is true when all chunks are being written to disk, server will likely freeze during this time
* @param force whether servers that have save-off set should save
*/
void onAfterSave(MinecraftServer server, boolean flush, boolean force);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,14 @@ private void endResourceReload(Collection<String> collection, CallbackInfoReturn
return value;
}, (MinecraftServer) (Object) this);
}

@Inject(method = "save", at = @At("HEAD"))
private void startSave(boolean suppressLogs, boolean flush, boolean force, CallbackInfoReturnable<Boolean> cir) {
ServerLifecycleEvents.BEFORE_SAVE.invoker().onBeforeSave((MinecraftServer) (Object) this, flush, force);
}

@Inject(method = "save", at = @At("TAIL"))
private void endSave(boolean suppressLogs, boolean flush, boolean force, CallbackInfoReturnable<Boolean> cir) {
ServerLifecycleEvents.AFTER_SAVE.invoker().onAfterSave((MinecraftServer) (Object) this, flush, force);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,13 @@ public void onInitialize() {
ServerLifecycleEvents.SYNC_DATA_PACK_CONTENTS.register((player, joined) -> {
LOGGER.info("SyncDataPackContents received for {}", joined ? "join" : "reload");
});

ServerLifecycleEvents.BEFORE_SAVE.register((server, flush, force) -> {
LOGGER.info("Starting Save with settings: Flush:{} Force:{}", flush, force);
});

ServerLifecycleEvents.AFTER_SAVE.register((server, flush, force) -> {
LOGGER.info("Save Finished with settings: Flush:{} Force:{}", flush, force);
});
}
}

0 comments on commit a67ffb5

Please sign in to comment.