Jump to content

Removing XP From Player


elfin8er

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
	}
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 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
Link to comment
Share on other sites

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

Announcements



×
×
  • Create New...

Important Information

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