Skip to content

Commit

Permalink
Prevents placing of water in nether biome greenhouses.
Browse files Browse the repository at this point in the history
Fixes #71
  • Loading branch information
tastybento committed Jan 30, 2021
1 parent 79d1c74 commit a8dec9a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -27,6 +31,16 @@
*/
public class GreenhouseEvents implements Listener {
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;

public GreenhouseEvents(final Greenhouses addon) {
Expand All @@ -41,7 +55,8 @@ public GreenhouseEvents(final Greenhouses addon) {
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);
}
}
Expand All @@ -56,7 +71,8 @@ public void onIceBreak(BlockBreakEvent e) {
|| !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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,10 @@ public void setUp() throws Exception {
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);
Expand Down Expand Up @@ -144,6 +146,22 @@ public void testOnPlayerInteractInNether() {
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)}.
*/
Expand Down Expand Up @@ -182,7 +200,7 @@ public void testOnPlayerInteractInNetherNotWaterBucket() {
*/
@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);
Expand All @@ -204,12 +222,30 @@ public void testOnIceBreak() {
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)}.
*/
Expand Down

0 comments on commit a8dec9a

Please sign in to comment.