From a8dec9a89befca32ec6bc042dea72fa07c9971fd Mon Sep 17 00:00:00 2001 From: tastybento Date: Fri, 29 Jan 2021 20:13:34 -0800 Subject: [PATCH] Prevents placing of water in nether biome greenhouses. Fixes https://github.com/BentoBoxWorld/Greenhouses/issues/71 --- .../listeners/GreenhouseEvents.java | 20 +++++++++- .../listeners/GreenhouseEventsTest.java | 38 ++++++++++++++++++- 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/src/main/java/world/bentobox/greenhouses/listeners/GreenhouseEvents.java b/src/main/java/world/bentobox/greenhouses/listeners/GreenhouseEvents.java index 5b4cf37..0ed2ec2 100644 --- a/src/main/java/world/bentobox/greenhouses/listeners/GreenhouseEvents.java +++ b/src/main/java/world/bentobox/greenhouses/listeners/GreenhouseEvents.java @@ -1,11 +1,15 @@ package world.bentobox.greenhouses.listeners; +import java.util.Collections; +import java.util.HashSet; import java.util.Optional; +import java.util.Set; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.Tag; import org.bukkit.World; +import org.bukkit.block.Biome; import org.bukkit.block.Block; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -27,6 +31,16 @@ import world.bentobox.greenhouses.data.Greenhouse; */ public class GreenhouseEvents implements Listener { private static final String BIOME = "[biome]"; + private static final Set NETHER_BIOMES; + static { + Set nb = new HashSet<>(); + nb.add(Biome.NETHER_WASTES); + nb.add(Biome.WARPED_FOREST); + nb.add(Biome.CRIMSON_FOREST); + nb.add(Biome.SOUL_SAND_VALLEY); + nb.add(Biome.BASALT_DELTAS); + NETHER_BIOMES = Collections.unmodifiableSet(nb); + } private final Greenhouses addon; public GreenhouseEvents(final Greenhouses addon) { @@ -41,7 +55,8 @@ public class GreenhouseEvents implements Listener { public void onPlayerInteractInNether(PlayerBucketEmptyEvent e) { if (e.getPlayer().getWorld().getEnvironment().equals(World.Environment.NETHER) && e.getBucket().equals(Material.WATER_BUCKET) - && addon.getManager().getMap().inGreenhouse(e.getBlockClicked().getLocation())) { + && !addon.getManager().getMap().getGreenhouse(e.getBlockClicked().getLocation()) + .map(gh -> gh.getBiomeRecipe().getBiome()).map(NETHER_BIOMES::contains).orElse(true)) { e.getBlockClicked().getRelative(e.getBlockFace()).setType(Material.WATER); } } @@ -56,7 +71,8 @@ public class GreenhouseEvents implements Listener { || !Tag.ICE.isTagged(e.getBlock().getType())) { return; } - if (addon.getManager().getMap().inGreenhouse(e.getBlock().getLocation())) { + if (!addon.getManager().getMap().getGreenhouse(e.getBlock().getLocation()) + .map(gh -> gh.getBiomeRecipe().getBiome()).map(NETHER_BIOMES::contains).orElse(true)) { e.setCancelled(true); e.getBlock().setType(Material.WATER); } diff --git a/src/test/java/world/bentobox/greenhouses/listeners/GreenhouseEventsTest.java b/src/test/java/world/bentobox/greenhouses/listeners/GreenhouseEventsTest.java index ef07cf4..61f3abf 100644 --- a/src/test/java/world/bentobox/greenhouses/listeners/GreenhouseEventsTest.java +++ b/src/test/java/world/bentobox/greenhouses/listeners/GreenhouseEventsTest.java @@ -95,8 +95,10 @@ public class GreenhouseEventsTest { when(map.getGreenhouse(eq(location2))).thenReturn(Optional.of(gh2)); BiomeRecipe br = new BiomeRecipe(); br.setFriendlyName("recipe1"); + br.setType(Biome.PLAINS); BiomeRecipe br2 = new BiomeRecipe(); br2.setFriendlyName("recipe2"); + br2.setType(Biome.NETHER_WASTES); // Names when(gh1.getBiomeRecipe()).thenReturn(br); when(gh2.getBiomeRecipe()).thenReturn(br2); @@ -144,6 +146,22 @@ 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 testOnPlayerInteractInNetherGreenhouse() { + Block clickedBlock = mock(Block.class); + when(clickedBlock.getLocation()).thenReturn(location2); + Block nextBlock = mock(Block.class); + when(clickedBlock.getRelative(any())).thenReturn(nextBlock); + 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)}. */ @@ -182,7 +200,7 @@ public class GreenhouseEventsTest { */ @Test public void testOnPlayerInteractInNetherNotInGreenhouse() { - when(map.inGreenhouse(any())).thenReturn(false); + when(map.getGreenhouse(eq(location))).thenReturn(Optional.empty()); Block clickedBlock = mock(Block.class); when(clickedBlock.getLocation()).thenReturn(location); Block nextBlock = mock(Block.class); @@ -204,12 +222,30 @@ public class GreenhouseEventsTest { 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); verify(block).setType(Material.WATER); assertTrue(e.isCancelled()); } + /** + * Test method for {@link world.bentobox.greenhouses.listeners.GreenhouseEvents#onIceBreak(org.bukkit.event.block.BlockBreakEvent)}. + */ + @Test + public void testOnIceBreakNetherBiomeGreenhouse() { + 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(location2); + BlockBreakEvent e = new BlockBreakEvent(block, player); + ghe.onIceBreak(e); + verify(block, never()).setType(Material.WATER); + assertFalse(e.isCancelled()); + } + /** * Test method for {@link world.bentobox.greenhouses.listeners.GreenhouseEvents#onIceBreak(org.bukkit.event.block.BlockBreakEvent)}. */