Jump to content

[1.11.2] Consume Item when GuiButton is pressed


SuperHB

Recommended Posts

What I'm trying to accomplish is when the button is pressed it will consume an item in the players inventory.

 

This is what I have currently

@Override
    protected void actionPerformed (GuiButton button) throws IOException {
        if (button == insertCoin) {
            if (menu == -1){
                EntityPlayer player = Minecraft.getMinecraft().player;
                if (player.inventory.hasItemStack(new ItemStack(ArcadeItems.coin))) {
                    ItemStack coin = player.inventory.getStackInSlot(player.inventory.getSlotFor(new ItemStack(ArcadeItems.coin)));
                    if (coin.getCount() > cost) {
                        int slot = player.inventory.getSlotFor(new ItemStack(ArcadeItems.coin));
                        ItemStack newStack = player.inventory.decrStackSize(slot, coin.getCount() - cost);
                        player.inventory.setInventorySlotContents(slot, newStack);
                        menu = 0;
                    } else if (coin.getCount() == cost) {
                        player.inventory.setInventorySlotContents(player.inventory.getSlotFor(new ItemStack(ArcadeItems.coin)), ItemStack.EMPTY);
                        menu = 0;
                    } else enoughCoins = false;
                } else enoughCoins = false;
            }
        }
    }

 

It does remove a coin, but it only updates client side so relogging into the world I would have the original numbered stack.

 

How do I properly update the player's inventory?

Link to comment
Share on other sites

49 minutes ago, V0idWa1k3r said:

You need to send a custom packet to the server and let the server do everything you need to. You also should never trust a client in general so most of your checks and actions should be server-side.

Thank you. That helped. But what would be the best way to do the checks server-side?

Link to comment
Share on other sites

Just now, SuperHB said:

what would be the best way to do the checks server-side?

In your GUI only send the packet with most basic info that allows you to identify what the player is trying to do. On the server get all the values, perform the checks and send the response if needed. The approach is to do all logic on the server and simply notify the client when appropriate.

  • Like 1
Link to comment
Share on other sites

2 hours ago, V0idWa1k3r said:

In your GUI only send the packet with most basic info that allows you to identify what the player is trying to do. On the server get all the values, perform the checks and send the response if needed. The approach is to do all logic on the server and simply notify the client when appropriate.

I was able to move all the checks to server-side. But how do I send data back to the client? I've been looking around and haven't been able to find how to receive the return from the onMessage

Link to comment
Share on other sites

1 hour ago, SuperHB said:

I was able to move all the checks to server-side. But how do I send data back to the client? I've been looking around and haven't been able to find how to receive the return from the onMessage

 

Just send another packet. The return value of IMessageHandler#onMessage is largely useless now that packets are handled on a network thread and all the logic needs to be run on the main thread.

  • Like 1

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

11 minutes ago, SuperHB said:

How do I get the packet data from my Gui class?

 

When the server-to-client packet is received by the client, you can access the current GUI from the Minecraft#currentScreen field. If it's an instance of your GUI class, you can update it with the data from the packet.

  • Like 1

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

11 hours ago, Choonster said:

 

When the server-to-client packet is received by the client, you can access the current GUI from the Minecraft#currentScreen field. If it's an instance of your GUI class, you can update it with the data from the packet.

Thank you! I got everything working now.

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.

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.