Use the only/default track if one is not specifed in the promote/demote command (#1581)

This commit is contained in:
Luck 2020-06-20 21:03:01 +01:00
parent bd9718c5af
commit cb5a28f043
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
4 changed files with 34 additions and 5 deletions

View File

@ -50,11 +50,12 @@ import net.luckperms.api.context.MutableContextSet;
import net.luckperms.api.track.DemotionResult;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
public class UserDemote extends ChildCommand<User> {
public UserDemote(LocaleManager locale) {
super(CommandSpec.USER_DEMOTE.localize(locale), "demote", CommandPermission.USER_DEMOTE, Predicates.is(0));
super(CommandSpec.USER_DEMOTE.localize(locale), "demote", CommandPermission.USER_DEMOTE, Predicates.alwaysFalse());
}
@Override
@ -66,6 +67,19 @@ public class UserDemote extends ChildCommand<User> {
boolean removeFromFirst = !args.remove("--dont-remove-from-first");
// if args is empty - use the only/default track
if (args.isEmpty()) {
Set<String> tracks = plugin.getTrackManager().getAll().keySet();
if (tracks.size() == 1) {
args.add(tracks.iterator().next());
} else if (tracks.contains("default")) {
args.add("default");
} else {
Message.USER_TRACK_ERROR_AMBIGUOUS_TRACK_SELECTION.send(sender);
return CommandResult.INVALID_ARGS;
}
}
final String trackName = args.get(0).toLowerCase();
if (!DataConstraints.TRACK_NAME_TEST.test(trackName)) {
Message.TRACK_INVALID_ENTRY.send(sender, trackName);

View File

@ -50,11 +50,12 @@ import net.luckperms.api.context.MutableContextSet;
import net.luckperms.api.track.PromotionResult;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
public class UserPromote extends ChildCommand<User> {
public UserPromote(LocaleManager locale) {
super(CommandSpec.USER_PROMOTE.localize(locale), "promote", CommandPermission.USER_PROMOTE, Predicates.is(0));
super(CommandSpec.USER_PROMOTE.localize(locale), "promote", CommandPermission.USER_PROMOTE, Predicates.alwaysFalse());
}
@Override
@ -66,6 +67,19 @@ public class UserPromote extends ChildCommand<User> {
boolean addToFirst = !args.remove("--dont-add-to-first");
// if args is empty - use the only/default track
if (args.isEmpty()) {
Set<String> tracks = plugin.getTrackManager().getAll().keySet();
if (tracks.size() == 1) {
args.add(tracks.iterator().next());
} else if (tracks.contains("default")) {
args.add("default");
} else {
Message.USER_TRACK_ERROR_AMBIGUOUS_TRACK_SELECTION.send(sender);
return CommandResult.INVALID_ARGS;
}
}
final String trackName = args.get(0).toLowerCase();
if (!DataConstraints.TRACK_NAME_TEST.test(trackName)) {
Message.TRACK_INVALID_ENTRY.send(sender, trackName);

View File

@ -151,14 +151,14 @@ public enum CommandSpec {
),
USER_PROMOTE("Promotes the user up a track",
Argument.list(
Argument.create("track", true, "the track to promote the user up"),
Argument.create("track", false, "the track to promote the user up"),
Argument.create("context...", false, "the contexts to promote the user in"),
Argument.create("--dont-add-to-first", false, "only promote the user if they're already on the track")
)
),
USER_DEMOTE("Demotes the user down a track",
Argument.list(
Argument.create("track", true, "the track to demote the user down"),
Argument.create("track", false, "the track to demote the user down"),
Argument.create("context...", false, "the contexts to demote the user in"),
Argument.create("--dont-remove-from-first", false, "prevent the user from being removed from the first group")
)

View File

@ -382,17 +382,18 @@ public enum Message {
USER_PRIMARYGROUP_ERROR_ALREADYHAS("&b{}&a already has &b{}&a set as their primary group.", true),
USER_PRIMARYGROUP_ERROR_NOTMEMBER("&b{}&a was not already a member of &b{}&a, adding them now.", true),
USER_TRACK_ERROR_NOT_CONTAIN_GROUP("&b{}&a isn't already in any groups on &b{}&a.", true),
USER_TRACK_ERROR_AMBIGUOUS_TRACK_SELECTION("&cUnsure which track to use. Please specify it as an argument.", true),
USER_TRACK_ADDED_TO_FIRST("&b{}&a isn't in any groups on this track, so they were added to the first group, &b{}&a in context {}&a.", true),
USER_PROMOTE_NOT_ON_TRACK("&b{}&a isn't in any groups on this track, so was not promoted.", true),
USER_PROMOTE_SUCCESS("&aPromoting &b{}&a along track &b{}&a from &b{}&a to &b{}&a in context {}&a.", true),
USER_PROMOTE_ERROR_ENDOFTRACK("&aThe end of track &b{}&a was reached. Unable to promote &b{}&a.", true),
USER_PROMOTE_ERROR_MALFORMED(
"{PREFIX}&aThe next group on the track, &b{}&a, no longer exists. Unable to promote user." + "\n" +
"{PREFIX}&aEither create the group, or remove it from the track and try again.",
false
),
USER_DEMOTE_SUCCESS("&aDemoting &b{}&a along track &b{}&a from &b{}&a to &b{}&a in context {}&a.", true),
USER_DEMOTE_ENDOFTRACK("&aThe end of track &b{}&a was reached, so &b{}&a was removed from &b{}&a.", true),
USER_DEMOTE_ENDOFTRACK_NOT_REMOVED("&aThe end of track &b{}&a was reached, but &b{}&a was not removed from the first group.", true),