Skip to content

Commit

Permalink
Merge pull request #1237 from DoomFruit/JournalFix
Browse files Browse the repository at this point in the history
Fix for issue #1218 (journal restoring XP spent in an enchanting table)
  • Loading branch information
Mithion committed Sep 13, 2015
2 parents 2cfc581 + 8fb91cb commit 53e11ad
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 36 deletions.
13 changes: 3 additions & 10 deletions src/main/java/am2/items/ItemJournal.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,13 @@ public ItemStack onItemRightClick(ItemStack journal, World world, EntityPlayer p
if (getOwner(journal) == null){
setOwner(journal, player);
}else if (!getOwner(journal).equals(player.getCommandSenderName())){
player.addChatMessage(new ChatComponentText("am2.tooltip.notYourJournal"));
player.addChatMessage(new ChatComponentText(StatCollector.translateToLocal("am2.tooltip.notYourJournal")));
return super.onItemRightClick(journal, world, player);
}

if (player.isSneaking()){
try{
int amt = Math.min((Integer)ReflectionHelper.getPrivateValue(EntityPlayer.class, player, "field_71067_cb", "experienceTotal"), 10);
if (amt > 0){
EntityUtilities.deductXP(amt, player);
addXPToJournal(journal, amt);
}
}catch (Throwable t){
t.printStackTrace();
}
int removedXP = EntityUtilities.deductXP(10, player);
addXPToJournal(journal, removedXP);
}else{
int amt = Math.min(getXPInJournal(journal), 10);
if (amt > 0){
Expand Down
52 changes: 26 additions & 26 deletions src/main/java/am2/utility/EntityUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -327,40 +327,40 @@ 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){
try{
int xp = ReflectionHelper.getPrivateValue(EntityPlayer.class, player, "field_71067_cb", "experienceTotal");
float xpBarXP = ReflectionHelper.getPrivateValue(EntityPlayer.class, player, "field_71106_cc", "experience");
int currentLevel = ReflectionHelper.getPrivateValue(EntityPlayer.class, player, "field_71068_ca", "experienceLevel");

float fillRatio = (float)amount / xpBarCap(currentLevel);

if (xpBarXP > fillRatio){
xpBarXP -= fillRatio;
xp -= amount;
}else{
int level = getLevelFromXP(xp);
xp -= amount;
xpBarXP = (xpBarCap(level) + (xpBarXP - amount)) / xpBarCap(level);
ReflectionHelper.setPrivateValue(EntityPlayer.class, player, level, "field_71068_ca", "experienceLevel");
}

ReflectionHelper.setPrivateValue(EntityPlayer.class, player, xp, "field_71067_cb", "experienceTotal");
ReflectionHelper.setPrivateValue(EntityPlayer.class, player, xpBarXP, "field_71106_cc", "experience");
}catch (Throwable t){
t.printStackTrace();
}
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 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;
player.experienceLevel = 0;
player.experienceTotal -= removedXP;
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 53e11ad

Please sign in to comment.