diff --git a/src/main/java/com/gmail/nossr50/datatypes/PlayerStat.java b/src/main/java/com/gmail/nossr50/datatypes/PlayerStat.java index 4018ecbb0..948521e79 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/PlayerStat.java +++ b/src/main/java/com/gmail/nossr50/datatypes/PlayerStat.java @@ -3,4 +3,11 @@ package com.gmail.nossr50.datatypes; public class PlayerStat { public String name; public int statVal = 0; + + public PlayerStat() {}; + + public PlayerStat(String name, int value) { + this.name = name; + this.statVal = value; + } } \ No newline at end of file diff --git a/src/main/java/com/gmail/nossr50/datatypes/Tree.java b/src/main/java/com/gmail/nossr50/datatypes/Tree.java deleted file mode 100644 index e4a800ec4..000000000 --- a/src/main/java/com/gmail/nossr50/datatypes/Tree.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.gmail.nossr50.datatypes; - -import java.util.ArrayList; - -public class Tree { - - private TreeNode root = null; - - /** - * Add a node to this tree. - * - * @param p Player name - * @param in Stat value - */ - public void add(String p, int in) { - if (root == null) { - root = new TreeNode(p, in); - } - else { - root.add(p, in); - } - } - - /** - * Retrieve an array of PlayerStats from the Tree. - * - * @return the player stats of this tree, in order - */ - public PlayerStat[] inOrder() { - if (root != null) { - ArrayList order = root.inOrder(new ArrayList()); - return order.toArray(new PlayerStat[order.size()]); - } - else { - - /* - * Throw some dummy info in case the users file is empty. - * It's not a good fix but its better than rewriting the whole system. - */ - ArrayList x = new ArrayList(); - PlayerStat y = new PlayerStat(); - - y.name = "$mcMMO_DummyInfo"; - y.statVal = 0; - x.add(y); - - return x.toArray(new PlayerStat[x.size()]); - } - } -} diff --git a/src/main/java/com/gmail/nossr50/datatypes/TreeNode.java b/src/main/java/com/gmail/nossr50/datatypes/TreeNode.java deleted file mode 100644 index a5c73f077..000000000 --- a/src/main/java/com/gmail/nossr50/datatypes/TreeNode.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.gmail.nossr50.datatypes; - -import java.util.ArrayList; - -public class TreeNode { - TreeNode left = null; - TreeNode right = null; - PlayerStat ps = new PlayerStat(); - - public TreeNode(String p, int in) { - ps.statVal = in; - ps.name = p; - } - - public void add (String p, int in) { - if (in >= ps.statVal) { - if (left == null) { - left = new TreeNode(p, in); - } - else { - left.add(p, in); - } - } - else if(in < ps.statVal) { - if (right == null) { - right = new TreeNode(p, in); - } - else { - right.add(p, in); - } - } - } - - public ArrayList inOrder(ArrayList a) { - //if left node is not null than assign arrayList(a) to left.inOrder() - //GOES THROUGH THE ENTIRE LEFT BRANCH AND GRABS THE GREATEST NUMBER - - if (left != null) { - a = left.inOrder(a); - } - - a.add(ps); - - if (right != null) { - a = right.inOrder(a); - } - - return a; - } -} diff --git a/src/main/java/com/gmail/nossr50/skills/repair/Repair.java b/src/main/java/com/gmail/nossr50/skills/repair/Repair.java index 062ee7583..019ac2cf3 100644 --- a/src/main/java/com/gmail/nossr50/skills/repair/Repair.java +++ b/src/main/java/com/gmail/nossr50/skills/repair/Repair.java @@ -1,5 +1,6 @@ package com.gmail.nossr50.skills.repair; +import java.lang.reflect.Field; import java.util.Map; import java.util.Map.Entry; import java.util.Random; @@ -108,6 +109,7 @@ public class Repair { is.removeEnchantment(x); } player.sendMessage(LocaleLoader.getString("Repair.Arcane.Lost")); + clearEnchantTag(is); return; } @@ -141,6 +143,7 @@ public class Repair { if (newEnchants.isEmpty()) { player.sendMessage(LocaleLoader.getString("Repair.Arcane.Fail")); + clearEnchantTag(is); } else if (downgraded || newEnchants.size() < enchants.size()) { player.sendMessage(LocaleLoader.getString("Repair.Arcane.Downgrade")); @@ -150,6 +153,33 @@ public class Repair { } } + private static void clearEnchantTag(ItemStack is) { + Object o; + Class c; + Field f; + + o = is; + c = o.getClass(); + + try { + f = c.getDeclaredField("handle"); + f.setAccessible(true); + o = f.get(o); + + c = o.getClass(); + f = c.getDeclaredField("tag"); + o = f.get(o); + + c = o.getClass(); + f = c.getDeclaredField("map"); + f.setAccessible(true); + Map tagMap = (Map) f.get(o); + + tagMap.remove("ench"); + } + catch(Exception e) {} + } + /** * Gets chance of keeping enchantment during repair. * diff --git a/src/main/java/com/gmail/nossr50/util/Leaderboard.java b/src/main/java/com/gmail/nossr50/util/Leaderboard.java index fa461401a..933384017 100644 --- a/src/main/java/com/gmail/nossr50/util/Leaderboard.java +++ b/src/main/java/com/gmail/nossr50/util/Leaderboard.java @@ -6,12 +6,14 @@ import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.config.Config; import com.gmail.nossr50.datatypes.PlayerStat; import com.gmail.nossr50.datatypes.SkillType; -import com.gmail.nossr50.datatypes.Tree; public class Leaderboard { private static mcMMO plugin = mcMMO.p; @@ -22,22 +24,22 @@ public class Leaderboard { * Create the leaderboards. */ public static void makeLeaderboards() { - //Make Trees - Tree Mining = new Tree(); - Tree WoodCutting = new Tree(); - Tree Herbalism = new Tree(); - Tree Excavation = new Tree(); - Tree Acrobatics = new Tree(); - Tree Repair = new Tree(); - Tree Swords = new Tree(); - Tree Axes = new Tree(); - Tree Archery = new Tree(); - Tree Unarmed = new Tree(); - Tree Taming = new Tree(); - Tree Fishing = new Tree(); - Tree PowerLevel = new Tree(); + //Make Lists + List Mining = new ArrayList(); + List WoodCutting = new ArrayList(); + List Herbalism = new ArrayList(); + List Excavation = new ArrayList(); + List Acrobatics = new ArrayList(); + List Repair = new ArrayList(); + List Swords = new ArrayList(); + List Axes = new ArrayList(); + List Archery = new ArrayList(); + List Unarmed = new ArrayList(); + List Taming = new ArrayList(); + List Fishing = new ArrayList(); + List PowerLevel = new ArrayList(); - //Add Data To Trees + //Add Data To Lists try { FileReader file = new FileReader(location); BufferedReader in = new BufferedReader(file); @@ -58,66 +60,66 @@ public class Leaderboard { } if (character.length > 1 && Misc.isInt(character[1])) { - Mining.add(p, Integer.valueOf(character[1])); + Mining.add(new PlayerStat(p, Integer.valueOf(character[1]))); powerLevel += Integer.valueOf(character[1]); } if (character.length > 5 && Misc.isInt(character[5])) { - WoodCutting.add(p, Integer.valueOf(character[5])); + WoodCutting.add(new PlayerStat(p, Integer.valueOf(character[5]))); powerLevel += Integer.valueOf(character[5]); } if (character.length > 7 && Misc.isInt(character[7])) { - Repair.add(p, Integer.valueOf(character[7])); + Repair.add(new PlayerStat(p, Integer.valueOf(character[7]))); powerLevel += Integer.valueOf(character[7]); } if (character.length > 8 && Misc.isInt(character[8])) { - Unarmed.add(p, Integer.valueOf(character[8])); + Unarmed.add(new PlayerStat(p, Integer.valueOf(character[8]))); powerLevel += Integer.valueOf(character[8]); } if (character.length > 9 && Misc.isInt(character[9])) { - Herbalism.add(p, Integer.valueOf(character[9])); + Herbalism.add(new PlayerStat(p, Integer.valueOf(character[9]))); powerLevel += Integer.valueOf(character[9]); } if (character.length > 10 && Misc.isInt(character[10])) { - Excavation.add(p, Integer.valueOf(character[10])); + Excavation.add(new PlayerStat(p, Integer.valueOf(character[10]))); powerLevel += Integer.valueOf(character[10]); } if (character.length > 11 && Misc.isInt(character[11])) { - Archery.add(p, Integer.valueOf(character[11])); + Archery.add(new PlayerStat(p, Integer.valueOf(character[11]))); powerLevel += Integer.valueOf(character[11]); } if (character.length > 12 && Misc.isInt(character[12])) { - Swords.add(p, Integer.valueOf(character[12])); + Swords.add(new PlayerStat(p, Integer.valueOf(character[12]))); powerLevel += Integer.valueOf(character[12]); } if (character.length > 13 && Misc.isInt(character[13])) { - Axes.add(p, Integer.valueOf(character[13])); + Axes.add(new PlayerStat(p, Integer.valueOf(character[13]))); powerLevel += Integer.valueOf(character[13]); } if (character.length > 14 && Misc.isInt(character[14])) { - Acrobatics.add(p, Integer.valueOf(character[14])); + Acrobatics.add(new PlayerStat(p, Integer.valueOf(character[14]))); powerLevel += Integer.valueOf(character[14]); } if (character.length > 24 && Misc.isInt(character[24])) { - Taming.add(p, Integer.valueOf(character[24])); + Taming.add(new PlayerStat(p, Integer.valueOf(character[24]))); powerLevel += Integer.valueOf(character[24]); } if (character.length > 34 && Misc.isInt(character[34])) { - Fishing.add(p, Integer.valueOf(character[34])); + Fishing.add(new PlayerStat(p, Integer.valueOf(character[34]))); powerLevel += Integer.valueOf(character[34]); } - PowerLevel.add(p, powerLevel); + PowerLevel.add(new PlayerStat(p, powerLevel)); } in.close(); } @@ -125,20 +127,37 @@ public class Leaderboard { plugin.getLogger().severe(("Exception while reading " + location + " (Are you sure you formatted it correctly?)" + e.toString())); } + //Sort the leader boards + SkillComparator c = new SkillComparator(); + Collections.sort(Mining, c); + Collections.sort(WoodCutting, c); + Collections.sort(Repair, c); + Collections.sort(Unarmed, c); + Collections.sort(Herbalism, c); + Collections.sort(Excavation, c); + Collections.sort(Archery, c); + Collections.sort(Swords, c); + Collections.sort(Axes, c); + Collections.sort(Acrobatics, c); + Collections.sort(Taming, c); + Collections.sort(Fishing, c); + Collections.sort(PowerLevel, c); + //Write the leader board files - leaderWrite(Mining.inOrder(), SkillType.MINING); - leaderWrite(WoodCutting.inOrder(), SkillType.WOODCUTTING); - leaderWrite(Repair.inOrder(), SkillType.REPAIR); - leaderWrite(Unarmed.inOrder(), SkillType.UNARMED); - leaderWrite(Herbalism.inOrder(), SkillType.HERBALISM); - leaderWrite(Excavation.inOrder(), SkillType.EXCAVATION); - leaderWrite(Archery.inOrder(), SkillType.ARCHERY); - leaderWrite(Swords.inOrder(), SkillType.SWORDS); - leaderWrite(Axes.inOrder(), SkillType.AXES); - leaderWrite(Acrobatics.inOrder(), SkillType.ACROBATICS); - leaderWrite(Taming.inOrder(), SkillType.TAMING); - leaderWrite(Fishing.inOrder(), SkillType.FISHING); - leaderWrite(PowerLevel.inOrder(), SkillType.ALL); + PlayerStat[] a = new PlayerStat[1]; + leaderWrite(Mining.toArray(a), SkillType.MINING); + leaderWrite(WoodCutting.toArray(a), SkillType.WOODCUTTING); + leaderWrite(Repair.toArray(a), SkillType.REPAIR); + leaderWrite(Unarmed.toArray(a), SkillType.UNARMED); + leaderWrite(Herbalism.toArray(a), SkillType.HERBALISM); + leaderWrite(Excavation.toArray(a), SkillType.EXCAVATION); + leaderWrite(Archery.toArray(a), SkillType.ARCHERY); + leaderWrite(Swords.toArray(a), SkillType.SWORDS); + leaderWrite(Axes.toArray(a), SkillType.AXES); + leaderWrite(Acrobatics.toArray(a), SkillType.ACROBATICS); + leaderWrite(Taming.toArray(a), SkillType.TAMING); + leaderWrite(Fishing.toArray(a), SkillType.FISHING); + leaderWrite(PowerLevel.toArray(a), SkillType.ALL); } /** @@ -298,4 +317,10 @@ public class Leaderboard { plugin.getLogger().severe("Exception while writing to " + theLocation + " (Are you sure you formatted it correctly?)" + e.toString()); } } + private static class SkillComparator implements Comparator { + @Override + public int compare(PlayerStat o1, PlayerStat o2) { + return (o2.statVal - o1.statVal); + } + } } diff --git a/src/main/java/com/gmail/nossr50/util/Misc.java b/src/main/java/com/gmail/nossr50/util/Misc.java index c08d8730b..ffba54dfd 100644 --- a/src/main/java/com/gmail/nossr50/util/Misc.java +++ b/src/main/java/com/gmail/nossr50/util/Misc.java @@ -253,7 +253,7 @@ public class Misc { return; } - location.getWorld().dropItemNaturally(location, itemStack); + location.getWorld().dropItemNaturally(location, itemStack).setItemStack(itemStack); } /**