Jump to content

[1.10.2] [SOLVED] Gui That Doesn't Pause Game


Jimmeh

Recommended Posts

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!

Link to comment
Share on other sites

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());
    }

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

.

Link to comment
Share on other sites

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 ^?

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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
    }

}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
    }

}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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.