Jump to content

Recommended Posts

Posted

So I want the player to drop 16 blocks of dirt when crouching(yes I know, very funny) but the items just won't spawn. The event definitelly fires.

Here's the code.

PlayerEntity p = event.getPlayer();
ItemEntity dirt = new ItemEntity(p.world, p.getPosX(), p.getPosY(), p.getPosZ(), new ItemStack(Items.DIRT, 16));
dirt.setLocationAndAngles(p.getPosX(), p.getPosY(), p.getPosZ(), 0.0F, 0.0F);
p.world.addEntity(dirt);

 

Posted
1 hour ago, Error_Coding said:

So I want the player to drop 16 blocks of dirt when crouching(yes I know, very funny) but the items just won't spawn. The event definitelly fires.

Here's the code.


PlayerEntity p = event.getPlayer();
ItemEntity dirt = new ItemEntity(p.world, p.getPosX(), p.getPosY(), p.getPosZ(), new ItemStack(Items.DIRT, 16));
dirt.setLocationAndAngles(p.getPosX(), p.getPosY(), p.getPosZ(), 0.0F, 0.0F);
p.world.addEntity(dirt);

 

I see nothing wrong with the code, could you post more context?

Posted
3 minutes ago, vemerion said:

I see nothing wrong with the code, could you post more context?

There isn't much more to it but sure, here's the whole class.

@Mod.EventBusSubscriber(modid = FirstMod.MOD_ID, bus = Bus.FORGE)
public class DropDirtOnCrouchEvent {
	
	@SubscribeEvent
	public static void dropDirtOnCrouchEvent(InputUpdateEvent event) {
		if(event.getMovementInput().sneaking) {
			PlayerEntity p = event.getPlayer();
			ItemEntity dirt = new ItemEntity(p.world, p.getPosX(), p.getPosY(), p.getPosZ(), new ItemStack(Items.DIRT, 16));
			dirt.setLocationAndAngles(p.getPosX(), p.getPosY(), p.getPosZ(), 0.0F, 0.0F);
			p.world.addEntity(dirt);
		}
	}
}

I'm pretty sure that the error has to be in the code snippet I posted above tho(I tried adding a potion effect to the player to check if the event fires and that worked)

Posted
1 minute ago, Error_Coding said:

There isn't much more to it but sure, here's the whole class.

InputUpdateEvent is a client only event, you can not add entities to the world from the client.

Posted
6 minutes ago, Error_Coding said:

 

Oh, thanks. Guess I will have to read into packet transfer.

You could try to listen to the PlayerTickEvent and use the isSneaking() method on the player. That way you would avoid sending extra packets.

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.