Jump to content

[SOLVED] How to cancel an item drop/toss by player, without losing the item?


Evert

Recommended Posts

For a server-side mod, that can make some areas private, so other players can't break or build there.

I want to make that those players also can't drop(Q) items there.

 

I can use 'ItemTossEvent', but if that is canceled, the item disappears completely.

 

I tried 'ItemEvent', but that is called with item events where no players are involved and I can't find a way to determine what player dropped the item, so there is no way of knowing if the event has to be canceled.

 

What I'm looking for is:

- A method to return the lost item when using 'ItemTossEvent'.

or

- A method to determine the player name that dropped an item when using 'ItemEvent'.

or

- Some other way to make this work.

 

Thank you.

Link to comment
Share on other sites

Thank you. That works.

 

It is a bit buggy.

When an item is dropped with Q it gets replaced immediately, but when it is dropped with the right click from the inventory screen, it seems to disappear. It reappears later with an inventory change or after reconnecting.

 

	public void itemEvent(ItemTossEvent event)
{
	if ( event.isCancelable() )
	{
		ItemStack itemStack = event.entityItem.item;
		event.setCanceled(true);
		event.player.inventory.addItemStackToInventory(itemStack);
	}
}

Link to comment
Share on other sites

Thank you. That works.

 

It is a bit buggy.

When an item is dropped with Q it gets replaced immediately, but when it is dropped with the right click from the inventory screen, it seems to disappear. It reappears later with an inventory change or after reconnecting.

 

	public void itemEvent(ItemTossEvent event)
{
	if ( event.isCancelable() )
	{
		ItemStack itemStack = event.entityItem.item;
		event.setCanceled(true);
		event.player.inventory.addItemStackToInventory(itemStack);
	}
}

 

Maybe use:

event.player.inventory.onInventoryChanged();

 

after:

event.player.inventory.addItemStackToInventory(itemStack);

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Link to comment
Share on other sites

  • 2 months later...

Sorry for necrobumping, but does anybody have a solution?

 

Like i wrote before. It works, but when items are dropped from the inventory screen, they seem to disappear for a while, but they're not really lost. They reappear after some other action with the inventory or a reconnect.

It is a bit buggy.

When an item is dropped with Q it gets replaced immediately, but when it is dropped with the right click from the inventory screen, it seems to disappear. It reappears later with an inventory change or after reconnecting.

Using the drop key (default: Q) works fine.

 

	public void itemEvent(ItemTossEvent event)
{
	if ( event.isCancelable() )
	{
		ItemStack itemStack = event.entityItem.item;
		event.setCanceled(true);
		event.player.inventory.addItemStackToInventory(itemStack);
	}
}

 

Since mc 1.4.6, I had to change one line:

ItemStack itemStack = event.entityItem.func_92014_d();

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.