Jump to content

[1.18.2] Prevent item from being placed in inventory


Recommended Posts

I'm trying to prevent my block from being placed in certain vanilla inventories. I'm using MouseClickedEvent.Pre. By canceling the event, I can prevent items from being picked up by the mouse cursor, but for some reason canceling the event doesn't prevent items from being placed into a slot. If there's a better way other than listening for mouse clicks, I'm all ears.

Here's my code - the full logic isn't in place, I'm just trying to successfully prevent the block from being placed in any inventory to start off with.

    public static void onMouseInput(ScreenEvent.MouseClickedEvent.Pre event)
        Screen screen = event.getScreen();

        // If screen isn't a container, we don't care - exit
        if (!(screen instanceof AbstractContainerScreen)) return;

        Slot slotUnderMouse = ((AbstractContainerScreen<?>) screen).getSlotUnderMouse();

        // If player didn't click on a slot, we don't care - exit
        if (slotUnderMouse == null) return;

        Item itemOnCursor = ((AbstractContainerScreen<?>) screen).getMenu().getCarried().getItem();

        if (PrimalConfig.basketsRestrictedToHotBar.get()
                && itemOnCursor instanceof BasketBlockItem
                && slotUnderMouse.container instanceof Inventory)



Link to comment
Share on other sites

Just so you know, that's not the only way items can end up inside block inventories.

I know there's a way for the inventory to determine if an item is valid for its slot, but I'm not sure there's a reciprocal call asking the item if it can be placed in the slot.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.


Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.


DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

I'm aware. There are lots of ways for items to end up in a given slot. It's all stuff I handled in 1.12, just struggling to figure out how to do the same thing in 1.18. If I could modify the vanilla inventories or slots to override what items they allow, it would be much easier. Like if there were an event that hooked into Slot#mayPlace, I would be ecstatic.

I think I actually figured it out just now. Apparently there's a MouseReleasedEvent now, and I had to use that instead. I guess it's the release of the button that actually triggers the item going into the slot.

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.

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.


  • Create New...

Important Information

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