|
| 1 | +package io.github.optijava.opt_carpet_addition.mixins.async.optimizeTeleport; |
| 2 | + |
| 3 | +import io.github.optijava.opt_carpet_addition.OptCarpetSettings; |
| 4 | +import io.github.optijava.opt_carpet_addition.utils.threading.Threading; |
| 5 | +import net.minecraft.entity.Entity; |
| 6 | +import net.minecraft.network.packet.s2c.play.DifficultyS2CPacket; |
| 7 | +import net.minecraft.network.packet.s2c.play.PlayerRespawnS2CPacket; |
| 8 | +import net.minecraft.server.network.ServerPlayerEntity; |
| 9 | +import net.minecraft.server.world.ServerWorld; |
| 10 | +import net.minecraft.world.WorldProperties; |
| 11 | +import net.minecraft.world.biome.source.BiomeAccess; |
| 12 | +import org.spongepowered.asm.mixin.Mixin; |
| 13 | +import org.spongepowered.asm.mixin.injection.At; |
| 14 | +import org.spongepowered.asm.mixin.injection.Inject; |
| 15 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; |
| 16 | + |
| 17 | +@Mixin( |
| 18 | + value = ServerPlayerEntity.class, |
| 19 | + priority = 900 |
| 20 | +) |
| 21 | +public abstract class ServerPlayerEntity_Mixin { |
| 22 | + |
| 23 | + @Inject( |
| 24 | + method = "teleport", |
| 25 | + at = @At("HEAD"), |
| 26 | + cancellable = true |
| 27 | + ) |
| 28 | + public void injectTeleport(ServerWorld targetWorld, double x, double y, double z, float yaw, float pitch, CallbackInfo ci) { |
| 29 | + if (OptCarpetSettings.optimizeTeleport) { |
| 30 | + Threading.THREAD_POOL.submit(() -> { |
| 31 | + ServerPlayerEntity thisInstance = ((ServerPlayerEntity) (Object) this); |
| 32 | + |
| 33 | + thisInstance.setCameraEntity(thisInstance); |
| 34 | + thisInstance.stopRiding(); |
| 35 | + if (targetWorld == thisInstance.world) { |
| 36 | + thisInstance.networkHandler.requestTeleport(x, y, z, yaw, pitch); |
| 37 | + } else { |
| 38 | + ServerWorld serverWorld = thisInstance.getServerWorld(); |
| 39 | + WorldProperties worldProperties = targetWorld.getLevelProperties(); |
| 40 | + thisInstance.networkHandler.sendPacket(new PlayerRespawnS2CPacket(targetWorld.getDimension(), targetWorld.getRegistryKey(), BiomeAccess.hashSeed(targetWorld.getSeed()), thisInstance.interactionManager.getGameMode(), thisInstance.interactionManager.getPreviousGameMode(), targetWorld.isDebugWorld(), targetWorld.isFlat(), true)); |
| 41 | + thisInstance.networkHandler.sendPacket(new DifficultyS2CPacket(worldProperties.getDifficulty(), worldProperties.isDifficultyLocked())); |
| 42 | + thisInstance.server.getPlayerManager().sendCommandTree(thisInstance); |
| 43 | + serverWorld.removePlayer(thisInstance, Entity.RemovalReason.CHANGED_DIMENSION); |
| 44 | + thisInstance.unsetRemoved(); |
| 45 | + thisInstance.refreshPositionAndAngles(x, y, z, yaw, pitch); |
| 46 | + thisInstance.setWorld(targetWorld); |
| 47 | + targetWorld.onPlayerTeleport(thisInstance); |
| 48 | + thisInstance.worldChanged(serverWorld); |
| 49 | + thisInstance.networkHandler.requestTeleport(x, y, z, yaw, pitch); |
| 50 | + thisInstance.server.getPlayerManager().sendWorldInfo(thisInstance, targetWorld); |
| 51 | + thisInstance.server.getPlayerManager().sendPlayerStatus(thisInstance); |
| 52 | + } |
| 53 | + }); |
| 54 | + ci.cancel(); |
| 55 | + } |
| 56 | + } |
| 57 | +} |
0 commit comments