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

Hello,

I am trying to create a new GUI using Minecraft Forge and GUI****, however when I try to use this math to display a charge meter, for some obscure reason chargePx always returns 0 and the image is not displayed at all. Can anyone help me, or does anyone know why this would be happening?

 

int barHeight = 32;
int chargeMeter = 50; //Charge %
int chargePx = Math.round(barHeight * (chargeMeter / 100));
this.drawTexturedModalRect(x + 20, y + 16, 176, 16, 16, chargePx);

 

P.s. I tested out the sum on a scientific calculator, and it equalled 16, as it should. This is why I don't understand why Java cannot do simple calculations such as this.

 

I would also like to know how to find the contents of a particular slot in a GUI, in order to use up an item in a specific slot if said item is redstone, for example.

Your math is a bit incorrect, Try using this:

 

int chargePx = Math.round((float)barHeight * ((float)chargeMeter / 100F));

 

 

The issue is caused because you try to divide integers, which rounds down the values automatically.

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Okay, thank you very much. I'll change the math now :)

Any ideas for checking contents of a slot?

 

In which way? Nvm

 

Just get the slot ItemStack, check if it's equal to the redstone item and reduce the stackSize. Like this:

ItemStack stack = tileentity.getStackInSlot(SLOT_ID);
if(stack.isItemEqual(new ItemStack(Item.redstone))) {
    tileentity.decrStackSize(SLOT_ID, 1);
}

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

  • Author

I need to be able to check the contents (I assume an ItemStack) of a particular slot in my GUI, so I can use an if to check if the item in the slot matches redstone, to fill up my meter.

Check out my edit above.

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

  • Author

So in which class should I put this code, and where? GUI***?

I'm getting a "cannot make a static reference to thee non-static method".

You should put that in your TileEntity in your updateEntity method, because when placed in the GUI, it won't edit anything on the server and will be reset again.

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

  • Author

Okay, that works, so now how can I change the charge meter value, which is in drawGuiContainerBackgroundLayer()?

Also, why am I getting a NullPointerException with that last code?

whoops, forgot to check if it's null:

ItemStack stack = tileentity.getStackInSlot(SLOT_ID);
if(stack != null && stack.isItemEqual(new ItemStack(Item.redstone))) {
    tileentity.decrStackSize(SLOT_ID, 1);
}

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

  • Author

Okay, all is good, now i need to know how to change how filled up the meter is. Now the meter integer (chargeMeter) is in the draw background method, and I need to update it every time the redstone is filled up. It all works! :)

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.