diff --git a/PRE_PUSH_CHECKLIST.md b/PRE_PUSH_CHECKLIST.md index d87647e..9d3d8f3 100644 --- a/PRE_PUSH_CHECKLIST.md +++ b/PRE_PUSH_CHECKLIST.md @@ -7,9 +7,10 @@ Things to make sure I do before I push a new release version: - [ ] Update [wiki](https://claimchunk.cjburkey.com/) with new information. - [ ] Create changelog - Building - - [ ] Run `./gradlew clean`, `./gradlew googleFormat`, and `./gradlew build` - - These must be run separately for compatibility reasons (might be fixed in the future). - - Upload `OUT/claimchunk-VERSION-plugin.jar` as Github release with changelog + - [ ] Run `./gradlew clean googleFormat` + - [ ] Run `./gradlew build` + - Run separate from others because of stupid error + - Upload `OUT/claimchunk-VERSION-plugin.jar` as GitHub release with changelog - Add new release on [Spigot](https://www.spigotmc.org/resources/claimchunk.44458/) with changelog. - Done! diff --git a/build.gradle.kts b/build.gradle.kts index 9f69444..dbbc341 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -129,7 +129,7 @@ tasks { } build { - mustRunAfter("googleFormat") + mustRunAfter("googleFormat", "clean") // When the build task is run, copy the version into the testServerDir and output // (Also rebuild the README file) finalizedBy("updateReadme", diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/CCBukkitCommand.java b/src/main/java/com/cjburkey/claimchunk/smartcommand/CCBukkitCommand.java index bbd06d4..e8de48a 100644 --- a/src/main/java/com/cjburkey/claimchunk/smartcommand/CCBukkitCommand.java +++ b/src/main/java/com/cjburkey/claimchunk/smartcommand/CCBukkitCommand.java @@ -42,7 +42,6 @@ public CCBukkitCommand( this.register(); } - @SuppressWarnings("UnstableApiUsage") @Override public @NotNull List tabComplete( @NotNull CommandSender sender, @NotNull String alias, String[] args) { @@ -50,7 +49,6 @@ public CCBukkitCommand( return Objects.requireNonNull(baseCommand.onTabComplete(sender, this, alias, args)); } - @SuppressWarnings("UnstableApiUsage") @Override public boolean execute( @NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) { @@ -95,6 +93,7 @@ public void removeFromMap() { "Failed to unregister command! If you are reloading, updates to permissions" + " won't appear until a server reboot."); if (Utils.getDebugEnableOverride()) { + //noinspection CallToPrintStackTrace e.printStackTrace(); } } @@ -117,6 +116,7 @@ private void register() { } catch (IllegalAccessException | NoSuchFieldException e) { Utils.err("ERROR FINDING BUKKIT COMMAND MAP USING REFLECTION!!"); Utils.err("This is a slightly very big problem!!"); + //noinspection CallToPrintStackTrace e.printStackTrace(); } } diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/CCSubCommand.java b/src/main/java/com/cjburkey/claimchunk/smartcommand/CCSubCommand.java index cb4e910..539a22b 100644 --- a/src/main/java/com/cjburkey/claimchunk/smartcommand/CCSubCommand.java +++ b/src/main/java/com/cjburkey/claimchunk/smartcommand/CCSubCommand.java @@ -25,38 +25,33 @@ public abstract class CCSubCommand extends SmartSubCommand implements TabComplet protected final ClaimChunk claimChunk; + // allow multiple permissions here too public CCSubCommand( @NotNull ClaimChunk claimChunk, @NotNull Executor executorLevel, - @Nullable String permissionChild, - boolean isDefault) { + boolean isDefault, + String... permissionChildren) { super( executorLevel, (claimChunk.getConfigHandler().getDisablePermissions() && isDefault) - ? "" - : (permissionChild != null ? ("claimchunk." + permissionChild) : "")); + ? new String[0] + : Arrays.stream(permissionChildren) + .map(perm -> "claimchunk." + perm) + .toArray(String[]::new)); this.claimChunk = claimChunk; } - /** - * Get the description for this subcommand. - * - * @return The description for this subcommand. - */ - public abstract @NotNull Optional getDescription(); - /** * Get whether this command should be displayed in the help subcommand list for the provided * executor. * * @return Whether this command will be displayed within the help list. */ + // maybe make a method to just check sender permission against permission array, so we don't + // have to check everywhere everytime public boolean getShouldDisplayInHelp(@NotNull CommandSender sender) { - String perm = getPermission().orElse(null); - return (getExecutor() == Executor.CONSOLE_PLAYER - || Executor.fromSender(sender) == getExecutor()) - && (perm == null || sender.hasPermission(perm)); + return rightExecutorAndPermission(sender); } /** @@ -145,6 +140,7 @@ public final boolean onCommand( @NotNull String label, @NotNull String[] args) { // TODO: WAITING ON UPDATE TO COMMAND DISPATCHER + // Maybe I should do that instead? // Make sure the player provided the correct number of arguments if (args.length < getRequiredArguments() || args.length > getMaxArguments()) { displayUsage(label, sender); diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/ClaimChunkBaseCommand.java b/src/main/java/com/cjburkey/claimchunk/smartcommand/ClaimChunkBaseCommand.java index 7d827dc..7b574df 100644 --- a/src/main/java/com/cjburkey/claimchunk/smartcommand/ClaimChunkBaseCommand.java +++ b/src/main/java/com/cjburkey/claimchunk/smartcommand/ClaimChunkBaseCommand.java @@ -21,7 +21,7 @@ import java.util.stream.Collectors; /** - * Special thank you to Goldmensch for the new command API! Github link: github.com/Goldmensch/SmartCommandDispatcher. *
* I have included the JavaDoc comments for methods that I wasn't 100% certain on and/or were @@ -99,6 +99,7 @@ private void registerCmds(CommandStr... commands) { // Hopefully won't occur, but compile-time safety isn't one of // Java's strong-suits Utils.err("Failed to initialize subcommand: /chunk %s", String.join(" ", cmd.args)); + //noinspection CallToPrintStackTrace e.printStackTrace(); } } @@ -133,6 +134,7 @@ public boolean noSubFound( * @param cmd The command executed, along with the arguments passed. * @param sender The command sender. */ + @SuppressWarnings("GrazieInspection") @Override public void wrongExecutor( @NotNull SubCommandEntity cmd, diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/admin/AdminOverrideCmd.java b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/admin/AdminOverrideCmd.java index 243345a..eeb14b3 100644 --- a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/admin/AdminOverrideCmd.java +++ b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/admin/AdminOverrideCmd.java @@ -8,8 +8,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; - -import java.util.Optional; +import org.jetbrains.annotations.Nullable; /** * @since 0.0.23 @@ -17,12 +16,12 @@ public class AdminOverrideCmd extends CCSubCommand { public AdminOverrideCmd(ClaimChunk claimChunk) { - super(claimChunk, Executor.PLAYER, "admin", false); + super(claimChunk, Executor.PLAYER, false, "admin"); } @Override - public @NotNull Optional getDescription() { - return Optional.ofNullable(claimChunk.getMessages().cmdAdminOverride); + public @Nullable String getDescription() { + return claimChunk.getMessages().cmdAdminOverride; } @Override diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/admin/AdminReloadCmd.java b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/admin/AdminReloadCmd.java index 8edaa2b..65f4d97 100644 --- a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/admin/AdminReloadCmd.java +++ b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/admin/AdminReloadCmd.java @@ -8,18 +8,17 @@ import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; - -import java.util.Optional; +import org.jetbrains.annotations.Nullable; public class AdminReloadCmd extends CCSubCommand { public AdminReloadCmd(ClaimChunk claimChunk) { - super(claimChunk, Executor.CONSOLE_PLAYER, "admin", false); + super(claimChunk, Executor.CONSOLE_PLAYER, false, "admin"); } @Override - public @NotNull Optional getDescription() { - return Optional.ofNullable(claimChunk.getMessages().cmdReload); + public @Nullable String getDescription() { + return claimChunk.getMessages().cmdReload; } @Override diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/admin/AdminUnclaimAllCmd.java b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/admin/AdminUnclaimAllCmd.java index 695f9dd..8f9335b 100644 --- a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/admin/AdminUnclaimAllCmd.java +++ b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/admin/AdminUnclaimAllCmd.java @@ -8,18 +8,16 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; - -import java.util.Optional; +import org.jetbrains.annotations.Nullable; public class AdminUnclaimAllCmd extends CCSubCommand { public AdminUnclaimAllCmd(ClaimChunk claimChunk) { - super(claimChunk, Executor.PLAYER, "admin", false); + super(claimChunk, Executor.PLAYER, false, "admin"); } - @Override - public @NotNull Optional getDescription() { - return Optional.ofNullable(claimChunk.getMessages().cmdAdminUnclaimAll); + public @Nullable String getDescription() { + return claimChunk.getMessages().cmdAdminUnclaimAll; } @Override diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/admin/AdminUnclaimCmd.java b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/admin/AdminUnclaimCmd.java index 612da09..4c89e13 100644 --- a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/admin/AdminUnclaimCmd.java +++ b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/admin/AdminUnclaimCmd.java @@ -8,18 +8,16 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; - -import java.util.Optional; +import org.jetbrains.annotations.Nullable; public class AdminUnclaimCmd extends CCSubCommand { public AdminUnclaimCmd(ClaimChunk claimChunk) { - super(claimChunk, Executor.PLAYER, "admin", false); + super(claimChunk, Executor.PLAYER, false, "admin"); } - @Override - public @NotNull Optional getDescription() { - return Optional.ofNullable(claimChunk.getMessages().cmdAdminUnclaim); + public @Nullable String getDescription() { + return claimChunk.getMessages().cmdAdminUnclaim; } @Override diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/AccessCmd.java b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/AccessCmd.java index 3667d72..0428e16 100644 --- a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/AccessCmd.java +++ b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/AccessCmd.java @@ -8,11 +8,11 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.Arrays; import java.util.HashMap; import java.util.Map; -import java.util.Optional; /** * @since 0.0.24 @@ -35,12 +35,12 @@ public class AccessCmd extends CCSubCommand { public AccessCmd(ClaimChunk claimChunk) { // TODO: CREATE `/chunk admin access ` to allow listing from // console as well - super(claimChunk, Executor.PLAYER, "access", true); + super(claimChunk, Executor.PLAYER, true, "player", "access"); } @Override - public @NotNull Optional getDescription() { - return Optional.ofNullable(claimChunk.getMessages().cmdAccess); + public @Nullable String getDescription() { + return claimChunk.getMessages().cmdAccess; } @Override diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/AlertCmd.java b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/AlertCmd.java index 84ed01d..e8ecb3d 100644 --- a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/AlertCmd.java +++ b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/AlertCmd.java @@ -8,8 +8,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; - -import java.util.Optional; +import org.jetbrains.annotations.Nullable; /** * @since 0.0.23 @@ -17,12 +16,12 @@ public class AlertCmd extends CCSubCommand { public AlertCmd(ClaimChunk claimChunk) { - super(claimChunk, Executor.PLAYER, "alert", true); + super(claimChunk, Executor.PLAYER, true, "player", "alert"); } @Override - public @NotNull Optional getDescription() { - return Optional.ofNullable(claimChunk.getMessages().cmdAlert); + public @Nullable String getDescription() { + return claimChunk.getMessages().cmdAlert; } @Override diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/AutoCmd.java b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/AutoCmd.java index 3206836..0d60b7f 100644 --- a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/AutoCmd.java +++ b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/AutoCmd.java @@ -9,8 +9,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; - -import java.util.Optional; +import org.jetbrains.annotations.Nullable; /** * @since 0.0.23 @@ -18,12 +17,12 @@ public class AutoCmd extends CCSubCommand { public AutoCmd(ClaimChunk claimChunk) { - super(claimChunk, Executor.PLAYER, "auto", true); + super(claimChunk, Executor.PLAYER, true, "player", "auto"); } @Override - public @NotNull Optional getDescription() { - return Optional.ofNullable(claimChunk.getMessages().cmdAuto); + public @Nullable String getDescription() { + return claimChunk.getMessages().cmdAuto; } @Override diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/CheckAccessCmd.java b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/CheckAccessCmd.java index 6ab0490..fe9c77e 100644 --- a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/CheckAccessCmd.java +++ b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/CheckAccessCmd.java @@ -8,8 +8,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; - -import java.util.Optional; +import org.jetbrains.annotations.Nullable; /** * @since 0.0.24 @@ -17,12 +16,12 @@ public class CheckAccessCmd extends CCSubCommand { public CheckAccessCmd(ClaimChunk claimChunk) { - super(claimChunk, Executor.PLAYER, "access", true); + super(claimChunk, Executor.PLAYER, true, "player", "access"); } @Override - public @NotNull Optional getDescription() { - return Optional.ofNullable(claimChunk.getMessages().cmdCheckAccess); + public @Nullable String getDescription() { + return claimChunk.getMessages().cmdCheckAccess; } @Override diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/ClaimCmd.java b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/ClaimCmd.java index 4d88774..6d1fcb3 100644 --- a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/ClaimCmd.java +++ b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/ClaimCmd.java @@ -8,8 +8,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; - -import java.util.Optional; +import org.jetbrains.annotations.Nullable; /** * @since 0.0.23 @@ -22,12 +21,12 @@ public ClaimCmd(ClaimChunk claimChunk) { // ADD `/chunk admin claim ` to allow claiming the // chunk containing the world coordinates X and Y for the given // player. - super(claimChunk, Executor.PLAYER, "claim", true); + super(claimChunk, Executor.PLAYER, true, "player", "claim"); } @Override - public @NotNull Optional getDescription() { - return Optional.ofNullable(claimChunk.getMessages().cmdClaim); + public @Nullable String getDescription() { + return claimChunk.getMessages().cmdClaim; } @Override diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/GiveCmd.java b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/GiveCmd.java index 2d148a5..0cf4b70 100644 --- a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/GiveCmd.java +++ b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/GiveCmd.java @@ -8,8 +8,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; - -import java.util.Optional; +import org.jetbrains.annotations.Nullable; /** * @since 0.0.23 @@ -17,12 +16,12 @@ public class GiveCmd extends CCSubCommand { public GiveCmd(ClaimChunk claimChunk) { - super(claimChunk, Executor.PLAYER, "give", true); + super(claimChunk, Executor.PLAYER, true, "player", "give"); } @Override - public @NotNull Optional getDescription() { - return Optional.ofNullable(claimChunk.getMessages().cmdGive); + public @Nullable String getDescription() { + return claimChunk.getMessages().cmdGive; } @Override diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/HelpCmd.java b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/HelpCmd.java index 762d3cc..6ce3747 100644 --- a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/HelpCmd.java +++ b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/HelpCmd.java @@ -9,8 +9,7 @@ import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; - -import java.util.Optional; +import org.jetbrains.annotations.Nullable; /** * @since 0.0.23 @@ -21,14 +20,14 @@ public class HelpCmd extends CCSubCommand { public HelpCmd(ClaimChunk claimChunk, ClaimChunkBaseCommand baseCommand) { // TODO: MAKE ACCESSIBLE FROM CONSOLE - super(claimChunk, Executor.CONSOLE_PLAYER, "help", true); + super(claimChunk, Executor.CONSOLE_PLAYER, true, "player", "help"); this.baseCommand = baseCommand; } @Override - public @NotNull Optional getDescription() { - return Optional.ofNullable(claimChunk.getMessages().cmdHelp); + public @Nullable String getDescription() { + return claimChunk.getMessages().cmdHelp; } @Override @@ -94,6 +93,7 @@ public boolean onCall(@NotNull String cmdUsed, @NotNull CommandSender player, St } private @NotNull String getCommandDisplayStr(String cmdUsed, CCSubCommand cmd) { + @Nullable String desc = cmd.getDescription(); // Create the display string return claimChunk @@ -104,8 +104,8 @@ public boolean onCall(@NotNull String cmdUsed, @NotNull CommandSender player, St .replace("%%ARGS%%", cmd.getUsageArgs()) .replace( "%%DESC%%", - cmd.getDescription().isPresent() - ? cmd.getDescription().get() - : "No description! Oops! Let me know about this please :)"); + desc == null + ? "No description! Oops! Let me know about this please :)" + : desc); } } diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/InfoCmd.java b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/InfoCmd.java index 2c6e2ba..ceb99bb 100644 --- a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/InfoCmd.java +++ b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/InfoCmd.java @@ -8,8 +8,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; - -import java.util.Optional; +import org.jetbrains.annotations.Nullable; /** * @since 0.0.23 @@ -17,12 +16,12 @@ public class InfoCmd extends CCSubCommand { public InfoCmd(ClaimChunk claimChunk) { - super(claimChunk, Executor.PLAYER, "info", true); + super(claimChunk, Executor.PLAYER, true, "player", "info"); } @Override - public @NotNull Optional getDescription() { - return Optional.ofNullable(claimChunk.getMessages().cmdInfo); + public @Nullable String getDescription() { + return claimChunk.getMessages().cmdInfo; } @Override diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/ListCmd.java b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/ListCmd.java index eaad92d..88aca86 100644 --- a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/ListCmd.java +++ b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/ListCmd.java @@ -9,8 +9,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; - -import java.util.Optional; +import org.jetbrains.annotations.Nullable; /** * @since 0.0.23 @@ -18,12 +17,12 @@ public class ListCmd extends CCSubCommand { public ListCmd(ClaimChunk claimChunk) { - super(claimChunk, Executor.PLAYER, "list", true); + super(claimChunk, Executor.PLAYER, true, "player", "list"); } @Override - public @NotNull Optional getDescription() { - return Optional.ofNullable(claimChunk.getMessages().cmdList); + public @Nullable String getDescription() { + return claimChunk.getMessages().cmdList; } @Override diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/NameCmd.java b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/NameCmd.java index 518d08a..abbdb56 100644 --- a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/NameCmd.java +++ b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/NameCmd.java @@ -8,8 +8,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; - -import java.util.Optional; +import org.jetbrains.annotations.Nullable; /** * @since 0.0.23 @@ -17,12 +16,12 @@ public class NameCmd extends CCSubCommand { public NameCmd(ClaimChunk claimChunk) { - super(claimChunk, Executor.PLAYER, "name", true); + super(claimChunk, Executor.PLAYER, true, "player", "name"); } @Override - public @NotNull Optional getDescription() { - return Optional.ofNullable(claimChunk.getMessages().cmdName); + public @Nullable String getDescription() { + return claimChunk.getMessages().cmdName; } @Override diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/RevokeAccessCmd.java b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/RevokeAccessCmd.java index 8f12f36..f6de8ef 100644 --- a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/RevokeAccessCmd.java +++ b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/RevokeAccessCmd.java @@ -8,8 +8,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; - -import java.util.Optional; +import org.jetbrains.annotations.Nullable; /** * @since 0.0.24 @@ -17,12 +16,12 @@ public class RevokeAccessCmd extends CCSubCommand { public RevokeAccessCmd(ClaimChunk claimChunk) { - super(claimChunk, Executor.PLAYER, "access", true); + super(claimChunk, Executor.PLAYER, true, "player", "access"); } @Override - public @NotNull Optional getDescription() { - return Optional.ofNullable(claimChunk.getMessages().cmdRevokeAccess); + public @Nullable String getDescription() { + return claimChunk.getMessages().cmdRevokeAccess; } @Override diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/ScanCmd.java b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/ScanCmd.java index 47e56bd..921199f 100644 --- a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/ScanCmd.java +++ b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/ScanCmd.java @@ -9,20 +9,20 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.List; -import java.util.Optional; public class ScanCmd extends CCSubCommand { public ScanCmd(ClaimChunk claimChunk) { - super(claimChunk, Executor.PLAYER, "scan", true); + super(claimChunk, Executor.PLAYER, true, "player", "scan"); } @Override - public @NotNull Optional getDescription() { - return Optional.ofNullable(claimChunk.getMessages().cmdScan); + public @Nullable String getDescription() { + return claimChunk.getMessages().cmdScan; } @Override diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/ShowClaimedCmd.java b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/ShowClaimedCmd.java index af45642..9415064 100644 --- a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/ShowClaimedCmd.java +++ b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/ShowClaimedCmd.java @@ -9,9 +9,9 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.HashSet; -import java.util.Optional; /** * @since 0.0.23 @@ -22,12 +22,12 @@ public class ShowClaimedCmd extends CCSubCommand { public int maxRadius = 6; public ShowClaimedCmd(ClaimChunk claimChunk) { - super(claimChunk, Executor.PLAYER, "show-claimed", true); + super(claimChunk, Executor.PLAYER, true, "player", "show-claimed"); } @Override - public @NotNull Optional getDescription() { - return Optional.ofNullable(claimChunk.getMessages().cmdShow); + public @Nullable String getDescription() { + return claimChunk.getMessages().cmdShow; } @Override diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/ShowCmd.java b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/ShowCmd.java index 5cad200..5c35710 100644 --- a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/ShowCmd.java +++ b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/ShowCmd.java @@ -10,8 +10,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; - -import java.util.Optional; +import org.jetbrains.annotations.Nullable; /** * @since 0.0.23 @@ -21,12 +20,12 @@ public class ShowCmd extends CCSubCommand { public int maxSeconds = 60; public ShowCmd(ClaimChunk claimChunk) { - super(claimChunk, Executor.PLAYER, "show", true); + super(claimChunk, Executor.PLAYER, true, "player", "show"); } @Override - public @NotNull Optional getDescription() { - return Optional.ofNullable(claimChunk.getMessages().cmdShow); + public @Nullable String getDescription() { + return claimChunk.getMessages().cmdShow; } @Override diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/UnclaimAllCmd.java b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/UnclaimAllCmd.java index bfca344..7a643ed 100644 --- a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/UnclaimAllCmd.java +++ b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/UnclaimAllCmd.java @@ -8,18 +8,17 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; - -import java.util.Optional; +import org.jetbrains.annotations.Nullable; public class UnclaimAllCmd extends CCSubCommand { public UnclaimAllCmd(ClaimChunk claimChunk) { - super(claimChunk, Executor.PLAYER, "unclaim", true); + super(claimChunk, Executor.PLAYER, true, "player", "unclaim"); } @Override - public @NotNull Optional getDescription() { - return Optional.ofNullable(claimChunk.getMessages().cmdUnclaimAll); + public @Nullable String getDescription() { + return claimChunk.getMessages().cmdUnclaimAll; } @Override diff --git a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/UnclaimCmd.java b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/UnclaimCmd.java index 5ed3e2d..a26d05c 100644 --- a/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/UnclaimCmd.java +++ b/src/main/java/com/cjburkey/claimchunk/smartcommand/sub/ply/UnclaimCmd.java @@ -8,18 +8,17 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; - -import java.util.Optional; +import org.jetbrains.annotations.Nullable; public class UnclaimCmd extends CCSubCommand { public UnclaimCmd(ClaimChunk claimChunk) { - super(claimChunk, Executor.PLAYER, "unclaim", true); + super(claimChunk, Executor.PLAYER, true, "player", "unclaim"); } @Override - public @NotNull Optional getDescription() { - return Optional.ofNullable(claimChunk.getMessages().cmdUnclaim); + public @Nullable String getDescription() { + return claimChunk.getMessages().cmdUnclaim; } @Override diff --git a/src/main/java/de/goldmensch/commanddispatcher/Commands.java b/src/main/java/de/goldmensch/commanddispatcher/Commands.java deleted file mode 100644 index d90b4ac..0000000 --- a/src/main/java/de/goldmensch/commanddispatcher/Commands.java +++ /dev/null @@ -1,28 +0,0 @@ -package de.goldmensch.commanddispatcher; - -import de.goldmensch.commanddispatcher.subcommand.SmartSubCommand; - -import org.bukkit.command.CommandSender; -import org.jetbrains.annotations.NotNull; - -import java.util.Optional; - -public final class Commands { - - private Commands() {} - - public static boolean checkExecutor(@NotNull CommandSender sender, @NotNull Executor executor) { - return executor == Executor.CONSOLE_PLAYER || executor == Executor.fromSender(sender); - } - - public static boolean checkPermission( - @NotNull CommandSender sender, @NotNull Optional posPermission) { - return posPermission.isEmpty() || sender.hasPermission(posPermission.get()); - } - - public static boolean checkPermissionAndExecutor( - @NotNull CommandSender sender, @NotNull SmartSubCommand command) { - return Commands.checkExecutor(sender, command.getExecutor()) - && Commands.checkPermission(sender, command.getPermission()); - } -} diff --git a/src/main/java/de/goldmensch/commanddispatcher/Executor.java b/src/main/java/de/goldmensch/commanddispatcher/Executor.java index 42d6227..131a96c 100644 --- a/src/main/java/de/goldmensch/commanddispatcher/Executor.java +++ b/src/main/java/de/goldmensch/commanddispatcher/Executor.java @@ -23,7 +23,7 @@ public enum Executor { * @param sender The CommandSender * @return The ExecutorLevel that corresponds to the CommandSender. */ - public static Executor fromSender(@NotNull CommandSender sender) { + public static @NotNull Executor fromSender(@NotNull CommandSender sender) { if (sender instanceof Player) { return Executor.PLAYER; } diff --git a/src/main/java/de/goldmensch/commanddispatcher/command/SmartCommand.java b/src/main/java/de/goldmensch/commanddispatcher/command/SmartCommand.java index 2223a44..b74400c 100644 --- a/src/main/java/de/goldmensch/commanddispatcher/command/SmartCommand.java +++ b/src/main/java/de/goldmensch/commanddispatcher/command/SmartCommand.java @@ -2,7 +2,6 @@ import de.goldmensch.commanddispatcher.ArraySets; import de.goldmensch.commanddispatcher.ArrayUtil; -import de.goldmensch.commanddispatcher.Commands; import de.goldmensch.commanddispatcher.Executor; import de.goldmensch.commanddispatcher.annotations.Description; import de.goldmensch.commanddispatcher.exceptions.CommandNotValidException; @@ -112,12 +111,12 @@ public boolean onCommand( var foundCommand = posSubCommand.get(); var command = foundCommand.getCommand(); - if (!Commands.checkExecutor(sender, command.getExecutor())) { + if (!command.rightExecutor(sender)) { wrongExecutor(foundCommand, sender, command.getExecutor()); return true; } - if (!Commands.checkPermission(sender, command.getPermission())) { + if (!command.rightPermission(sender)) { noPermission(foundCommand, sender); return true; } @@ -146,7 +145,7 @@ public boolean onCommand( var argLength = args.length - 1; var comArgs = sub.getKey(); - if (!Commands.checkPermissionAndExecutor(sender, sub.getValue()) + if (!sub.getValue().rightExecutorAndPermission(sender) || (comArgs.length < args.length)) continue; var comPath = java.util.Arrays.copyOf(comArgs, argLength); if (java.util.Arrays.equals(comPath, argPath)) { @@ -159,7 +158,7 @@ public boolean onCommand( var foundCommand = searchSub(args); if (foundCommand.isPresent()) { var subCommand = foundCommand.get().getCommand(); - if (Commands.checkPermissionAndExecutor(sender, subCommand) + if (subCommand.rightExecutorAndPermission(sender) && subCommand instanceof TabCompleter tabCompleter) { var commandCompletion = tabCompleter.onTabComplete( diff --git a/src/main/java/de/goldmensch/commanddispatcher/subcommand/SmartSubCommand.java b/src/main/java/de/goldmensch/commanddispatcher/subcommand/SmartSubCommand.java index 601990d..ae45f7b 100644 --- a/src/main/java/de/goldmensch/commanddispatcher/subcommand/SmartSubCommand.java +++ b/src/main/java/de/goldmensch/commanddispatcher/subcommand/SmartSubCommand.java @@ -2,23 +2,26 @@ import de.goldmensch.commanddispatcher.Executor; +import lombok.Getter; + import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import java.util.Optional; +import java.util.Arrays; public abstract class SmartSubCommand implements CommandExecutor { private final Executor executor; - private final String permission; + @Getter private final String[] permissions; private String name; - private String description; + @Getter private String description; - public SmartSubCommand(@NotNull Executor executor, @Nullable String permission) { + // vararg + public SmartSubCommand(@NotNull Executor executor, String... permissions) { this.executor = executor; - this.permission = permission; + this.permissions = permissions; } @ApiStatus.Internal @@ -39,10 +42,36 @@ public void setDescription(@NotNull String description) { } /*** - * @return The permission of the SubCommand + * Checks if the provided player should be able to execute a given command. + * + * @param sender The player whose permission we need to check. + * @return Whether the player should be allowed to execute this command. + */ + public boolean rightPermission(@NotNull CommandSender sender) { + return permissions.length == 0 + || Arrays.stream(permissions).anyMatch(sender::hasPermission); + } + + /*** + * Checks if the provided command executor should be able to execute a given command. + * + * @param sender The sender whose executor type. + * @return Whether the executor should be allowed to execute this command. */ - public @NotNull Optional getPermission() { - return permission.isEmpty() ? Optional.empty() : Optional.of(permission); + public boolean rightExecutor(@NotNull CommandSender sender) { + Executor senderExecutor = Executor.fromSender(sender); + return senderExecutor == Executor.CONSOLE || senderExecutor == getExecutor(); + } + + /*** + * Checks if the provided sender should be able to execute a given command. If the console is + * the executor, {@code true} is always returned. + * + * @param sender The sender whose permission we need to check. + * @return Whether the given sender should be allowed to execute this command based on type and permission. + */ + public boolean rightExecutorAndPermission(@NotNull CommandSender sender) { + return rightExecutor(sender) && rightPermission(sender); } /*** @@ -53,8 +82,4 @@ public void setDescription(@NotNull String description) { public @NotNull String getName() { return name; } - - public @NotNull Optional getDescription() { - return Optional.ofNullable(description); - } }