From 5840bb7a8e3cdbd38cdfd11b338471b5a56fc59c Mon Sep 17 00:00:00 2001 From: DoomFruit Date: Sat, 12 Sep 2015 19:45:10 +0100 Subject: [PATCH 1/7] Fix for #1218 --- src/main/java/am2/items/ItemJournal.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/am2/items/ItemJournal.java b/src/main/java/am2/items/ItemJournal.java index 5837890cd..3bf3d897f 100644 --- a/src/main/java/am2/items/ItemJournal.java +++ b/src/main/java/am2/items/ItemJournal.java @@ -70,7 +70,7 @@ public ItemStack onItemRightClick(ItemStack journal, World world, EntityPlayer p if (player.isSneaking()){ try{ - int amt = Math.min((Integer)ReflectionHelper.getPrivateValue(EntityPlayer.class, player, "field_71067_cb", "experienceTotal"), 10); + int amt = Math.min(player.experienceTotal, 10); if (amt > 0){ EntityUtilities.deductXP(amt, player); addXPToJournal(journal, amt); From 441732cd7cafa62e1a6d18f49ed91d2d01b3eb13 Mon Sep 17 00:00:00 2001 From: DoomFruit Date: Sat, 12 Sep 2015 19:46:13 +0100 Subject: [PATCH 2/7] Fix for #1218 --- .../java/am2/utility/EntityUtilities.java | 37 ++++++++----------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/src/main/java/am2/utility/EntityUtilities.java b/src/main/java/am2/utility/EntityUtilities.java index 0df5d5f07..bc636518d 100644 --- a/src/main/java/am2/utility/EntityUtilities.java +++ b/src/main/java/am2/utility/EntityUtilities.java @@ -339,28 +339,23 @@ public static int xpBarCap(int experienceLevel){ } 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(); + // 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 = 0; + for(int i = 0; i < player.experienceLevel; i++){ + effectiveTotal += xpBarCap(i); } + effectiveTotal += (int)(player.experience * xpBarCap(player.experienceLevel)); + + int newTotal = effectiveTotal - amount; + if (newTotal < 0) + newTotal = 0; + + player.experience = 0.0F; + player.experienceLevel = 0; + player.experienceTotal = 0; + player.addExperience(newTotal); } public static float modifySoundPitch(Entity e, float p){ From 171c34783d5e7b5ccff11295820e9e6e3b98bd3f Mon Sep 17 00:00:00 2001 From: DoomFruit Date: Sat, 12 Sep 2015 19:54:04 +0100 Subject: [PATCH 3/7] Fix for #1218 --- src/main/java/am2/utility/EntityUtilities.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/am2/utility/EntityUtilities.java b/src/main/java/am2/utility/EntityUtilities.java index bc636518d..c7665e312 100644 --- a/src/main/java/am2/utility/EntityUtilities.java +++ b/src/main/java/am2/utility/EntityUtilities.java @@ -354,7 +354,9 @@ public static void deductXP(int amount, EntityPlayer player){ player.experience = 0.0F; player.experienceLevel = 0; - player.experienceTotal = 0; + player.experienceTotal -= amount; + if(player.experienceTotal < 0) + player.experienceTotal = 0; player.addExperience(newTotal); } From deed98e3137c933d28d01a02357e6d0a8f2595f6 Mon Sep 17 00:00:00 2001 From: DoomFruit Date: Sat, 12 Sep 2015 19:55:48 +0100 Subject: [PATCH 4/7] Fix for #1218 --- src/main/java/am2/utility/EntityUtilities.java | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/main/java/am2/utility/EntityUtilities.java b/src/main/java/am2/utility/EntityUtilities.java index c7665e312..47b634b55 100644 --- a/src/main/java/am2/utility/EntityUtilities.java +++ b/src/main/java/am2/utility/EntityUtilities.java @@ -342,13 +342,7 @@ public static void 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 = 0; - for(int i = 0; i < player.experienceLevel; i++){ - effectiveTotal += xpBarCap(i); - } - effectiveTotal += (int)(player.experience * xpBarCap(player.experienceLevel)); - - int newTotal = effectiveTotal - amount; + int newTotal = getXPFromLevel(player.experienceLevel) - amount; if (newTotal < 0) newTotal = 0; From e89a38705e661766c092d249a6a1ea25d8285a72 Mon Sep 17 00:00:00 2001 From: DoomFruit Date: Sat, 12 Sep 2015 20:50:00 +0100 Subject: [PATCH 5/7] Fix for #1218 --- .../java/am2/utility/EntityUtilities.java | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) 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){ From 1ffa493fbd14810e5986768337d8e468d40b9dd5 Mon Sep 17 00:00:00 2001 From: DoomFruit Date: Sat, 12 Sep 2015 20:50:36 +0100 Subject: [PATCH 6/7] Fix for #1218 --- src/main/java/am2/items/ItemJournal.java | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/main/java/am2/items/ItemJournal.java b/src/main/java/am2/items/ItemJournal.java index 3bf3d897f..1cdf39a91 100644 --- a/src/main/java/am2/items/ItemJournal.java +++ b/src/main/java/am2/items/ItemJournal.java @@ -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(player.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){ From 8fb91cb2b61e7e0be509c7f54741f5e73d640a0b Mon Sep 17 00:00:00 2001 From: DoomFruit Date: Sat, 12 Sep 2015 20:56:40 +0100 Subject: [PATCH 7/7] Subtracted the wrong variable from experienceTotal --- src/main/java/am2/utility/EntityUtilities.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/am2/utility/EntityUtilities.java b/src/main/java/am2/utility/EntityUtilities.java index 6aee75718..23d641305 100644 --- a/src/main/java/am2/utility/EntityUtilities.java +++ b/src/main/java/am2/utility/EntityUtilities.java @@ -355,7 +355,7 @@ public static int deductXP(int amount, EntityPlayer player){ player.experience = 0.0F; player.experienceLevel = 0; - player.experienceTotal -= amount; + player.experienceTotal -= removedXP; if(player.experienceTotal < 0) player.experienceTotal = 0; player.addExperience(newTotal);