From 285b2a9dad3495fe97e1aab5b7c960fe7f06724a Mon Sep 17 00:00:00 2001 From: Rika <40230165+Moreonenight@users.noreply.github.com> Date: Tue, 2 Feb 2021 14:16:36 +0800 Subject: [PATCH] Fix /back bug Fix server crash on /back command if teleporting to a dimension add by mods. --- .../vanilla/commands/TeleportCommand.java | 23 +++++++++++++++---- .../core/impl/commands/BackLocationCommand.kt | 20 ++++++++++++---- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/mairwunnx/projectessentials/core/impl/vanilla/commands/TeleportCommand.java b/src/main/java/com/mairwunnx/projectessentials/core/impl/vanilla/commands/TeleportCommand.java index 7b5472d..4eac1bd 100644 --- a/src/main/java/com/mairwunnx/projectessentials/core/impl/vanilla/commands/TeleportCommand.java +++ b/src/main/java/com/mairwunnx/projectessentials/core/impl/vanilla/commands/TeleportCommand.java @@ -26,12 +26,15 @@ import net.minecraft.world.server.ServerWorld; import net.minecraft.world.server.TicketType; import org.jetbrains.annotations.NotNull; +import net.minecraftforge.common.util.ITeleporter; +import net.minecraft.world.dimension.DimensionType; import javax.annotation.Nullable; import java.util.Collection; import java.util.Collections; import java.util.EnumSet; import java.util.Set; +import java.util.function.Function; public class TeleportCommand extends VanillaCommandBase { public TeleportCommand() { @@ -116,6 +119,18 @@ private static int teleportToPos(CommandSource source, Collection repositionEntity) + { + Entity repositionedEntity = repositionEntity.apply(false); + return repositionedEntity; + } + }); + } + public static void teleport(CommandSource source, Entity entityIn, ServerWorld worldIn, double x, double y, double z, Set relativeList, float yaw, float pitch, @Nullable Facing facing) { if (entityIn instanceof ServerPlayerEntity) { ChunkPos chunkpos = new ChunkPos(new BlockPos(x, y, z)); @@ -125,12 +140,10 @@ public static void teleport(CommandSource source, Entity entityIn, ServerWorld w ((ServerPlayerEntity) entityIn).stopSleepInBed(true, true); } - if (worldIn == entityIn.world) { - ((ServerPlayerEntity) entityIn).connection.setPlayerLocation(x, y, z, yaw, pitch, relativeList); - } else { - ((ServerPlayerEntity) entityIn).teleport(worldIn, x, y, z, yaw, pitch); + if (worldIn != entityIn.world) { + changeDimension(entityIn, worldIn); } - + ((ServerPlayerEntity) entityIn).connection.setPlayerLocation(x, y, z, yaw, pitch, relativeList); entityIn.setRotationYawHead(yaw); } else { float f1 = MathHelper.wrapDegrees(yaw); diff --git a/src/main/kotlin/com/mairwunnx/projectessentials/core/impl/commands/BackLocationCommand.kt b/src/main/kotlin/com/mairwunnx/projectessentials/core/impl/commands/BackLocationCommand.kt index 5fee581..6daa219 100644 --- a/src/main/kotlin/com/mairwunnx/projectessentials/core/impl/commands/BackLocationCommand.kt +++ b/src/main/kotlin/com/mairwunnx/projectessentials/core/impl/commands/BackLocationCommand.kt @@ -2,6 +2,7 @@ package com.mairwunnx.projectessentials.core.impl.commands import com.mairwunnx.projectessentials.core.api.v1.MESSAGE_CORE_PREFIX import com.mairwunnx.projectessentials.core.api.v1.commands.CommandBase +import com.mairwunnx.projectessentials.core.impl.vanilla.commands.TeleportCommand import com.mairwunnx.projectessentials.core.api.v1.commands.back.BackLocationAPI import com.mairwunnx.projectessentials.core.api.v1.extensions.getPlayer import com.mairwunnx.projectessentials.core.api.v1.extensions.isPlayerSender @@ -10,7 +11,11 @@ import com.mairwunnx.projectessentials.core.api.v1.messaging.ServerMessagingAPI import com.mairwunnx.projectessentials.core.api.v1.permissions.hasPermission import com.mojang.brigadier.builder.LiteralArgumentBuilder.literal import com.mojang.brigadier.context.CommandContext +import net.minecraft.world.server.TicketType +import net.minecraft.entity.player.ServerPlayerEntity import net.minecraft.command.CommandSource +import net.minecraft.util.math.ChunkPos +import net.minecraft.util.math.BlockPos internal object BackLocationCommand : CommandBase(literal("back")) { override val name = "back" @@ -28,11 +33,16 @@ internal object BackLocationCommand : CommandBase(literal("back")) { if (data != null) { val pos = data.position val rot = data.rotation - - player.teleport( - data.world, pos.xPos, pos.yPos, pos.zPos, rot.yaw, rot.pitch - ) - + val chunkpos = ChunkPos(BlockPos(pos.xPos, pos.yPos, pos.zPos)) + data.world.getChunkProvider().registerTicket(TicketType.POST_TELEPORT, chunkpos, 1, player.getEntityId()) + player.stopRiding() + if (player.isSleeping()) { + player.stopSleepInBed(true, true) + } + if (data.world != player.serverWorld) { + TeleportCommand.changeDimension(player, data.world) + } + player.connection.setPlayerLocation(pos.xPos, pos.yPos, pos.zPos, rot.yaw, rot.pitch) BackLocationAPI.revoke(player) MessagingAPI.sendMessage(player, "$MESSAGE_CORE_PREFIX.back.success") } else {