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

    • diesieben07
      [1.16.4] Getting values from config

      By diesieben07 · Posted 12 minutes ago

      Look at Forge's config for an example: https://github.com/MinecraftForge/MinecraftForge/blob/1.16.x/src/main/java/net/minecraftforge/common/ForgeConfig.java
    • BeardlessBrady
      [1.16.4] Getting values from config

      By BeardlessBrady · Posted 17 minutes ago

      How do I get access to the config instance though? Since now you register your config via "ModLoadingContext.get().registerConfig"
    • pitbox46
      Pushing Redstone

      By pitbox46 · Posted 21 minutes ago

      Well, that sucks. Then I would have to use a core mod? I'm not too knowlegeable on core modding other than it is advised to use a different apporach and 99% of the time there is some better way. Would a purely additive core mod, a mod that only adds fields and classes, still cause issues with other mods?    Also, is there a way to change the class of a minecraft block without registering an entirely new block? If I could simply make a subclass of a block's existing class, I could register the block as being a part of the new sub-class which would contain functionality for what I want to do. This would, of course, only apply the functionality to blocks that I make a new sub-class for. 
    • diesieben07
      Error: net.minecraftforge.fml.common.LoaderExceptionModCrash: Caught exception from Advanced Machines (advanced_machines) Код завершения: -1

      By diesieben07 · Posted 48 minutes ago

      1.12 is no longer supported on this forum. Please update to a modern version of Minecraft to receive support.
    • DeadPony
      Error: net.minecraftforge.fml.common.LoaderExceptionModCrash: Caught exception from Advanced Machines (advanced_machines) Код завершения: -1

      By DeadPony · Posted 1 hour ago

      ? Help pls ? crash-2021-02-26_17.28.35-client.txt
  • Topics

    • BeardlessBrady
      3
      [1.16.4] Getting values from config

      By BeardlessBrady
      Started 12 hours ago

    • pitbox46
      2
      Pushing Redstone

      By pitbox46
      Started 8 hours ago

    • DeadPony
      1
      Error: net.minecraftforge.fml.common.LoaderExceptionModCrash: Caught exception from Advanced Machines (advanced_machines) Код завершения: -1

      By DeadPony
      Started 1 hour ago

    • IntentScarab
      1
      [1.16.4] Question on implementing recipes based on NBT data

      By IntentScarab
      Started 14 hours ago

    • than00ber1
      4
      [1.16.4] Change Biome RainType through Reflections Casting error

      By than00ber1
      Started Tuesday at 03:26 PM

  • Who's Online (See full list)

    • Banane
    • Skyriis
    • Tanguygab
    • DeadPony
    • pitbox46
    • diesieben07
    • BeardlessBrady
    • Funyaah
  • 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