Browse files

Fixing a bug where repairing could leave items with the enchant effec…

…t but no enchantments.
  • Loading branch information...
1 parent e607e04 commit 0e853d44b7369826dfbd1b442d28a4185209b0ab @Glitchfinder Glitchfinder committed Dec 31, 2012
Showing with 30 additions and 0 deletions.
  1. +30 −0 src/main/java/com/gmail/nossr50/skills/repair/Repair.java
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, player);
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, player);
}
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, Player player) {
+ 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.
*

5 comments on commit 0e853d4

@Bestle

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

@TfT-02
mcMMO Development member

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

@Glitchfinder
mcMMO Development member

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.

@Bestle

This was fixed in Bukkit itself.

@Glitchfinder
mcMMO Development member

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.

Please sign in to comment.