Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[Solved] [1.16.5] Teleporting a player in the same Dimension using entityInside()


Zeher_Monkey
 Share

Recommended Posts

I have tried in vain to teleport a player within the same Dimension when they collide with a block with no collision. In creative mode, it works flawlessly, but in singleplayer I keep getting this error: 

[01:50:38] [Server thread/WARN] [minecraft/ServerPlayNetHandler]: TheCosmicNebula_ moved wrongly!

I have tried many methods, from many classes that teleport. I can use

ServerPlayerEntity.changeDimension()

However it causes massive lag because it is repeating the same code with all of the blocks.

 

Any help would be appreciated, i'm pulling my hair out!

Edited by Zeher_Monkey
Issue was Solved.
Link to comment
Share on other sites

Here is where I call the teleport:

TileEntityPortal

Spoiler
	public void entityInside(BlockState stateIn, World worldIn, BlockPos posIn, Entity entityIn) {
		if (!entityIn.isPassenger() && !entityIn.isVehicle() && entityIn.canChangeDimensions()) {
			if (!this.dimension.getNamespace().isBlank() && !this.dimension.getPath().isBlank()) {
				if (this.teleport_pos.getPos() != BlockPos.ZERO) {
					
					if (entityIn instanceof ServerPlayerEntity) {
						ServerPlayerEntity playerIn = (ServerPlayerEntity) entityIn;
						
						BlockPos targetPos = this.getTeleportPos();
						float yaw = this.teleport_pos.getYaw();
						float pitch = this.teleport_pos.getPitch();
						
						CosmosTeleporter teleporter = CosmosTeleporter.createTeleporter(this.getDimension(), targetPos, yaw, pitch, true, false, true);
						CosmosTeleportCore.shiftPlayerToDimension(playerIn, teleporter, null);
					}
				}
			}
		}
	}

 

Here is my ITeleporter:

Spoiler
package com.tcn.cosmoslibrary.core.teleport;

import java.util.function.Function;

import com.tcn.cosmoslibrary.common.math.CosmosMathUtil;

import net.minecraft.block.PortalInfo;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.util.RegistryKey;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;
import net.minecraftforge.common.util.ITeleporter;

public class CosmosTeleporter implements ITeleporter {

	private RegistryKey<World> dimension_key;
	private BlockPos target_pos;
	private float target_yaw;
	private float target_pitch;
	private boolean playVanillaSound;
	private boolean sendMessage;
	private boolean safeSpawn;

	public CosmosTeleporter(RegistryKey<World> dimensionKeyIn, BlockPos targetPosIn, float targetYawIn, float targetPitchIn, boolean playVanillaSoundIn, boolean sendMessageIn, boolean safeSpawnIn) {
		this.dimension_key = dimensionKeyIn;
		this.target_pos = targetPosIn;
		this.target_yaw = targetYawIn;
		this.target_pitch = targetPitchIn;
		this.playVanillaSound = playVanillaSoundIn;
		this.sendMessage = sendMessageIn;
		this.safeSpawn = safeSpawnIn;
	}
	
	public BlockPos getTargetPos() {
		return this.target_pos;
	}
	
	public double[] getTargetPosA () {
		return new double[] { this.target_pos.getX(), this.target_pos.getY(), this.target_pos.getZ() };
	}

	public float getTargetYaw() {
		return this.target_yaw;
	}

	public float getTargetPitch() {
		return this.target_pitch;
	}
	
	public float[] getTargetRotation() {
		return new float[] { this.target_yaw, this.target_pitch };
	}

	public RegistryKey<World> getDimensionKey() {
		return this.dimension_key;
	}

	public static CosmosTeleporter createTeleporter(RegistryKey<World> dimensionKeyIn, BlockPos targetPosIn, float targetYawIn, float targetPitchIn, boolean playVanillaSoundIn, boolean sendMessageIn, boolean safeSpawnIn) {
		return new CosmosTeleporter(dimensionKeyIn, targetPosIn, targetYawIn, targetPitchIn, playVanillaSoundIn, sendMessageIn, safeSpawnIn);
	}
	
	public boolean playVanillaSound() {
		return this.playVanillaSound;
	}

	public boolean getSendMessage() {
		return this.sendMessage;
	}

	@Override
	public Entity placeEntity(Entity entity, ServerWorld currentWorld, ServerWorld destWorld, float yaw, Function<Boolean, Entity> repositionEntity) {
		return repositionEntity.apply(true);
    }
	
	@Override
    public boolean playTeleportSound(ServerPlayerEntity player, ServerWorld sourceWorld, ServerWorld destWorld) {
    	return this.playVanillaSound;
    }
	
    @Override
    public PortalInfo getPortalInfo(Entity entity, ServerWorld destWorld, Function<ServerWorld, PortalInfo> defaultPortalInfo) {
    	if (this.safeSpawn) {
    		EnumSafeTeleport spawnLocation = EnumSafeTeleport.getValidTeleportLocation(destWorld, this.target_pos);
    		
    		if (spawnLocation != EnumSafeTeleport.UNKNOWN) {
    			BlockPos resultPos = spawnLocation.toBlockPos();
    			BlockPos combinedPos = CosmosMathUtil.addBlockPos(this.target_pos, resultPos);
    			
    			return new PortalInfo(new Vector3d(combinedPos.getX() + 0.5F, combinedPos.getY(), combinedPos.getZ() + 0.5F), Vector3d.ZERO, this.getTargetYaw(), this.getTargetPitch());
    		}
    	}
    	
    	return new PortalInfo(new Vector3d(target_pos.getX() + 0.5F, target_pos.getY(), target_pos.getZ() + 0.5F), Vector3d.ZERO, this.getTargetYaw(), this.getTargetPitch());
    }
}

 

CosmosTeleportCore

Spoiler
package com.tcn.cosmoslibrary.core.teleport;

import java.util.Set;

import javax.annotation.Nullable;

import net.minecraft.entity.CreatureEntity;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.network.play.server.SPlaySoundEffectPacket;
import net.minecraft.network.play.server.SPlayerPositionLookPacket;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.RegistryKey;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;
import net.minecraft.world.server.TicketType;
import net.minecraftforge.fml.server.ServerLifecycleHooks;

public class CosmosTeleportCore {
	
	public static void shiftPlayerToDimension(PlayerEntity playerIn, CosmosTeleporter teleporterIn, @Nullable SoundEvent soundIn) {
		if (playerIn instanceof ServerPlayerEntity) {
			ServerPlayerEntity server_player = (ServerPlayerEntity) playerIn;
			RegistryKey<World> dimension_key = teleporterIn.getDimensionKey();
			
			if (dimension_key != null) {
				MinecraftServer Mserver = ServerLifecycleHooks.getCurrentServer();
				ServerWorld server_world = Mserver.getLevel(dimension_key);
				BlockPos target_pos = teleporterIn.getTargetPos();
				
				if (server_world != null) {
					if (target_pos != null) {
						double[] position = teleporterIn.getTargetPosA();
						
						if (!teleporterIn.playVanillaSound() && soundIn != null) {
							server_player.connection.send(new SPlaySoundEffectPacket(soundIn, SoundCategory.AMBIENT, position[0], position[1], position[2], 0.4F, 1));
						}
						
						if (dimension_key == playerIn.level.dimension()) {
							//server_player.changeDimension(server_world, teleporterIn);
							
							//teleportPlayer(server_player, server_world, position[0], position[1], position[2], EnumSet.noneOf(SPlayerPositionLookPacket.Flags.class), teleporterIn.getTargetYaw(), teleporterIn.getTargetPitch());
							//playerIn.restoreFrom(playerIn);
							//playerIn.moveTo(position[0], position[1], position[2], teleporterIn.getTargetYaw(), teleporterIn.getTargetPitch());
				            //playerIn.setDeltaMovement(teleporterIn.getPortalInfo(playerIn, server_world, null).speed);
				            
							//playerIn.moveTo(position[0], position[1], position[2], teleporterIn.getTargetYaw(), teleporterIn.getTargetPitch());
							
							//playerIn.lerpMotion(position[0], position[1], position[2]);
							//playerIn.move(MoverType.PISTON, new Vector3d(position[0], position[1], position[2]));
							
							//server_player.setPosRaw(position[0], position[1], position[2]);
							
							//server_player.moveTo(position[0], position[1], position[2]);
							//server_player.teleportTo(server_world, position[0], position[1], position[2], teleporterIn.getTargetYaw(), teleporterIn.getTargetPitch());
							//server_player.changeDimension(server_world, teleporterIn);
						} else {
							server_player.changeDimension(server_world, teleporterIn);
						
							if (!teleporterIn.playVanillaSound() && soundIn != null) {
								server_player.connection.send(new SPlaySoundEffectPacket(soundIn, SoundCategory.AMBIENT, position[0], position[1], position[2], 0.4F, 1));
							}
						}
					} else {
						server_player.changeDimension(server_world, teleporterIn);
					}
				}
			}
		}
	}

	public static void teleportPlayer(Entity entityIn, ServerWorld worldIn, double x, double y, double z, Set<SPlayerPositionLookPacket.Flags> relativelist, float yaw, float pitch) {
		
		if (entityIn instanceof ServerPlayerEntity) {
			ChunkPos chunkpos = new ChunkPos(new BlockPos(x, y, z));
			worldIn.getChunkSource().addRegionTicket(TicketType.POST_TELEPORT, chunkpos, 1, entityIn.getId());
			entityIn.stopRiding();
			if (((ServerPlayerEntity) entityIn).isSleeping()) {
				((ServerPlayerEntity) entityIn).stopSleepInBed(true, true);
			}

			if (worldIn == entityIn.level) {
				((ServerPlayerEntity) entityIn).connection.teleport(x, y, z, yaw, pitch, relativelist);
			} else {
				((ServerPlayerEntity) entityIn).teleportTo(worldIn, x, y, z, yaw, pitch);
			}

			entityIn.setYHeadRot(yaw);
		} else {
			float f1 = MathHelper.wrapDegrees(yaw);
			float f = MathHelper.wrapDegrees(pitch);
			f = MathHelper.clamp(f, -90.0F, 90.0F);
			if (worldIn == entityIn.level) {
				entityIn.moveTo(x, y, z, f1, f);
				entityIn.setYHeadRot(f1);
			} else {
				entityIn.unRide();
				Entity entity = entityIn;
				entityIn = entityIn.getType().create(worldIn);
				if (entityIn == null) {
					return;
				}

				entityIn.restoreFrom(entity);
				entityIn.moveTo(x, y, z, f1, f);
				entityIn.setYHeadRot(f1);
				worldIn.addFromAnotherDimension(entityIn);
			}
		}

		if (!(entityIn instanceof LivingEntity) || !((LivingEntity) entityIn).isFallFlying()) {
			entityIn.setDeltaMovement(entityIn.getDeltaMovement().multiply(1.0D, 0.0D, 1.0D));
			entityIn.setOnGround(true);
		}

		if (entityIn instanceof CreatureEntity) {
			((CreatureEntity) entityIn).getNavigation().stop();
		}
	}
}

 

As you can see, I have tried many variations on 

ServerPlayerEntity.teleport()

 

And I cannot just pull the 

ServerPlayerEntity.changeDimension()

method because it uses variables that are private.

Link to comment
Share on other sites

When teleporting within the same dimension no ITeleporter is necessary. Simply call ServerPlayerNetHandler#teleport with the target coordinates (this is what /teleport does).

When teleporting across dimensions you can use ServerPlayerNetHandler#teleportTo to just do a direct teleport, without any dimension change effects. To have those you need to call ServerPlayerEntity#changeDimension with a custom teleporter. There is no reason to copy any of this code.

Link to comment
Share on other sites

8 minutes ago, diesieben07 said:

When teleporting within the same dimension no ITeleporter is necessary. Simply call ServerPlayerNetHandler#teleport with the target coordinates (this is what /teleport does).

When teleporting across dimensions you can use ServerPlayerNetHandler#teleportTo to just do a direct teleport, without any dimension change effects. To have those you need to call ServerPlayerEntity#changeDimension with a custom teleporter. There is no reason to copy any of this code.

 

I have tried this:

playerIn.connection.teleport(targetPos.getX(), targetPos.getY(), targetPos.getZ(), yaw, pitch);

And I am still getting the same error:

[12:42:47] [Server thread/WARN] [minecraft/ServerPlayNetHandler]: TheCosmicNebula_ moved wrongly!

 

Link to comment
Share on other sites

Just now, diesieben07 said:

Show that code, specifically where exactly you are calling it.

 

Spoiler
	public void entityInside(BlockState stateIn, World worldIn, BlockPos posIn, Entity entityIn) {
		if (!entityIn.isPassenger() && !entityIn.isVehicle() && entityIn.canChangeDimensions()) {
			if (!this.dimension.getNamespace().isBlank() && !this.dimension.getPath().isBlank()) {
				RegistryKey<World> full_dim = RegistryKey.create(Registry.DIMENSION_REGISTRY, this.dimension);
				
				if (this.teleport_pos.getPos() != BlockPos.ZERO) {
					BlockPos targetPos = this.getTeleportPos();
					float yaw = this.teleport_pos.getYaw();
					float pitch = this.teleport_pos.getPitch();
					
					if (entityIn instanceof ServerPlayerEntity) {
						ServerPlayerEntity playerIn = (ServerPlayerEntity) entityIn;
						
						if (playerIn.level.dimension().equals(full_dim)) {
							playerIn.connection.teleport(targetPos.getX(), targetPos.getY(), targetPos.getZ(), yaw, pitch);
						} else {
							CosmosTeleporter teleporter = CosmosTeleporter.createTeleporter(this.getDimension(), targetPos, yaw, pitch, true, false, true);
							CosmosTeleportCore.shiftPlayerToDimension(playerIn, teleporter, null);
						}
					}
				}
			}
		}
	}

 

 

Link to comment
Share on other sites

Just now, diesieben07 said:

Are you sure you are only calling that once and not repeatedly every tick for the same entity?

 

I'm using vanilla code to create the Portal Blocks, so when the player is trying to enter the Portal it could be calling the code multiple times, but it works in Creative mode and when travelling across dimensions. Just not the same dimension in survival mode.

 

Here is where I pass the method to my TileEntity:

BlockPortal:

Spoiler
	@Override
	public void entityInside(BlockState stateIn, World worldIn, BlockPos posIn, Entity entityIn) {
		if (!worldIn.isClientSide) {
			TileEntity tile = worldIn.getBlockEntity(posIn);
			
			if (tile != null && tile instanceof TileEntityPortal) {
				((TileEntityPortal) tile).entityInside(stateIn, worldIn, posIn, entityIn);
			}
		}
	}
Link to comment
Share on other sites

  • Zeher_Monkey changed the title to [Solved] [1.16.5] Teleporting a player in the same Dimension using entityInside()

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.