Prevents placing of water in nether biome greenhouses.

Fixes https://github.com/BentoBoxWorld/Greenhouses/issues/71
This commit is contained in:
tastybento 2021-01-29 20:13:34 -08:00
parent 79d1c743c1
commit a8dec9a89b
2 changed files with 55 additions and 3 deletions

View File

@ -1,11 +1,15 @@
package world.bentobox.greenhouses.listeners; package world.bentobox.greenhouses.listeners;
import java.util.Collections;
import java.util.HashSet;
import java.util.Optional; import java.util.Optional;
import java.util.Set;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Tag; import org.bukkit.Tag;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Biome;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
@ -27,6 +31,16 @@ import world.bentobox.greenhouses.data.Greenhouse;
*/ */
public class GreenhouseEvents implements Listener { public class GreenhouseEvents implements Listener {
private static final String BIOME = "[biome]"; private static final String BIOME = "[biome]";
private static final Set<Biome> NETHER_BIOMES;
static {
Set<Biome> 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; private final Greenhouses addon;
public GreenhouseEvents(final Greenhouses addon) { public GreenhouseEvents(final Greenhouses addon) {
@ -41,7 +55,8 @@ public class GreenhouseEvents implements Listener {
public void onPlayerInteractInNether(PlayerBucketEmptyEvent e) { public void onPlayerInteractInNether(PlayerBucketEmptyEvent e) {
if (e.getPlayer().getWorld().getEnvironment().equals(World.Environment.NETHER) if (e.getPlayer().getWorld().getEnvironment().equals(World.Environment.NETHER)
&& e.getBucket().equals(Material.WATER_BUCKET) && 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); e.getBlockClicked().getRelative(e.getBlockFace()).setType(Material.WATER);
} }
} }
@ -56,7 +71,8 @@ public class GreenhouseEvents implements Listener {
|| !Tag.ICE.isTagged(e.getBlock().getType())) { || !Tag.ICE.isTagged(e.getBlock().getType())) {
return; 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.setCancelled(true);
e.getBlock().setType(Material.WATER); e.getBlock().setType(Material.WATER);
} }

View File

@ -95,8 +95,10 @@ public class GreenhouseEventsTest {
when(map.getGreenhouse(eq(location2))).thenReturn(Optional.of(gh2)); when(map.getGreenhouse(eq(location2))).thenReturn(Optional.of(gh2));
BiomeRecipe br = new BiomeRecipe(); BiomeRecipe br = new BiomeRecipe();
br.setFriendlyName("recipe1"); br.setFriendlyName("recipe1");
br.setType(Biome.PLAINS);
BiomeRecipe br2 = new BiomeRecipe(); BiomeRecipe br2 = new BiomeRecipe();
br2.setFriendlyName("recipe2"); br2.setFriendlyName("recipe2");
br2.setType(Biome.NETHER_WASTES);
// Names // Names
when(gh1.getBiomeRecipe()).thenReturn(br); when(gh1.getBiomeRecipe()).thenReturn(br);
when(gh2.getBiomeRecipe()).thenReturn(br2); when(gh2.getBiomeRecipe()).thenReturn(br2);
@ -144,6 +146,22 @@ public class GreenhouseEventsTest {
verify(nextBlock).setType(Material.WATER); 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)}. * Test method for {@link world.bentobox.greenhouses.listeners.GreenhouseEvents#onPlayerInteractInNether(org.bukkit.event.player.PlayerInteractEvent)}.
*/ */
@ -182,7 +200,7 @@ public class GreenhouseEventsTest {
*/ */
@Test @Test
public void testOnPlayerInteractInNetherNotInGreenhouse() { public void testOnPlayerInteractInNetherNotInGreenhouse() {
when(map.inGreenhouse(any())).thenReturn(false); when(map.getGreenhouse(eq(location))).thenReturn(Optional.empty());
Block clickedBlock = mock(Block.class); Block clickedBlock = mock(Block.class);
when(clickedBlock.getLocation()).thenReturn(location); when(clickedBlock.getLocation()).thenReturn(location);
Block nextBlock = mock(Block.class); Block nextBlock = mock(Block.class);
@ -204,12 +222,30 @@ public class GreenhouseEventsTest {
Block block = mock(Block.class); Block block = mock(Block.class);
when(block.getType()).thenReturn(Material.ICE); when(block.getType()).thenReturn(Material.ICE);
when(block.getWorld()).thenReturn(world); when(block.getWorld()).thenReturn(world);
when(block.getLocation()).thenReturn(location);
BlockBreakEvent e = new BlockBreakEvent(block, player); BlockBreakEvent e = new BlockBreakEvent(block, player);
ghe.onIceBreak(e); ghe.onIceBreak(e);
verify(block).setType(Material.WATER); verify(block).setType(Material.WATER);
assertTrue(e.isCancelled()); 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)}. * Test method for {@link world.bentobox.greenhouses.listeners.GreenhouseEvents#onIceBreak(org.bukkit.event.block.BlockBreakEvent)}.
*/ */