Jump to content

Recommended Posts

Posted (edited)

Basically, I have some UI with area where player can place and move items as it want. This area has no grid and slots at all. I even can say, that these objects inside that area not exactly items, they just have item icons. However I had to write container to be able to display player inventory on my screen. Now I want to be able to take and add items to mouse slot on my own whenever I want, but PlayerInventory seems to have only getter getItemStack.

Below you can see my hacky implementation, where I'm emulating clicks on some transition slot. It, actually, doesn't work completely. Despite the fact that I'm cleaning my transition slot, sometimes I still have some "trash" inside it. Taking items to mouse doesn't work too, sometimes. Does containers have server-side logic? It all just looks like I'm working with invalid remote data
 

    private final Inventory transitionInventory = new Inventory(1);
    private final Slot transitionSlot = new Slot(transitionInventory, 0, -10000, -10000);

    public void putItemFromScreen(ResearchTableScreen screen, int mouseX, int mouseY) {
        screen.mouseClicked(screen.getGuiLeft() + transitionSlot.xPos, screen.getGuiTop() + transitionSlot.yPos, 1);
        screen.mouseReleased(screen.getGuiLeft() + transitionSlot.xPos, screen.getGuiTop() + transitionSlot.yPos, 1);
        if (!transitionSlot.getStack().isEmpty())
        {
            ResearchTableEntity.ItemState itemState = new ResearchTableEntity.ItemState();
            itemState.item = transitionSlot.getStack().copy();
            itemState.pos = screen.getTableRect().getLocalPointLocation(mouseX, mouseY);
            itemState.pos.x = Utils.clamp(itemState.pos.x, 8, screen.getTableRect().width - 8);
            itemState.pos.y = Utils.clamp(itemState.pos.y, 8, screen.getTableRect().height - 8);

            researchTableEntity.putItemState(itemState);
            transitionInventory.clear();
        }
    }

    public void putItemToScreen(ResearchTableScreen screen, ResearchTableEntity.ItemState itemState) {
        PlayerInventory playerInventory = Minecraft.getInstance().player.inventory;
        if (itemState != null && playerInventory.getItemStack().isEmpty()) {
            transitionSlot.putStack(itemState.item);
            screen.mouseClicked(screen.getGuiLeft() + transitionSlot.xPos, screen.getGuiTop() + transitionSlot.yPos, 1);
            screen.mouseReleased(screen.getGuiLeft() + transitionSlot.xPos, screen.getGuiTop() + transitionSlot.yPos, 1);

            researchTableEntity.removeItemState(itemState);
            transitionInventory.clear();
        }
    }

 

Edited by Soft-fur dragon
closed
  • Soft-fur dragon changed the title to (1.16.5) Access PlayerInventory mouse slot from container
Posted

Don't know why, but I did not noticed setItemStack function... However, it still doesn't work and my question about network is still relevant

P.S. This is my new code. It being called from UI

    public void putItemFromScreen(ResearchTableScreen screen, int mouseX, int mouseY) {
        PlayerInventory playerInventory = Minecraft.getInstance().player.inventory;
        if (!playerInventory.getItemStack().isEmpty())
        {
            ResearchTableEntity.ItemState itemState = new ResearchTableEntity.ItemState();
            itemState.item = playerInventory.getItemStack().copy();
            itemState.item.setCount(1);
            playerInventory.getItemStack().shrink(1);

            itemState.pos = screen.getTableRect().getLocalPointLocation(mouseX, mouseY);
            itemState.pos.x = Utils.clamp(itemState.pos.x, 8, screen.getTableRect().width - 8);
            itemState.pos.y = Utils.clamp(itemState.pos.y, 8, screen.getTableRect().height - 8);

            researchTableEntity.putItemState(itemState);
        }
    }

    public void putItemToScreen(ResearchTableScreen screen, ResearchTableEntity.ItemState itemState) {
        PlayerInventory playerInventory = Minecraft.getInstance().player.inventory;
        if (itemState != null && (playerInventory.getItemStack().isEmpty() || playerInventory.getItemStack().isItemEqual(itemState.item))) {
            if (playerInventory.getItemStack().isEmpty()) {
                playerInventory.setItemStack(itemState.item.copy());
                playerInventory.getItemStack().setCount(1);
            } else {
                playerInventory.getItemStack().grow(1);
            }

            researchTableEntity.removeItemState(itemState);
        }
    }

 

  • Soft-fur dragon changed the title to (1.16.5) Modify PlayerInventory content from UI on the client
  • Soft-fur dragon changed the title to [SOLVED] (1.16.5) Modify PlayerInventory content from UI on the 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.