Adds banned commands when falling to WorldSettings

To complete the functionality, the addon will need to overrider the
banned falling command method

https://github.com/BentoBoxWorld/BentoBox/issues/863
This commit is contained in:
tastybento 2019-10-03 13:34:03 -07:00
parent 75ec023249
commit 42ba6dcc50
6 changed files with 109 additions and 53 deletions

View File

@ -25,7 +25,7 @@ import world.bentobox.bentobox.hooks.VaultHook;
import world.bentobox.bentobox.hooks.WorldEditHook; import world.bentobox.bentobox.hooks.WorldEditHook;
import world.bentobox.bentobox.hooks.placeholders.MVdWPlaceholderAPIHook; import world.bentobox.bentobox.hooks.placeholders.MVdWPlaceholderAPIHook;
import world.bentobox.bentobox.hooks.placeholders.PlaceholderAPIHook; import world.bentobox.bentobox.hooks.placeholders.PlaceholderAPIHook;
import world.bentobox.bentobox.listeners.BannedVisitorCommands; import world.bentobox.bentobox.listeners.BannedCommands;
import world.bentobox.bentobox.listeners.BlockEndDragon; import world.bentobox.bentobox.listeners.BlockEndDragon;
import world.bentobox.bentobox.listeners.DeathListener; import world.bentobox.bentobox.listeners.DeathListener;
import world.bentobox.bentobox.listeners.JoinLeaveListener; import world.bentobox.bentobox.listeners.JoinLeaveListener;
@ -247,7 +247,7 @@ public class BentoBox extends JavaPlugin {
// End dragon blocking // End dragon blocking
manager.registerEvents(new BlockEndDragon(this), this); manager.registerEvents(new BlockEndDragon(this), this);
// Banned visitor commands // Banned visitor commands
manager.registerEvents(new BannedVisitorCommands(this), this); manager.registerEvents(new BannedCommands(this), this);
// Death counter // Death counter
manager.registerEvents(new DeathListener(this), this); manager.registerEvents(new DeathListener(this), this);
// Island Delete Manager // Island Delete Manager

View File

@ -1,5 +1,6 @@
package world.bentobox.bentobox.api.configuration; package world.bentobox.bentobox.api.configuration;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -7,8 +8,8 @@ import java.util.Set;
import org.bukkit.Difficulty; import org.bukkit.Difficulty;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNull;
import world.bentobox.bentobox.api.flags.Flag; import world.bentobox.bentobox.api.flags.Flag;
/** /**
@ -136,6 +137,15 @@ public interface WorldSettings extends ConfigObject {
*/ */
List<String> getVisitorBannedCommands(); List<String> getVisitorBannedCommands();
/**
* Optional list of commands that are banned when falling. Not applicable to all game modes so defaults to empty.
* @return the fallingBannedCommands
* @since 1.8.0
*/
default List<String> getFallingBannedCommands() {
return Collections.emptyList();
}
/** /**
* Get world flags * Get world flags
* @return Map of world flags * @return Map of world flags

View File

@ -11,18 +11,18 @@ import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
/** /**
* Blocks visitors from executing commands that they should not in the island world * Blocks command usage for various scenarios
* @author tastybento * @author tastybento
* *
*/ */
public class BannedVisitorCommands implements Listener { public class BannedCommands implements Listener {
private BentoBox plugin; private BentoBox plugin;
/** /**
* @param plugin - plugin * @param plugin - plugin
*/ */
public BannedVisitorCommands(@NonNull BentoBox plugin) { public BannedCommands(@NonNull BentoBox plugin) {
this.plugin = plugin; this.plugin = plugin;
} }
@ -30,8 +30,8 @@ public class BannedVisitorCommands implements Listener {
* Prevents visitors from using commands on islands, like /spawner * Prevents visitors from using commands on islands, like /spawner
* @param e - event * @param e - event
*/ */
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onVisitorCommand(PlayerCommandPreprocessEvent e) { public void onCommand(PlayerCommandPreprocessEvent e) {
if (!plugin.getIWM().inWorld(e.getPlayer().getLocation()) || e.getPlayer().isOp() if (!plugin.getIWM().inWorld(e.getPlayer().getLocation()) || e.getPlayer().isOp()
|| e.getPlayer().hasPermission(plugin.getIWM().getPermissionPrefix(e.getPlayer().getWorld()) + "mod.bypassprotect") || e.getPlayer().hasPermission(plugin.getIWM().getPermissionPrefix(e.getPlayer().getWorld()) + "mod.bypassprotect")
|| plugin.getIslands().locationIsOnIsland(e.getPlayer(), e.getPlayer().getLocation())) { || plugin.getIslands().locationIsOnIsland(e.getPlayer(), e.getPlayer().getLocation())) {
@ -39,7 +39,8 @@ public class BannedVisitorCommands implements Listener {
} }
// Check banned commands // Check banned commands
String[] args = e.getMessage().substring(1).toLowerCase(java.util.Locale.ENGLISH).split(" "); String[] args = e.getMessage().substring(1).toLowerCase(java.util.Locale.ENGLISH).split(" ");
if (plugin.getIWM().getVisitorBannedCommands(e.getPlayer().getWorld()).contains(args[0])) { if (plugin.getIWM().getVisitorBannedCommands(e.getPlayer().getWorld()).contains(args[0])
|| plugin.getIWM().getFallingBannedCommands(e.getPlayer().getWorld()).contains(args[0])) {
User user = User.getInstance(e.getPlayer()); User user = User.getInstance(e.getPlayer());
user.notify("protection.protected", TextVariables.DESCRIPTION, user.getTranslation("protection.command-is-banned")); user.notify("protection.protected", TextVariables.DESCRIPTION, user.getTranslation("protection.command-is-banned"));
e.setCancelled(true); e.setCancelled(true);

View File

@ -670,6 +670,14 @@ public class IslandWorldManager {
return gameModes.get(world).getWorldSettings().getVisitorBannedCommands(); return gameModes.get(world).getWorldSettings().getVisitorBannedCommands();
} }
/**
* Return banned commands when falling
* @return the fallingbannedcommands
*/
public List<String> getFallingBannedCommands(@NonNull World world) {
return gameModes.get(world).getWorldSettings().getFallingBannedCommands();
}
/** /**
* Check if water is not safe, e.g., it is acid, in the world * Check if water is not safe, e.g., it is acid, in the world
* @param world - world * @param world - world

View File

@ -5,7 +5,7 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;

View File

@ -1,10 +1,10 @@
package world.bentobox.bentobox.listeners; package world.bentobox.bentobox.listeners;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import java.util.ArrayList; import java.util.ArrayList;
@ -23,6 +23,7 @@ import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.stubbing.Answer; import org.mockito.stubbing.Answer;
import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.core.classloader.annotations.PrepareForTest;
@ -40,11 +41,15 @@ import world.bentobox.bentobox.util.Util;
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({BentoBox.class, Util.class, Bukkit.class }) @PrepareForTest({BentoBox.class, Util.class, Bukkit.class })
public class BannedVisitorCommandsTest { public class BannedCommandsTest {
@Mock
private IslandWorldManager iwm; private IslandWorldManager iwm;
@Mock
private Player player; private Player player;
@Mock
private BentoBox plugin; private BentoBox plugin;
@Mock
private IslandsManager im; private IslandsManager im;
@Before @Before
@ -53,15 +58,14 @@ public class BannedVisitorCommandsTest {
plugin = mock(BentoBox.class); plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin); Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// Island World Manager // Island World Manager
iwm = mock(IslandWorldManager.class);
when(iwm.inWorld(any(World.class))).thenReturn(true); when(iwm.inWorld(any(World.class))).thenReturn(true);
when(iwm.inWorld(any(Location.class))).thenReturn(true); when(iwm.inWorld(any(Location.class))).thenReturn(true);
when(iwm.getPermissionPrefix(Mockito.any())).thenReturn("bskyblock."); when(iwm.getPermissionPrefix(any())).thenReturn("bskyblock.");
when(iwm.getVisitorBannedCommands(Mockito.any())).thenReturn(new ArrayList<>()); when(iwm.getVisitorBannedCommands(any())).thenReturn(new ArrayList<>());
when(iwm.getFallingBannedCommands(any())).thenReturn(new ArrayList<>());
when(plugin.getIWM()).thenReturn(iwm); when(plugin.getIWM()).thenReturn(iwm);
// Player // Player
player = mock(Player.class);
when(player.isOp()).thenReturn(false); when(player.isOp()).thenReturn(false);
when(player.hasPermission(Mockito.anyString())).thenReturn(false); when(player.hasPermission(Mockito.anyString())).thenReturn(false);
when(player.getWorld()).thenReturn(mock(World.class)); when(player.getWorld()).thenReturn(mock(World.class));
@ -80,9 +84,8 @@ public class BannedVisitorCommandsTest {
when(player.getServer()).thenReturn(server); when(player.getServer()).thenReturn(server);
// Island manager // Island manager
im = mock(IslandsManager.class);
// Default not on island, so is a visitor // Default not on island, so is a visitor
when(im.locationIsOnIsland(Mockito.any(), Mockito.any())).thenReturn(false); when(im.locationIsOnIsland(any(), any())).thenReturn(false);
when(plugin.getIslands()).thenReturn(im); when(plugin.getIslands()).thenReturn(im);
// Locales // Locales
@ -100,26 +103,24 @@ public class BannedVisitorCommandsTest {
when(plugin.getNotifier()).thenReturn(notifier); when(plugin.getNotifier()).thenReturn(notifier);
// Addon // Addon
when(iwm.getAddon(Mockito.any())).thenReturn(Optional.empty()); when(iwm.getAddon(any())).thenReturn(Optional.empty());
} }
@Test /**
public void testBannedVisitorCommands() { * Test for {@link BannedCommands#onCommand(PlayerCommandPreprocessEvent)}
assertNotNull(new BannedVisitorCommands(plugin)); */
}
@Test @Test
public void testInstantReturn() { public void testInstantReturn() {
PlayerCommandPreprocessEvent e = new PlayerCommandPreprocessEvent(player, "/blah"); PlayerCommandPreprocessEvent e = new PlayerCommandPreprocessEvent(player, "/blah");
BannedVisitorCommands bvc = new BannedVisitorCommands(plugin); BannedCommands bvc = new BannedCommands(plugin);
// Not in world // 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);
bvc.onVisitorCommand(e); bvc.onCommand(e);
assertFalse(e.isCancelled()); assertFalse(e.isCancelled());
// In world // In world
@ -127,96 +128,132 @@ public class BannedVisitorCommandsTest {
when(iwm.inWorld(any(Location.class))).thenReturn(true); when(iwm.inWorld(any(Location.class))).thenReturn(true);
// Op // Op
when(player.isOp()).thenReturn(true); when(player.isOp()).thenReturn(true);
bvc.onVisitorCommand(e); bvc.onCommand(e);
assertFalse(e.isCancelled()); assertFalse(e.isCancelled());
// Not op // Not op
when(player.isOp()).thenReturn(false); when(player.isOp()).thenReturn(false);
// Has bypass perm // Has bypass perm
when(player.hasPermission(Mockito.anyString())).thenReturn(true); when(player.hasPermission(Mockito.anyString())).thenReturn(true);
bvc.onVisitorCommand(e); bvc.onCommand(e);
assertFalse(e.isCancelled()); assertFalse(e.isCancelled());
// Does not have perm // Does not have perm
when(player.hasPermission(Mockito.anyString())).thenReturn(false); when(player.hasPermission(Mockito.anyString())).thenReturn(false);
// Not a visitor // Not a visitor
when(im.locationIsOnIsland(Mockito.any(), Mockito.any())).thenReturn(true); when(im.locationIsOnIsland(any(), any())).thenReturn(true);
bvc.onVisitorCommand(e); bvc.onCommand(e);
assertFalse(e.isCancelled()); assertFalse(e.isCancelled());
} }
/**
* Test for {@link BannedCommands#onCommand(PlayerCommandPreprocessEvent)}
*/
@Test @Test
public void testEmptyBannedCommands() { public void testEmptyBannedCommands() {
PlayerCommandPreprocessEvent e = new PlayerCommandPreprocessEvent(player, "/blah"); PlayerCommandPreprocessEvent e = new PlayerCommandPreprocessEvent(player, "/blah");
BannedVisitorCommands bvc = new BannedVisitorCommands(plugin); BannedCommands bvc = new BannedCommands(plugin);
bvc.onVisitorCommand(e); bvc.onCommand(e);
assertFalse(e.isCancelled()); assertFalse(e.isCancelled());
} }
/**
* Test for {@link BannedCommands#onCommand(PlayerCommandPreprocessEvent)}
*/
@Test @Test
public void testBannedCommands() { public void testBannedCommands() {
PlayerCommandPreprocessEvent e = new PlayerCommandPreprocessEvent(player, "/blah"); PlayerCommandPreprocessEvent e = new PlayerCommandPreprocessEvent(player, "/blah");
BannedVisitorCommands bvc = new BannedVisitorCommands(plugin); BannedCommands bvc = new BannedCommands(plugin);
List<String> banned = new ArrayList<>(); List<String> banned = new ArrayList<>();
banned.add("banned_command"); banned.add("banned_command");
banned.add("another_banned_command"); banned.add("another_banned_command");
when(iwm.getVisitorBannedCommands(Mockito.any())).thenReturn(banned); when(iwm.getVisitorBannedCommands(any())).thenReturn(banned);
bvc.onVisitorCommand(e); bvc.onCommand(e);
assertFalse(e.isCancelled()); assertFalse(e.isCancelled());
Mockito.verify(iwm).getVisitorBannedCommands(Mockito.any()); verify(iwm).getVisitorBannedCommands(any());
} }
/**
* Test for {@link BannedCommands#onCommand(PlayerCommandPreprocessEvent)}
*/
@Test @Test
public void testBannedCommandsWithExtra() { public void testBannedCommandsWithExtra() {
PlayerCommandPreprocessEvent e = new PlayerCommandPreprocessEvent(player, "/blah with extra stuff"); PlayerCommandPreprocessEvent e = new PlayerCommandPreprocessEvent(player, "/blah with extra stuff");
BannedVisitorCommands bvc = new BannedVisitorCommands(plugin); BannedCommands bvc = new BannedCommands(plugin);
List<String> banned = new ArrayList<>(); List<String> banned = new ArrayList<>();
banned.add("banned_command"); banned.add("banned_command");
banned.add("another_banned_command"); banned.add("another_banned_command");
when(iwm.getVisitorBannedCommands(Mockito.any())).thenReturn(banned); when(iwm.getVisitorBannedCommands(any())).thenReturn(banned);
bvc.onVisitorCommand(e); bvc.onCommand(e);
assertFalse(e.isCancelled()); assertFalse(e.isCancelled());
Mockito.verify(iwm).getVisitorBannedCommands(Mockito.any()); verify(iwm).getVisitorBannedCommands(any());
} }
/**
* Test for {@link BannedCommands#onCommand(PlayerCommandPreprocessEvent)}
*/
@Test @Test
public void testBannedCommandsWithBannedCommand() { public void testBannedCommandsWithBannedCommand() {
PlayerCommandPreprocessEvent e = new PlayerCommandPreprocessEvent(player, "/banned_command"); PlayerCommandPreprocessEvent e = new PlayerCommandPreprocessEvent(player, "/banned_command");
BannedVisitorCommands bvc = new BannedVisitorCommands(plugin); BannedCommands bvc = new BannedCommands(plugin);
List<String> banned = new ArrayList<>(); List<String> banned = new ArrayList<>();
banned.add("banned_command"); banned.add("banned_command");
banned.add("another_banned_command"); banned.add("another_banned_command");
when(iwm.getVisitorBannedCommands(Mockito.any())).thenReturn(banned); when(iwm.getVisitorBannedCommands(any())).thenReturn(banned);
bvc.onVisitorCommand(e); bvc.onCommand(e);
Mockito.verify(iwm).getVisitorBannedCommands(Mockito.any()); verify(iwm).getVisitorBannedCommands(any());
assertTrue(e.isCancelled()); assertTrue(e.isCancelled());
} }
/**
* Test for {@link BannedCommands#onCommand(PlayerCommandPreprocessEvent)}
*/
@Test @Test
public void testBannedCommandsWithBannedCommandWithExtra() { public void testBannedCommandsWithBannedCommandWithExtra() {
PlayerCommandPreprocessEvent e = new PlayerCommandPreprocessEvent(player, "/banned_command with extra stuff"); PlayerCommandPreprocessEvent e = new PlayerCommandPreprocessEvent(player, "/banned_command with extra stuff");
BannedVisitorCommands bvc = new BannedVisitorCommands(plugin); BannedCommands bvc = new BannedCommands(plugin);
List<String> banned = new ArrayList<>(); List<String> banned = new ArrayList<>();
banned.add("banned_command"); banned.add("banned_command");
banned.add("another_banned_command"); banned.add("another_banned_command");
when(iwm.getVisitorBannedCommands(Mockito.any())).thenReturn(banned); when(iwm.getVisitorBannedCommands(any())).thenReturn(banned);
bvc.onVisitorCommand(e); bvc.onCommand(e);
Mockito.verify(iwm).getVisitorBannedCommands(Mockito.any()); verify(iwm).getVisitorBannedCommands(any());
assertTrue(e.isCancelled()); assertTrue(e.isCancelled());
} }
/**
* Test for {@link BannedCommands#onCommand(PlayerCommandPreprocessEvent)}
*/
@Test @Test
public void testAnotherBannedCommandsWithBannedCommandWithExtra() { public void testAnotherBannedCommandsWithBannedCommandWithExtra() {
PlayerCommandPreprocessEvent e = new PlayerCommandPreprocessEvent(player, "/another_banned_command with extra stuff"); PlayerCommandPreprocessEvent e = new PlayerCommandPreprocessEvent(player, "/another_banned_command with extra stuff");
BannedVisitorCommands bvc = new BannedVisitorCommands(plugin); BannedCommands bvc = new BannedCommands(plugin);
List<String> banned = new ArrayList<>(); List<String> banned = new ArrayList<>();
banned.add("banned_command"); banned.add("banned_command");
banned.add("another_banned_command"); banned.add("another_banned_command");
when(iwm.getVisitorBannedCommands(Mockito.any())).thenReturn(banned); when(iwm.getVisitorBannedCommands(any())).thenReturn(banned);
bvc.onVisitorCommand(e); bvc.onCommand(e);
Mockito.verify(iwm).getVisitorBannedCommands(Mockito.any()); verify(iwm).getVisitorBannedCommands(any());
assertTrue(e.isCancelled());
}
/**
* Test for {@link BannedCommands#onCommand(PlayerCommandPreprocessEvent)}
*/
@Test
public void testBannedCommandsWithBannedFallingCommand() {
PlayerCommandPreprocessEvent e = new PlayerCommandPreprocessEvent(player, "/banned_command");
BannedCommands bvc = new BannedCommands(plugin);
List<String> banned = new ArrayList<>();
banned.add("banned_command");
banned.add("another_banned_command");
when(iwm.getFallingBannedCommands(any())).thenReturn(banned);
bvc.onCommand(e);
verify(iwm).getVisitorBannedCommands(any());
assertTrue(e.isCancelled()); assertTrue(e.isCancelled());
} }