mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2025-01-25 09:31:46 +01:00
Prevent bucket duping when scooping obsidian Part 35
https://github.com/BentoBoxWorld/BentoBox/issues/1825
This commit is contained in:
parent
7cadebc792
commit
6b839d9c69
@ -5,6 +5,7 @@ import java.util.HashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.FluidCollisionMode;
|
||||||
import org.bukkit.GameMode;
|
import org.bukkit.GameMode;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.Sound;
|
import org.bukkit.Sound;
|
||||||
@ -51,7 +52,13 @@ public class ObsidianScoopingListener extends FlagListener {
|
|||||||
private boolean lookForLava(PlayerInteractEvent e) {
|
private boolean lookForLava(PlayerInteractEvent e) {
|
||||||
Player player = e.getPlayer();
|
Player player = e.getPlayer();
|
||||||
ItemStack bucket = e.getItem();
|
ItemStack bucket = e.getItem();
|
||||||
Block b = e.getClickedBlock();
|
|
||||||
|
// Get block player is looking at
|
||||||
|
Block b = e.getPlayer().rayTraceBlocks(5, FluidCollisionMode.ALWAYS).getHitBlock();
|
||||||
|
if (!b.getType().equals(Material.OBSIDIAN)) {
|
||||||
|
// This should not be needed but might catch some attempts
|
||||||
|
return false;
|
||||||
|
}
|
||||||
User user = User.getInstance(player);
|
User user = User.getInstance(player);
|
||||||
if (getIslands().userIsOnIsland(user.getWorld(), user)) {
|
if (getIslands().userIsOnIsland(user.getWorld(), user)) {
|
||||||
// Look around to see if this is a lone obsidian block
|
// Look around to see if this is a lone obsidian block
|
||||||
@ -61,15 +68,16 @@ public class ObsidianScoopingListener extends FlagListener {
|
|||||||
}
|
}
|
||||||
user.sendMessage("protection.flags.OBSIDIAN_SCOOPING.scooping");
|
user.sendMessage("protection.flags.OBSIDIAN_SCOOPING.scooping");
|
||||||
player.getWorld().playSound(player.getLocation(), Sound.ITEM_BUCKET_FILL_LAVA, 1F, 1F);
|
player.getWorld().playSound(player.getLocation(), Sound.ITEM_BUCKET_FILL_LAVA, 1F, 1F);
|
||||||
b.setType(Material.AIR);
|
|
||||||
e.setCancelled(true);
|
e.setCancelled(true);
|
||||||
Bukkit.getScheduler().runTask(BentoBox.getInstance(), () -> givePlayerLava(player, bucket));
|
|
||||||
|
Bukkit.getScheduler().runTask(BentoBox.getInstance(), () -> givePlayerLava(player, b, bucket));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
}
|
}
|
||||||
private void givePlayerLava(Player player, ItemStack bucket) {
|
private void givePlayerLava(Player player, Block b, ItemStack bucket) {
|
||||||
if (bucket.getAmount() == 1) {
|
if (bucket.getAmount() == 1) {
|
||||||
// Needs some special handling when there is only 1 bucket in the stack
|
// Needs some special handling when there is only 1 bucket in the stack
|
||||||
bucket.setType(Material.LAVA_BUCKET);
|
bucket.setType(Material.LAVA_BUCKET);
|
||||||
@ -81,6 +89,8 @@ public class ObsidianScoopingListener extends FlagListener {
|
|||||||
map.values().forEach(i -> player.getWorld().dropItem(player.getLocation(), i));
|
map.values().forEach(i -> player.getWorld().dropItem(player.getLocation(), i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Set block to air only after giving bucket
|
||||||
|
b.setType(Material.AIR);
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Block> getBlocksAround(Block b) {
|
private List<Block> getBlocksAround(Block b) {
|
||||||
|
@ -12,6 +12,7 @@ import java.util.Optional;
|
|||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.FluidCollisionMode;
|
||||||
import org.bukkit.GameMode;
|
import org.bukkit.GameMode;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
@ -27,6 +28,7 @@ import org.bukkit.inventory.ItemFactory;
|
|||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.PlayerInventory;
|
import org.bukkit.inventory.PlayerInventory;
|
||||||
import org.bukkit.plugin.PluginManager;
|
import org.bukkit.plugin.PluginManager;
|
||||||
|
import org.bukkit.util.RayTraceResult;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@ -60,7 +62,7 @@ public class ObsidianScoopingListenerTest {
|
|||||||
@Mock
|
@Mock
|
||||||
private BentoBox plugin;
|
private BentoBox plugin;
|
||||||
@Mock
|
@Mock
|
||||||
private Player who;
|
private Player p;
|
||||||
@Mock
|
@Mock
|
||||||
private IslandWorldManager iwm;
|
private IslandWorldManager iwm;
|
||||||
@Mock
|
@Mock
|
||||||
@ -93,16 +95,19 @@ public class ObsidianScoopingListenerTest {
|
|||||||
listener = new ObsidianScoopingListener();
|
listener = new ObsidianScoopingListener();
|
||||||
|
|
||||||
// Mock player
|
// Mock player
|
||||||
when(who.getWorld()).thenReturn(world);
|
when(p.getWorld()).thenReturn(world);
|
||||||
|
RayTraceResult rtr = mock(RayTraceResult.class);
|
||||||
|
when(p.rayTraceBlocks(5, FluidCollisionMode.ALWAYS)).thenReturn(rtr);
|
||||||
|
when(rtr.getHitBlock()).thenReturn(clickedBlock);
|
||||||
|
|
||||||
Location location = mock(Location.class);
|
Location location = mock(Location.class);
|
||||||
when(location.getWorld()).thenReturn(world);
|
when(location.getWorld()).thenReturn(world);
|
||||||
when(location.getBlockX()).thenReturn(0);
|
when(location.getBlockX()).thenReturn(0);
|
||||||
when(location.getBlockY()).thenReturn(0);
|
when(location.getBlockY()).thenReturn(0);
|
||||||
when(location.getBlockZ()).thenReturn(0);
|
when(location.getBlockZ()).thenReturn(0);
|
||||||
when(who.getLocation()).thenReturn(location);
|
when(p.getLocation()).thenReturn(location);
|
||||||
|
|
||||||
when(who.getInventory()).thenReturn(mock(PlayerInventory.class));
|
when(p.getInventory()).thenReturn(mock(PlayerInventory.class));
|
||||||
|
|
||||||
// Worlds
|
// Worlds
|
||||||
when(plugin.getIWM()).thenReturn(iwm);
|
when(plugin.getIWM()).thenReturn(iwm);
|
||||||
@ -130,7 +135,7 @@ public class ObsidianScoopingListenerTest {
|
|||||||
// Put player on island
|
// Put player on island
|
||||||
when(im.userIsOnIsland(Mockito.any(), Mockito.any())).thenReturn(true);
|
when(im.userIsOnIsland(Mockito.any(), Mockito.any())).thenReturn(true);
|
||||||
// Set as survival
|
// Set as survival
|
||||||
when(who.getGameMode()).thenReturn(GameMode.SURVIVAL);
|
when(p.getGameMode()).thenReturn(GameMode.SURVIVAL);
|
||||||
|
|
||||||
// Locales
|
// Locales
|
||||||
when(plugin.getLocalesManager()).thenReturn(lm);
|
when(plugin.getLocalesManager()).thenReturn(lm);
|
||||||
@ -208,7 +213,7 @@ public class ObsidianScoopingListenerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOnPlayerInteractNotInWorld() {
|
public void testOnPlayerInteractNotInWorld() {
|
||||||
PlayerInteractEvent event = new PlayerInteractEvent(who, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST);
|
PlayerInteractEvent event = new PlayerInteractEvent(p, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST);
|
||||||
// Test not in world
|
// Test not in world
|
||||||
when(iwm.inWorld(any(World.class))).thenReturn(false);
|
when(iwm.inWorld(any(World.class))).thenReturn(false);
|
||||||
when(iwm.inWorld(any(Location.class))).thenReturn(false);
|
when(iwm.inWorld(any(Location.class))).thenReturn(false);
|
||||||
@ -224,11 +229,11 @@ public class ObsidianScoopingListenerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOnPlayerInteractGameModes() {
|
public void testOnPlayerInteractGameModes() {
|
||||||
PlayerInteractEvent event = new PlayerInteractEvent(who, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST);
|
PlayerInteractEvent event = new PlayerInteractEvent(p, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST);
|
||||||
|
|
||||||
// Test different game modes
|
// Test different game modes
|
||||||
for (GameMode gm : GameMode.values()) {
|
for (GameMode gm : GameMode.values()) {
|
||||||
when(who.getGameMode()).thenReturn(gm);
|
when(p.getGameMode()).thenReturn(gm);
|
||||||
if (!gm.equals(GameMode.SURVIVAL)) {
|
if (!gm.equals(GameMode.SURVIVAL)) {
|
||||||
assertFalse(listener.onPlayerInteract(event));
|
assertFalse(listener.onPlayerInteract(event));
|
||||||
}
|
}
|
||||||
@ -237,10 +242,10 @@ public class ObsidianScoopingListenerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOnPlayerInteractSurvivalNotOnIsland() {
|
public void testOnPlayerInteractSurvivalNotOnIsland() {
|
||||||
PlayerInteractEvent event = new PlayerInteractEvent(who, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST);
|
PlayerInteractEvent event = new PlayerInteractEvent(p, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST);
|
||||||
|
|
||||||
// Set as survival
|
// Set as survival
|
||||||
when(who.getGameMode()).thenReturn(GameMode.SURVIVAL);
|
when(p.getGameMode()).thenReturn(GameMode.SURVIVAL);
|
||||||
|
|
||||||
// Positive test with 1 bucket in the stack
|
// Positive test with 1 bucket in the stack
|
||||||
inHand = Material.BUCKET;
|
inHand = Material.BUCKET;
|
||||||
@ -262,7 +267,7 @@ public class ObsidianScoopingListenerTest {
|
|||||||
when(airBlock.getType()).thenReturn(Material.AIR);
|
when(airBlock.getType()).thenReturn(Material.AIR);
|
||||||
|
|
||||||
ObsidianScoopingListener listener = new ObsidianScoopingListener();
|
ObsidianScoopingListener listener = new ObsidianScoopingListener();
|
||||||
PlayerInteractEvent event = new PlayerInteractEvent(who, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST);
|
PlayerInteractEvent event = new PlayerInteractEvent(p, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST);
|
||||||
if (!item.getType().equals(Material.BUCKET)
|
if (!item.getType().equals(Material.BUCKET)
|
||||||
|| !clickedBlock.getType().equals(Material.OBSIDIAN)) {
|
|| !clickedBlock.getType().equals(Material.OBSIDIAN)) {
|
||||||
assertFalse(listener.onPlayerInteract(event));
|
assertFalse(listener.onPlayerInteract(event));
|
||||||
|
Loading…
Reference in New Issue
Block a user