From d99c95f6d1b9a91f5f9c254b8e2fa615ea31e3c6 Mon Sep 17 00:00:00 2001 From: fullwall Date: Mon, 9 Jan 2012 14:39:06 +0800 Subject: [Bleeding] Added Potions API. Fixes BUKKIT-389 --- .../org/bukkit/plugin/messaging/TestPlayer.java | 27 +++++ src/test/java/org/bukkit/potion/PotionTest.java | 109 +++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 src/test/java/org/bukkit/potion/PotionTest.java (limited to 'src/test/java') diff --git a/src/test/java/org/bukkit/plugin/messaging/TestPlayer.java b/src/test/java/org/bukkit/plugin/messaging/TestPlayer.java index 95e02c76..63180720 100644 --- a/src/test/java/org/bukkit/plugin/messaging/TestPlayer.java +++ b/src/test/java/org/bukkit/plugin/messaging/TestPlayer.java @@ -1,6 +1,7 @@ package org.bukkit.plugin.messaging; import java.net.InetSocketAddress; +import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -23,6 +24,8 @@ import org.bukkit.permissions.Permission; import org.bukkit.permissions.PermissionAttachment; import org.bukkit.permissions.PermissionAttachmentInfo; import org.bukkit.plugin.Plugin; +import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionEffectType; import org.bukkit.util.Vector; public class TestPlayer implements Player { @@ -621,4 +624,28 @@ public class TestPlayer implements Player { public boolean canSee(Player player) { throw new UnsupportedOperationException("Not supported yet."); } + + public boolean addPotionEffect(PotionEffect effect) { + throw new UnsupportedOperationException("Not supported yet."); + } + + public boolean addPotionEffect(PotionEffect effect, boolean force) { + throw new UnsupportedOperationException("Not supported yet."); + } + + public boolean addPotionEffects(Collection effects) { + throw new UnsupportedOperationException("Not supported yet."); + } + + public boolean hasPotionEffect(PotionEffectType type) { + throw new UnsupportedOperationException("Not supported yet."); + } + + public void removePotionEffect(PotionEffectType type) { + throw new UnsupportedOperationException("Not supported yet."); + } + + public Collection getActivePotionEffects() { + throw new UnsupportedOperationException("Not supported yet."); + } } diff --git a/src/test/java/org/bukkit/potion/PotionTest.java b/src/test/java/org/bukkit/potion/PotionTest.java new file mode 100644 index 00000000..fe641b86 --- /dev/null +++ b/src/test/java/org/bukkit/potion/PotionTest.java @@ -0,0 +1,109 @@ +package org.bukkit.potion; + +import static org.junit.Assert.*; +import static org.hamcrest.Matchers.is; + +import org.bukkit.Material; +import org.bukkit.entity.LivingEntity; +import org.bukkit.inventory.ItemStack; +import org.bukkit.potion.Potion.Tier; +import org.junit.Test; + +public class PotionTest { + @Test + public void applyToItemStack() { + Potion potion = new Potion(PotionType.POISON); + ItemStack stack = new ItemStack(Material.POTION, 1); + potion.apply(stack); + assertTrue(stack.getDurability() == potion.toDamageValue()); + } + + @Test + public void fromDamage() { + Potion potion = Potion.fromDamage(PotionType.POISON.getDamageValue()); + assertTrue(potion.getType() == PotionType.POISON); + potion = Potion.fromDamage(PotionType.POISON.getDamageValue() | SPLASH_BIT); + assertTrue(potion.getType() == PotionType.POISON && potion.isSplash()); + potion = Potion.fromDamage(0x25 /* Potion of Healing II */); + assertTrue(potion.getType() == PotionType.INSTANT_HEAL && potion.getTier() == Tier.TWO); + } + + @Test(expected = IllegalArgumentException.class) + public void illegalApplyToItemStack() { + Potion potion = new Potion(PotionType.POISON); + potion.apply(new ItemStack(Material.AIR, 1)); + } + + @Test + public void ItemStackConversion() { + Potion potion = new Potion(PotionType.POISON); + ItemStack itemstack = potion.toItemStack(1); + assertThat(itemstack.getType(), is(Material.POTION)); + assertTrue(itemstack.getAmount() == 1); + assertTrue(itemstack.getDurability() == potion.toDamageValue()); + } + + @Test + public void setExtended() { + Potion potion = new Potion(PotionType.POISON); + assertFalse(potion.hasExtendedDuration()); + potion.setHasExtendedDuration(true); + assertTrue(potion.hasExtendedDuration()); + assertTrue((potion.toDamageValue() & EXTENDED_BIT) != 0); + } + + @Test + public void setSplash() { + Potion potion = new Potion(PotionType.POISON); + assertFalse(potion.isSplash()); + potion.setSplash(true); + assertTrue(potion.isSplash()); + assertTrue((potion.toDamageValue() & SPLASH_BIT) != 0); + } + + @Test + public void setTier() { + Potion potion = new Potion(PotionType.POISON); + assertThat(potion.getTier(), is(Tier.ONE)); + potion.setTier(Tier.TWO); + assertThat(potion.getTier(), is(Tier.TWO)); + assertTrue(potion.toDamageValue() == (PotionType.POISON.getDamageValue() | potion.getTier().getDamageBit())); + } + + @Test + public void useNulls() { + try { + new Potion(null); + fail("cannot use null type in constructor"); + } catch (IllegalArgumentException ex) { + } + + try { + new Potion(PotionType.POISON, null); + fail("cannot use null tier in constructor"); + } catch (IllegalArgumentException ex) { + } + + Potion potion = new Potion(PotionType.POISON); + try { + potion.setTier(null); + fail("cannot set a null tier"); + } catch (IllegalArgumentException ex) { + } + + try { + potion.apply((ItemStack) null); + fail("cannot apply to a null itemstack"); + } catch (IllegalArgumentException ex) { + } + + try { + potion.apply((LivingEntity) null); + fail("cannot apply to a null entity"); + } catch (IllegalArgumentException ex) { + } + } + + private static final int EXTENDED_BIT = 0x40; + private static final int SPLASH_BIT = 0x4000; +} -- cgit v1.2.3