Posted August 18, 201411 yr I have wanted to create an item that stores a player's experience, it uses a GUI inside of which I have created an int (xpStored) that should increase/decrease when you press a button, while simultaneously removing your experience/giving it back. But it doesn't seem to work. The GUI has three buttons, one that adds 1 to xpStored and removes 1 level, one that adds 1 level and decreases xpStored by 1, and the other one which closes the GUI and writes a chat message to the player, telling him how much Experience he has stored. When I have an amount of levels over 0 and try to press the first button, it executes the action of the close button. My GUI code: package com.nignog.mymod.gui; import org.lwjgl.opengl.GL11; import com.nignog.mymod.MainClass; import net.minecraft.client.Minecraft; import net.minecraft.client.entity.EntityClientPlayerMP; import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.GuiScreen; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.ChatComponentText; import net.minecraft.util.ResourceLocation; import net.minecraft.world.World; public class XPStorageGui extends GuiScreen { ResourceLocation texture = new ResourceLocation(MainClass.MODID, "textures/gui/background.png"); private final int xSizeTexture = 248; private final int ySizeTexture = 166; public XPStorageGui(EntityPlayer player) { } @Override public void drawScreen(int x, int y, float f) { drawDefaultBackground(); GL11.glColor4f(1F, 1F, 1F, 1F); mc.getMinecraft().getTextureManager().bindTexture(texture); int posX = (this.width - xSizeTexture) / 2; int posY = (this.height - ySizeTexture) / 2; drawTexturedModalRect(posX, posY, 0, 0, xSizeTexture, ySizeTexture); drawCenteredString(fontRendererObj, "XP Storage", posX + 125, (int) (posY + 7.5), 16777215); super.drawScreen(x, y, f); } @Override public boolean doesGuiPauseGame() { return false; } @Override public void initGui() { this.buttonList.clear(); int posX = (this.width - xSizeTexture) / 2; int posY = (this.height - ySizeTexture) / 2; GuiButton XPAdd = new GuiButton(0, posX + 75, posY + 55, 100, 20, "Add XP to storage"); GuiButton XPGet = new GuiButton(0, posX + 75, posY + 85, 100, 20, "Add XP to your bar"); GuiButton close = new GuiButton(0, posX + 100, posY + 115, 50, 20, "Close"); XPAdd.id = 1; XPGet.id = 2; close.id = 3; this.buttonList.add(close); this.buttonList.add(XPAdd); this.buttonList.add(XPGet); } @Override protected void actionPerformed(GuiButton button) { EntityClientPlayerMP player = mc.getMinecraft().thePlayer; World world = mc.getMinecraft().theWorld; int xpStored = 0; switch(button.id) { case 1: if(player.experienceLevel > 0) { player.addExperienceLevel(-1); xpStored++; } else if (player.experienceLevel < 0){ player.closeScreen(); if(world.isRemote) { player.addChatMessage(new ChatComponentText("You don't have any levels!")); } } case 2: if(xpStored > 0) { player.addExperienceLevel(1); xpStored--; } case 3: player.closeScreen(); if(world.isRemote) { player.addChatMessage(new ChatComponentText("Your XP Storage currently holds:" + " " + xpStored + " " + "Levels.")); } } } } I tried adding System.out.println(player.experienceLevel); in case 1 to test whether it can tell the player's experience level amount, and it prints it out correctly.
August 18, 201411 yr Try adding break; after each of the cases. No Java expert here but this has happened to me before http://i.imgur.com/J2keQOC.png[/img]http://i.imgur.com/YHnvIfh.png[/img]
August 18, 201411 yr Author Try adding break; after each of the cases. No Java expert here but this has happened to me before Oh, seems I forgot them... Thanks, works now!
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.