Jump to content

[1.12.2] GuiButton releases many outputs instead of one


MisterGamer _

Recommended Posts

Hi,

I have a problem with GuiButton.

First the code:

if(button.id == 2) {
	if(world.isRemote) {
		Minecraft.getMinecraft().player.sendChatMessage("/give @p " + Reference.MOD_ID + ":mystical_ingot 1");
	}
	Minecraft.getMinecraft().getSoundHandler().playSound(PositionedSoundRecord.getMasterRecord(SoundEvents.UI_BUTTON_CLICK, 1.0F));
}

The code works very well, but instead of one ingot, i receive many ingots.

So I tried to send a chat message without a command but it appears many times instead of one.

 

Some fixes?

Link to comment
Share on other sites

Try with:

if(button.id == 2 && button.id.isPressed()) {
	if(world.isRemote) {
		Minecraft.getMinecraft().player.sendChatMessage("/give @p " + Reference.MOD_ID + ":mystical_ingot 1");
	}
	Minecraft.getMinecraft().getSoundHandler().playSound(PositionedSoundRecord.getMasterRecord(SoundEvents.UI_BUTTON_CLICK, 1.0F));
}

 

Maybe this should fix it

Link to comment
Share on other sites

11 minutes ago, PJack said:

Try with:


if(button.id == 2 && button.id.isPressed()) {
	if(world.isRemote) {
		Minecraft.getMinecraft().player.sendChatMessage("/give @p " + Reference.MOD_ID + ":mystical_ingot 1");
	}
	Minecraft.getMinecraft().getSoundHandler().playSound(PositionedSoundRecord.getMasterRecord(SoundEvents.UI_BUTTON_CLICK, 1.0F));
}

 

Maybe this should fix it

button.id.isPressed() doesn't exists.

A little info I didn't remember: this if statement is in the actionPerformed method of a GUI.

Link to comment
Share on other sites

13 minutes ago, diesieben07 said:

Here it is:

package com.mistergamer.mysticalmod.gui;

public class CompactorGui extends GuiContainer{

	public static final int WIDTH = 176;
    public static final int HEIGHT = 166;
    public GuiButton button1;
    public String crafting;
    BlockPos pos = Minecraft.getMinecraft().player.getPosition();
    World world = Minecraft.getMinecraft().world;
    EntityPlayer player = Minecraft.getMinecraft().player;
    Entity entity;
    
    public static final ResourceLocation background = new ResourceLocation(Reference.MOD_ID, "textures/gui/container/compactor_gui.png");
    public static final int GUI_ID = 2;
	
	public CompactorGui(CompactorTileEntity tileEntity, CompactorContainer container) {
		super(container);
		
		xSize = WIDTH;
		ySize = HEIGHT;
	}

	@Override
	protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
		GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
		GL11.glDisable(GL11.GL_LIGHTING);
		mc.getTextureManager().bindTexture(background);
        drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);
        this.renderHoveredToolTip (mouseX, mouseY);
	}
	
	protected void drawGuiContainerForegroundLayer(float partialTicks, int mouseX, int mouseY) {
        this.renderHoveredToolTip(mouseX, mouseY);
	}
	
	@Override
	public boolean doesGuiPauseGame() {
		return false;
	}

	@Override
	public void drawScreen(int mouseX, int mouseY, float partialTicks) {
		this.drawDefaultBackground();
		super.drawScreen(mouseX, mouseY, partialTicks);
        this.renderHoveredToolTip(mouseX, mouseY);
        fontRenderer.drawString("Inventory", this.getGuiLeft() + 8, this.getGuiTop() + 72, 4210752);
        //Draw the Help button
        this.buttonList.add(new GuiButton(1, this.getGuiLeft() + 128, this.getGuiTop() + 62, 20, 20, "?"));
        //Draw the Mystical Ingot button and icon
        this.buttonList.add(new GuiButton(2, this.getGuiLeft() + 8, this.getGuiTop() + 8, 20, 20, ""));
        mc.getRenderItem().renderItemAndEffectIntoGUI(new ItemStack(ModItems.MYSTICAL_INGOT), this.getGuiLeft() + 10, this.getGuiTop() + 10);
        //Draw the Mystical Fuel button and icon
        this.buttonList.add(new GuiButton(3, this.getGuiLeft() + 29, this.getGuiTop() + 8, 20, 20, ""));
        mc.getRenderItem().renderItemAndEffectIntoGUI(new ItemStack(ModItems.MYSTICAL_FUEL), this.getGuiLeft() + 31, this.getGuiTop() + 10);
	}

	@Override
	protected void actionPerformed(GuiButton button) throws IOException {
		if(button.id == 1) {
			if(world.isRemote) {
				Minecraft.getMinecraft().displayGuiScreen(new CompactorHelpGui());
				Minecraft.getMinecraft().getSoundHandler().playSound(PositionedSoundRecord.getMasterRecord(SoundEvents.UI_BUTTON_CLICK, 1.0F));
			}
		}else if(button.id == 2) {
			if(world.isRemote) {
				Minecraft.getMinecraft().player.sendChatMessage("/give @p " + Reference.MOD_ID + ":mystical_ingot 1");
			}
			Minecraft.getMinecraft().getSoundHandler().playSound(PositionedSoundRecord.getMasterRecord(SoundEvents.UI_BUTTON_CLICK, 1.0F));
		}else if(button.id == 3) {
			if(world.isRemote) {
				Minecraft.getMinecraft().player.sendChatMessage("/give @p " + Reference.MOD_ID + ":mystical_fuel 1");
			}
			Minecraft.getMinecraft().getSoundHandler().playSound(PositionedSoundRecord.getMasterRecord(SoundEvents.UI_BUTTON_CLICK, 1.0F));
		}
	}
}

 

Link to comment
Share on other sites

36 minutes ago, diesieben07 said:

Nessuno del codice che hai postato disegna i pulsanti. Li stai semplicemente creando per loro per poi essere disegnati dal codice GuiScreen .

Guarda gli esempi di vaniglia delle implementazioni di GuiScreen su come gestire i pulsanti.

I had a look at the Beacon GUI and it works now! Thank you so much!

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.