Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fixing a few issues in the Tracker #406

Merged
merged 3 commits into from

3 participants

@Glitchfinder
Owner

This pull request is intended to patch a few different issues currently in the issue tracker.

  • Commt @0e853d4 fixes a bug where the repair skill could remove all enchantments from an item but leave the enchantment effect in place, preventing it from being re-enchanted. This addresses issue #346.
  • Commit @d333347 fixes a potential stack overflow involving recursion by switching from an unnecessary binary tree to a simple ArrayList. This addresses issue #264.
  • Commit @707a388 fixes a bug where dropped items do not retain any customized NBT data. This addresses issue #337.
@Glitchfinder Glitchfinder was assigned
@Glitchfinder Glitchfinder referenced this pull request
Closed

Additions to mcMMO #301

@Glitchfinder Glitchfinder merged commit bfdb825 into mcMMO-Dev:master
@Bestle

Surely using the new ItemMeta API to remove the enchantments would be a lot nicer than using reflection.

Owner

As far as I'm aware, that method doesn't work. This isn't implemented properly in the Bukkit API.

Bestle, ItemMeta doesn't touch enchantments. At all. The whole issue was that bukkit doesn't remove the NBT tag for enchantments when the last enchantment is removed, and an item with an empty enchantment tag is still treated as an enchanted item.

This was fixed in Bukkit itself.

That was mentioned in IRC last night, For the moment, I'm leaving the code in place because not everyone will update bukkit and all of their plugins at the same time, and so this actually will help a few people.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
7 src/main/java/com/gmail/nossr50/datatypes/PlayerStat.java
@@ -3,4 +3,11 @@
public class PlayerStat {
public String name;
public int statVal = 0;
+
+ public PlayerStat() {};
+
+ public PlayerStat(String name, int value) {
+ this.name = name;
+ this.statVal = value;
+ }
}
View
50 src/main/java/com/gmail/nossr50/datatypes/Tree.java
@@ -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
50 src/main/java/com/gmail/nossr50/datatypes/TreeNode.java
@@ -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
30 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 @@ protected static void addEnchants(Player player, ItemStack is) {
is.removeEnchantment(x);
}
player.sendMessage(LocaleLoader.getString("Repair.Arcane.Lost"));
+ clearEnchantTag(is);
return;
}
@@ -141,6 +143,7 @@ protected static void addEnchants(Player player, ItemStack is) {
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 @@ else if (downgraded || newEnchants.size() < enchants.size()) {
}
}
+ 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.
*
View
111 src/main/java/com/gmail/nossr50/util/Leaderboard.java
@@ -6,12 +6,14 @@
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 @@
* 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();
-
- //Add Data To Trees
+ //Make Lists
+ List<PlayerStat> Mining = new ArrayList<PlayerStat>();
+ List<PlayerStat> WoodCutting = new ArrayList<PlayerStat>();
+ List<PlayerStat> Herbalism = new ArrayList<PlayerStat>();
+ List<PlayerStat> Excavation = new ArrayList<PlayerStat>();
+ List<PlayerStat> Acrobatics = new ArrayList<PlayerStat>();
+ List<PlayerStat> Repair = new ArrayList<PlayerStat>();
+ List<PlayerStat> Swords = new ArrayList<PlayerStat>();
+ List<PlayerStat> Axes = new ArrayList<PlayerStat>();
+ List<PlayerStat> Archery = new ArrayList<PlayerStat>();
+ List<PlayerStat> Unarmed = new ArrayList<PlayerStat>();
+ List<PlayerStat> Taming = new ArrayList<PlayerStat>();
+ List<PlayerStat> Fishing = new ArrayList<PlayerStat>();
+ List<PlayerStat> PowerLevel = new ArrayList<PlayerStat>();
+
+ //Add Data To Lists
try {
FileReader file = new FileReader(location);
BufferedReader in = new BufferedReader(file);
@@ -58,66 +60,66 @@ public static void makeLeaderboards() {
}
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 static void makeLeaderboards() {
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 static void updateLeaderboard(PlayerStat ps, SkillType skillType) {
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);
+ }
+ }
}
View
2  src/main/java/com/gmail/nossr50/util/Misc.java
@@ -253,7 +253,7 @@ public static void dropItem(Location location, ItemStack itemStack) {
return;
}
- location.getWorld().dropItemNaturally(location, itemStack);
+ location.getWorld().dropItemNaturally(location, itemStack).setItemStack(itemStack);
}
/**
Something went wrong with that request. Please try again.