Skip to content

Commit

Permalink
Fixed obsidian scooping replacing whole bucket stack with lava buckets
Browse files Browse the repository at this point in the history
Fixes #338.
  • Loading branch information
Poslovitch committed Nov 10, 2018
1 parent 1027084 commit d507efb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;

import org.bukkit.inventory.ItemStack;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.user.User;

Expand Down Expand Up @@ -61,7 +62,15 @@ public boolean onPlayerInteract(final PlayerInteractEvent e) {
}

user.sendMessage("general.tips.changing-obsidian-to-lava");
e.getItem().setType(Material.LAVA_BUCKET);
if (e.getItem().getAmount() == 1) {
// Needs some special handling when there is only 1 bucket in the stack
e.getItem().setType(Material.LAVA_BUCKET);
} else {
// Remove one empty bucket and add a lava bucket to the player's inventory
e.getItem().setAmount(e.getItem().getAmount() - 1);
e.getPlayer().getInventory().addItem(new ItemStack(Material.LAVA_BUCKET));
}

e.getPlayer().getWorld().playSound(e.getPlayer().getLocation(), Sound.ITEM_BUCKET_FILL_LAVA, 1F, 1F);
e.getClickedBlock().setType(Material.AIR);
e.setCancelled(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.plugin.PluginManager;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -94,6 +96,8 @@ public void testOnPlayerInteract() {
when(location.getBlockZ()).thenReturn(0);
when(who.getLocation()).thenReturn(location);

when(who.getInventory()).thenReturn(mock(PlayerInventory.class));

// Worlds
IslandWorldManager iwm = mock(IslandWorldManager.class);
when(plugin.getIWM()).thenReturn(iwm);
Expand Down Expand Up @@ -137,6 +141,7 @@ public void testOnPlayerInteract() {
Material inHand = Material.ACACIA_DOOR;
Material block = Material.BROWN_MUSHROOM;
when(item.getType()).thenReturn(inHand);
when(item.getAmount()).thenReturn(1);
when(clickedBlock.getType()).thenReturn(block);
// Create the event
testEvent(plugin, who, action, item, clickedBlock);
Expand All @@ -154,14 +159,21 @@ public void testOnPlayerInteract() {
when(clickedBlock.getType()).thenReturn(block);
// Create the event
testEvent(plugin, who, action, item, clickedBlock);
// Test positive


// Positive test with 1 bucket in the stack
inHand = Material.BUCKET;
block = Material.OBSIDIAN;
when(item.getType()).thenReturn(inHand);
when(clickedBlock.getType()).thenReturn(block);
// Create the event
testEvent(plugin, who, action, item, clickedBlock);

// Positive test with 32 bucket in the stack
when(item.getAmount()).thenReturn(32);
// Create the event
testEvent(plugin, who, action, item, clickedBlock);


PlayerInteractEvent event = new PlayerInteractEvent(who, action, item, clickedBlock, BlockFace.EAST);

Expand All @@ -186,8 +198,6 @@ public void testOnPlayerInteract() {
// Test when player is not on island
when(im.userIsOnIsland(Mockito.any(), Mockito.any())).thenReturn(false);
assertFalse(listener.onPlayerInteract(event));


}

private void testEvent(BentoBox plugin, Player who, Action action, ItemStack item, Block clickedBlock) {
Expand Down

0 comments on commit d507efb

Please sign in to comment.