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.

Featured Replies

Posted

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?

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.

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

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.

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

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.

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.

  • Author
57 minutes ago, Choonster said:

 

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.

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

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.

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.

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

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.