From cb5a28f043fc8a401002861daeced138046f8364 Mon Sep 17 00:00:00 2001 From: Luck Date: Sat, 20 Jun 2020 21:03:01 +0100 Subject: [PATCH] Use the only/default track if one is not specifed in the promote/demote command (#1581) --- .../common/commands/user/UserDemote.java | 16 +++++++++++++++- .../common/commands/user/UserPromote.java | 16 +++++++++++++++- .../common/locale/command/CommandSpec.java | 4 ++-- .../luckperms/common/locale/message/Message.java | 3 ++- 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/user/UserDemote.java b/common/src/main/java/me/lucko/luckperms/common/commands/user/UserDemote.java index 964c2bf5e..6d8e7d19e 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/user/UserDemote.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/user/UserDemote.java @@ -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 { 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 { boolean removeFromFirst = !args.remove("--dont-remove-from-first"); + // if args is empty - use the only/default track + if (args.isEmpty()) { + Set 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); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/user/UserPromote.java b/common/src/main/java/me/lucko/luckperms/common/commands/user/UserPromote.java index b62b67c86..2a43b0b2a 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/user/UserPromote.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/user/UserPromote.java @@ -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 { 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 { boolean addToFirst = !args.remove("--dont-add-to-first"); + // if args is empty - use the only/default track + if (args.isEmpty()) { + Set 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); diff --git a/common/src/main/java/me/lucko/luckperms/common/locale/command/CommandSpec.java b/common/src/main/java/me/lucko/luckperms/common/locale/command/CommandSpec.java index 9f8c88434..a024d0d56 100644 --- a/common/src/main/java/me/lucko/luckperms/common/locale/command/CommandSpec.java +++ b/common/src/main/java/me/lucko/luckperms/common/locale/command/CommandSpec.java @@ -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") ) diff --git a/common/src/main/java/me/lucko/luckperms/common/locale/message/Message.java b/common/src/main/java/me/lucko/luckperms/common/locale/message/Message.java index 92253bde8..d9d41c328 100644 --- a/common/src/main/java/me/lucko/luckperms/common/locale/message/Message.java +++ b/common/src/main/java/me/lucko/luckperms/common/locale/message/Message.java @@ -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),