From d8d307a7351d7dc3a535b34bd0fe1d60a0af6647 Mon Sep 17 00:00:00 2001 From: haykam821 <24855774+haykam821@users.noreply.github.com> Date: Thu, 26 Dec 2024 16:51:04 -0500 Subject: [PATCH] Fix server tick listeners not being invoked while the server is paused --- .../xyz/nucleoid/extras/NucleoidExtras.java | 4 +-- .../extras/event/NucleoidExtrasEvents.java | 25 +++++++++++++++++++ .../game/GameStatusIntegration.java | 4 +-- .../relay/ChatRelayIntegration.java | 4 +-- .../relay/RemoteCommandIntegration.java | 4 +-- .../status/PlayerStatusIntegration.java | 4 +-- .../status/ServerPerformanceIntegration.java | 4 +-- .../extras/mixin/MinecraftServerMixin.java | 11 ++++++++ .../extras/scheduled_stop/ScheduledStop.java | 4 +-- 9 files changed, 50 insertions(+), 14 deletions(-) create mode 100644 src/main/java/xyz/nucleoid/extras/event/NucleoidExtrasEvents.java diff --git a/src/main/java/xyz/nucleoid/extras/NucleoidExtras.java b/src/main/java/xyz/nucleoid/extras/NucleoidExtras.java index dc2c14d4..5f7afaba 100644 --- a/src/main/java/xyz/nucleoid/extras/NucleoidExtras.java +++ b/src/main/java/xyz/nucleoid/extras/NucleoidExtras.java @@ -3,7 +3,6 @@ import eu.pb4.playerdata.api.PlayerDataApi; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; -import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; import net.fabricmc.fabric.api.networking.v1.PacketSender; import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; import net.minecraft.server.MinecraftServer; @@ -19,6 +18,7 @@ import xyz.nucleoid.extras.command.ExtraCommands; import xyz.nucleoid.extras.component.NEDataComponentTypes; import xyz.nucleoid.extras.error.ExtrasErrorReporter; +import xyz.nucleoid.extras.event.NucleoidExtrasEvents; import xyz.nucleoid.extras.game_portal.ExtrasGamePortals; import xyz.nucleoid.extras.game_portal.ServerChangePortalBackend; import xyz.nucleoid.extras.game_portal.entry.ExtraMenuEntries; @@ -61,7 +61,7 @@ public void onInitialize() { PlayerDataApi.register(PlayerLobbyState.STORAGE); - ServerTickEvents.END_SERVER_TICK.register(NucleoidExtras::onServerTick); + NucleoidExtrasEvents.END_SERVER_TICK.register(NucleoidExtras::onServerTick); ServerLifecycleEvents.SERVER_STOPPED.register(NucleoidExtras::onServerStopped); ServerPlayConnectionEvents.JOIN.register(NucleoidExtras::onPlayerJoin); NucleoidExtrasNetworking.register(); diff --git a/src/main/java/xyz/nucleoid/extras/event/NucleoidExtrasEvents.java b/src/main/java/xyz/nucleoid/extras/event/NucleoidExtrasEvents.java new file mode 100644 index 00000000..199e5a7a --- /dev/null +++ b/src/main/java/xyz/nucleoid/extras/event/NucleoidExtrasEvents.java @@ -0,0 +1,25 @@ +package xyz.nucleoid.extras.event; + +import net.minecraft.server.MinecraftServer; +import net.fabricmc.fabric.api.event.Event; +import net.fabricmc.fabric.api.event.EventFactory; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; + +public final class NucleoidExtrasEvents { + private NucleoidExtrasEvents() { + } + + /** + * Called at the end of the server tick, including when the server is paused (compared to {@link ServerTickEvents#END_SERVER_TICK}). + */ + public static final Event END_SERVER_TICK = EventFactory.createArrayBacked(EndTick.class, listeners -> server -> { + for (var listener : listeners) { + listener.onEndTick(server); + } + }); + + @FunctionalInterface + public interface EndTick { + void onEndTick(MinecraftServer server); + } +} diff --git a/src/main/java/xyz/nucleoid/extras/integrations/game/GameStatusIntegration.java b/src/main/java/xyz/nucleoid/extras/integrations/game/GameStatusIntegration.java index 7ea576f3..478973a7 100644 --- a/src/main/java/xyz/nucleoid/extras/integrations/game/GameStatusIntegration.java +++ b/src/main/java/xyz/nucleoid/extras/integrations/game/GameStatusIntegration.java @@ -2,11 +2,11 @@ import com.google.gson.JsonArray; import com.google.gson.JsonObject; -import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.server.MinecraftServer; import net.minecraft.util.Identifier; import org.jetbrains.annotations.Nullable; +import xyz.nucleoid.extras.event.NucleoidExtrasEvents; import xyz.nucleoid.extras.integrations.IntegrationSender; import xyz.nucleoid.extras.integrations.IntegrationsConfig; import xyz.nucleoid.extras.integrations.NucleoidIntegrations; @@ -35,7 +35,7 @@ public static void bind(NucleoidIntegrations integrations, IntegrationsConfig co var statusSender = integrations.openSender("status"); var integration = new GameStatusIntegration(statusSender); - ServerTickEvents.END_SERVER_TICK.register(integration::tick); + NucleoidExtrasEvents.END_SERVER_TICK.register(integration::tick); } } diff --git a/src/main/java/xyz/nucleoid/extras/integrations/relay/ChatRelayIntegration.java b/src/main/java/xyz/nucleoid/extras/integrations/relay/ChatRelayIntegration.java index d41b2869..fcae0a5f 100644 --- a/src/main/java/xyz/nucleoid/extras/integrations/relay/ChatRelayIntegration.java +++ b/src/main/java/xyz/nucleoid/extras/integrations/relay/ChatRelayIntegration.java @@ -1,7 +1,6 @@ package xyz.nucleoid.extras.integrations.relay; import com.google.gson.JsonObject; -import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; import net.fabricmc.fabric.api.message.v1.ServerMessageEvents; import net.minecraft.screen.ScreenTexts; import net.minecraft.server.MinecraftServer; @@ -10,6 +9,7 @@ import net.minecraft.util.Formatting; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import xyz.nucleoid.extras.event.NucleoidExtrasEvents; import xyz.nucleoid.extras.integrations.IntegrationSender; import xyz.nucleoid.extras.integrations.IntegrationsConfig; import xyz.nucleoid.extras.integrations.NucleoidIntegrations; @@ -36,7 +36,7 @@ public static void bind(NucleoidIntegrations integrations, IntegrationsConfig co integration.messageQueue.add(message); }); - ServerTickEvents.END_SERVER_TICK.register(integration::tick); + NucleoidExtrasEvents.END_SERVER_TICK.register(integration::tick); ServerMessageEvents.CHAT_MESSAGE.register((message, sender, parameters) -> { integration.onSendChatMessage(sender, message.getContent().getString()); diff --git a/src/main/java/xyz/nucleoid/extras/integrations/relay/RemoteCommandIntegration.java b/src/main/java/xyz/nucleoid/extras/integrations/relay/RemoteCommandIntegration.java index 78c80b94..1983c8c6 100644 --- a/src/main/java/xyz/nucleoid/extras/integrations/relay/RemoteCommandIntegration.java +++ b/src/main/java/xyz/nucleoid/extras/integrations/relay/RemoteCommandIntegration.java @@ -1,11 +1,11 @@ package xyz.nucleoid.extras.integrations.relay; import com.google.gson.JsonObject; -import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; import net.minecraft.server.MinecraftServer; import net.minecraft.server.command.CommandOutput; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.text.Text; +import xyz.nucleoid.extras.event.NucleoidExtrasEvents; import xyz.nucleoid.extras.integrations.IntegrationSender; import xyz.nucleoid.extras.integrations.IntegrationsConfig; import xyz.nucleoid.extras.integrations.NucleoidIntegrations; @@ -56,7 +56,7 @@ public static void bind(NucleoidIntegrations integrations, IntegrationsConfig co integration.commandQueue.add(new RemoteCommand(command, sender, silent, permissionLevel, roles)); }); - ServerTickEvents.END_SERVER_TICK.register(integration::tick); + NucleoidExtrasEvents.END_SERVER_TICK.register(integration::tick); } } diff --git a/src/main/java/xyz/nucleoid/extras/integrations/status/PlayerStatusIntegration.java b/src/main/java/xyz/nucleoid/extras/integrations/status/PlayerStatusIntegration.java index 6e4ac4c4..b819dcd9 100644 --- a/src/main/java/xyz/nucleoid/extras/integrations/status/PlayerStatusIntegration.java +++ b/src/main/java/xyz/nucleoid/extras/integrations/status/PlayerStatusIntegration.java @@ -3,9 +3,9 @@ import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.mojang.authlib.GameProfile; -import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; import net.minecraft.server.MinecraftServer; import org.jetbrains.annotations.Nullable; +import xyz.nucleoid.extras.event.NucleoidExtrasEvents; import xyz.nucleoid.extras.integrations.IntegrationSender; import xyz.nucleoid.extras.integrations.IntegrationsConfig; import xyz.nucleoid.extras.integrations.NucleoidIntegrations; @@ -32,7 +32,7 @@ public static void bind(NucleoidIntegrations integrations, IntegrationsConfig co var statusSender = integrations.openSender("status"); var integration = new PlayerStatusIntegration(statusSender); - ServerTickEvents.END_SERVER_TICK.register(integration::tick); + NucleoidExtrasEvents.END_SERVER_TICK.register(integration::tick); } } diff --git a/src/main/java/xyz/nucleoid/extras/integrations/status/ServerPerformanceIntegration.java b/src/main/java/xyz/nucleoid/extras/integrations/status/ServerPerformanceIntegration.java index 70098018..2bc36f4d 100644 --- a/src/main/java/xyz/nucleoid/extras/integrations/status/ServerPerformanceIntegration.java +++ b/src/main/java/xyz/nucleoid/extras/integrations/status/ServerPerformanceIntegration.java @@ -1,9 +1,9 @@ package xyz.nucleoid.extras.integrations.status; import com.google.gson.JsonObject; -import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; import net.minecraft.server.MinecraftServer; import net.minecraft.util.profiler.log.MultiValueDebugSampleLog; +import xyz.nucleoid.extras.event.NucleoidExtrasEvents; import xyz.nucleoid.extras.integrations.IntegrationSender; import xyz.nucleoid.extras.integrations.IntegrationsConfig; import xyz.nucleoid.extras.integrations.NucleoidIntegrations; @@ -28,7 +28,7 @@ public static void bind(NucleoidIntegrations integrations, IntegrationsConfig co var performanceSender = integrations.openSender("performance"); var integration = new ServerPerformanceIntegration(performanceSender); - ServerTickEvents.END_SERVER_TICK.register(integration::tick); + NucleoidExtrasEvents.END_SERVER_TICK.register(integration::tick); } private void tick(MinecraftServer server) { diff --git a/src/main/java/xyz/nucleoid/extras/mixin/MinecraftServerMixin.java b/src/main/java/xyz/nucleoid/extras/mixin/MinecraftServerMixin.java index be382251..b733e9db 100644 --- a/src/main/java/xyz/nucleoid/extras/mixin/MinecraftServerMixin.java +++ b/src/main/java/xyz/nucleoid/extras/mixin/MinecraftServerMixin.java @@ -14,9 +14,12 @@ import org.spongepowered.asm.mixin.injection.ModifyArg; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import xyz.nucleoid.extras.error.ExtrasErrorReporter; +import xyz.nucleoid.extras.event.NucleoidExtrasEvents; import xyz.nucleoid.extras.integrations.status.HasTickPerformanceLog; import xyz.nucleoid.extras.integrations.status.ServerLifecycleIntegration; +import java.util.function.BooleanSupplier; + @Mixin(MinecraftServer.class) public class MinecraftServerMixin implements HasTickPerformanceLog { @Unique @@ -34,6 +37,14 @@ public class MinecraftServerMixin implements HasTickPerformanceLog { return report; } + @Inject( + method = "tick", + at = @At("RETURN") + ) + private void onEndTickIncludingPaused(BooleanSupplier shouldKeepTicking, CallbackInfo ci) { + NucleoidExtrasEvents.END_SERVER_TICK.invoker().onEndTick((MinecraftServer) (Object) this); + } + @Inject( method = "pushTickLog", at = @At(value = "HEAD") diff --git a/src/main/java/xyz/nucleoid/extras/scheduled_stop/ScheduledStop.java b/src/main/java/xyz/nucleoid/extras/scheduled_stop/ScheduledStop.java index 29a3e173..5e7d7000 100644 --- a/src/main/java/xyz/nucleoid/extras/scheduled_stop/ScheduledStop.java +++ b/src/main/java/xyz/nucleoid/extras/scheduled_stop/ScheduledStop.java @@ -4,12 +4,12 @@ import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.context.CommandContext; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; -import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.server.MinecraftServer; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.text.Text; import net.minecraft.util.Formatting; +import xyz.nucleoid.extras.event.NucleoidExtrasEvents; import xyz.nucleoid.plasmid.api.event.GameEvents; import xyz.nucleoid.plasmid.api.game.GameCloseReason; import xyz.nucleoid.plasmid.api.game.GameOpenException; @@ -36,7 +36,7 @@ public static void register() { registerCommands(dispatcher) ); - ServerTickEvents.END_SERVER_TICK.register(ScheduledStop::tick); + NucleoidExtrasEvents.END_SERVER_TICK.register(ScheduledStop::tick); GameEvents.OPENED.register(ScheduledStop::openGame); }