Fixes enderchest world setting

It was being double handled by two listeners.

Fixes:
https://github.com/BentoBoxWorld/BentoBox/issues/516
This commit is contained in:
tastybento 2019-02-04 22:01:43 -08:00
parent f4c149dd6f
commit fd882c018b
4 changed files with 40 additions and 39 deletions

View File

@ -9,7 +9,6 @@ import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.Listener;
import org.bukkit.metadata.MetadataValue;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
@ -157,6 +156,7 @@ public abstract class FlagListener implements Listener {
report(user, e, loc, flag, Why.UNPROTECTED_WORLD);
return true;
}
// Get the island and if present
Optional<Island> island = getIslands().getProtectedIslandAt(loc);
// Handle Settings Flag
@ -192,6 +192,20 @@ public abstract class FlagListener implements Listener {
return true;
}
// Handle World Settings
if (flag.getType().equals(Flag.Type.WORLD_SETTING)) {
if (flag.isSetForWorld(loc.getWorld())) {
report(user, e, loc, flag, Why.ALLOWED_IN_WORLD);
user = null;
return true;
}
report(user, e, loc, flag, Why.NOT_ALLOWED_IN_WORLD);
noGo(e, flag, silent);
// Clear the user for the next time
user = null;
return false;
}
// Check if the plugin is set in User (required for testing)
User.setPlugin(plugin);

View File

@ -10,6 +10,7 @@ import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import world.bentobox.bentobox.api.flags.FlagListener;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.lists.Flags;
/**
@ -34,6 +35,9 @@ public class BlockInteractionListener extends FlagListener {
if (!e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
return;
}
// Set user
this.setUser(User.getInstance(e.getPlayer()));
// Check clicked block
checkClickedBlock(e, e.getClickedBlock().getLocation(), e.getClickedBlock().getType());
// Now check for in-hand items

View File

@ -7,33 +7,22 @@ import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.Action;
import org.bukkit.event.inventory.CraftItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import world.bentobox.bentobox.api.flags.FlagListener;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.listeners.flags.protection.BlockInteractionListener;
import world.bentobox.bentobox.lists.Flags;
/**
* Prevents enderchest use and creation in world if it is not allowed
* Prevents enderchest creation in world if it is not allowed
* Enderchest opening is handled in {@link BlockInteractionListener}
* @author tastybento
*
*/
public class EnderChestListener extends FlagListener {
/**
* Prevents opening ender chest unless player has permission
* @param e - event
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onEnderChestOpen(PlayerInteractEvent e) {
if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
e.setCancelled(checkEnderChest(e.getPlayer(), e.getClickedBlock().getType()));
}
}
/**
* Prevents crafting of EnderChest unless the player has permission
*

View File

@ -39,11 +39,12 @@ import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.user.Notifier;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.listeners.flags.worldsettings.EnderChestListener;
import world.bentobox.bentobox.listeners.flags.protection.BlockInteractionListener;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
@ -127,14 +128,20 @@ public class EnderChestListenerTest {
notifier = mock(Notifier.class);
when(plugin.getNotifier()).thenReturn(notifier);
// Acition, Item and clicked block
// Action, Item and clicked block
action = Action.RIGHT_CLICK_BLOCK;
item = mock(ItemStack.class);
when(item.getType()).thenReturn(Material.ENDER_CHEST);
clickedBlock = mock(Block.class);
when(clickedBlock.getLocation()).thenReturn(inside);
when(clickedBlock.getType()).thenReturn(Material.ENDER_CHEST);
// Addon
when(iwm.getAddon(Mockito.any())).thenReturn(Optional.empty());
Settings settings = mock(Settings.class);
// Fake players
when(plugin.getSettings()).thenReturn(settings);
}
@After
@ -147,74 +154,61 @@ public class EnderChestListenerTest {
action = Action.LEFT_CLICK_AIR;
BlockFace clickedBlockFace = BlockFace.EAST;
PlayerInteractEvent e = new PlayerInteractEvent(player, action, item, clickedBlock, clickedBlockFace);
new EnderChestListener().onEnderChestOpen(e);
assertFalse(e.isCancelled());
}
@Test
public void testOnEnderChestOpenNotEnderChest() {
when(clickedBlock.getType()).thenReturn(Material.STONE);
BlockFace clickedBlockFace = BlockFace.EAST;
PlayerInteractEvent e = new PlayerInteractEvent(player, action, item, clickedBlock, clickedBlockFace);
new EnderChestListener().onEnderChestOpen(e);
new BlockInteractionListener().onPlayerInteract(e);
assertFalse(e.isCancelled());
}
@Test
public void testOnEnderChestOpenEnderChestNotInWorld() {
when(clickedBlock.getType()).thenReturn(Material.ENDER_CHEST);
BlockFace clickedBlockFace = BlockFace.EAST;
PlayerInteractEvent e = new PlayerInteractEvent(player, action, item, clickedBlock, clickedBlockFace);
// Not in world
when(iwm.inWorld(any(World.class))).thenReturn(false);
when(iwm.inWorld(any(Location.class))).thenReturn(false);
new EnderChestListener().onEnderChestOpen(e);
new BlockInteractionListener().onPlayerInteract(e);
assertFalse(e.isCancelled());
}
@Test
public void testOnEnderChestOpenEnderChestOpPlayer() {
when(clickedBlock.getType()).thenReturn(Material.ENDER_CHEST);
BlockFace clickedBlockFace = BlockFace.EAST;
PlayerInteractEvent e = new PlayerInteractEvent(player, action, item, clickedBlock, clickedBlockFace);
// Op player
when(player.isOp()).thenReturn(true);
new EnderChestListener().onEnderChestOpen(e);
new BlockInteractionListener().onPlayerInteract(e);
assertFalse(e.isCancelled());
}
@Test
public void testOnEnderChestOpenEnderChestHasBypassPerm() {
when(clickedBlock.getType()).thenReturn(Material.ENDER_CHEST);
BlockFace clickedBlockFace = BlockFace.EAST;
PlayerInteractEvent e = new PlayerInteractEvent(player, action, item, clickedBlock, clickedBlockFace);
// Has bypass perm
when(player.hasPermission(Mockito.anyString())).thenReturn(true);
new EnderChestListener().onEnderChestOpen(e);
new BlockInteractionListener().onPlayerInteract(e);
assertFalse(e.isCancelled());
}
@Test
public void testOnEnderChestOpenEnderChestOkay() {
when(clickedBlock.getType()).thenReturn(Material.ENDER_CHEST);
BlockFace clickedBlockFace = BlockFace.EAST;
PlayerInteractEvent e = new PlayerInteractEvent(player, action, item, clickedBlock, clickedBlockFace);
// Enderchest use is okay
Flags.ENDER_CHEST.setSetting(world, true);
new EnderChestListener().onEnderChestOpen(e);
BlockInteractionListener bil = new BlockInteractionListener();
bil.setUser(User.getInstance(player));
bil.onPlayerInteract(e);
assertFalse(e.isCancelled());
Mockito.verify(notifier, Mockito.never()).notify(Mockito.anyObject(), Mockito.anyString());
}
@Test
public void testOnEnderChestOpenEnderChestBlocked() {
when(clickedBlock.getType()).thenReturn(Material.ENDER_CHEST);
BlockFace clickedBlockFace = BlockFace.EAST;
PlayerInteractEvent e = new PlayerInteractEvent(player, action, item, clickedBlock, clickedBlockFace);
// Enderchest use is blocked
Flags.ENDER_CHEST.setSetting(world, false);
new EnderChestListener().onEnderChestOpen(e);
new BlockInteractionListener().onPlayerInteract(e);
assertTrue(e.isCancelled());
Mockito.verify(notifier).notify(Mockito.any(User.class), Mockito.eq("protection.protected"));
}