Jump to content

Recommended Posts

Posted

I've run into an awkward issue, and I'm not sure how to move forward. I have a storage item, that can hold other items. They can be added and removed by opening a container window, accessed by right clicking the storage item. The problem I'm trying to solve is that once the container is open, the player can drop the storage item on the ground, which allows the player to access the container even without the item. That makes no sense and causes undefined behavior, so it definitely needs fixing.

I tried to use ItemTossEvent to detect when the item is dropped, and close the container when it's called, like so:

	@SubscribeEvent
	public void onTossedItem(ItemTossEvent event) {
		if(event.getEntityItem().getItem().getItem() instanceof MyItem & event.getPlayer().openContainer instanceof MyContainer) {
			event.getPlayer().closeScreen();
		}
	}

 

This does work in some circumstances; if you press Q on the item slot with the correct item in it, the code works as intended. However, if you try to drag and drop an item off screen, things get messy! Closing the screen calls Container.onContainerClosed, which drops the itemstack in the players hand, which triggers the ItemTossEvent, which calls Container.onContainerClosed, so on and so forth until a StackOverflowException shuts down the recursion.

How do I pull this off without getting getting stuck in a recursive loop? My immediate ideas are hacky, and would break in the event that the player presses Q on the item...

Posted
36 minutes ago, pacguy said:

The problem I'm trying to solve is that once the container is open, the player can drop the storage item on the ground, which allows the player to access the container even without the item. That makes no sense and causes undefined behavior, so it definitely needs fixing.

Here is what I would do: override the canInteractWith() method in your custom container, and return false if the player is no longer holding your storage item.

  • Like 1

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.