mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2024-11-27 05:05:18 +01:00
Adds or enhances tab complete for kick/uncoop/untrust
https://github.com/BentoBoxWorld/BentoBox/issues/1509
This commit is contained in:
parent
514af670bc
commit
845d27de68
@ -2,7 +2,12 @@ package world.bentobox.bentobox.api.commands.island.team;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
|
||||
import world.bentobox.bentobox.api.commands.CompositeCommand;
|
||||
import world.bentobox.bentobox.api.commands.ConfirmableCommand;
|
||||
@ -141,4 +146,20 @@ public class IslandTeamKickCommand extends ConfirmableCommand {
|
||||
getSettings().getInviteCooldown() * 60));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
|
||||
Island island = getIslands().getIsland(getWorld(), user.getUniqueId());
|
||||
if (island != null) {
|
||||
List<String> options = island.getMemberSet().stream()
|
||||
.filter(uuid -> island.getRank(uuid) >= RanksManager.MEMBER_RANK)
|
||||
.map(Bukkit::getOfflinePlayer)
|
||||
.map(OfflinePlayer::getName).collect(Collectors.toList());
|
||||
|
||||
String lastArg = !args.isEmpty() ? args.get(args.size()-1) : "";
|
||||
return Optional.of(Util.tabLimit(options, lastArg));
|
||||
} else {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -109,14 +109,10 @@ public class IslandTeamUncoopCommand extends CompositeCommand {
|
||||
|
||||
@Override
|
||||
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
|
||||
if (args.isEmpty()) {
|
||||
// Don't show every player on the server. Require at least the first letter
|
||||
return Optional.empty();
|
||||
}
|
||||
Island island = getIslands().getIsland(getWorld(), user.getUniqueId());
|
||||
if (island != null) {
|
||||
List<String> options = island.getMemberSet().stream()
|
||||
.filter(uuid -> island.getRank(User.getInstance(uuid)) == RanksManager.COOP_RANK)
|
||||
.filter(uuid -> island.getRank(uuid) == RanksManager.COOP_RANK)
|
||||
.map(Bukkit::getOfflinePlayer)
|
||||
.map(OfflinePlayer::getName).collect(Collectors.toList());
|
||||
|
||||
|
@ -109,14 +109,10 @@ public class IslandTeamUntrustCommand extends CompositeCommand {
|
||||
|
||||
@Override
|
||||
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
|
||||
if (args.isEmpty()) {
|
||||
// Don't show every player on the server. Require at least the first letter
|
||||
return Optional.empty();
|
||||
}
|
||||
Island island = getIslands().getIsland(getWorld(), user.getUniqueId());
|
||||
if (island != null) {
|
||||
List<String> options = island.getMemberSet().stream()
|
||||
.filter(uuid -> island.getRank(User.getInstance(uuid)) == RanksManager.TRUSTED_RANK)
|
||||
.filter(uuid -> island.getRank(uuid) == RanksManager.TRUSTED_RANK)
|
||||
.map(Bukkit::getOfflinePlayer)
|
||||
.map(OfflinePlayer::getName).collect(Collectors.toList());
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
package world.bentobox.bentobox.api.commands.island.team;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@ -12,11 +13,15 @@ import static org.mockito.Mockito.when;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.PlayerInventory;
|
||||
@ -33,6 +38,9 @@ import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.reflect.Whitebox;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.ImmutableSet.Builder;
|
||||
|
||||
import world.bentobox.bentobox.BentoBox;
|
||||
import world.bentobox.bentobox.Settings;
|
||||
import world.bentobox.bentobox.api.addons.Addon;
|
||||
@ -405,4 +413,129 @@ public class IslandTeamKickCommandTest {
|
||||
testExecuteNoConfirmation();
|
||||
verify(subCommand).setCooldown("uniqueid", notUUID.toString(), 600);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTabCompleteNoArgument() {
|
||||
|
||||
Builder<UUID> memberSet = new ImmutableSet.Builder<>();
|
||||
for (int j = 0; j < 11; j++) {
|
||||
memberSet.add(UUID.randomUUID());
|
||||
}
|
||||
|
||||
when(island.getMemberSet()).thenReturn(memberSet.build());
|
||||
// Return a set of players
|
||||
PowerMockito.mockStatic(Bukkit.class);
|
||||
OfflinePlayer offlinePlayer = mock(OfflinePlayer.class);
|
||||
when(Bukkit.getOfflinePlayer(any(UUID.class))).thenReturn(offlinePlayer);
|
||||
when(offlinePlayer.getName()).thenReturn("adam", "ben", "cara", "dave", "ed", "frank", "freddy", "george", "harry", "ian", "joe");
|
||||
when(island.getRank(any(UUID.class))).thenReturn(
|
||||
RanksManager.COOP_RANK,
|
||||
RanksManager.COOP_RANK,
|
||||
RanksManager.COOP_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK
|
||||
);
|
||||
|
||||
IslandTeamKickCommand ibc = new IslandTeamKickCommand(ic);
|
||||
// Get the tab-complete list with no argument
|
||||
Optional<List<String>> result = ibc.tabComplete(user, "", new LinkedList<>());
|
||||
assertTrue(result.isPresent());
|
||||
List<String> r = result.get().stream().sorted().collect(Collectors.toList());
|
||||
// Compare the expected with the actual - first names in the list
|
||||
String[] expectedNames = {"adam", "ben", "cara", "dave", "ed", "frank", "freddy", "george"};
|
||||
int i = 0;
|
||||
for (String name : r) {
|
||||
assertEquals("Rank " + i, expectedNames[i++], name);
|
||||
}
|
||||
// assertTrue(Arrays.equals(expectedNames, r.toArray()));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTabCompleteWithArgument() {
|
||||
|
||||
Builder<UUID> memberSet = new ImmutableSet.Builder<>();
|
||||
for (int j = 0; j < 11; j++) {
|
||||
memberSet.add(UUID.randomUUID());
|
||||
}
|
||||
|
||||
when(island.getMemberSet()).thenReturn(memberSet.build());
|
||||
// Return a set of players
|
||||
PowerMockito.mockStatic(Bukkit.class);
|
||||
OfflinePlayer offlinePlayer = mock(OfflinePlayer.class);
|
||||
when(Bukkit.getOfflinePlayer(any(UUID.class))).thenReturn(offlinePlayer);
|
||||
when(offlinePlayer.getName()).thenReturn("adam", "ben", "cara", "dave", "ed", "frank", "freddy", "george", "harry", "ian", "joe");
|
||||
when(island.getRank(any(UUID.class))).thenReturn(
|
||||
RanksManager.COOP_RANK,
|
||||
RanksManager.COOP_RANK,
|
||||
RanksManager.COOP_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK
|
||||
);
|
||||
|
||||
IslandTeamKickCommand ibc = new IslandTeamKickCommand(ic);
|
||||
// Get the tab-complete list with argument
|
||||
Optional<List<String>> result = ibc.tabComplete(user, "", Collections.singletonList("g"));
|
||||
assertTrue(result.isPresent());
|
||||
List<String> r = result.get().stream().sorted().collect(Collectors.toList());
|
||||
assertFalse(r.isEmpty());
|
||||
// Compare the expected with the actual
|
||||
String[] expectedNames = {"george"};
|
||||
int i = 0;
|
||||
for (String name : r) {
|
||||
assertEquals("Rank " + i, expectedNames[i++], name);
|
||||
}
|
||||
|
||||
//assertTrue(Arrays.equals(expectedNames, r.toArray()));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTabCompleteWithWrongArgument() {
|
||||
|
||||
Builder<UUID> memberSet = new ImmutableSet.Builder<>();
|
||||
for (int j = 0; j < 11; j++) {
|
||||
memberSet.add(UUID.randomUUID());
|
||||
}
|
||||
|
||||
when(island.getMemberSet()).thenReturn(memberSet.build());
|
||||
// Return a set of players
|
||||
PowerMockito.mockStatic(Bukkit.class);
|
||||
OfflinePlayer offlinePlayer = mock(OfflinePlayer.class);
|
||||
when(Bukkit.getOfflinePlayer(any(UUID.class))).thenReturn(offlinePlayer);
|
||||
when(offlinePlayer.getName()).thenReturn("adam", "ben", "cara", "dave", "ed", "frank", "freddy", "george", "harry", "ian", "joe");
|
||||
when(island.getRank(any(User.class))).thenReturn(
|
||||
RanksManager.COOP_RANK,
|
||||
RanksManager.COOP_RANK,
|
||||
RanksManager.COOP_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK
|
||||
);
|
||||
|
||||
IslandTeamKickCommand ibc = new IslandTeamKickCommand(ic);
|
||||
// Get the tab-complete list with argument
|
||||
LinkedList<String> args = new LinkedList<>();
|
||||
args.add("c");
|
||||
Optional<List<String>> result = ibc.tabComplete(user, "", args);
|
||||
assertTrue(result.isPresent());
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -96,7 +96,6 @@ public class IslandTeamUncoopCommandTest {
|
||||
when(user.getPlayer()).thenReturn(p);
|
||||
when(user.getName()).thenReturn("tastybento");
|
||||
when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class));
|
||||
|
||||
User.setPlugin(plugin);
|
||||
|
||||
// Parent command has no aliases
|
||||
@ -260,7 +259,7 @@ public class IslandTeamUncoopCommandTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTabComplete() {
|
||||
public void testTabCompleteNoArgument() {
|
||||
|
||||
Builder<UUID> memberSet = new ImmutableSet.Builder<>();
|
||||
for (int j = 0; j < 11; j++) {
|
||||
@ -273,7 +272,7 @@ public class IslandTeamUncoopCommandTest {
|
||||
OfflinePlayer offlinePlayer = mock(OfflinePlayer.class);
|
||||
when(Bukkit.getOfflinePlayer(any(UUID.class))).thenReturn(offlinePlayer);
|
||||
when(offlinePlayer.getName()).thenReturn("adam", "ben", "cara", "dave", "ed", "frank", "freddy", "george", "harry", "ian", "joe");
|
||||
when(island.getRank(any(User.class))).thenReturn(
|
||||
when(island.getRank(any(UUID.class))).thenReturn(
|
||||
RanksManager.COOP_RANK,
|
||||
RanksManager.COOP_RANK,
|
||||
RanksManager.COOP_RANK,
|
||||
@ -290,12 +289,6 @@ public class IslandTeamUncoopCommandTest {
|
||||
IslandTeamUncoopCommand ibc = new IslandTeamUncoopCommand(ic);
|
||||
// Get the tab-complete list with no argument
|
||||
Optional<List<String>> result = ibc.tabComplete(user, "", new LinkedList<>());
|
||||
assertFalse(result.isPresent());
|
||||
|
||||
// Get the tab-complete list with no argument
|
||||
LinkedList<String> args = new LinkedList<>();
|
||||
args.add("");
|
||||
result = ibc.tabComplete(user, "", args);
|
||||
assertTrue(result.isPresent());
|
||||
List<String> r = result.get().stream().sorted().collect(Collectors.toList());
|
||||
// Compare the expected with the actual
|
||||
@ -305,4 +298,45 @@ public class IslandTeamUncoopCommandTest {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTabCompleteWithArgument() {
|
||||
|
||||
Builder<UUID> memberSet = new ImmutableSet.Builder<>();
|
||||
for (int j = 0; j < 11; j++) {
|
||||
memberSet.add(UUID.randomUUID());
|
||||
}
|
||||
|
||||
when(island.getMemberSet()).thenReturn(memberSet.build());
|
||||
// Return a set of players
|
||||
PowerMockito.mockStatic(Bukkit.class);
|
||||
OfflinePlayer offlinePlayer = mock(OfflinePlayer.class);
|
||||
when(Bukkit.getOfflinePlayer(any(UUID.class))).thenReturn(offlinePlayer);
|
||||
when(offlinePlayer.getName()).thenReturn("adam", "ben", "cara", "dave", "ed", "frank", "freddy", "george", "harry", "ian", "joe");
|
||||
when(island.getRank(any(UUID.class))).thenReturn(
|
||||
RanksManager.COOP_RANK,
|
||||
RanksManager.COOP_RANK,
|
||||
RanksManager.COOP_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK
|
||||
);
|
||||
|
||||
IslandTeamUncoopCommand ibc = new IslandTeamUncoopCommand(ic);
|
||||
// Get the tab-complete list with argument
|
||||
LinkedList<String> args = new LinkedList<>();
|
||||
args.add("c");
|
||||
Optional<List<String>> result = ibc.tabComplete(user, "", args);
|
||||
assertTrue(result.isPresent());
|
||||
List<String> r = result.get().stream().sorted().collect(Collectors.toList());
|
||||
// Compare the expected with the actual
|
||||
String[] expectedNames = {"cara"};
|
||||
|
||||
assertTrue(Arrays.equals(expectedNames, r.toArray()));
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -258,7 +258,7 @@ public class IslandTeamUntrustCommandTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTabComplete() {
|
||||
public void testTabCompleteNoArgument() {
|
||||
|
||||
Builder<UUID> memberSet = new ImmutableSet.Builder<>();
|
||||
for (int j = 0; j < 11; j++) {
|
||||
@ -271,7 +271,7 @@ public class IslandTeamUntrustCommandTest {
|
||||
OfflinePlayer offlinePlayer = mock(OfflinePlayer.class);
|
||||
when(Bukkit.getOfflinePlayer(any(UUID.class))).thenReturn(offlinePlayer);
|
||||
when(offlinePlayer.getName()).thenReturn("adam", "ben", "cara", "dave", "ed", "frank", "freddy", "george", "harry", "ian", "joe");
|
||||
when(island.getRank(any(User.class))).thenReturn(
|
||||
when(island.getRank(any(UUID.class))).thenReturn(
|
||||
RanksManager.TRUSTED_RANK,
|
||||
RanksManager.TRUSTED_RANK,
|
||||
RanksManager.TRUSTED_RANK,
|
||||
@ -288,12 +288,6 @@ public class IslandTeamUntrustCommandTest {
|
||||
IslandTeamUntrustCommand ibc = new IslandTeamUntrustCommand(ic);
|
||||
// Get the tab-complete list with no argument
|
||||
Optional<List<String>> result = ibc.tabComplete(user, "", new LinkedList<>());
|
||||
assertFalse(result.isPresent());
|
||||
|
||||
// Get the tab-complete list with no argument
|
||||
LinkedList<String> args = new LinkedList<>();
|
||||
args.add("");
|
||||
result = ibc.tabComplete(user, "", args);
|
||||
assertTrue(result.isPresent());
|
||||
List<String> r = result.get().stream().sorted().collect(Collectors.toList());
|
||||
// Compare the expected with the actual
|
||||
@ -302,4 +296,46 @@ public class IslandTeamUntrustCommandTest {
|
||||
assertTrue(Arrays.equals(expectedNames, r.toArray()));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTabCompleteWithArgument() {
|
||||
|
||||
Builder<UUID> memberSet = new ImmutableSet.Builder<>();
|
||||
for (int j = 0; j < 11; j++) {
|
||||
memberSet.add(UUID.randomUUID());
|
||||
}
|
||||
|
||||
when(island.getMemberSet()).thenReturn(memberSet.build());
|
||||
// Return a set of players
|
||||
PowerMockito.mockStatic(Bukkit.class);
|
||||
OfflinePlayer offlinePlayer = mock(OfflinePlayer.class);
|
||||
when(Bukkit.getOfflinePlayer(any(UUID.class))).thenReturn(offlinePlayer);
|
||||
when(offlinePlayer.getName()).thenReturn("adam", "ben", "cara", "dave", "ed", "frank", "freddy", "george", "harry", "ian", "joe");
|
||||
when(island.getRank(any(UUID.class))).thenReturn(
|
||||
RanksManager.TRUSTED_RANK,
|
||||
RanksManager.TRUSTED_RANK,
|
||||
RanksManager.TRUSTED_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK,
|
||||
RanksManager.MEMBER_RANK
|
||||
);
|
||||
|
||||
IslandTeamUntrustCommand ibc = new IslandTeamUntrustCommand(ic);
|
||||
// Get the tab-complete list with argument
|
||||
LinkedList<String> args = new LinkedList<>();
|
||||
args.add("c");
|
||||
Optional<List<String>> result = ibc.tabComplete(user, "", args);
|
||||
assertTrue(result.isPresent());
|
||||
List<String> r = result.get().stream().sorted().collect(Collectors.toList());
|
||||
// Compare the expected with the actual
|
||||
String[] expectedNames = {"cara"};
|
||||
|
||||
assertTrue(Arrays.equals(expectedNames, r.toArray()));
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user