Jump to content

[SOLVED][1.12.2] Prevent item from going to inventory after closing crafting grid


Recommended Posts

Posted (edited)

I have a block that should only be allowed in the player's hot bar--not their regular inventory slots and not in other containers. So far I have been using GuiScreenEvent to detect the various mouse clicks and keyboard shortcuts that move items around. It's working except one scenario. If the player puts the block in the crafting grid, fills up their hot bar, then closes the crafting GUI with the block still in the crafting grid--the block automatically gets put into their regular inventory (assuming they have empty slots there).

 

How can I detect this situation? I've been hoping to avoid the need to loop through every player's inventory every tick to search for the block.

Edited by Daeruin
Posted
3 hours ago, Daeruin said:

If the player puts the block in the crafting grid, fills up their hot bar, then closes the crafting GUI with the block still in the crafting grid--the block automatically gets put into their regular inventory (assuming they have empty slots there).

Post your code.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Posted

I didn't originally post any code, because I was asking about ideas for something I hadn't written code for yet. I have since attempted something. This is my current attempt to remove my block from the regular inventory after closing the crafting table container. The only problem is that removing the item from the slot doesn't get synced to the client.

 

Spoiler

    @SubscribeEvent
    public void onContainerClosed(PlayerContainerEvent.Close event)
    {
        Container container = event.getContainer();
        List<Slot> slotList = container.inventorySlots;

        for (int i = 0; i < slotList.size(); i++)
        {
            Slot slot = container.inventorySlots.get(i);
            IInventory inventoryForSlot = slot.inventory;

            if (inventoryForSlot instanceof InventoryPlayer && slot.getSlotIndex() > 8) // I think this guarantees the slot is NOT in the hot bar
            {
                ItemStack stackInSlot = slot.getStack().copy();

                if (stackInSlot.getItem() instanceof ItemBlockBasket)
                {
                    event.getEntityPlayer().dropItem(stackInSlot, false);
                    // None of these methods sync to the client
                    slot.putStack(ItemStack.EMPTY);
                    inventoryForSlot.removeStackFromSlot(slot.getSlotIndex());
                    event.getEntityPlayer().inventory.setInventorySlotContents(slot.getSlotIndex(), ItemStack.EMPTY);
                    container.putStackInSlot(i, ItemStack.EMPTY);
                    // These methods don't seem to cause the removed item to be synced to the client
                    container.detectAndSendChanges();
                    inventoryForSlot.markDirty();
                }
            }
        }
    }

 

 

Current working code that prevents my block from being placed in the regular inventory via mouse clicks or keyboard.

 

Spoiler

    @SubscribeEvent
    public void guiMouse(GuiScreenEvent.MouseInputEvent.Pre event)
    {
        GuiScreen gui = event.getGui();

        if (gui instanceof GuiContainer)
        {
            GuiContainer guiContainer = (GuiContainer) event.getGui();
            Slot slotUnderMouse = guiContainer.getSlotUnderMouse();

            if (slotUnderMouse != null)
            {
                int slotIndex = slotUnderMouse.getSlotIndex();
                Item itemOnCursor = Minecraft.getMinecraft().player.inventory.getItemStack().getItem(); // Get stack held by the mouse

                // Clicking in regular inventory (not hot bar) with basket on cursor - cancel event (don't place basket in inventory)
                if (itemOnCursor instanceof ItemBlockBasket && slotIndex > 8)
                {
                    event.setCanceled(true);
                }

                ItemStack stackUnderMouse = slotUnderMouse.getStack();

                // Clicking on basket in hot bar with shift key down
                if (!stackUnderMouse.isEmpty() && stackUnderMouse.getItem() instanceof ItemBlockBasket && slotIndex < 9 && GuiContainer.isShiftKeyDown())
                {
                    event.setCanceled(true);
                }
            }
        }
    }

    @SubscribeEvent
    public void guiKey(GuiScreenEvent.KeyboardInputEvent.Pre event)
    {
        if (event.getGui() instanceof GuiContainer)
        {
            GuiContainer guiContainer = (GuiContainer) event.getGui();
            Slot slotUnderMouse = guiContainer.getSlotUnderMouse();

            // Prevents basket from going to hotbar
            if (slotUnderMouse != null)
            {
                ItemStack stackInSlotUnderMouse = slotUnderMouse.getStack();

                for (int i = 0; i < 9; ++i)
                {
                    Minecraft minecraft = Minecraft.getMinecraft();
                    boolean keyPressedIsHotbarKey = minecraft.gameSettings.keyBindsHotbar[i].isActiveAndMatches(Keyboard.getEventKey());

                    if (keyPressedIsHotbarKey)
                    {
                        ItemStack stackInTargetHotbarSlot = minecraft.player.inventory.mainInventory.get(i);

                        // Destination hotbar slot has a basket
                        if (!stackInTargetHotbarSlot.isEmpty() && stackInTargetHotbarSlot.getItem() instanceof ItemBlockBasket)
                        {
                            IInventory inventoryUnderMouse = slotUnderMouse.inventory;

                            if (!(inventoryUnderMouse instanceof InventoryPlayer || inventoryUnderMouse instanceof InventoryCrafting) || slotUnderMouse.getSlotIndex() > 8)
                            {
                                event.setCanceled(true);
                            }
                        }
                    }
                }
            }
        }
    }

 

 

Posted

I did this:

 

event.getEntityPlayer().inventoryContainer.detectAndSendChanges();
event.getEntityPlayer().openContainer.detectAndSendChanges();

 

No dice. When I open my inventory, the item still appears to be there, but if I try to click on it, it immediately disappears. If I open another container, like the crafting table, the item disappears.

Posted

Latest code:

 

    @SubscribeEvent
    public void onContainerClosed(PlayerContainerEvent.Close event)
    {
        Container container = event.getContainer();
        List<Slot> slotList = container.inventorySlots;

        for (int i = 0; i < slotList.size(); i++)
        {
            Slot slot = container.inventorySlots.get(i);
            IInventory inventoryForSlot = slot.inventory;

            if (inventoryForSlot instanceof InventoryPlayer && slot.getSlotIndex() > 8) // I think this guarantees the slot is NOT in the hot bar
            {
                ItemStack stackInSlot = slot.getStack().copy();

                if (stackInSlot.getItem() instanceof ItemBlockBasket)
                {
                    event.getEntityPlayer().dropItem(stackInSlot, false); // Spawns the EntityItem
                    event.getEntityPlayer().inventory.setInventorySlotContents(slot.getSlotIndex(), ItemStack.EMPTY);
                    event.getEntityPlayer().inventoryContainer.detectAndSendChanges();
                }
            }
        }
    }

 

Posted

Alright, I found something that seems to be working:

 

EntityPlayerMP player = (EntityPlayerMP)event.getEntityPlayer();
player.dropItem(stackInSlot, false); // Spawns the EntityItem
player.inventoryContainer.putStackInSlot(i, ItemStack.EMPTY);
player.sendContainerToPlayer(player.inventoryContainer);

 

This only works in my regular survival inventory so far (placing item in the 4x4 crafting grid and closing it with hot bar full so item is forced to go into my regular inventory). If I do it with a crafting table, there seems to be something wrong with my slot indexes, because I'm getting two copies of the item, with one of them in a slot I don't expect. I'll try to figure out that problem tomorrow.

Posted

Found the problem. I was mixing up slot index with slot number (for like the tenth time). Slot indexes start over for each inventory, but slot numbers are continuous across inventories (like a chest inventory and survival inventory). I also made my code a little more efficient by only searching the player's inventory, since that's where I don't want items to end up. Here's the final code, for anyone who's interested.

 

Spoiler

    @SubscribeEvent
    public void onContainerClosed(PlayerContainerEvent.Close event)
    {
        EntityPlayerMP player = (EntityPlayerMP) event.getEntityPlayer();
        Container playerContainer = player.inventoryContainer;

        for (Slot slot : playerContainer.inventorySlots)
        {
            int slotIndex = slot.getSlotIndex();

            if (slotIndex > 8) // This slot isn't in the hot bar
            {
                ItemStack stackInSlot = slot.getStack().copy();

                if (stackInSlot.getItem() instanceof ItemBlockBasket)
                {
                    player.dropItem(stackInSlot, false); // Spawns the EntityItem
                    playerContainer.putStackInSlot(slotIndex, ItemStack.EMPTY); // Deletes basket from inventory
                    player.sendContainerToPlayer(player.inventoryContainer); // Syncs client with server by sending entire Container to client
                }
            }
        }
}

 

 

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.