Skip to content

Commit

Permalink
Implements new call API for commands
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Jun 16, 2019
1 parent 2364aaa commit 20a5107
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 9 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down
Expand Up @@ -73,10 +73,10 @@ public boolean execute(User user, String label, List<String> args) {
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;
Expand Down
1 change: 0 additions & 1 deletion src/test/java/world/bentobox/bskyblock/BSkyBlockTest.java
Expand Up @@ -24,7 +24,6 @@
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;
Expand Down
Expand Up @@ -6,10 +6,15 @@
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;
Expand All @@ -19,14 +24,17 @@
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;

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;
Expand Down Expand Up @@ -69,6 +77,7 @@ public void setUp() throws Exception {
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
Expand All @@ -91,6 +100,19 @@ public void setUp() throws Exception {
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);

}


Expand Down Expand Up @@ -136,14 +158,25 @@ public void testExecuteUserStringListOfStringNullUsers() {
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 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 testExecuteUserStringListOfStringNoArgs() {
public void testExecuteUserStringListOfStringNoArgsSuccess() {
when(user.hasPermission(anyString())).thenReturn(true);
IslandCommand cmd = new IslandCommand(addon);
assertTrue(cmd.execute(user, "island", Collections.emptyList()));
}
Expand All @@ -152,10 +185,46 @@ public void testExecuteUserStringListOfStringNoArgs() {
* 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");
}

}

0 comments on commit 20a5107

Please sign in to comment.