Add methods to check conflicting enchantments. Adds BUKKIT-3830

This commit is contained in:
GJ 2013-03-20 01:35:22 -04:00
parent 5f089137ee
commit 96ba65d506
3 changed files with 69 additions and 0 deletions

View file

@ -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);
}
}

View file

@ -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<String> getLore() {
return this.lore == null ? null : new ArrayList<String>(this.lore);
}
@ -555,6 +559,20 @@ class CraftMetaItem implements ItemMeta, Repairable {
}
}
static boolean checkConflictingEnchants(Map<Enchantment, Integer> 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

View file

@ -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));
}