Jump to content

Gui Button Click


mrkirby153

Recommended Posts

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

}

Link to comment
Share on other sites

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

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

Link to comment
Share on other sites

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 )

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

Link to comment
Share on other sites

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

}

Link to comment
Share on other sites

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

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

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.