View difference between Paste ID: hQiU3RVQ and XCD91dv4
SHOW: | | - or go back to the newest paste.
1
// transfer entity to dimension. do not transfer player using this method! use transferPlayerToDimension
2
	static boolean transferEntityToDimension(Entity sourceEntity, int destinationDimension, double x, double y, double z)
3
	{
4
		WorldServer sourceWorldServer = MinecraftServer.getServer().worldServerForDimension(sourceEntity.dimension);
5
		WorldServer destinationWorldServer = MinecraftServer.getServer().worldServerForDimension(destinationDimension);
6
		
7
		if(sourceWorldServer == null || destinationWorldServer == null)
8
		{
9
			return false;
10
		}
11
		
12
		NBTTagCompound tagCompound = new NBTTagCompound();
13
		
14
		// if write fails entity is a mount; must write parent to instead
15
        while(sourceEntity.writeToNBTOptional(tagCompound) == false)
16
        {
17
        	sourceEntity = sourceEntity.riddenByEntity;
18
        	sourceEntity.writeToNBTOptional(tagCompound);
19
        }
20
21
        // get rotation values from entity
22
		float rotationYaw = sourceEntity.rotationYaw;
23
		float rotationPitch = sourceEntity.rotationPitch;
24
		
25
		// remove entity from source world
26
        sourceWorldServer.removeEntity(sourceEntity);
27
               
28
        // create entity from saved nbt tag
29
        Entity destinationEntity = EntityList.createEntityFromNBT(tagCompound, destinationWorldServer);
30
        
31
        // set entity location and orientation
32
        destinationEntity.setLocationAndAngles(x, y, z, rotationYaw, rotationPitch);     
33
34
        // spawn entity in destination world
35
        destinationWorldServer.spawnEntityInWorld(destinationEntity);   
36
        
37
        // register destination entity with IExtendedEntityProperties - TeleporterEntity
38
        TeleporterEntity destinationEntityProperties = TeleporterEntity.get(destinationEntity);
39
        if(destinationEntityProperties == null)
40
        {
41
        	TeleporterEntity.register(destinationEntity);
42
        }
43
            
44
        // so the entity doesn't teleport again when the dimension loads
45
        destinationEntityProperties.setOnTeleporter(true);
46
        destinationEntityProperties.setTeleported(true);
47
        		
48
        // finally, apply the teleportation transfer to ensure the enemy is in the correct location
49
        TeleporterEntity.transferToLocation(destinationEntity, x, y, z);
50
		
51
		return true;
52
	}