Skip to content

Commit

Permalink
new: reimplementation of async chunks on login
Browse files Browse the repository at this point in the history
  • Loading branch information
ishland committed Sep 22, 2024
1 parent e994fbd commit 7e0b751
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 347 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.ishland.vmp.mixins.chunk.loading.async_chunk_on_player_login;

import com.google.common.base.Stopwatch;
import com.ishland.vmp.common.chunk.loading.async_chunks_on_player_login.AsyncChunkLoadUtil;
import com.ishland.vmp.common.config.Config;
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.mojang.authlib.GameProfile;
import com.mojang.serialization.Dynamic;
import net.minecraft.nbt.NbtOps;
import net.minecraft.network.ClientConnection;
import net.minecraft.network.listener.ServerConfigurationPacketListener;
import net.minecraft.network.listener.TickablePacketListener;
import net.minecraft.registry.RegistryKey;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.PlayerManager;
import net.minecraft.server.network.ConnectedClientData;
import net.minecraft.server.network.ServerCommonNetworkHandler;
import net.minecraft.server.network.ServerConfigurationNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.world.World;
import net.minecraft.world.dimension.DimensionType;
import org.slf4j.Logger;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;

@Mixin(ServerConfigurationNetworkHandler.class)
public abstract class MixinServerConfigurationNetworkHandler extends ServerCommonNetworkHandler implements ServerConfigurationPacketListener, TickablePacketListener {

@Shadow @Final private static Logger LOGGER;

@Shadow public abstract boolean isConnectionOpen();

@Shadow @Final private GameProfile profile;

public MixinServerConfigurationNetworkHandler(MinecraftServer server, ClientConnection connection, ConnectedClientData clientData) {
super(server, connection, clientData);
}

@WrapOperation(method = "onReady", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/PlayerManager;onPlayerConnect(Lnet/minecraft/network/ClientConnection;Lnet/minecraft/server/network/ServerPlayerEntity;Lnet/minecraft/server/network/ConnectedClientData;)V"))
private void wrapOnPlayerConnect(PlayerManager instance, ClientConnection connection, ServerPlayerEntity player, ConnectedClientData clientData, Operation<Void> original) {
RegistryKey<World> registryKey = instance.loadPlayerData(player)
.flatMap(nbt -> DimensionType.worldFromDimensionNbt(new Dynamic<>(NbtOps.INSTANCE, nbt.get("Dimension"))).resultOrPartial(LOGGER::error))
.orElse(World.OVERWORLD);
ServerWorld storedWorld = instance.getServer().getWorld(registryKey);
ServerWorld actualWorld;
if (storedWorld == null) {
LOGGER.warn("Unknown respawn dimension {}, defaulting to overworld", registryKey);
actualWorld = instance.getServer().getOverworld();
} else {
actualWorld = storedWorld;
}

Stopwatch timing = Stopwatch.createStarted();
AsyncChunkLoadUtil.scheduleChunkLoad(actualWorld, new ChunkPos(player.getBlockPos()))
.thenRunAsync(() -> {
if (!this.isConnectionOpen()) {
return;
}

if (instance.getPlayer(this.profile.getId()) != null) {
this.disconnect(PlayerManager.DUPLICATE_LOGIN_TEXT);
return;
}

if (Config.SHOW_ASYNC_LOADING_MESSAGES) {
LOGGER.info("Async chunk loading for player {} completed after {}", profile.getName(), timing);
}

original.call(instance, connection, player, clientData);
}, instance.getServer());
}

}

This file was deleted.

Loading

2 comments on commit 7e0b751

@Luracasmus
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still disabled in the config btw

@ishland
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still disabled in the config btw

It is intentional to allow more testing before it becomes enabled by default.

Please sign in to comment.