Fixed obsidian scooping replacing whole bucket stack with lava buckets

Fixes #338.
This commit is contained in:
Florian CUNY 2018-11-10 10:04:10 +01:00
parent 10270846b2
commit d507efbe77
2 changed files with 23 additions and 4 deletions

View File

@ -13,6 +13,7 @@ import org.bukkit.event.Listener;
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;
@ -61,7 +62,15 @@ public class ObsidianToLava implements Listener {
}
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);

View File

@ -21,8 +21,10 @@ import org.bukkit.entity.Player;
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;
@ -94,6 +96,8 @@ public class ObsidianToLavaTest {
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);
@ -137,6 +141,7 @@ public class ObsidianToLavaTest {
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);
@ -154,7 +159,9 @@ public class ObsidianToLavaTest {
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);
@ -162,6 +169,11 @@ public class ObsidianToLavaTest {
// 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);
@ -186,8 +198,6 @@ public class ObsidianToLavaTest {
// 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) {