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

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.

  • Author

Try adding break; after each of the cases. No Java expert here but this has happened to me before :P

 

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.

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.