Jump to content
  • Home
  • Files
  • Docs
Topics
  • All Content

  • This Topic
  • This Forum

  • Advanced Search
  • Existing user? Sign In  

    Sign In



    • Not recommended on shared computers


    • Forgot your password?

  • Sign Up
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • Gui Button Click
Currently Supported: 1.16.X (Latest) and 1.15.X (LTS)
Sign in to follow this  
Followers 1
mrkirby153

Gui Button Click

By mrkirby153, September 22, 2013 in Modder Support

  • Reply to this topic
  • Start new topic

Recommended Posts

mrkirby153    3

mrkirby153

mrkirby153    3

  • Creeper Killer
  • mrkirby153
  • Members
  • 3
  • 186 posts
Posted September 22, 2013

Hello,

 

When I click on a button on my GUI, it runs the actionPreformed method 5 times. Why?

 

package mrkirby153.MscHouses.block.GUI;

import mrkirby153.MscHouses.block.Container.ContainerHouseGenerator;
import mrkirby153.MscHouses.block.tileEntity.TileEntityHouseGen;
import mrkirby153.MscHouses.lib.ResourceFile;
import mrkirby153.MscHouses.lib.Strings;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.util.StatCollector;

import org.lwjgl.opengl.GL11;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
/**
* 
* Msc Houses
*
* GuiBlockBase
*
* @author mrkirby153
* @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html)
*/
@SideOnly(Side.CLIENT)
public class GuiHouseGenerator extends GuiContainer{

private GuiButton generate;
public GuiHouseGenerator (InventoryPlayer inventoryPlayer,
		TileEntityHouseGen tileEntity) {
	//the container is instanciated and passed to the superclass for handling
	super(new ContainerHouseGenerator(inventoryPlayer, tileEntity));
	this.ySize = 176;
	this.xSize = 176;
}

@Override
protected void drawGuiContainerForegroundLayer(int param1, int param2) {
	//draw text and stuff here
	//the parameters for drawString are: string, x, y, color
	fontRenderer.drawString(StatCollector.translateToLocal(Strings.RESOURCE_PREFIX+Strings.TILE_HOUSE_GEN), 8, 6, 4210752);
	//draws "Inventory" or your regional equivalent
	fontRenderer.drawString(StatCollector.translateToLocal("container.inventory"), 8, ySize - 96 + 2, 4210752);
	this.buttonList.add(generate = new GuiButton(0, this.width /2 + 20, this.height /2 - 65, 60, 20, "Generate"));
}

@Override
protected void actionPerformed(GuiButton button) {
	if(button.id==0){
		this.mc.displayGuiScreen((GuiScreen)null);
	}
}

@Override
protected void drawGuiContainerBackgroundLayer(float par1, int par2,
		int par3) {
	//draw your Gui here, only thing you need to change is the path
	GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
	this.mc.getTextureManager().bindTexture(ResourceFile.houseGen_Img);

	int x = (width - xSize) / 2;
	int y = (height - ySize) / 2;
	this.drawTexturedModalRect(x, y, 0, 0, xSize, ySize);
}

}

  • Quote

width=728 height=100http://i.imgur.com/gWwyMMO.jpg[/img]

Share this post


Link to post
Share on other sites

Mew    36

Mew

Mew    36

  • Dragon Slayer
  • Mew
  • Members
  • 36
  • 567 posts
Posted September 22, 2013

Well first thing first, you have done this slightly wrong.

 

You need to override the method initGui() and move your buttonList call to that method. That should clear it up :)

 

If it doesn't... Just post again xD

  • Quote

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Share this post


Link to post
Share on other sites

mrkirby153    3

mrkirby153

mrkirby153    3

  • Creeper Killer
  • mrkirby153
  • Members
  • 3
  • 186 posts
Posted September 22, 2013

Ok, that fixed it but if moved my container slots to the top left instead of center https://www.dropbox.com/s/3g782qkxl2nfxg2/2013-09-22_16.53.03.png

  • Quote

width=728 height=100http://i.imgur.com/gWwyMMO.jpg[/img]

Share this post


Link to post
Share on other sites

Mew    36

Mew

Mew    36

  • Dragon Slayer
  • Mew
  • Members
  • 36
  • 567 posts
Posted September 22, 2013

Can I see the Container class as well please? Also, try not to use xSize and ySize, they aren't actual variables for getting the size of the screen. That is the width/height variables. So use those instead. xSize/ySize are used for the TEXTURE size. ( just a hint :P )

  • Quote

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Share this post


Link to post
Share on other sites

mrkirby153    3

mrkirby153

mrkirby153    3

  • Creeper Killer
  • mrkirby153
  • Members
  • 3
  • 186 posts
Posted September 23, 2013

Sure

 

package mrkirby153.MscHouses.block.Container;

import mrkirby153.MscHouses.block.tileEntity.TileEntityHouseGen;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.FurnaceRecipes;
import net.minecraft.tileentity.TileEntityFurnace;
/**
* 
* Msc Houses
*
* ContainerBlockBase
*
* @author mrkirby153
* @license Lesser GNU Public License v3 (http://www.gnu.org/licenses/lgpl.html)
*/
public class ContainerHouseGenerator extends Container{
public ContainerHouseGenerator(InventoryPlayer inventoryPlayer, TileEntityHouseGen block_base) {

	// Add the Moduel slot to the base
        this.addSlotToContainer(new Slot(block_base, 0, 81, 18));
        //Add the material modifyer to the block base
        this.addSlotToContainer(new Slot(block_base, 2, 81, 40));
        // Add the fuel slot to the Block Base
        this.addSlotToContainer(new Slot(block_base, 1, 81, 63));


	// Add the player's inventory slots to the container
        for (int inventoryRowIndex = 0; inventoryRowIndex < 3; ++inventoryRowIndex) {
            for (int inventoryColumnIndex = 0; inventoryColumnIndex < 9; ++inventoryColumnIndex) {
                this.addSlotToContainer(new Slot(inventoryPlayer, inventoryColumnIndex + inventoryRowIndex * 9 + 9, 8 + inventoryColumnIndex * 18, 94 + inventoryRowIndex * 18));
            }
        }

        // Add the player's action bar slots to the container
        for (int actionBarSlotIndex = 0; actionBarSlotIndex < 9; ++actionBarSlotIndex) {
            this.addSlotToContainer(new Slot(inventoryPlayer, actionBarSlotIndex, 8 + actionBarSlotIndex * 18, 152));
        }
}

@Override
public boolean canInteractWith(EntityPlayer player) {

	return true;
}

@Override
public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2) {

	ItemStack itemstack = null;
        Slot slot = (Slot)this.inventorySlots.get(par2);

        if (slot != null && slot.getHasStack())
        {
            ItemStack itemstack1 = slot.getStack();
            itemstack = itemstack1.copy();

            if (par2 == 2)
            {
                if (!this.mergeItemStack(itemstack1, 3, 39, true))
                {
                    return null;
                }

                slot.onSlotChange(itemstack1, itemstack);
            }
            else if (par2 != 1 && par2 != 0)
            {
                if (FurnaceRecipes.smelting().getSmeltingResult(itemstack1) != null)
                {
                    if (!this.mergeItemStack(itemstack1, 0, 1, false))
                    {
                        return null;
                    }
                }
                else if (TileEntityFurnace.isItemFuel(itemstack1))
                {
                    if (!this.mergeItemStack(itemstack1, 1, 2, false))
                    {
                        return null;
                    }
                }
                else if (par2 >= 3 && par2 < 30)
                {
                    if (!this.mergeItemStack(itemstack1, 30, 39, false))
                    {
                        return null;
                    }
                }
                else if (par2 >= 30 && par2 < 39 && !this.mergeItemStack(itemstack1, 3, 30, false))
                {
                    return null;
                }
            }
            else if (!this.mergeItemStack(itemstack1, 3, 39, false))
            {
                return null;
            }

            if (itemstack1.stackSize == 0)
            {
                slot.putStack((ItemStack)null);
            }
            else
            {
                slot.onSlotChanged();
            }

            if (itemstack1.stackSize == itemstack.stackSize)
            {
                return null;
            }

            slot.onPickupFromSlot(par1EntityPlayer, itemstack1);
        }

        return itemstack;
}

}

  • Quote

width=728 height=100http://i.imgur.com/gWwyMMO.jpg[/img]

Share this post


Link to post
Share on other sites

Mew    36

Mew

Mew    36

  • Dragon Slayer
  • Mew
  • Members
  • 36
  • 567 posts
Posted September 23, 2013

Well, I see nothing wrong with the code... So I am not sure what the problem is. It is probably something to do with your positioning of the slots...

 

Anyway, I hope this is solved fairly quickly. I hate small problems like this that take forever to be solved >.>

  • Quote

I am Mew. The Legendary Psychic. I behave oddly and am always playing practical jokes.

 

I have also found that I really love making extremely long and extremely but sometimes not so descriptive variables. Sort of like what I just did there xD

Share this post


Link to post
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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

    • Insert image from URL
×
  • Desktop
  • Tablet
  • Phone
Sign in to follow this  
Followers 1
Go To Topic Listing



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • Jack Richard
      [1.15.2] Minecraft Registry Problem When Loading from Jar

      By Jack Richard · Posted 1 hour ago

      Thanks so much for your help! I removed the log4j dependency, I honesty don't remember when I put that there. Once I fixed the jar.finalizedBy line, everything worked! (One small issue where I was relocating all of "com.google" when I just should've relocated "com.google.common," too)   Thanks again!
    • Teedledee
      1.16.4 Failed to synchronize registry data with server LAN

      By Teedledee · Posted 1 hour ago

      Fixed the issue, one config didnt match, downloaded randompatches to solve badly compressed packet afterwards.
    • DaemonUmbra
      [1.15.2] Minecraft Registry Problem When Loading from Jar

      By DaemonUmbra · Posted 1 hour ago

      First of all, why are you shadowing log4j? Minecraft already provides log4j2. Second if build is running shadowjar instead of jar then you might need to tweak the line where it says jar.finalizedBy('reobfJar') to finalize shadowJar instead
    • DaemonUmbra
      "The game crashed whilst rendering overlay"

      By DaemonUmbra · Posted 1 hour ago

      What do you think you should do about this?
    • DaemonUmbra
      IntelliJ doesn't include resources folder while debugging

      By DaemonUmbra · Posted 1 hour ago

      What version of Minecraft are you using?
  • Topics

    • Jack Richard
      2
      [1.15.2] Minecraft Registry Problem When Loading from Jar

      By Jack Richard
      Started 1 hour ago

    • Teedledee
      2
      1.16.4 Failed to synchronize registry data with server LAN

      By Teedledee
      Started January 17

    • GuyWithTVHead
      1
      "The game crashed whilst rendering overlay"

      By GuyWithTVHead
      Started 1 hour ago

    • GermanBucket
      2
      IntelliJ doesn't include resources folder while debugging

      By GermanBucket
      Started 3 hours ago

    • Arthurmeade12
      8
      Error Conecting to My Minecraft Server

      By Arthurmeade12
      Started Yesterday at 06:05 PM

  • Who's Online (See full list)

    • pupymk1
    • StealthyNoodle
    • PyRoTheLifeLess
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • Gui Button Click
  • Theme

Copyright © 2019 ForgeDevelopment LLC · Ads by Longitude Ads LLC Powered by Invision Community