diff --git a/src/main/java/world/bentobox/greenhouses/listeners/GreenhouseEvents.java b/src/main/java/world/bentobox/greenhouses/listeners/GreenhouseEvents.java index 0ed2ec2..1cf1cb7 100644 --- a/src/main/java/world/bentobox/greenhouses/listeners/GreenhouseEvents.java +++ b/src/main/java/world/bentobox/greenhouses/listeners/GreenhouseEvents.java @@ -7,6 +7,8 @@ import java.util.Set; import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.Particle; +import org.bukkit.Sound; import org.bukkit.Tag; import org.bukkit.World; import org.bukkit.block.Biome; @@ -51,13 +53,25 @@ public class GreenhouseEvents implements Listener { * Permits water to be placed in the Nether if in a greenhouse and in an acceptable biome * @param e - event */ - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled=true) + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled=true) public void onPlayerInteractInNether(PlayerBucketEmptyEvent e) { + if (!e.getBucket().equals(Material.WATER_BUCKET)) { + return; + } + Block b = e.getBlockClicked().getRelative(e.getBlockFace()); if (e.getPlayer().getWorld().getEnvironment().equals(World.Environment.NETHER) - && e.getBucket().equals(Material.WATER_BUCKET) - && !addon.getManager().getMap().getGreenhouse(e.getBlockClicked().getLocation()) + && !addon.getManager().getMap().getGreenhouse(b.getLocation()) .map(gh -> gh.getBiomeRecipe().getBiome()).map(NETHER_BIOMES::contains).orElse(true)) { - e.getBlockClicked().getRelative(e.getBlockFace()).setType(Material.WATER); + // In Nether not a nether greenhouse + b.setType(Material.WATER); + } else if (!e.getPlayer().getWorld().getEnvironment().equals(World.Environment.NETHER) + && addon.getManager().getMap().getGreenhouse(b.getLocation()) + .map(gh -> gh.getBiomeRecipe().getBiome()).map(NETHER_BIOMES::contains).orElse(true)) { + // Not in Nether, in a nether greenhouse + e.setCancelled(true); + e.getPlayer().getInventory().getItemInMainHand().setType(Material.BUCKET); + b.getWorld().spawnParticle(Particle.SMOKE_NORMAL, b.getLocation(), 10); + b.getWorld().playSound(b.getLocation(), Sound.ENTITY_GENERIC_EXTINGUISH_FIRE, 1F, 5F); } } @@ -65,16 +79,25 @@ public class GreenhouseEvents implements Listener { * Makes water in the Nether if ice is broken and in a greenhouse * @param e - event */ - @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled=true) + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled=true) public void onIceBreak(BlockBreakEvent e) { - if (!e.getBlock().getWorld().getEnvironment().equals(World.Environment.NETHER) - || !Tag.ICE.isTagged(e.getBlock().getType())) { + if (!Tag.ICE.isTagged(e.getBlock().getType())) { return; } - if (!addon.getManager().getMap().getGreenhouse(e.getBlock().getLocation()) + Block b = e.getBlock(); + if (b.getWorld().getEnvironment().equals(World.Environment.NETHER) + && !addon.getManager().getMap().getGreenhouse(b.getLocation()) .map(gh -> gh.getBiomeRecipe().getBiome()).map(NETHER_BIOMES::contains).orElse(true)) { + // e.setCancelled(true); - e.getBlock().setType(Material.WATER); + b.setType(Material.WATER); + } else if (!e.getPlayer().getWorld().getEnvironment().equals(World.Environment.NETHER) + && addon.getManager().getMap().getGreenhouse(b.getLocation()) + .map(gh -> gh.getBiomeRecipe().getBiome()).map(NETHER_BIOMES::contains).orElse(true)) { + // Not in Nether, in a nether greenhouse + e.setCancelled(true); + b.setType(Material.AIR); + b.getWorld().playSound(b.getLocation(), Sound.BLOCK_GLASS_BREAK, 1F, 1F); } } diff --git a/src/test/java/world/bentobox/greenhouses/listeners/GreenhouseEventsTest.java b/src/test/java/world/bentobox/greenhouses/listeners/GreenhouseEventsTest.java index 61f3abf..3da3ad4 100644 --- a/src/test/java/world/bentobox/greenhouses/listeners/GreenhouseEventsTest.java +++ b/src/test/java/world/bentobox/greenhouses/listeners/GreenhouseEventsTest.java @@ -15,6 +15,7 @@ import java.util.Optional; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.Sound; import org.bukkit.Tag; import org.bukkit.World; import org.bukkit.World.Environment; @@ -29,6 +30,7 @@ import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.PlayerInventory; import org.bukkit.util.BoundingBox; import org.junit.After; import org.junit.Before; @@ -77,6 +79,10 @@ public class GreenhouseEventsTest { private Greenhouse gh1; @Mock private Greenhouse gh2; + @Mock + private PlayerInventory inv; + @Mock + private ItemStack waterBucket; /** * @throws java.lang.Exception @@ -106,6 +112,9 @@ public class GreenhouseEventsTest { when(player.getWorld()).thenReturn(world); when(world.getEnvironment()).thenReturn(Environment.NETHER); + when(player.getInventory()).thenReturn(inv); + when(inv.getItemInMainHand()).thenReturn(waterBucket); + // Location when(location.getBlockX()).thenReturn(5); when(location.getBlockY()).thenReturn(15); @@ -134,11 +143,12 @@ public class GreenhouseEventsTest { * Test method for {@link world.bentobox.greenhouses.listeners.GreenhouseEvents#onPlayerInteractInNether(org.bukkit.event.player.PlayerInteractEvent)}. */ @Test - public void testOnPlayerInteractInNether() { + public void testOnPlayerInteractInNetherInGreenhouse() { Block clickedBlock = mock(Block.class); when(clickedBlock.getLocation()).thenReturn(location); Block nextBlock = mock(Block.class); when(clickedBlock.getRelative(any())).thenReturn(nextBlock); + when(nextBlock.getLocation()).thenReturn(location); ItemStack item = mock(ItemStack.class); when(item.getType()).thenReturn(Material.WATER_BUCKET); PlayerBucketEmptyEvent e = new PlayerBucketEmptyEvent(player, nextBlock, clickedBlock, BlockFace.UP, Material.WATER_BUCKET, item); @@ -146,6 +156,23 @@ public class GreenhouseEventsTest { verify(nextBlock).setType(Material.WATER); } + /** + * Test method for {@link world.bentobox.greenhouses.listeners.GreenhouseEvents#onPlayerInteractInNether(org.bukkit.event.player.PlayerInteractEvent)}. + */ + @Test + public void testOnPlayerInteractInNetherOutsideOfGreenhouse() { + Block clickedBlock = mock(Block.class); + when(clickedBlock.getLocation()).thenReturn(location); + Block nextBlock = mock(Block.class); + when(clickedBlock.getRelative(any())).thenReturn(nextBlock); + when(nextBlock.getLocation()).thenReturn(mock(Location.class)); + ItemStack item = mock(ItemStack.class); + when(item.getType()).thenReturn(Material.WATER_BUCKET); + PlayerBucketEmptyEvent e = new PlayerBucketEmptyEvent(player, nextBlock, clickedBlock, BlockFace.UP, Material.WATER_BUCKET, item); + ghe.onPlayerInteractInNether(e); + verify(nextBlock, never()).setType(Material.WATER); + } + /** * Test method for {@link world.bentobox.greenhouses.listeners.GreenhouseEvents#onPlayerInteractInNether(org.bukkit.event.player.PlayerInteractEvent)}. */ @@ -172,6 +199,8 @@ public class GreenhouseEventsTest { when(clickedBlock.getLocation()).thenReturn(location); Block nextBlock = mock(Block.class); when(clickedBlock.getRelative(any())).thenReturn(nextBlock); + when(clickedBlock.getWorld()).thenReturn(world); + when(nextBlock.getWorld()).thenReturn(world); ItemStack item = mock(ItemStack.class); when(item.getType()).thenReturn(Material.WATER_BUCKET); PlayerBucketEmptyEvent e = new PlayerBucketEmptyEvent(player, nextBlock, clickedBlock, BlockFace.UP, Material.WATER_BUCKET, item); @@ -266,7 +295,7 @@ public class GreenhouseEventsTest { * Test method for {@link world.bentobox.greenhouses.listeners.GreenhouseEvents#onIceBreak(org.bukkit.event.block.BlockBreakEvent)}. */ @Test - public void testOnIceBreakNotNether() { + public void testOnIceBreakNotNetherNetherGreenhouse() { when(world.getEnvironment()).thenReturn(Environment.THE_END); when(Tag.ICE.isTagged(any(Material.class))).thenReturn(true); @@ -275,10 +304,31 @@ public class GreenhouseEventsTest { when(block.getWorld()).thenReturn(world); BlockBreakEvent e = new BlockBreakEvent(block, player); ghe.onIceBreak(e); - verify(block, never()).setType(Material.WATER); - assertFalse(e.isCancelled()); + verify(block).setType(Material.AIR); + assertTrue(e.isCancelled()); + verify(world).playSound(any(), eq(Sound.BLOCK_GLASS_BREAK), eq(1F), eq(1F)); + } + /** + * Test method for {@link world.bentobox.greenhouses.listeners.GreenhouseEvents#onIceBreak(org.bukkit.event.block.BlockBreakEvent)}. + */ + @Test + public void testOnIceBreakNotNetherNotNetherGreenhouse() { + when(world.getEnvironment()).thenReturn(Environment.THE_END); + when(Tag.ICE.isTagged(any(Material.class))).thenReturn(true); + + Block block = mock(Block.class); + when(block.getType()).thenReturn(Material.ICE); + when(block.getWorld()).thenReturn(world); + when(block.getLocation()).thenReturn(location); + BlockBreakEvent e = new BlockBreakEvent(block, player); + ghe.onIceBreak(e); + assertFalse(e.isCancelled()); + verify(block, never()).setType(any()); + } + + /** * Test method for {@link world.bentobox.greenhouses.listeners.GreenhouseEvents#onIceBreak(org.bukkit.event.block.BlockBreakEvent)}. */ @@ -290,6 +340,7 @@ public class GreenhouseEventsTest { Block block = mock(Block.class); when(block.getType()).thenReturn(Material.ICE); when(block.getWorld()).thenReturn(world); + BlockBreakEvent e = new BlockBreakEvent(block, player); ghe.onIceBreak(e); verify(block, never()).setType(Material.WATER);