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.

[SOLVED] (1.16.5) Modify PlayerInventory content from UI on the client

Featured Replies

Posted

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
  • Author

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
  • Author

Yes. Player inventory have network part. I have to send messages to server and perform all mouse item modifications there.

P.S. no need to perform them on both client and server, server would be enough

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

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.