Skip to content

Commit

Permalink
feat: Run post-initialization method at the first server tick
Browse files Browse the repository at this point in the history
### Motivation

Certain features of CloudNet react to services updating and thus changing their state to "Running". In the current implementation, this update happens at the time the bridge module/plugin/extension is enabled.
This timing is not ideal as a lot of time-consuming tasks, like generating or loading the worlds/dimensions and other modules/plugins/extensions may happen after that.
This causes the other components, like the signs module or the NPCs module, to report those services as being already online and ready to join by players, whilst the opposite is actually the case.

### Modification

All currently supported server software platform integrations are augmented, so that `PlatformBridgeManagement#postInit` is called in the first game tick. This tick is only called, once the server is actually initialized, loaded and running. This also means that the server is ready to accept players.

### Result

Amongst other things, signs now show the server for joining only, once the server is actually ready to accept players.
  • Loading branch information
GiantTreeLP committed Jul 26, 2024
1 parent 18530b6 commit 86d7171
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public BukkitBridgePlugin(
public void onLoad() {
// init the bridge management
this.bridgeManagement.registerServices(this.serviceRegistry);
this.bridgeManagement.postInit();

this.plugin.getServer().getScheduler().runTask(this.plugin, this.bridgeManagement::postInit);
// register the bukkit listener
this.pluginManager.registerEvents(this.playerListener, this.plugin);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import eu.cloudnetservice.modules.bridge.platform.fabric.util.FabricInjectionHolder;
import eu.cloudnetservice.modules.bridge.player.NetworkPlayerServerInfo;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import lombok.NonNull;
Expand All @@ -32,6 +33,7 @@
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.players.PlayerList;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
Expand All @@ -54,6 +56,13 @@ public abstract class MinecraftServerMixin implements BridgedServer {
@Shadow
public abstract String getMotd();

@Shadow
public abstract void addTickable(Runnable runnable);

@Shadow
@Final
private List<Runnable> tickables;

@Inject(
at = @At(
value = "INVOKE",
Expand All @@ -76,7 +85,7 @@ public abstract class MinecraftServerMixin implements BridgedServer {
this.cloudnet_bridge$injectionHolder.serviceProvider(),
this.cloudnet_bridge$injectionHolder.wrapperConfiguration());
this.cloudnet_bridge$management.registerServices(this.cloudnet_bridge$injectionHolder.serviceRegistry());
this.cloudnet_bridge$management.postInit();
this.addTickable(this::cloudnet_bridge$postInitTickable);
}

@Override
Expand Down Expand Up @@ -122,4 +131,10 @@ public abstract class MinecraftServerMixin implements BridgedServer {
public @NonNull FabricInjectionHolder cloudnet_bridge$injectionHolder() {
return this.cloudnet_bridge$injectionHolder;
}

@Override
public void cloudnet_bridge$postInitTickable() {
this.cloudnet_bridge$management.postInit();
this.tickables.remove((Runnable) this::cloudnet_bridge$postInitTickable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@ public interface BridgedServer {
@NonNull PlatformBridgeManagement<ServerPlayer, NetworkPlayerServerInfo> cloudnet_bridge$management();

@NonNull FabricInjectionHolder cloudnet_bridge$injectionHolder();

void cloudnet_bridge$postInitTickable();
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public LimboLoohpBridgePlugin(
public void onLoad() {
// init the bridge management
this.bridgeManagement.registerServices(this.serviceRegistry);
this.bridgeManagement.postInit();

this.plugin.getServer().getScheduler().runTask(this.plugin, this.bridgeManagement::postInit);
// register the bukkit listener
this.eventsManager.registerEvents(this.plugin, this.playerListener);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import jakarta.inject.Singleton;
import lombok.NonNull;
import net.kyori.adventure.text.logger.slf4j.ComponentLogger;
import net.minestom.server.MinecraftServer;
import net.minestom.server.extras.MojangAuth;
import net.minestom.server.extras.bungee.BungeeCordProxy;
import net.minestom.server.extras.velocity.VelocityProxy;
Expand Down Expand Up @@ -67,7 +68,8 @@ public MinestomBridgeExtension(
@Override
public void onLoad() {
this.bridgeManagement.registerServices(this.serviceRegistry);
this.bridgeManagement.postInit();

MinecraftServer.getSchedulerManager().scheduleNextTick(this.bridgeManagement::postInit);

// force initialize the bungeecord proxy forwarding
if (!VelocityProxy.isEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public NukkitBridgePlugin(
@Override
public void onLoad() {
this.bridgeManagement.registerServices(this.serviceRegistry);
this.bridgeManagement.postInit();

this.plugin.getServer().getScheduler().scheduleTask(this.plugin, this.bridgeManagement::postInit);
// register the listener
this.pluginManager.registerEvents(this.playerListener, this.plugin);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
import eu.cloudnetservice.ext.platforminject.api.stereotype.PlatformPlugin;
import jakarta.inject.Inject;
import lombok.NonNull;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.event.EventManager;
import org.spongepowered.api.scheduler.Task;
import org.spongepowered.api.util.Ticks;
import org.spongepowered.plugin.PluginContainer;

@Singleton
Expand Down Expand Up @@ -65,7 +68,12 @@ public SpongeBridgePlugin(
@Override
public void onLoad() {
this.bridgeManagement.registerServices(this.serviceRegistry);
this.bridgeManagement.postInit();
Sponge.server().scheduler().submit(
Task.builder()
.delay(Ticks.of(1))
.plugin(this.plugin)
.execute(this.bridgeManagement::postInit)
.build());
// register the listener
this.eventManager.registerListeners(this.plugin, this.playerListener);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public WaterDogPEBridgePlugin(
public void onLoad() {
// init the management
this.bridgeManagement.registerServices(this.serviceRegistry);
this.bridgeManagement.postInit();
this.proxyServer.getScheduler().scheduleDelayed(this.bridgeManagement::postInit, 1);

// register the WaterDog handlers
var handlers = new WaterDogPEHandlers(this.proxyServer, this.bridgeManagement);
Expand Down

0 comments on commit 86d7171

Please sign in to comment.