Adjusts tabComplete for a number of commands.

Enforces the need to type at least one letter for whole-server tab
completes.
This commit is contained in:
tastybento 2020-08-31 22:03:51 -07:00
parent c1ede3c66a
commit ea4ef1e21a
12 changed files with 119 additions and 99 deletions

View File

@ -128,7 +128,8 @@ public class IslandBanCommand extends CompositeCommand {
@Override
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
if (args.isEmpty()) {
String lastArg = !args.isEmpty() ? args.get(args.size()-1) : "";
if (lastArg.isEmpty()) {
// Don't show every player on the server. Require at least the first letter
return Optional.empty();
}
@ -139,7 +140,6 @@ public class IslandBanCommand extends CompositeCommand {
.filter(p -> !island.isBanned(p.getUniqueId()))
.filter(p -> user.getPlayer().canSee(p))
.map(Player::getName).collect(Collectors.toList());
String lastArg = !args.isEmpty() ? args.get(args.size()-1) : "";
return Optional.of(Util.tabLimit(options, lastArg));
} else {
return Optional.empty();

View File

@ -2,9 +2,12 @@ package world.bentobox.bentobox.api.commands.island;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.api.commands.CompositeCommand;
@ -13,6 +16,7 @@ import world.bentobox.bentobox.api.events.island.IslandEvent;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.Util;
/**
* @author tastybento
@ -140,4 +144,19 @@ public class IslandExpelCommand extends CompositeCommand {
user.sendMessage(CANNOT_EXPEL);
return false;
}
@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.getPlayersOnIsland().stream()
.filter(p -> !p.equals(user.getPlayer()))
.map(Player::getName).collect(Collectors.toList());
String lastArg = !args.isEmpty() ? args.get(args.size()-1) : "";
return Optional.of(Util.tabLimit(options, lastArg));
} else {
return Optional.empty();
}
}
}

View File

@ -1,12 +1,15 @@
package world.bentobox.bentobox.api.commands.island;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.Util;
/**
* @author Poslovitch
@ -56,4 +59,15 @@ public class IslandInfoCommand extends CompositeCommand {
island.showInfo(user);
return true;
}
@Override
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
String lastArg = !args.isEmpty() ? args.get(args.size()-1) : "";
if (lastArg.isEmpty()) {
// Don't show every player on the server. Require at least the first letter
return Optional.empty();
}
List<String> options = new ArrayList<>(Util.getOnlinePlayerList(user));
return Optional.of(Util.tabLimit(options, lastArg));
}
}

View File

@ -118,11 +118,11 @@ public class IslandTeamCoopCommand extends CompositeCommand {
@Override
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
if (args.isEmpty()) {
String lastArg = !args.isEmpty() ? args.get(args.size()-1) : "";
if (lastArg.isEmpty()) {
// Don't show every player on the server. Require at least the first letter
return Optional.empty();
}
String lastArg = args.get(args.size()-1);
return Optional.of(Util.tabLimit(Util.getOnlinePlayerList(user), lastArg));
}

View File

@ -148,7 +148,7 @@ public class IslandTeamInviteCommand extends CompositeCommand {
@Override
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
String lastArg = !args.isEmpty() ? args.get(args.size()-1) : "";
if (args.isEmpty()) {
if (lastArg.isEmpty()) {
// Don't show every player on the server. Require at least the first letter
return Optional.empty();
}

View File

@ -2,6 +2,11 @@ package world.bentobox.bentobox.api.commands.island.team;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
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.events.island.IslandEvent;
@ -9,6 +14,7 @@ import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.util.Util;
public class IslandTeamPromoteCommand extends CompositeCommand {
@ -113,4 +119,18 @@ public class IslandTeamPromoteCommand extends CompositeCommand {
}
}
@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()
.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();
}
}
}

View File

@ -120,11 +120,11 @@ public class IslandTeamTrustCommand extends CompositeCommand {
@Override
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
if (args.isEmpty()) {
String lastArg = !args.isEmpty() ? args.get(args.size()-1) : "";
if (lastArg.isEmpty()) {
// Don't show every player on the server. Require at least the first letter
return Optional.empty();
}
String lastArg = args.get(args.size()-1);
return Optional.of(Util.tabLimit(Util.getOnlinePlayerList(user), lastArg));
}

View File

@ -111,11 +111,10 @@ public class IslandTeamUncoopCommand extends CompositeCommand {
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.COOP_RANK)
.map(Bukkit::getOfflinePlayer)
List<String> options = island.getMembers().entrySet().stream()
.filter(e -> e.getValue() == RanksManager.COOP_RANK)
.map(e -> Bukkit.getOfflinePlayer(e.getKey()))
.map(OfflinePlayer::getName).collect(Collectors.toList());
String lastArg = !args.isEmpty() ? args.get(args.size()-1) : "";
return Optional.of(Util.tabLimit(options, lastArg));
} else {

View File

@ -111,11 +111,10 @@ public class IslandTeamUntrustCommand extends CompositeCommand {
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.TRUSTED_RANK)
.map(Bukkit::getOfflinePlayer)
List<String> options = island.getMembers().entrySet().stream()
.filter(e -> e.getValue() == RanksManager.TRUSTED_RANK)
.map(e -> Bukkit.getOfflinePlayer(e.getKey()))
.map(OfflinePlayer::getName).collect(Collectors.toList());
String lastArg = !args.isEmpty() ? args.get(args.size()-1) : "";
return Optional.of(Util.tabLimit(options, lastArg));
} else {

View File

@ -381,18 +381,14 @@ public class IslandBanCommandTest {
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
String[] expectedNames = {"dave", "ed", "frank", "freddy", "george", "harry", "joe"};
assertTrue(Arrays.equals(expectedNames, r.toArray()));
assertFalse(result.isPresent());
// Get the tab-complete list with one letter argument
args = new LinkedList<>();
args.add("d");
result = ibc.tabComplete(user, "", args);
assertTrue(result.isPresent());
r = result.get().stream().sorted().collect(Collectors.toList());
List<String> r = result.get().stream().sorted().collect(Collectors.toList());
// Compare the expected with the actual
String[] expectedName = {"dave"};
assertTrue(Arrays.equals(expectedName, r.toArray()));

View File

@ -15,6 +15,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
@ -32,9 +33,6 @@ 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.commands.CompositeCommand;
@ -261,30 +259,24 @@ public class IslandTeamUncoopCommandTest {
@Test
public void testTabCompleteNoArgument() {
Builder<UUID> memberSet = new ImmutableSet.Builder<>();
for (int j = 0; j < 11; j++) {
memberSet.add(UUID.randomUUID());
}
Map<UUID, Integer> map = new HashMap<>();
map.put(UUID.randomUUID(),RanksManager.COOP_RANK);
map.put(UUID.randomUUID(),RanksManager.COOP_RANK);
map.put(UUID.randomUUID(),RanksManager.COOP_RANK);
map.put(UUID.randomUUID(),RanksManager.MEMBER_RANK);
map.put(UUID.randomUUID(),RanksManager.MEMBER_RANK);
map.put(UUID.randomUUID(),RanksManager.MEMBER_RANK);
map.put(UUID.randomUUID(),RanksManager.MEMBER_RANK);
map.put(UUID.randomUUID(),RanksManager.MEMBER_RANK);
map.put(UUID.randomUUID(),RanksManager.MEMBER_RANK);
map.put(UUID.randomUUID(),RanksManager.MEMBER_RANK);
when(island.getMemberSet()).thenReturn(memberSet.build());
when(island.getMembers()).thenReturn(map);
// 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 no argument
@ -301,30 +293,25 @@ public class IslandTeamUncoopCommandTest {
@Test
public void testTabCompleteWithArgument() {
Builder<UUID> memberSet = new ImmutableSet.Builder<>();
for (int j = 0; j < 11; j++) {
memberSet.add(UUID.randomUUID());
}
Map<UUID, Integer> map = new HashMap<>();
map.put(UUID.randomUUID(),RanksManager.COOP_RANK);
map.put(UUID.randomUUID(),RanksManager.COOP_RANK);
map.put(UUID.randomUUID(),RanksManager.COOP_RANK);
map.put(UUID.randomUUID(),RanksManager.MEMBER_RANK);
map.put(UUID.randomUUID(),RanksManager.MEMBER_RANK);
map.put(UUID.randomUUID(),RanksManager.MEMBER_RANK);
map.put(UUID.randomUUID(),RanksManager.MEMBER_RANK);
map.put(UUID.randomUUID(),RanksManager.MEMBER_RANK);
map.put(UUID.randomUUID(),RanksManager.MEMBER_RANK);
map.put(UUID.randomUUID(),RanksManager.MEMBER_RANK);
when(island.getMemberSet()).thenReturn(memberSet.build());
when(island.getMembers()).thenReturn(map);
// 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

View File

@ -14,6 +14,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
@ -31,9 +32,6 @@ 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.commands.CompositeCommand;
@ -260,30 +258,24 @@ public class IslandTeamUntrustCommandTest {
@Test
public void testTabCompleteNoArgument() {
Builder<UUID> memberSet = new ImmutableSet.Builder<>();
for (int j = 0; j < 11; j++) {
memberSet.add(UUID.randomUUID());
}
Map<UUID, Integer> map = new HashMap<>();
map.put(UUID.randomUUID(),RanksManager.TRUSTED_RANK);
map.put(UUID.randomUUID(),RanksManager.TRUSTED_RANK);
map.put(UUID.randomUUID(),RanksManager.TRUSTED_RANK);
map.put(UUID.randomUUID(),RanksManager.MEMBER_RANK);
map.put(UUID.randomUUID(),RanksManager.MEMBER_RANK);
map.put(UUID.randomUUID(),RanksManager.MEMBER_RANK);
map.put(UUID.randomUUID(),RanksManager.MEMBER_RANK);
map.put(UUID.randomUUID(),RanksManager.MEMBER_RANK);
map.put(UUID.randomUUID(),RanksManager.MEMBER_RANK);
map.put(UUID.randomUUID(),RanksManager.MEMBER_RANK);
when(island.getMemberSet()).thenReturn(memberSet.build());
when(island.getMembers()).thenReturn(map);
// 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 no argument
@ -300,30 +292,24 @@ public class IslandTeamUntrustCommandTest {
@Test
public void testTabCompleteWithArgument() {
Builder<UUID> memberSet = new ImmutableSet.Builder<>();
for (int j = 0; j < 11; j++) {
memberSet.add(UUID.randomUUID());
}
Map<UUID, Integer> map = new HashMap<>();
map.put(UUID.randomUUID(),RanksManager.TRUSTED_RANK);
map.put(UUID.randomUUID(),RanksManager.TRUSTED_RANK);
map.put(UUID.randomUUID(),RanksManager.TRUSTED_RANK);
map.put(UUID.randomUUID(),RanksManager.MEMBER_RANK);
map.put(UUID.randomUUID(),RanksManager.MEMBER_RANK);
map.put(UUID.randomUUID(),RanksManager.MEMBER_RANK);
map.put(UUID.randomUUID(),RanksManager.MEMBER_RANK);
map.put(UUID.randomUUID(),RanksManager.MEMBER_RANK);
map.put(UUID.randomUUID(),RanksManager.MEMBER_RANK);
map.put(UUID.randomUUID(),RanksManager.MEMBER_RANK);
when(island.getMemberSet()).thenReturn(memberSet.build());
when(island.getMembers()).thenReturn(map);
// 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