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

So I've made a very simple "energy bar" gui thing xD

It currently displays in the top left of the screen. Whenever it's displayed, it pauses the whole game even though I'm not telling it to.

 

Here's the Gui class itself:

public class GuiEnergyBar extends GuiScreen {

    private int xSize = 116, ySize = 17; //size of desired window. Max is 256x256

    @Override
    public void initGui(){
        super.initGui();

    }

    @Override
    public void drawScreen(int mouseX, int mouseY, float partialTicks){
        super.drawScreen(mouseX, mouseY, partialTicks);

        TextureManager textureManager = this.mc.getTextureManager();

        textureManager.bindTexture(new ResourceLocation(Test.MODID + ":textures/gui/energybar.png"));

        GlStateManager.pushMatrix();
        GlStateManager.enableAlpha();
        drawTexturedModalRect(0, 0, 0, 0, xSize, ySize);
        GlStateManager.popMatrix();
    }

    @Override
    public boolean doesGuiPauseGame(){
        return false;
    }

    @Override
    public void drawDefaultBackground(){ //darkens screen outside of GUI window
        super.drawDefaultBackground();
    }

}

 

Here's what it looks like: https://gyazo.com/02147c190bc037ce923c03ef4a93bd68

It just pauses the whole game when displayed. Also, if you have any suggestions on how I could "fill it up", that'd be great! :D

 

Thanks!

  • Author

Okay, so that solved one issue of when to actually display it. However, I still can't move, I can't bring up chat, and I can't even bring up the singleplayer menu :/

  • Author

So I'm using the exact same "... extends GuiScreen" class, but here's the event:

@SideOnly(Side.CLIENT)
public class ClientEvents {

    private boolean showWelcomePage = true;

    @SubscribeEvent
    public void onUpdate(TickEvent.ClientTickEvent event){

    }

    @SubscribeEvent
    public void onGuiOpened(GuiOpenEvent event){
        if(showWelcomePage && event.getGui() instanceof GuiMainMenu){ //we do this because when the GUI button is clicked in GUIWElcome, it opens GUiMainMenu
            event.setGui(new GuiWelcome());
            showWelcomePage = false;
        }
    }

    @SubscribeEvent
    public void onRender(RenderGameOverlayEvent event){
        ClientProxy.MINECRAFT.displayGuiScreen(new GuiEnergyBar());
    }

    @SubscribeEvent
    public void onRenderPlayerPre(RenderPlayerEvent.Pre event){

        float rotation = 90;
        GlStateManager.pushMatrix();

        /*float baseRotation = (event.getEntityPlayer().renderYawOffset - event.getEntityPlayer().prevRenderYawOffset)
                * event.getPartialRenderTick() + event.getEntityPlayer().prevRenderYawOffset;
        GlStateManager.rotate(-baseRotation, 0, 1, 0);*/

        if(Keyboard.isKeyDown(Keyboard.KEY_X)){
            rotation++;
            GlStateManager.rotate(rotation, 0, 1, 0);
            event.getEntityPlayer().addVelocity(0.015 ,0.05, 0.015);
        }
    }

    @SubscribeEvent
    public void onRenderPlayerPost(RenderPlayerEvent.Post event){
        GlStateManager.popMatrix();
    }

}

 

More specifically:

@SubscribeEvent
    public void onRender(RenderGameOverlayEvent event){
        ClientProxy.MINECRAFT.displayGuiScreen(new GuiEnergyBar());
    }

  • Author

Hmmm, okay. That was going to be my next question: what's the difference between GuiScreen and Gui? I can't seem to find a tutorial that actually explains this stuff, and there's no paragraph comments in those classes explaining their uses, unfortunately.

 

Also, I'm looking in the RenderGameOverlayEvent class and I see Pre, Post, BossInfo, Text and Chat. I'm assuming I should use Pre or Post. Which would be better practice there?

 

Thanks for all the insight!

I think it's extending

GuiScreen

that is the problem. That class is used for things like the inventory and comes with a bunch of inbuilt methods for receiving mouse and keyboard input. Based on how simple your GUI is, you can probably just extend the

Gui

class itself and draw it in

RenderGameOverlayEvent

.

  • Author

Hm, I suppose that would make sense. So it'd be safe to assume that GuiScreen is more like a menu type of thing, which would make sense why I couldn't move or do anything.

  • Author

Thanks! That all makes sense. So then I'd do something along the lines of:

@SubscribeEvent
    public void onRender(RenderGameOverlayEvent.Post event){
        if(event.getType().equals(RenderGameOverlayEvent.ElementType.ALL))
            //do stuff
            //ClientProxy.MINECRAFT.displayGuiScreen(new GuiEnergyBar());
    }

?

 

And what should I use instead of GuiScreen? Gui, as the other person said up there ^?

  • Author

You should just render your stuff directly inside the event handler.

 

How would you recommend doing that? All through GlStateManager (if that's possible... I didn't see any kind of "drawRectangle" method in that class)?

  • Author

You can use direct GL through GlStateManager, you can use the Tessellator, you can use the helper methods in the GUI class, whatever you want.

 

Okay, hopefully last question. What's the Tessellator for? If there's GUI helper methods, GlStateManager, and Tessellator, surely they're all for (at least, slightly) different purposes, right? I'm just trying to figure everything out!

  • Author

The Tessellator is just an API for buffering GL calls, because they are somewhat expensive.

 

Okay, I think this is the last problem. I created this new class, hold an instance of it in the ClientProxy, and just manually call ClientProxy.energyGui.draw() in the event you said above. The outline draws fine now and allows for movement, but the "inner" area (drawRect(...)) doesn't display at all. Would you happen to have any hunches off the top of your head?

 

public class GuiEnergyBarTwo extends Gui {

    private int xSize = 116, ySize = 17; //size of desired window. Max is 256x256

    public void draw(){

        TextureManager textureManager = ClientProxy.MINECRAFT.getTextureManager();
        textureManager.bindTexture(new ResourceLocation(Test.MODID + ":textures/gui/energybar.png"));
        GlStateManager.pushMatrix();

        //---- inner ----
        drawRect(0, 0, getRightCoord(), ySize - 1, 1);

        //---- outline ----
        GlStateManager.enableAlpha();
        drawTexturedModalRect(0, 0, 0, 0, xSize, ySize);

        GlStateManager.popMatrix();
    }

    private int getRightCoord(){
        int energyPercent = ClientProxy.clientInfo.getEnergyPercentage();
        return xSize * (energyPercent/100); //this returns the needed percentage of the length of the rectangle
    }

}

The Tessellator is just an API for buffering GL calls, because they are somewhat expensive.

 

Okay, I think this is the last problem. I created this new class, hold an instance of it in the ClientProxy, and just manually call ClientProxy.energyGui.draw() in the event you said above. The outline draws fine now and allows for movement, but the "inner" area (drawRect(...)) doesn't display at all. Would you happen to have any hunches off the top of your head?

 

public class GuiEnergyBarTwo extends Gui {

    private int xSize = 116, ySize = 17; //size of desired window. Max is 256x256

    public void draw(){

        TextureManager textureManager = ClientProxy.MINECRAFT.getTextureManager();
        textureManager.bindTexture(new ResourceLocation(Test.MODID + ":textures/gui/energybar.png"));
        GlStateManager.pushMatrix();

        //---- inner ----
        drawRect(0, 0, getRightCoord(), ySize - 1, 1);

        //---- outline ----
        GlStateManager.enableAlpha();
        drawTexturedModalRect(0, 0, 0, 0, xSize, ySize);

        GlStateManager.popMatrix();
    }

    private int getRightCoord(){
        int energyPercent = ClientProxy.clientInfo.getEnergyPercentage();
        return xSize * (energyPercent/100); //this returns the needed percentage of the length of the rectangle
    }

}

Could you post ClientProxy.clientInfo.getEnergyPercentage();

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

  • Author

Sure thing:

 

public class ClientInfo {

    private int energy, maxEnergy;

    public ClientInfo(){
        energy = 5;
        maxEnergy = 10;
    }

    public int getEnergy() {
        return energy;
    }

    public int getMaxEnergy() {
        return maxEnergy;
    }

    public int getEnergyPercentage(){
        return (energy / maxEnergy) * 100;
    }
}

 

It's just getting the percentage of how much energy the player current has compared to what they'd have at "full charge".

Sure thing:

 

public class ClientInfo {

    private int energy, maxEnergy;

    public ClientInfo(){
        energy = 5;
        maxEnergy = 10;
    }

    public int getEnergy() {
        return energy;
    }

    public int getMaxEnergy() {
        return maxEnergy;
    }

    public int getEnergyPercentage(){
        return (energy / maxEnergy) * 100;
    }
}

 

It's just getting the percentage of how much energy the player current has compared to what they'd have at "full charge".

First thing is first you are using integers when you should be using doubles or floats. That may fix your problem.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

  • Author

See, that's what I thought at first, but that draw method takes int's and I figured it wouldn't matter much to the player if they have a fraction of energy, ya know?

See, that's what I thought at first, but that draw method takes int's and I figured it wouldn't matter much to the player if they have a fraction of energy, ya know?

Only the percentage method has to be a double.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

  • Author

See, that's what I thought at first, but that draw method takes int's and I figured it wouldn't matter much to the player if they have a fraction of energy, ya know?

Only the percentage method has to be a double.

 

I see what you were saying about the doubles. I felt dumb when I realized what you meant. haha. Unfortunately, the rectangle still isn't being drawn. No errors are being thrown. I checked the value for "getRightCoord()" and it's 58, just as it should be. I'm not sure what's going on. Here's the code again:

 

public class GuiEnergyBarTwo extends Gui {

    private int xSize = 116, ySize = 17; //size of desired window. Max is 256x256

    public void draw(){

        System.out.println("" + getRightCoord());

        TextureManager textureManager = ClientProxy.MINECRAFT.getTextureManager();
        textureManager.bindTexture(new ResourceLocation(Test.MODID + ":textures/gui/energybar.png"));
        GlStateManager.pushMatrix();

        //---- inner ----
        drawRect(0, 0, getRightCoord(), ySize - 1, 1);

        //---- outline ----
        GlStateManager.enableAlpha();
        drawTexturedModalRect(0, 0, 0, 0, xSize, ySize);

        GlStateManager.popMatrix();
    }

    private int getRightCoord(){
        return  (int)((ClientProxy.clientInfo.getEnergyPercentage() /100) * xSize); //this returns the retrieved percentage of the length of the rectangle
    }

}

See, that's what I thought at first, but that draw method takes int's and I figured it wouldn't matter much to the player if they have a fraction of energy, ya know?

Only the percentage method has to be a double.

 

I see what you were saying about the doubles. I felt dumb when I realized what you meant. haha. Unfortunately, the rectangle still isn't being drawn. No errors are being thrown. I checked the value for "getRightCoord()" and it's 58, just as it should be. I'm not sure what's going on. Here's the code again:

 

public class GuiEnergyBarTwo extends Gui {

    private int xSize = 116, ySize = 17; //size of desired window. Max is 256x256

    public void draw(){

        System.out.println("" + getRightCoord());

        TextureManager textureManager = ClientProxy.MINECRAFT.getTextureManager();
        textureManager.bindTexture(new ResourceLocation(Test.MODID + ":textures/gui/energybar.png"));
        GlStateManager.pushMatrix();

        //---- inner ----
        drawRect(0, 0, getRightCoord(), ySize - 1, 1);

        //---- outline ----
        GlStateManager.enableAlpha();
        drawTexturedModalRect(0, 0, 0, 0, xSize, ySize);

        GlStateManager.popMatrix();
    }

    private int getRightCoord(){
        return  (int)((ClientProxy.clientInfo.getEnergyPercentage() /100) * xSize); //this returns the retrieved percentage of the length of the rectangle
    }

}

Try making the last value in drawRect a RGB value using Color#getRGB()

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

  • Author

Do you have any idea which import that is? I can't seem to find any that returns an int value (that's what drawRect() is looking for) :/ The only one I see that returns an int is Color.HSBtoRGB(float hue, float saturation, float brightness)

Do you have any idea which import that is? I can't seem to find any that returns an int value (that's what drawRect() is looking for) :/ The only one I see that returns an int is Color.HSBtoRGB(float hue, float saturation, float brightness)

The # means you need an instance of that type. So do Color color = new Color(R, G, B).getRGB();

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

  • Author

Do you have any idea which import that is? I can't seem to find any that returns an int value (that's what drawRect() is looking for) :/ The only one I see that returns an int is Color.HSBtoRGB(float hue, float saturation, float brightness)

The # means you need an instance of that type. So do Color color = new Color(R, G, B).getRGB();

 

Oooooooh. I thought you guys just did that instead of using a decimal xD

Well, good news is something displayed up top. Bad news is, I have no idea where any of that is coming from O_O

https://gyazo.com/57afd183d694cde09cd5c1a2df39ce5a

Do you have any idea which import that is? I can't seem to find any that returns an int value (that's what drawRect() is looking for) :/ The only one I see that returns an int is Color.HSBtoRGB(float hue, float saturation, float brightness)

The # means you need an instance of that type. So do Color color = new Color(R, G, B).getRGB();

 

Oooooooh. I thought you guys just did that instead of using a decimal xD

Well, good news is something displayed up top. Bad news is, I have no idea where any of that is coming from O_O

https://gyazo.com/57afd183d694cde09cd5c1a2df39ce5a

I believe that means that textures are bound weirdly. Instead of

TextureManager textureManager = ClientProxy.MINECRAFT.getTextureManager();
textureManager.bindTexture(new ResourceLocation(Test.MODID + ":textures/gui/energybar.png"));

Just do

this.mc.getTextureManager().bindTexture(// Create ResourceLocation in a variable in the class instead of creating it every time the code is ran.//)

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

  • Author

Thanks! I actually did that right as you replied, just to clean up code. haha. So we're almost there. Last thing: The color of that box is being applied to everything (the outline of the bar should be grey, and so should the background of that shop menu (which is a totally different object)). https://gyazo.com/efe4cfd800fa818a2c161fa189d7fd29

  • Author

Following up on this, I think either TextureManager or something in GlStateManager needs to be "reset" after being used each time, that way colors don't appear in other objects they're not suppose to. I tried GlStateManager.resetColor(), but that doesn't work

Following up on this, I think either TextureManager or something in GlStateManager needs to be "reset" after being used each time, that way colors don't appear in other objects they're not suppose to. I tried GlStateManager.resetColor(), but that doesn't work

I would use the pop and push methods.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

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.