Jump to content

[1.12] Spawn animal on a leash


Tuhljin

Recommended Posts

One of my village buildings - upon its creation - spawns an animal and puts it on a leash, attached to a fence. This works fine. The problem, however, is that the leash does not render until the player exits the world and then loads it back up.

 

I think that the problem has to do with the packet which informs about the leash state not being sent to the player due to the player not being considered to be "watching" the entity at the time entity.SetLeashedToEntity (which sends the packet) is called.

 

Spoiler

public class TestHouse extends ModVillagePiece { // extends StructureVillagePieces.Village
	// snip

	@Override
	public boolean addComponentParts(World worldIn, Random randomIn, StructureBoundingBox structureBoundingBoxIn)
	{
		boolean ret = super.addComponentParts(worldIn, randomIn, structureBoundingBoxIn);
		if (!ret)  return ret;
		// snip -- Blocks are placed here, including the fence block. This part is working fine.

		if (!spawnedAnimal) {
				EntitySheep sheep = new EntitySheep(worldIn);
				sheep.setGrowingAge(-24000); // value for new baby in EntityAIMate
				spawnedAnimal = spawnLeashedAnimal(worldIn, sheep, pos.getX(), pos.getY(), pos.getZ());
		}

		return true;
	}

	protected boolean spawnLeashedAnimal(World world, EntityLiving entity, int x, int y, int z)
	{
		// x, y, and z are relative. Get the real position:
		BlockPos fencePos = getRealPos(x, y, z);
		BlockPos animalPos = null;
		for (EnumFacing d : EnumFacing.HORIZONTALS) {
			BlockPos p = fencePos.offset(d);
			if (world.isAirBlock(p)) {
				animalPos = p;
				break;
			}
		}
		if (animalPos == null)  return false;

		entity.setLocationAndAngles(animalPos.getX(), animalPos.getY(), animalPos.getZ(), 0.0F, 0.0F);
		world.spawnEntity(entity);

		EntityLeashKnot entityleashknot = EntityLeashKnot.getKnotForPosition(world, fencePos);
		if (entityleashknot == null)  entityleashknot = EntityLeashKnot.createKnot(world, fencePos);
		entity.setLeashedToEntity(entityleashknot, true);

		return true;
	}

	protected BlockPos getRealPos(BlockPos pos)
	{
		return getRealPos(pos.getX(), pos.getY(), pos.getZ());
	}

	protected BlockPos getRealPos(int x, int y, int z)
	{
		return new BlockPos(this.getXWithOffset(x, z), this.getYWithOffset(y), this.getZWithOffset(x, z));
	}

}

 

 

Like I said, the animal is spawned fine and it is properly attached to the fence by a lead, but the lead doesn't render. How can I make it render without needing the player to exit and rejoin the world?

 

I tried sending the packet myself at the end of spawnLeashedAnimal, but of course that doesn't work (presumably for the same reason that the packet doesn't work when it's sent by entity.setLeashedToEntity). If I could reliably send the packet a little later, I think that would work, though. I know that if you send the packet later on, the leash starts rendering, since I created a test item which, on right-click, looks at entities in the area and sends those packet(s) out.

 

Spoiler

public class ItemTest extends ModItem {

	public ItemTest() {
		super("test");
	}

	@Override
	public ActionResult<ItemStack> onItemRightClick(World worldIn,
			EntityPlayer playerIn, EnumHand handIn) {
		if (!worldIn.isRemote) {
			List<EntityLiving> list = worldIn.<EntityLiving>getEntitiesWithinAABB(EntityLiving.class, new AxisAlignedBB(playerIn.posX - 7.0D, playerIn.posY - 7.0D, playerIn.posZ - 7.0D, playerIn.posX + 7.0D, playerIn.posY + 7.0D, playerIn.posZ + 7.0D));
			for (EntityLiving entityliving : list) {
				if (entityliving.getLeashed()) {
					if (worldIn instanceof WorldServer) {
						((WorldServer)worldIn).getEntityTracker().sendToTracking(entityliving, new SPacketEntityAttach(entityliving, entityliving.getLeashedToEntity()));
      				// This is the same packet that entity.setLeashedToEntity should send.
					}
				}
			}
		}
		return super.onItemRightClick(worldIn, playerIn, handIn);
	}


}

 

 

Is there maybe some way to delay sending the packet to a moment after the world generation bit is done and presumably the player is now "watching" the entity? Or if there a better way to fix the rendering problem, what would that be?

Edited by Tuhljin
Indicate MC version in title
Link to comment
Share on other sites

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.



×
×
  • Create New...

Important Information

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