Fix home tab completions (#3206)

Fixes #1337 😎 

This PR fixes tab completions for all of the home commands in Essentials. Prior to this PR, the behavior is approximately as follows:

- `/sethome` has no implementation for completions, and so it provides player names, which isn't very useful, and in my opinion can cause more harm than good by confusing users.
- `/home` and `/delhome` fail to provide valid completions when a user has the `essentials.home.others` permission. The argument syntax is `[player:]<name>` but it tries to complete it as `<player> <name>`. Not only does it not show you suggestions for your own homes, but it proceeds to show you invalid suggestions!

This PR provides completions that accurately reflect the syntax and real behavior of the command, including suggesting homes for player names that are partially matched. It will provide suggestions for all of your own homes, as well as providing suggestions based on how far along you are in the command (players if you haven't specified `:` yet, otherwise a specific player's homes).
This commit is contained in:
pop4959 2020-04-30 12:26:27 -07:00 committed by GitHub
parent b7eec09307
commit d5ffed09b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 32 deletions

View File

@ -4,6 +4,7 @@ import com.earth2me.essentials.CommandSource;
import com.earth2me.essentials.User;
import org.bukkit.Server;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
@ -54,22 +55,25 @@ public class Commanddelhome extends EssentialsCommand {
@Override
protected List<String> getTabCompleteOptions(final Server server, final CommandSource sender, final String commandLabel, final String[] args) {
User user = ess.getUser(sender.getPlayer());
boolean canDelOthers = (user == null || user.isAuthorized("essentials.delhome.others"));
boolean canDelOthers = user == null || user.isAuthorized("essentials.delhome.others");
if (args.length == 1) {
List<String> homes = user == null ? new ArrayList<>() : user.getHomes();
if (canDelOthers) {
return getPlayers(server, sender);
} else {
return user.getHomes();
}
} else if (args.length == 2 && canDelOthers) {
try {
user = getPlayer(server, args, 0, true, true);
return user.getHomes();
} catch (Exception ex) {
// No such user
return Collections.emptyList();
int sepIndex = args[0].indexOf(':');
if (sepIndex < 0) {
getPlayers(server, sender).forEach(player -> homes.add(player + ":"));
} else {
String namePart = args[0].substring(0, sepIndex);
User otherUser;
try {
otherUser = getPlayer(server, new String[]{namePart}, 0, true, true);
} catch (Exception ex) {
return homes;
}
otherUser.getHomes().forEach(home -> homes.add(namePart + ":" + home));
}
}
return homes;
} else {
return Collections.emptyList();
}

View File

@ -104,29 +104,31 @@ public class Commandhome extends EssentialsCommand {
@Override
protected List<String> getTabCompleteOptions(final Server server, final User user, final String commandLabel, final String[] args) {
boolean canVisitOthers = user.isAuthorized("essentials.home.others");
boolean canVisitBed = user.isAuthorized("essentials.home.bed");
if (args.length == 1) {
List<String> homes = user.getHomes();
if (canVisitBed) {
homes.add("bed");
}
if (canVisitOthers) {
return getPlayers(server, user);
} else {
List<String> homes = user.getHomes();
if (user.isAuthorized("essentials.home.bed")) {
homes.add("bed");
int sepIndex = args[0].indexOf(':');
if (sepIndex < 0) {
getPlayers(server, user).forEach(player -> homes.add(player + ":"));
} else {
String namePart = args[0].substring(0, sepIndex);
User otherUser;
try {
otherUser = getPlayer(server, new String[]{namePart}, 0, true, true);
} catch (Exception ex) {
return homes;
}
otherUser.getHomes().forEach(home -> homes.add(namePart + ":" + home));
if (canVisitBed) {
homes.add(namePart + ":bed");
}
}
return homes;
}
} else if (args.length == 2 && canVisitOthers) {
try {
User otherUser = getPlayer(server, args, 0, true, true);
List<String> homes = otherUser.getHomes();
if (user.isAuthorized("essentials.home.bed")) {
homes.add("bed");
}
return homes;
} catch (Exception ex) {
// No such user
return Collections.emptyList();
}
return homes;
} else {
return Collections.emptyList();
}

View File

@ -1,11 +1,14 @@
package com.earth2me.essentials.commands;
import com.earth2me.essentials.CommandSource;
import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.LocationUtil;
import com.earth2me.essentials.utils.NumberUtil;
import org.bukkit.Location;
import org.bukkit.Server;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import static com.earth2me.essentials.I18n.tl;
@ -70,4 +73,9 @@ public class Commandsethome extends EssentialsCommand {
}
return false;
}
@Override
protected List<String> getTabCompleteOptions(final Server server, final CommandSource sender, final String commandLabel, final String[] args) {
return Collections.emptyList();
}
}