diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaEnchantedBook.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaEnchantedBook.java index 612a630d4a..f975e867e9 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaEnchantedBook.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaEnchantedBook.java @@ -161,4 +161,8 @@ class CraftMetaEnchantedBook extends CraftMetaItem implements EnchantmentStorage public boolean hasStoredEnchants() { return !(enchantments == null || enchantments.isEmpty()); } + + public boolean hasConflictingStoredEnchant(Enchantment ench) { + return checkConflictingEnchants(enchantments, ench); + } } diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java index 30916f9946..90e7ffc352 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java @@ -405,6 +405,10 @@ class CraftMetaItem implements ItemMeta, Repairable { return !(enchantments == null || enchantments.isEmpty()); } + public boolean hasConflictingEnchant(Enchantment ench) { + return checkConflictingEnchants(enchantments, ench); + } + public List getLore() { return this.lore == null ? null : new ArrayList(this.lore); } @@ -555,6 +559,20 @@ class CraftMetaItem implements ItemMeta, Repairable { } } + static boolean checkConflictingEnchants(Map enchantments, Enchantment ench) { + if (enchantments == null || enchantments.isEmpty()) { + return false; + } + + for (Enchantment enchant : enchantments.keySet()) { + if (enchant.conflictsWith(ench)) { + return true; + } + } + + return false; + } + @Override public final String toString() { return SerializableMeta.classMap.get(getClass()) + "_META:" + serialize(); // TODO: cry diff --git a/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java b/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java index b59b36a96b..abf84ed635 100644 --- a/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java +++ b/src/test/java/org/bukkit/craftbukkit/inventory/ItemMetaTest.java @@ -21,6 +21,7 @@ import org.bukkit.inventory.meta.BookMeta; import org.bukkit.inventory.meta.EnchantmentStorageMeta; import org.bukkit.inventory.meta.FireworkEffectMeta; import org.bukkit.inventory.meta.FireworkMeta; +import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.LeatherArmorMeta; import org.bukkit.inventory.meta.MapMeta; import org.bukkit.inventory.meta.PotionMeta; @@ -62,6 +63,52 @@ public class ItemMetaTest extends AbstractTestingBase { } } + @Test + public void testConflictingEnchantment() { + ItemMeta itemMeta = Bukkit.getItemFactory().getItemMeta(Material.DIAMOND_PICKAXE); + assertThat(itemMeta.hasConflictingEnchant(Enchantment.DURABILITY), is(false)); + + itemMeta.addEnchant(Enchantment.SILK_TOUCH, 1, false); + assertThat(itemMeta.hasConflictingEnchant(Enchantment.DURABILITY), is(false)); + assertThat(itemMeta.hasConflictingEnchant(Enchantment.LOOT_BONUS_BLOCKS), is(true)); + assertThat(itemMeta.hasConflictingEnchant(null), is(false)); + } + + @Test + public void testConflictingStoredEnchantment() { + EnchantmentStorageMeta itemMeta = (EnchantmentStorageMeta) Bukkit.getItemFactory().getItemMeta(Material.ENCHANTED_BOOK); + assertThat(itemMeta.hasConflictingStoredEnchant(Enchantment.DURABILITY), is(false)); + + itemMeta.addStoredEnchant(Enchantment.SILK_TOUCH, 1, false); + assertThat(itemMeta.hasConflictingStoredEnchant(Enchantment.DURABILITY), is(false)); + assertThat(itemMeta.hasConflictingStoredEnchant(Enchantment.LOOT_BONUS_BLOCKS), is(true)); + assertThat(itemMeta.hasConflictingStoredEnchant(null), is(false)); + } + + @Test + public void testConflictingEnchantments() { + ItemMeta itemMeta = Bukkit.getItemFactory().getItemMeta(Material.DIAMOND_PICKAXE); + itemMeta.addEnchant(Enchantment.DURABILITY, 6, true); + itemMeta.addEnchant(Enchantment.DIG_SPEED, 6, true); + assertThat(itemMeta.hasConflictingEnchant(Enchantment.LOOT_BONUS_BLOCKS), is(false)); + + itemMeta.addEnchant(Enchantment.SILK_TOUCH, 1, false); + assertThat(itemMeta.hasConflictingEnchant(Enchantment.LOOT_BONUS_BLOCKS), is(true)); + assertThat(itemMeta.hasConflictingEnchant(null), is(false)); + } + + @Test + public void testConflictingStoredEnchantments() { + EnchantmentStorageMeta itemMeta = (EnchantmentStorageMeta) Bukkit.getItemFactory().getItemMeta(Material.ENCHANTED_BOOK); + itemMeta.addStoredEnchant(Enchantment.DURABILITY, 6, true); + itemMeta.addStoredEnchant(Enchantment.DIG_SPEED, 6, true); + assertThat(itemMeta.hasConflictingStoredEnchant(Enchantment.LOOT_BONUS_BLOCKS), is(false)); + + itemMeta.addStoredEnchant(Enchantment.SILK_TOUCH, 1, false); + assertThat(itemMeta.hasConflictingStoredEnchant(Enchantment.LOOT_BONUS_BLOCKS), is(true)); + assertThat(itemMeta.hasConflictingStoredEnchant(null), is(false)); + } + private static FireworkMeta newFireworkMeta() { return ((FireworkMeta) Bukkit.getItemFactory().getItemMeta(Material.FIREWORK)); }