Skip to content

Commit

Permalink
Fixes water placement in nether-related areas
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Jan 30, 2021
1 parent a8dec9a commit b0a0ae6
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

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;
Expand Down Expand Up @@ -51,30 +53,51 @@ public GreenhouseEvents(final Greenhouses addon) {
* 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)) {
// 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)) {
e.getBlockClicked().getRelative(e.getBlockFace()).setType(Material.WATER);
// 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);
}
}

/**
* 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);
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.getBlock().setType(Material.WATER);
b.setType(Material.AIR);
b.getWorld().playSound(b.getLocation(), Sound.BLOCK_GLASS_BREAK, 1F, 1F);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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;
Expand All @@ -29,6 +30,7 @@
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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -106,6 +112,9 @@ public void setUp() throws Exception {

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);
Expand Down Expand Up @@ -134,18 +143,36 @@ public void tearDown() throws Exception {
* 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);
ghe.onPlayerInteractInNether(e);
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)}.
*/
Expand All @@ -172,6 +199,8 @@ public void testOnPlayerInteractInNetherNotInNether() {
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);
Expand Down Expand Up @@ -266,7 +295,7 @@ public void testOnIceBreakNotIce() {
* 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);

Expand All @@ -275,10 +304,31 @@ public void testOnIceBreakNotNether() {
when(block.getWorld()).thenReturn(world);
BlockBreakEvent e = new BlockBreakEvent(block, player);
ghe.onIceBreak(e);
verify(block, never()).setType(Material.WATER);
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)}.
*/
Expand All @@ -290,6 +340,7 @@ public void testOnIceBreakNotInGreenhouse() {
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);
Expand Down

0 comments on commit b0a0ae6

Please sign in to comment.