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.placeholders.MVdWPlaceholderAPIHook;
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.DeathListener;
import world.bentobox.bentobox.listeners.JoinLeaveListener;
@ -247,7 +247,7 @@ public class BentoBox extends JavaPlugin {
// End dragon blocking
manager.registerEvents(new BlockEndDragon(this), this);
// Banned visitor commands
manager.registerEvents(new BannedVisitorCommands(this), this);
manager.registerEvents(new BannedCommands(this), this);
// Death counter
manager.registerEvents(new DeathListener(this), this);
// Island Delete Manager

View File

@ -1,5 +1,6 @@
package world.bentobox.bentobox.api.configuration;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -7,8 +8,8 @@ import java.util.Set;
import org.bukkit.Difficulty;
import org.bukkit.GameMode;
import org.bukkit.entity.EntityType;
import org.eclipse.jdt.annotation.NonNull;
import world.bentobox.bentobox.api.flags.Flag;
/**
@ -136,6 +137,15 @@ public interface WorldSettings extends ConfigObject {
*/
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
* @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;
/**
* Blocks visitors from executing commands that they should not in the island world
* Blocks command usage for various scenarios
* @author tastybento
*
*/
public class BannedVisitorCommands implements Listener {
public class BannedCommands implements Listener {
private BentoBox plugin;
/**
* @param plugin - plugin
*/
public BannedVisitorCommands(@NonNull BentoBox plugin) {
public BannedCommands(@NonNull BentoBox plugin) {
this.plugin = plugin;
}
@ -30,8 +30,8 @@ public class BannedVisitorCommands implements Listener {
* Prevents visitors from using commands on islands, like /spawner
* @param e - event
*/
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onVisitorCommand(PlayerCommandPreprocessEvent e) {
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onCommand(PlayerCommandPreprocessEvent e) {
if (!plugin.getIWM().inWorld(e.getPlayer().getLocation()) || e.getPlayer().isOp()
|| e.getPlayer().hasPermission(plugin.getIWM().getPermissionPrefix(e.getPlayer().getWorld()) + "mod.bypassprotect")
|| plugin.getIslands().locationIsOnIsland(e.getPlayer(), e.getPlayer().getLocation())) {
@ -39,7 +39,8 @@ public class BannedVisitorCommands implements Listener {
}
// Check banned commands
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.notify("protection.protected", TextVariables.DESCRIPTION, user.getTranslation("protection.command-is-banned"));
e.setCancelled(true);

View File

@ -670,6 +670,14 @@ public class IslandWorldManager {
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
* @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.assertNull;
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.when;

View File

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