Implements new call API for commands

https://github.com/BentoBoxWorld/BentoBox/issues/759
This commit is contained in:
tastybento 2019-06-16 14:14:05 -07:00 committed by tastybento
parent 2364aaa835
commit d783d40c71
4 changed files with 77 additions and 9 deletions

View File

@ -6,7 +6,7 @@
<groupId>world.bentobox</groupId>
<artifactId>bskyblock</artifactId>
<version>1.5.0</version>
<version>1.5.3-SNAPSHOT</version>
<name>BSkyBlock</name>
<description>BSkyBlock is an add-on for BentoBox, an expandable Minecraft Bukkit plugin for island-type games like SkyBlock or AcidIsland.</description>
@ -91,7 +91,7 @@
<dependency>
<groupId>world.bentobox</groupId>
<artifactId>bentobox</artifactId>
<version>1.5.0</version>
<version>1.5.3-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>

View File

@ -73,10 +73,10 @@ public class IslandCommand extends CompositeCommand {
if (args.isEmpty()) {
// If user has an island, go
if (getPlugin().getIslands().getIsland(getWorld(), user.getUniqueId()) != null) {
return getSubCommand("go").map(goCmd -> goCmd.execute(user, goCmd.getLabel(), new ArrayList<>())).orElse(false);
return getSubCommand("go").map(goCmd -> goCmd.call(user, goCmd.getLabel(), new ArrayList<>())).orElse(false);
}
// No islands currently
return getSubCommand("create").map(createCmd -> createCmd.execute(user, createCmd.getLabel(), new ArrayList<>())).orElse(false);
return getSubCommand("create").map(createCmd -> createCmd.call(user, createCmd.getLabel(), new ArrayList<>())).orElse(false);
}
user.sendMessage("general.errors.unknown-command", TextVariables.LABEL, getTopLabel());
return false;

View File

@ -24,7 +24,6 @@ import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.eclipse.jdt.annotation.Nullable;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

View File

@ -6,10 +6,15 @@ package world.bentobox.bskyblock.commands;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.bukkit.Bukkit;
@ -19,6 +24,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
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;
@ -26,7 +32,9 @@ import org.powermock.reflect.Whitebox;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.blueprints.dataobjects.BlueprintBundle;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.BlueprintsManager;
import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
@ -69,6 +77,7 @@ public class IslandCommandTest {
when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(p);
when(user.getName()).thenReturn("tastybento");
when(user.isPlayer()).thenReturn(true);
User.setPlugin(plugin);
// Island World Manager
@ -91,6 +100,19 @@ public class IslandCommandTest {
when(settings.getIslandCommand()).thenReturn("island");
when(addon.getSettings()).thenReturn(settings);
// Blueprints
BlueprintsManager bpm = mock(BlueprintsManager.class);
Map<String, BlueprintBundle> map = new HashMap<>();
BlueprintBundle bun = mock(BlueprintBundle.class);
when(bun.getDisplayName()).thenReturn("aaa", "bbb");
map.put("aaa", bun);
map.put("bbb", bun);
when(bun.getUniqueId()).thenReturn("unique1", "unique2");
when(bun.isRequirePermission()).thenReturn(true);
when(bpm.getBlueprintBundles(Mockito.any())).thenReturn(map);
when(plugin.getBlueprintsManager()).thenReturn(bpm);
PowerMockito.mockStatic(Bukkit.class);
}
@ -136,14 +158,25 @@ public class IslandCommandTest {
public void testExecuteUserStringListOfStringUnknownCommand() {
IslandCommand cmd = new IslandCommand(addon);
assertFalse(cmd.execute(user, "island", Collections.singletonList("unknown")));
Mockito.verify(user).sendMessage("general.errors.unknown-command", TextVariables.LABEL, "island");
verify(user).sendMessage("general.errors.unknown-command", TextVariables.LABEL, "island");
}
/**
* Test method for {@link world.bentobox.bskyblock.commands.IslandCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringNoArgs() {
public void testExecuteUserStringListOfStringNoArgsNoPermission() {
IslandCommand cmd = new IslandCommand(addon);
assertFalse(cmd.execute(user, "island", Collections.emptyList()));
verify(user).sendMessage("general.errors.no-permission", "[permission]", "island.home");
}
/**
* Test method for {@link world.bentobox.bskyblock.commands.IslandCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringNoArgsSuccess() {
when(user.hasPermission(anyString())).thenReturn(true);
IslandCommand cmd = new IslandCommand(addon);
assertTrue(cmd.execute(user, "island", Collections.emptyList()));
}
@ -152,10 +185,46 @@ public class IslandCommandTest {
* Test method for {@link world.bentobox.bskyblock.commands.IslandCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringNoArgsNoIsland() {
island = null;
public void testExecuteUserStringListOfStringNoArgsConsole() {
when(user.isPlayer()).thenReturn(false);
IslandCommand cmd = new IslandCommand(addon);
assertFalse(cmd.execute(user, "island", Collections.emptyList()));
verify(user).sendMessage("general.errors.use-in-game");
}
/**
* Test method for {@link world.bentobox.bskyblock.commands.IslandCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringNoArgsNoIslandConsole() {
when(user.isPlayer()).thenReturn(false);
when(im.getIsland(any(), any(UUID.class))).thenReturn(null);
IslandCommand cmd = new IslandCommand(addon);
assertFalse(cmd.execute(user, "island", Collections.emptyList()));
verify(user).sendMessage("general.errors.use-in-game");
}
/**
* Test method for {@link world.bentobox.bskyblock.commands.IslandCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringNoArgsNoIslandNoPermission() {
when(im.getIsland(any(), any(UUID.class))).thenReturn(null);
IslandCommand cmd = new IslandCommand(addon);
assertFalse(cmd.execute(user, "island", Collections.emptyList()));
verify(user).sendMessage("general.errors.no-permission", "[permission]", "island.create");
}
/**
* Test method for {@link world.bentobox.bskyblock.commands.IslandCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringNoArgsNoIslandCreateSuccess() {
when(im.getIsland(any(), any(UUID.class))).thenReturn(null);
when(user.hasPermission(Mockito.eq("island.create"))).thenReturn(true);
IslandCommand cmd = new IslandCommand(addon);
assertTrue(cmd.execute(user, "island", Collections.emptyList()));
verify(user).getTranslation("commands.island.create.pick");
}
}