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'm attempting to remove XP from a player using player.addExperience(-1) (player being an EntityPlayer), however it doesn't quite work properly. When XP reaches below the current level, the XP bar begins to stay empty, and the level stays the same.

 

I've also tried player.experienceTotal = player.experienceTotal - 1; with no luck.\

 

Anybody have any suggestions for removing XP from the player?

Here's some code from 1.6.4, not sure how valid it still is, but it did work at the time.

 

	                            	if(player.experience > 0 && player.experienceLevel > 0) {
                                	player.experienceTotal--;
                            		player.experience -= 1F/player.xpBarCap();
                            	}
                            	else if(player.experienceLevel > 0) {
                                	player.experienceTotal--;
                            		player.experienceLevel--;
                            		player.experience = (float)(player.xpBarCap()-1)/player.xpBarCap();
                            		if(player.experienceLevel == 0)
                            			player.experience = 0;
                            	}

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

  • Author

if(player.experienceTotal >= 10){
        System.out.println("Removing XP");
player.experienceTotal = player.experienceTotal - 1;
}

Isn't working :/ In fact, it's not doing anything (except printing "Removing XP"). Replacing it with player.addExperience(-1); does work however (with the reported issues).

Well, you should use

if(player.experience > 0 && player.experienceLevel > 0)

not

if(player.experienceTotal >= 10)

 

As your exact issue is what the second half of the code I posted is for.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

  • Author

Ah, my bad. My apologizes. It seems to be working with what you posted. Thank you very much :)

 

EDIT: Having a really hard time using values other than -1. I'm sure I'll get it eventually though xD

public static void removeXP(EntityPlayer player, int amt){
	if(player.experience > amt && player.experienceLevel > 0) {
    	player.experienceTotal = player.experienceTotal - amt;
		player.experience -= 1F/player.xpBarCap();
	}
	else if(player.experienceLevel > 0) {
		player.experienceTotal = player.experienceTotal - amt;
		player.experienceLevel--;
		player.experience = (float)(player.xpBarCap()-1)/player.xpBarCap();
		if(player.experienceLevel == 0)
			player.experience = 0;
	}
}

Well for starters, you should do player.experience >= amt

 

Second the 1F/player.xpBarCap(); adjusts the bar...by 1 point of exp, you should use amt/player.xpBarCap().

 

Third, you'll have to similarly adjust the second block.

 

Fourth, what if the player has less than that amount of exp and no levels?

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

  • Author

Hmm...

 

public static void removeXP(EntityPlayer player, int amt){
	if(player.experience >= amt && player.experienceLevel == 0) {
    	player.experienceTotal = player.experienceTotal - amt;
		player.experience -= amt/player.xpBarCap();
	}else if(player.experienceLevel > 0 && player.experience >= amt) {
		player.experienceTotal = player.experienceTotal - amt;
		player.experienceLevel--;
		player.experience = (float)(player.xpBarCap()-amt)/player.xpBarCap();
		if(player.experienceLevel == 0)
			player.experience = 0;
	}
}

Thanks for the help so far.

what happens when...

amt = 10
player.experience = 5
player.experienceLevel = 2

..?

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

  • 5 years later...
/** Decreases player's experience properly */
public static void decreaseExp(EntityPlayer player, float amount)
{
        if (player.experienceTotal - amount <= 0)
        {
            player.experienceLevel = 0;
            player.experience = 0;
            player.experienceTotal = 0;
            return;
        }
        
        player.experienceTotal -= amount;

        if (player.experience * (float)player.xpBarCap() <= amount)
        {
        	amount -= player.experience * (float)player.xpBarCap();
        	player.experience = 1.0f;
        	player.experienceLevel--;
        }

        while (player.xpBarCap() < amount)
        {
        	amount -= player.xpBarCap();
            player.experienceLevel--;
        }
        
        player.experience -= amount / (float)player.xpBarCap();
}

1.12.2

 

Edited by AntonBespoiasov

  • Guest locked this topic
Guest
This topic is now closed to further replies.

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.