Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

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...

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.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.