Daeruin Posted March 12, 2023 Posted March 12, 2023 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. Reveal hidden contents @SubscribeEvent 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) { event.setCanceled(true); } } Quote
Draco18s Posted March 13, 2023 Posted March 13, 2023 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. Quote 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.
Daeruin Posted March 13, 2023 Author Posted March 13, 2023 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. Quote
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.