diff --git a/src/main/java/am2/utility/EntityUtilities.java b/src/main/java/am2/utility/EntityUtilities.java index 47b634b55..6aee75718 100644 --- a/src/main/java/am2/utility/EntityUtilities.java +++ b/src/main/java/am2/utility/EntityUtilities.java @@ -327,10 +327,10 @@ 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; } @@ -338,12 +338,19 @@ 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; @@ -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){