Jump to content

[1.11.2] Spawn Custom Entity in world from subscribe event


MSpace-Dev

Recommended Posts

Hey all,

 

I am trying to spawn one of my entities in the world when a player right clicks on a creeper (for now)

 

	@SubscribeEvent
	public void interactEntity(PlayerInteractEvent.EntityInteract event)
	{
		if(event.getTarget().getClass().equals(EntityCreeper.class))
		{
          		Utils.getlogger.info("Creeper clicked");
			event.getWorld().spawnEntity(new EntitySpiritCreeper(event.getWorld()));
		}
	}

 

Note, the message does get logged. So the check up to the point of spawning code does work.

Nothing is spawning. However, I feel like I should be able to enter more parameters, like location and such. Thanks in advance.

Edited by MSpace-Dev
Link to comment
Share on other sites

You need to set location and angles of the entity. You can use the setLocationAndAngles method for that, from the Entity class.

        EntityModMob mob = new EntityModMob(event.getWorld());
        mob.setLocationAndAngles(DESIRED POSITION AND ROTATION GOES HERE);
        event.getWorld().spawnEntity(mob);

 

Edited by TheUnnamed
Link to comment
Share on other sites

Actually, still having an issue. Since the position of the target only is returned as an int, and not a float. The entity spawns aligned to the grid, rather at the exact location. Also, the rotation of the entity is not applied.

 

EntitySpiritCreeper creeper = new EntitySpiritCreeper(event.getWorld());
creeper.setPositionAndRotation(event.getTarget().getPosition().getX(), event.getTarget().getPosition().getY(), event.getTarget().getPosition().getZ(), event.getTarget().rotationYaw, event.getTarget().rotationPitch);
event.getWorld().spawnEntity(creeper);

Excuse the long line of code. Going to refactor soon.

I tried with both methods btw.

Link to comment
Share on other sites

Not 100% sure as I don't have a 1.11.2 workspace set up, but in 1.14.2 the Javadocs tells me that setRotationAndPosition() sets the position and rotation, clamping and wrapping params to valid values. Used by network code.

Whereas setLocationAndAngles() simply sets the location and Yaw/Pitch of the entity in the world.

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.

Announcements



×
×
  • Create New...

Important Information

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