pacguy Posted November 17, 2020 Share Posted November 17, 2020 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... Quote Link to comment Share on other sites More sharing options...
vemerion Posted November 17, 2020 Share Posted November 17, 2020 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. 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.