Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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

  • Author

I've tried 

world.spawnEntity(entity);

both before and after attaching the leash. It didn't make a difference.

Edited by Tuhljin

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.