MisterGamer _ Posted April 30, 2019 Posted April 30, 2019 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? Quote
PJack Posted April 30, 2019 Posted April 30, 2019 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 Quote
MisterGamer _ Posted April 30, 2019 Author Posted April 30, 2019 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. Quote
MisterGamer _ Posted April 30, 2019 Author Posted April 30, 2019 13 minutes ago, diesieben07 said: Checking World#isRemote inside a GuiScreen is pointless and shows a fundamental misunderstanding of how client & server work in Minecraft. Show your complete GuiScreen. 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)); } } } Quote
MisterGamer _ Posted April 30, 2019 Author Posted April 30, 2019 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? Quote
MisterGamer _ Posted April 30, 2019 Author Posted April 30, 2019 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! Quote
Recommended Posts
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.