Added admin "world" command.

This is used to set world settings.
This commit is contained in:
tastybento 2018-06-10 17:23:48 -07:00
parent a4ec8ad266
commit 92a7b0e596
7 changed files with 285 additions and 6 deletions

View File

@ -135,6 +135,8 @@ commands:
set-pos2: "&aPosition 2 set at [vector]"
need-pos1-pos2: "&cSet pos1 and pos2 first!"
copied-blocks: "&bCopied [number] blocks to clipboard"
world:
description: "Manage world settings"
island:
about:
description: "display copyright and license info"
@ -457,6 +459,7 @@ protection:
panel:
title: "Island flags"
world-settings: "[world_name] Settings"
flag-item:
name-layout: "&a[name]"
description-layout: |+

View File

@ -212,13 +212,13 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
// Check for console and permissions
if (cmd.onlyPlayer && !(sender instanceof Player)) {
user.sendMessage("general.errors.use-in-game");
return true;
return false;
}
// Check perms, but only if this isn't the console
if ((sender instanceof Player) && !sender.isOp() && !cmd.getPermission().isEmpty() && !sender.hasPermission(cmd.getPermission())) {
user.sendMessage("general.errors.no-permission");
user.sendMessage("general.errors.you-need", TextVariables.PERMISSION, cmd.getPermission());
return true;
return false;
}
// Fire an event to see if this command should be cancelled
CommandEvent event = CommandEvent.builder()
@ -228,7 +228,7 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
.setArgs(args)
.build();
if (event.isCancelled()) {
return true;
return false;
}
// Execute and trim args
return cmd.execute(user, Arrays.asList(args).subList(cmd.subCommandLevel, args.length));

View File

@ -140,7 +140,7 @@ public class Flag implements Comparable<Flag> {
/**
* Converts a flag to a panel item. The content of the flag will change depending on who the user is and where they are.
* The panel item may reflect their island settings, the island they are on, or the world in general.
* @param plugin - plugin
* @param user - user that will see this flag
* @return - PanelItem for this flag
*/
@ -209,4 +209,5 @@ public class Flag implements Comparable<Flag> {
return getID().compareTo(o.getID());
}
}

View File

@ -12,6 +12,7 @@ import us.tastybento.bskyblock.commands.admin.AdminSchemCommand;
import us.tastybento.bskyblock.commands.admin.AdminSetRankCommand;
import us.tastybento.bskyblock.commands.admin.AdminTeleportCommand;
import us.tastybento.bskyblock.commands.admin.AdminVersionCommand;
import us.tastybento.bskyblock.commands.admin.AdminWorldCommand;
import us.tastybento.bskyblock.commands.admin.teams.AdminTeamAddCommand;
import us.tastybento.bskyblock.commands.admin.teams.AdminTeamDisbandCommand;
import us.tastybento.bskyblock.commands.admin.teams.AdminTeamKickCommand;
@ -46,6 +47,8 @@ public class AdminCommand extends CompositeCommand {
new AdminTeamMakeLeaderCommand(this);
// Schems
new AdminSchemCommand(this);
// World
new AdminWorldCommand(this);
}
@Override

View File

@ -0,0 +1,44 @@
package us.tastybento.bskyblock.commands.admin;
import java.util.List;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.panels.SettingsPanel;
import us.tastybento.bskyblock.util.Util;
/**
* World settings command
* @author tastybento
*/
public class AdminWorldCommand extends CompositeCommand {
public AdminWorldCommand(CompositeCommand islandCommand) {
super(islandCommand, "world");
}
/* (non-Javadoc)
* @see us.tastybento.bskyblock.api.commands.CompositeCommand#setup()
*/
@Override
public void setup() {
setPermission("admin.world");
setOnlyPlayer(true);
setDescription("commands.admin.world.description");
}
/* (non-Javadoc)
* @see us.tastybento.bskyblock.api.commands.CommandArgument#execute(org.bukkit.command.CommandSender, java.lang.String[])
*/
@Override
public boolean execute(User user, List<String> args) {
// Settings are only shown if you are in the right world
if (Util.getWorld(user.getWorld()).equals(getWorld())) {
SettingsPanel.openWorldSettingsPanel(getPlugin(), user);
return true;
} else {
user.sendMessage("general.errors.wrong-world");
return false;
}
}
}

View File

@ -1,16 +1,19 @@
package us.tastybento.bskyblock.panels;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.flags.Flag;
import us.tastybento.bskyblock.api.panels.builders.PanelBuilder;
import us.tastybento.bskyblock.api.user.User;
/**
* @author Poslovitch
* Creates settings panels
* @author Poslovitch, tastybento
*/
public class SettingsPanel {
/**
* Dynamically creates the panel.
* @param plugin - plugin
* @param user the User to show the panel to
*/
public static void openPanel(BSkyBlock plugin, User user) {
@ -19,7 +22,23 @@ public class SettingsPanel {
.name(user.getTranslation("protection.panel.title"));
// Add flags, sorted
plugin.getFlagsManager().getFlags().stream().sorted((e1, e2) -> e1.getID().compareTo(e2.getID())).forEach((f -> panelBuilder.item(f.toPanelItem(plugin, user))));
plugin.getFlagsManager().getFlags().stream().filter(f -> !f.getType().equals(Flag.Type.WORLD_SETTING))
.sorted((e1, e2) -> e1.getID().compareTo(e2.getID())).forEach((f -> panelBuilder.item(f.toPanelItem(plugin, user))));
// Make the panel
panelBuilder.build().open(user);
}
/**
* Dynamically creates the world settings panel.
* @param plugin - plugin
* @param user the User to show the panel to
*/
public static void openWorldSettingsPanel(BSkyBlock plugin, User user) {
// Make a panel for settings
PanelBuilder panelBuilder = new PanelBuilder().name(user.getTranslation("protection.panel.world-settings", "[world_name]", plugin.getIWM().getWorldName(user.getWorld())));
// Add flags, sorted
plugin.getFlagsManager().getFlags().stream().filter(f -> f.getType().equals(Flag.Type.WORLD_SETTING))
.sorted((e1, e2) -> e1.getID().compareTo(e2.getID())).forEach((f -> panelBuilder.item(f.toPanelItem(plugin, user))));
// Make the panel
panelBuilder.build().open(user);
}

View File

@ -0,0 +1,209 @@
/**
*
*/
package us.tastybento.bskyblock.commands.admin;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitScheduler;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.Settings;
import us.tastybento.bskyblock.api.flags.Flag;
import us.tastybento.bskyblock.api.panels.PanelItem;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.commands.AdminCommand;
import us.tastybento.bskyblock.managers.CommandsManager;
import us.tastybento.bskyblock.managers.FlagsManager;
import us.tastybento.bskyblock.managers.IslandWorldManager;
import us.tastybento.bskyblock.managers.IslandsManager;
import us.tastybento.bskyblock.managers.LocalesManager;
import us.tastybento.bskyblock.managers.PlayersManager;
import us.tastybento.bskyblock.util.Util;
/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BSkyBlock.class, User.class, Util.class})
public class AdminWorldCommandTest {
private BSkyBlock plugin;
private AdminCommand ac;
private UUID uuid;
private User user;
private IslandsManager im;
private PlayersManager pm;
private UUID notUUID;
private Player p;
private World world;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
// Set up plugin
plugin = mock(BSkyBlock.class);
Whitebox.setInternalState(BSkyBlock.class, "instance", plugin);
// Command manager
CommandsManager cm = mock(CommandsManager.class);
when(plugin.getCommandsManager()).thenReturn(cm);
// Settings
Settings s = mock(Settings.class);
when(s.getResetWait()).thenReturn(0L);
when(s.getResetLimit()).thenReturn(3);
when(plugin.getSettings()).thenReturn(s);
// Player
p = mock(Player.class);
// Sometimes use Mockito.withSettings().verboseLogging()
user = mock(User.class);
when(user.isOp()).thenReturn(false);
uuid = UUID.randomUUID();
notUUID = UUID.randomUUID();
while(notUUID.equals(uuid)) {
notUUID = UUID.randomUUID();
}
when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(p);
when(user.getName()).thenReturn("tastybento");
User.setPlugin(plugin);
// Parent command has no aliases
ac = mock(AdminCommand.class);
when(ac.getSubCommandAliases()).thenReturn(new HashMap<>());
// Island World Manager
IslandWorldManager iwm = mock(IslandWorldManager.class);
world = mock(World.class);
when(iwm.getIslandWorld()).thenReturn(world);
when(iwm.getFriendlyName(Mockito.any())).thenReturn("BSkyBlock");
when(plugin.getIWM()).thenReturn(iwm);
when(user.getWorld()).thenReturn(world);
when(iwm.getWorldName(Mockito.any())).thenReturn("BSkyBlock_world");
// Player has island to begin with
im = mock(IslandsManager.class);
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true);
when(im.hasIsland(Mockito.any(), Mockito.any(User.class))).thenReturn(true);
when(im.isOwner(Mockito.any(),Mockito.any())).thenReturn(true);
when(im.getTeamLeader(Mockito.any(),Mockito.any())).thenReturn(uuid);
when(plugin.getIslands()).thenReturn(im);
// Has team
pm = mock(PlayersManager.class);
when(im.inTeam(Mockito.any(), Mockito.eq(uuid))).thenReturn(true);
when(plugin.getPlayers()).thenReturn(pm);
// Server & Scheduler
BukkitScheduler sch = mock(BukkitScheduler.class);
PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getScheduler()).thenReturn(sch);
// Locales
LocalesManager lm = mock(LocalesManager.class);
when(lm.get(Mockito.any(), Mockito.any())).thenAnswer(new Answer<String>() {
@Override
public String answer(InvocationOnMock invocation) throws Throwable {
return invocation.getArguments().length > 1 ? invocation.getArgumentAt(1, String.class) : "mock";
}});
when(plugin.getLocalesManager()).thenReturn(lm);
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(Mockito.any())).thenReturn(world);
// Inventory
Inventory inv = mock(Inventory.class);
when(Bukkit.createInventory(Mockito.any(InventoryHolder.class), Mockito.anyInt(), Mockito.anyString())).thenReturn(inv);
}
/**
* Test method for {@link us.tastybento.bskyblock.commands.admin.AdminWorldCommand#execute(us.tastybento.bskyblock.api.user.User, java.util.List)}.
*/
@Test
public void testExecuteConsole() {
// Not for console
AdminWorldCommand awc = new AdminWorldCommand(ac);
// , Mockito.withSettings().verboseLogging()
CommandSender sender = mock(CommandSender.class);
String[] args = {};
assertFalse(awc.execute(sender, "world", args));
Mockito.verify(sender).sendMessage("general.errors.use-in-game");
}
/**
* Test method for {@link us.tastybento.bskyblock.commands.admin.AdminWorldCommand#execute(us.tastybento.bskyblock.api.user.User, java.util.List)}.
*/
@Test
public void testExecutePlayerWrongWorld() {
AdminWorldCommand awc = new AdminWorldCommand(ac);
// Set world to something other that what the user is in
awc.setWorld(mock(World.class));
assertFalse(awc.execute(user, new ArrayList<>()));
Mockito.verify(user).sendMessage("general.errors.wrong-world");
}
/**
* Test method for {@link us.tastybento.bskyblock.commands.admin.AdminWorldCommand#execute(us.tastybento.bskyblock.api.user.User, java.util.List)}.
*/
@Test
public void testExecutePlayerRightWorld() {
AdminWorldCommand awc = new AdminWorldCommand(ac);
// Set world correctly
awc.setWorld(world);
// Flags manager
FlagsManager fm = mock(FlagsManager.class);
when(plugin.getFlagsManager()).thenReturn(fm);
List<Flag> list = new ArrayList<>();
Flag flag = mock(Flag.class);
when(flag.getType()).thenReturn(Flag.Type.WORLD_SETTING);
PanelItem pi = mock(PanelItem.class);
when(pi.getItem()).thenReturn(new ItemStack(Material.GLASS));
when(flag.toPanelItem(Mockito.any(), Mockito.any())).thenReturn(pi);
list.add(flag);
when(fm.getFlags()).thenReturn(list);
assertTrue(awc.execute(user, new ArrayList<>()));
Mockito.verify(user).getTranslation(
"protection.panel.world-settings",
"[world_name]",
"BSkyBlock_world"
);
}
}