Fixing recursion errors by switching from an unnecessary btree to a simple ArrayList.

This commit is contained in:
Glitchfinder 2012-12-31 19:40:33 -08:00
parent 0e853d44b7
commit d33334708d
5 changed files with 77 additions and 145 deletions

View File

@ -3,4 +3,11 @@ package com.gmail.nossr50.datatypes;
public class PlayerStat { public class PlayerStat {
public String name; public String name;
public int statVal = 0; public int statVal = 0;
public PlayerStat() {};
public PlayerStat(String name, int value) {
this.name = name;
this.statVal = value;
}
} }

View File

@ -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<PlayerStat> order = root.inOrder(new ArrayList<PlayerStat>());
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<PlayerStat> x = new ArrayList<PlayerStat>();
PlayerStat y = new PlayerStat();
y.name = "$mcMMO_DummyInfo";
y.statVal = 0;
x.add(y);
return x.toArray(new PlayerStat[x.size()]);
}
}
}

View File

@ -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<PlayerStat> inOrder(ArrayList<PlayerStat> 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;
}
}

View File

@ -109,7 +109,7 @@ public class Repair {
is.removeEnchantment(x); is.removeEnchantment(x);
} }
player.sendMessage(LocaleLoader.getString("Repair.Arcane.Lost")); player.sendMessage(LocaleLoader.getString("Repair.Arcane.Lost"));
clearEnchantTag(is, player); clearEnchantTag(is);
return; return;
} }
@ -143,7 +143,7 @@ public class Repair {
if (newEnchants.isEmpty()) { if (newEnchants.isEmpty()) {
player.sendMessage(LocaleLoader.getString("Repair.Arcane.Fail")); player.sendMessage(LocaleLoader.getString("Repair.Arcane.Fail"));
clearEnchantTag(is, player); clearEnchantTag(is);
} }
else if (downgraded || newEnchants.size() < enchants.size()) { else if (downgraded || newEnchants.size() < enchants.size()) {
player.sendMessage(LocaleLoader.getString("Repair.Arcane.Downgrade")); player.sendMessage(LocaleLoader.getString("Repair.Arcane.Downgrade"));
@ -153,7 +153,7 @@ public class Repair {
} }
} }
private static void clearEnchantTag(ItemStack is, Player player) { private static void clearEnchantTag(ItemStack is) {
Object o; Object o;
Class c; Class c;
Field f; Field f;

View File

@ -6,12 +6,14 @@ import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; 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.mcMMO;
import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.datatypes.PlayerStat; import com.gmail.nossr50.datatypes.PlayerStat;
import com.gmail.nossr50.datatypes.SkillType; import com.gmail.nossr50.datatypes.SkillType;
import com.gmail.nossr50.datatypes.Tree;
public class Leaderboard { public class Leaderboard {
private static mcMMO plugin = mcMMO.p; private static mcMMO plugin = mcMMO.p;
@ -22,22 +24,22 @@ public class Leaderboard {
* Create the leaderboards. * Create the leaderboards.
*/ */
public static void makeLeaderboards() { public static void makeLeaderboards() {
//Make Trees //Make Lists
Tree Mining = new Tree(); List<PlayerStat> Mining = new ArrayList<PlayerStat>();
Tree WoodCutting = new Tree(); List<PlayerStat> WoodCutting = new ArrayList<PlayerStat>();
Tree Herbalism = new Tree(); List<PlayerStat> Herbalism = new ArrayList<PlayerStat>();
Tree Excavation = new Tree(); List<PlayerStat> Excavation = new ArrayList<PlayerStat>();
Tree Acrobatics = new Tree(); List<PlayerStat> Acrobatics = new ArrayList<PlayerStat>();
Tree Repair = new Tree(); List<PlayerStat> Repair = new ArrayList<PlayerStat>();
Tree Swords = new Tree(); List<PlayerStat> Swords = new ArrayList<PlayerStat>();
Tree Axes = new Tree(); List<PlayerStat> Axes = new ArrayList<PlayerStat>();
Tree Archery = new Tree(); List<PlayerStat> Archery = new ArrayList<PlayerStat>();
Tree Unarmed = new Tree(); List<PlayerStat> Unarmed = new ArrayList<PlayerStat>();
Tree Taming = new Tree(); List<PlayerStat> Taming = new ArrayList<PlayerStat>();
Tree Fishing = new Tree(); List<PlayerStat> Fishing = new ArrayList<PlayerStat>();
Tree PowerLevel = new Tree(); List<PlayerStat> PowerLevel = new ArrayList<PlayerStat>();
//Add Data To Trees //Add Data To Lists
try { try {
FileReader file = new FileReader(location); FileReader file = new FileReader(location);
BufferedReader in = new BufferedReader(file); BufferedReader in = new BufferedReader(file);
@ -58,66 +60,66 @@ public class Leaderboard {
} }
if (character.length > 1 && Misc.isInt(character[1])) { 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]); powerLevel += Integer.valueOf(character[1]);
} }
if (character.length > 5 && Misc.isInt(character[5])) { 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]); powerLevel += Integer.valueOf(character[5]);
} }
if (character.length > 7 && Misc.isInt(character[7])) { 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]); powerLevel += Integer.valueOf(character[7]);
} }
if (character.length > 8 && Misc.isInt(character[8])) { 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]); powerLevel += Integer.valueOf(character[8]);
} }
if (character.length > 9 && Misc.isInt(character[9])) { 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]); powerLevel += Integer.valueOf(character[9]);
} }
if (character.length > 10 && Misc.isInt(character[10])) { 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]); powerLevel += Integer.valueOf(character[10]);
} }
if (character.length > 11 && Misc.isInt(character[11])) { 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]); powerLevel += Integer.valueOf(character[11]);
} }
if (character.length > 12 && Misc.isInt(character[12])) { 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]); powerLevel += Integer.valueOf(character[12]);
} }
if (character.length > 13 && Misc.isInt(character[13])) { 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]); powerLevel += Integer.valueOf(character[13]);
} }
if (character.length > 14 && Misc.isInt(character[14])) { 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]); powerLevel += Integer.valueOf(character[14]);
} }
if (character.length > 24 && Misc.isInt(character[24])) { 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]); powerLevel += Integer.valueOf(character[24]);
} }
if (character.length > 34 && Misc.isInt(character[34])) { 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 += Integer.valueOf(character[34]);
} }
PowerLevel.add(p, powerLevel); PowerLevel.add(new PlayerStat(p, powerLevel));
} }
in.close(); 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())); 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 //Write the leader board files
leaderWrite(Mining.inOrder(), SkillType.MINING); PlayerStat[] a = new PlayerStat[1];
leaderWrite(WoodCutting.inOrder(), SkillType.WOODCUTTING); leaderWrite(Mining.toArray(a), SkillType.MINING);
leaderWrite(Repair.inOrder(), SkillType.REPAIR); leaderWrite(WoodCutting.toArray(a), SkillType.WOODCUTTING);
leaderWrite(Unarmed.inOrder(), SkillType.UNARMED); leaderWrite(Repair.toArray(a), SkillType.REPAIR);
leaderWrite(Herbalism.inOrder(), SkillType.HERBALISM); leaderWrite(Unarmed.toArray(a), SkillType.UNARMED);
leaderWrite(Excavation.inOrder(), SkillType.EXCAVATION); leaderWrite(Herbalism.toArray(a), SkillType.HERBALISM);
leaderWrite(Archery.inOrder(), SkillType.ARCHERY); leaderWrite(Excavation.toArray(a), SkillType.EXCAVATION);
leaderWrite(Swords.inOrder(), SkillType.SWORDS); leaderWrite(Archery.toArray(a), SkillType.ARCHERY);
leaderWrite(Axes.inOrder(), SkillType.AXES); leaderWrite(Swords.toArray(a), SkillType.SWORDS);
leaderWrite(Acrobatics.inOrder(), SkillType.ACROBATICS); leaderWrite(Axes.toArray(a), SkillType.AXES);
leaderWrite(Taming.inOrder(), SkillType.TAMING); leaderWrite(Acrobatics.toArray(a), SkillType.ACROBATICS);
leaderWrite(Fishing.inOrder(), SkillType.FISHING); leaderWrite(Taming.toArray(a), SkillType.TAMING);
leaderWrite(PowerLevel.inOrder(), SkillType.ALL); 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()); plugin.getLogger().severe("Exception while writing to " + theLocation + " (Are you sure you formatted it correctly?)" + e.toString());
} }
} }
private static class SkillComparator implements Comparator<PlayerStat> {
@Override
public int compare(PlayerStat o1, PlayerStat o2) {
return (o2.statVal - o1.statVal);
}
}
} }