Jump to content

[1.7.10] changing button texture - solved but have questions


Thornack

Recommended Posts

Hi everyone, so I have created my own button class where I override the draw button method to insert my own texture. It works but for some reason I cannot pass in a different texture using the constructor. I have to hard code the texture and then it works. I was wondering if anyone could tell me why.

 

 

public class CustomGuiButton extends GuiButton
{
 public static String buttonTextureName = "";// CANNOT PASS IN A TEXTURE MUST BE HARD CODED TO ACTUALLY GET THE TEXTURE NOT SURE WHY

 public CustomGuiButton(int id, int width, int height, String displayString, String textureName) {
	super(id, width, height, displayString);
	buttonTextureName = textureName; //WAS HOPING TO PASS IN THE TEXTURE HERE AS A STRING FROM MY BUTTON CONSTRUCTOR BUT IT DOESNT WORK
}

 public static ResourceLocation buttonTexture = new ResourceLocation("custommod:textures/gui/" + buttonTextureName);
/**
     * Draws this button to the screen.
     */
@Override
public void drawButton(Minecraft minecraft, int xCoord, int yCoord)
    {
        if (this.visible)
        {
            FontRenderer fontrenderer = minecraft.fontRenderer;
            minecraft.getTextureManager().bindTexture(buttonTexture);
            GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
            this.field_146123_n = xCoord >= this.xPosition && yCoord >= this.yPosition && xCoord < this.xPosition + this.width && yCoord < this.yPosition + this.height;
            int k = this.getHoverState(this.field_146123_n);
            GL11.glEnable(GL11.GL_BLEND);
            OpenGlHelper.glBlendFunc(770, 771, 1, 0);
            GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
            this.drawTexturedModalRect(this.xPosition, this.yPosition, 0, 46 + k * 20, this.width / 2, this.height);
            this.drawTexturedModalRect(this.xPosition + this.width / 2, this.yPosition, 200 - this.width / 2, 46 + k * 20, this.width / 2, this.height);
            this.mouseDragged(minecraft, xCoord, yCoord);
            int l = 14737632;

            if (packedFGColour != 0)
            {
                l = packedFGColour;
            }
            else if (!this.enabled)
            {
                l = 10526880;
            }
            else if (this.field_146123_n)
            {
                l = 16777120;
            }

            this.drawCenteredString(fontrenderer, this.displayString, this.xPosition + this.width / 2, this.yPosition + (this.height -  / 2, l);
        }
    }
}

 

 

This is the constructor that I use inside my GUI class's initGui() method. If I try to pass in my texture name via a string as "Test.png" where my texture is located in my appropriate directory specified by custommod:textures/gui/Test.png then I get the pink/black missing texture texture.

 

   

this.testBtn = new CustomGuiButton(BUTTON_TEST_ID, this.width - 408, this.height - 157, I18n.format("Test", new Object[0]), "Test.png");
        this.testBtn.width = 37;

 

 

To actually change the texture if instead I replace this line

 

 

public static ResourceLocation buttonTexture = new ResourceLocation("custommod:textures/gui/" + buttonTextureName);

which is located inside the CustomGuiButton class, with this line

public static ResourceLocation buttonTexture = new ResourceLocation("custommod:textures/gui/Test.png");

 

 

then I get my custom texture rendering as the buttons texture. Can anyone explain why I cannot pass in my texture via a string in this way?

 

any help is appreciated

Link to comment
Share on other sites

public static ResourceLocation buttonTexture = new ResourceLocation("custommod:textures/gui/" + buttonTextureName);

 

Check what static means - it is static for this class. Also, how do you even expect it to work?

 

ResourceLocation is an object like any other. When you want to change it, you have to reinitialize it.

 

public ResourceLocation buttonTexture;

public CustomGuiButton(int id, int width, int height, String displayString, String textureName)
{
super(id, width, height, displayString);
buttonTexture = new ResourceLocation("custommod:textures/gui/" + textureName);
}

If any.

 

EDIT

I accidentaly put "static" before ResourceLocation (copy/paste), don't do that, it will cause this resource to be shared between all buttons (this is what static does).

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

 

thanks alot Ernio your suggestion worked

 

working class

public class CustomGuiButton extends GuiButton
{
 public ResourceLocation buttonTexture;

 public CustomGuiButton(int id, int width, int height, String displayString, String textureName) {
	super(id, width, height, displayString);
	buttonTexture = new ResourceLocation("yourmodsnamehere:textures/gui/" + textureName);
}
/**
     * Draws this button to the screen.
     */
@Override
public void drawButton(Minecraft minecraft, int xCoord, int yCoord)
    {
        if (this.visible)
        {
            FontRenderer fontrenderer = minecraft.fontRenderer;
            minecraft.getTextureManager().bindTexture(buttonTexture);
            GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
            this.field_146123_n = xCoord >= this.xPosition && yCoord >= this.yPosition && xCoord < this.xPosition + this.width && yCoord < this.yPosition + this.height;
            int k = this.getHoverState(this.field_146123_n);
            GL11.glEnable(GL11.GL_BLEND);
            OpenGlHelper.glBlendFunc(770, 771, 1, 0);
            GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
            this.drawTexturedModalRect(this.xPosition, this.yPosition, 0, 46 + k * 20, this.width / 2, this.height);
            this.drawTexturedModalRect(this.xPosition + this.width / 2, this.yPosition, 200 - this.width / 2, 46 + k * 20, this.width / 2, this.height);
            this.mouseDragged(minecraft, xCoord, yCoord);
            int l = 14737632;

            if (packedFGColour != 0)
            {
                l = packedFGColour;
            }
            else if (!this.enabled)
            {
                l = 10526880;
            }
            else if (this.field_146123_n)
            {
                l = 16777120;
            }

            this.drawCenteredString(fontrenderer, this.displayString, this.xPosition + this.width / 2, this.yPosition + (this.height -  / 2, l);
        }
    }
}

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.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Your drivers are in a broken state, follow the guide in the FAQ: https://forums.minecraftforge.net/topic/125488-rules-and-frequently-asked-questions-faq/#:~:text=How do I update my drivers%3F
    • A fatal error has been detected by the Java Runtime Environment: # # EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00007ffc3fe22b60, pid=15228, tid=5140 # # JRE version: OpenJDK Runtime Environment Microsoft-8035246 (17.0.8+7) (build 17.0.8+7-LTS) # Java VM: OpenJDK 64-Bit Server VM Microsoft-8035246 (17.0.8+7-LTS, mixed mode, tiered, compressed oops, compressed class ptrs, g1 gc, windows-amd64) # Problematic frame: # C [atio6axx.dll+0x192b60] # # No core dump will be written. Minidumps are not enabled by default on client versions of Windows # # If you would like to submit a bug report, please visit: # https://aka.ms/minecraftjavacrashes # The crash happened outside the Java Virtual Machine in native code. # See problematic frame for where to report the bug i have no idea why please help
    • Quick-Books is great with regards to coordinating your monetary data. You can decide to live talk with a specialist at Quick-Books to get the answer for your questions. You will actually want to get to the talk going to the landing page and call us +1855-210-1428.
    • Update: I managed to make the item not disappear after crafting, but it doesn't remove any durability. import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.enchantment.Enchantments; import net.minecraft.enchantment.UnbreakingEnchantment; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; public class NuggetHammer extends Item { private boolean damage; public NuggetHammer(Properties p_i48487_1_) { super(p_i48487_1_); } @Override public int getMaxDamage(ItemStack stack) { return 54 - 1; } public boolean isBarVisible(ItemStack stack) { return false; } @Override public ItemStack getContainerItem(ItemStack stack) { ItemStack copy = stack.copy(); copy.setCount(1); if (!this.damage) return copy; int unbreaking = EnchantmentHelper.getItemEnchantmentLevel(Enchantments.UNBREAKING, stack); for (int i = 0; i < unbreaking; i++) { if (UnbreakingEnchantment.shouldIgnoreDurabilityDrop(stack, unbreaking, random)) return copy; } copy.setDamageValue(stack.getDamageValue() + 1); if (copy.getDamageValue() > stack.getMaxDamage()) return ItemStack.EMPTY; return copy; } }  
  • Topics

  • Who's Online (See full list)

    • There are no registered users currently online
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.