Jump to content

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


Zeher_Monkey

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

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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Etumax Royal Honey Price In Pakistan - Get Free Delivery Shop Today Online With Online Shopping in Pakistan Etumax Royal Honey Available At Our Store Online Shopping in Pakistan, 
    • I want to make a tree decorator that will generate a beehive under branches of my tree. I have no idea how to check for branches and make beehives generate because TreeDecorator.Context.logs() is just a block pos and i dont understand how it works. i hope ill get an answer here.
    • Imagine this: you've painstakingly accumulated $97,000 worth of Bitcoin, only to see it vanish into the digital abyss at the hands of cunning scammers. It's a devastating blow, leaving you feeling helpless and betrayed. But fear not, for Lee Ultimate Hacker is here to turn the tide in your favor. After conducting extensive research on cryptocurrency recovery options, I stumbled upon Lee Ultimate Hacker, and it proved to be the most suitable choice for the daunting task at hand. Despite my initial skepticism, they shattered my doubts by successfully retrieving $92,000 of the lost Bitcoin—a feat I once deemed impossible. From the moment I reached out to Lee Ultimate Hacker and provided them with all the pertinent information about the fraudulent transaction, they sprang into action with unwavering determination. True to their word, they delivered on their promise to recover the lost Bitcoin within an impressive timeframe of 24 to 72 hours. Their professionalism, expertise, and commitment to their clients were truly commendable, transforming what seemed like an insurmountable ordeal into a resounding triumph. In my eyes, the investment of both time and money was more than justified by the remarkable outcome achieved by Lee Ultimate Hacker. So, if you've fallen victim to cryptocurrency scams and are grappling with the anguish of lost funds, don't despair. Reach out to Lee Ultimate Hacker and let them work their magic. Their track record of success speaks for itself, and with their assistance, you can reclaim what's rightfully yours and emerge stronger than ever before. Don't let the darkness of cybercrime overshadow your financial future. Take a stand against fraudsters with the help of Lee Ultimate Hacker, and witness the transformation from despair to triumph. Your journey to recovery starts here. LEEULTIMATEHACKER@ AOL. COM or Support @ leeultimatehacker . com. telegram:LEEULTIMATE or wh@tsapp +1  (715) 314  -  9248 https://leeultimatehacker.com Thank you.
    • There's a scheme I got into where they promised to trade Bitcoin for me and take a cut as a commission. Seemed like a good idea at the time. But then, things went south real fast. They ended up transferring   $190,000 worth of my Bitcoin. I was devastated and felt completely helpless. That's when I stumbled upon the Wizard Web Recovery Tool. It was like a beacon of hope amid chaos. With this tool, I could finally start digging into what went wrong and hopefully get my Bitcoin back. Using Wizard Web was surprisingly easy. I just had to plug in some details about my Bitcoin account and let it do its thing. It started scanning the internet, looking for any clues about what happened to my Bitcoin. It felt like having a detective on my side, searching for answers. And guess what? Wizard Web found some leads. It uncovered evidence of the scheme's shady dealings and helped me track down the people responsible for losing my Bitcoin. Armed with this information, I took the case to court. After a long and hard-fought legal battle, the court ruled in my favor. The perpetrators were held accountable for their actions and faced criminal charges for their involvement in the scheme. It was a victory not just for me, but for anyone who's been taken advantage of by these kinds of scams. Thanks to Wizard Web Recovery, I was able to get justice and reclaim what was rightfully mine. It showed me that even in the face of adversity, there's always a way to fight back. And with the right tools and determination, anything is possible.   The following is the contact information for Wizard Web Recovery.   Email: wizard web recovery((@))programmer . net
    • Hello, good morning. I know some programming and I'm interested in mod creation. That's why I've decided to follow a tutorial guide on YouTube by TurtyWurty. https://www.youtube.com/watch?v=DhoX9cmAZqA&t=160s&ab_channel=TurtyWurty I've followed the tutorial perfectly. The problem is that when checking the food, the texture doesn't load for me. However, everything seems fine no matter how much I check. I'm sure it's something trivial, the problem is that I can't find it. Could you help me solve it, please? I leave a zip of my file so you can edit it freely. forge-1.20-Civicraft.rar
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.