Skip to content

Commit

Permalink
Fix server tick listeners not being invoked while the server is paused
Browse files Browse the repository at this point in the history
  • Loading branch information
haykam821 committed Dec 26, 2024
1 parent a0fde1c commit d8d307a
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/main/java/xyz/nucleoid/extras/NucleoidExtras.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/xyz/nucleoid/extras/event/NucleoidExtrasEvents.java
Original file line number Diff line number Diff line change
@@ -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<EndTick> END_SERVER_TICK = EventFactory.createArrayBacked(EndTick.class, listeners -> server -> {
for (var listener : listeners) {
listener.onEndTick(server);
}
});

@FunctionalInterface
public interface EndTick {
void onEndTick(MinecraftServer server);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/xyz/nucleoid/extras/mixin/MinecraftServerMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down

0 comments on commit d8d307a

Please sign in to comment.