Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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?

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

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

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

 

  • Author
22 minutes ago, diesieben07 said:

You are adding new buttons to the button list every frame.

So, yes, there will be multiple buttons. In fact, there will be as many buttons as frames that your screen has been visible.

So where should I draw my buttons?

  • Author
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!

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.