Skip to content

Commit

Permalink
Fix for Mithion#1218
Browse files Browse the repository at this point in the history
  • Loading branch information
DoomFruit committed Sep 12, 2015
1 parent deed98e commit e89a387
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/main/java/am2/utility/EntityUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -327,23 +327,30 @@ public static int getLevelFromXP(float totalXP){
}

public static int getXPFromLevel(int level){
int lCount = 0;
int totalXP = 0;
while (lCount <= level)
totalXP += xpBarCap(lCount++);
for(int i = 0; i < level; i++){
totalXP += xpBarCap(i);
}
return totalXP;
}

public static int xpBarCap(int experienceLevel){
return experienceLevel >= 30 ? 62 + (experienceLevel - 30) * 7 : (experienceLevel >= 15 ? 17 + (experienceLevel - 15) * 3 : 17);
}

public static void deductXP(int amount, EntityPlayer player){
public static int deductXP(int amount, EntityPlayer player){
// the problem with the enchanting table is that it directly "adds" experience levels
// doing it like this does not update experienceTotal
// we therefore need to go and calculate an effective total ourselves
int newTotal = getXPFromLevel(player.experienceLevel) - amount;
if (newTotal < 0)
int effectiveTotal = getXPFromLevel(player.experienceLevel) + (int)(player.experience * (float)xpBarCap(player.experienceLevel));

// since we already calculate the player's total XP here,
// we may as well do the zero check and pass the result (how much was actually deducted) back
// there's no sense in duplicating work
int removedXP = effectiveTotal >= amount ? amount : effectiveTotal;

int newTotal = effectiveTotal - amount;
if(newTotal < 0)
newTotal = 0;

player.experience = 0.0F;
Expand All @@ -352,6 +359,8 @@ public static void deductXP(int amount, EntityPlayer player){
if(player.experienceTotal < 0)
player.experienceTotal = 0;
player.addExperience(newTotal);

return removedXP;
}

public static float modifySoundPitch(Entity e, float p){
Expand Down

0 comments on commit e89a387

Please sign in to comment.