From 337c4573e09a0a0a669f1518ebbaf1f400fabda2 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Fri, 5 Aug 2022 14:28:03 +0200 Subject: [PATCH] Adds in-game command info, and fixes some permission bugs --- README.md | 3 + .../net/knarcraft/paidsigns/PaidSigns.java | 3 + .../command/PaidSignsTabCommand.java | 81 +++++++++++++++++++ src/main/resources/plugin.yml | 19 ++++- 4 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 src/main/java/net/knarcraft/paidsigns/command/PaidSignsTabCommand.java diff --git a/README.md b/README.md index 51707b4..1bfc9fe 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ arguments, such as no paid sign permission, you should use empty quotes (""). | Command | Arguments | Permission | Description | | -------- | ------ | ------- | ------- | +| /paidsigns | | paidsigns.info | Used to display in-game information about all other commands | | [/addpaidsign](#addpaidsign) | \ \ \[permission] \[ignore case] \[ignore color] \[match any condition] | paidsigns.manage | Used to add a new paid sign | | [/addpaidsigncondition](#addpaidsigncondition) | \ \ \ \[executeRegEx] \[ignoreCase] \[ignoreColor] | paidsigns.manage | Used to add a condition to a paid sign | | [/listpaidsigns](#listpaidsigns) | \[name (of a paid sign)] \[line number] | paidsigns.manage | Used to list registered paid signs or a registered paid sign's conditions | @@ -61,6 +62,8 @@ arguments, such as no paid sign permission, you should use empty quotes (""). ## Command explanation +### /paidsigns + ### /addpaidsign This command adds a new paid sign that does nothing until a condition is added. diff --git a/src/main/java/net/knarcraft/paidsigns/PaidSigns.java b/src/main/java/net/knarcraft/paidsigns/PaidSigns.java index 0e90225..283dcf6 100644 --- a/src/main/java/net/knarcraft/paidsigns/PaidSigns.java +++ b/src/main/java/net/knarcraft/paidsigns/PaidSigns.java @@ -8,6 +8,7 @@ import net.knarcraft.paidsigns.command.EditCommand; import net.knarcraft.paidsigns.command.EditTabCompleter; import net.knarcraft.paidsigns.command.ListCommand; import net.knarcraft.paidsigns.command.ListTabCompleter; +import net.knarcraft.paidsigns.command.PaidSignsTabCommand; import net.knarcraft.paidsigns.command.ReloadTabCommand; import net.knarcraft.paidsigns.command.RemoveConditionCommand; import net.knarcraft.paidsigns.command.RemoveConditionTabCompleter; @@ -153,6 +154,8 @@ public final class PaidSigns extends JavaPlugin { * Registers the commands used by this plugin */ private void registerCommands() { + TabExecutor paidSignsExecutor = new PaidSignsTabCommand(); + registerCommand("paidSigns", paidSignsExecutor, paidSignsExecutor); registerCommand("addPaidSign", new AddCommand(), new AddTabCompleter()); registerCommand("listPaidSigns", new ListCommand(), new ListTabCompleter()); registerCommand("addPaidSignCondition", new AddConditionCommand(), new AddConditionTabCompleter()); diff --git a/src/main/java/net/knarcraft/paidsigns/command/PaidSignsTabCommand.java b/src/main/java/net/knarcraft/paidsigns/command/PaidSignsTabCommand.java new file mode 100644 index 0000000..fe70c1e --- /dev/null +++ b/src/main/java/net/knarcraft/paidsigns/command/PaidSignsTabCommand.java @@ -0,0 +1,81 @@ +package net.knarcraft.paidsigns.command; + +import net.knarcraft.paidsigns.PaidSigns; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.command.PluginCommand; +import org.bukkit.command.TabExecutor; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.List; + +public class PaidSignsTabCommand implements TabExecutor { + + private static final ChatColor successColor = ChatColor.GREEN; + private static final ChatColor commandColor = ChatColor.YELLOW; + + @Override + public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + sender.sendMessage(commandColor + "[] denote optional parameters"); + sender.sendMessage(commandColor + "<> denote required parameters"); + sender.sendMessage(commandColor + "{} denote required permission"); + sender.sendMessage(commandColor + "In some cases, commands with required parameters can be called with no parameters"); + if (sender instanceof Player) { + showPlayerCommands(sender); + } + return true; + } + + /** + * Shows all commands available to the sending player + * + * @param sender

The player which sent the command

+ */ + private void showPlayerCommands(CommandSender sender) { + sender.sendMessage(commandColor + "Commands:"); + + showCommandInfo("addPaidSign", sender); + showCommandInfo("addPaidSignCondition", sender); + showCommandInfo("listPaidSigns", sender); + showCommandInfo("editPaidSign", sender); + showCommandInfo("removePaidSignCondition", sender); + showCommandInfo("removePaidSign", sender); + showCommandInfo("reload", sender); + } + + /** + * Shows information about the given command + * + * @param command

The command to get information about

+ * @param sender

The sender asking to see command info

+ */ + private void showCommandInfo(String command, CommandSender sender) { + PluginCommand pluginCommand = PaidSigns.getInstance().getCommand(command); + if (pluginCommand != null) { + String permission = pluginCommand.getPermission(); + if (permission == null || sender.hasPermission(permission)) { + String commandInfo = "\n" + commandColor + pluginCommand.getUsage().replace("", + pluginCommand.getName()) + ": " + successColor + pluginCommand.getDescription(); + if (sender.hasPermission("paidsigns.admin")) { + if (permission == null) { + permission = "None"; + } + commandInfo += commandColor + " {" + permission + "}"; + } + sender.sendMessage(commandInfo); + } + } + } + + @Nullable + @Override + public List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, + @NotNull String[] args) { + return new ArrayList<>(); + } + +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index d73ed49..aac6b1a 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -8,6 +8,10 @@ authors: [ EpicKnarvik97 ] description: Add costs for creating plugin signs website: https://git.knarcraft.net/EpicKnarvik97/PaidSigns commands: + paidsigns: + description: Displays information about paid signs commends + usage: / + permission: paidsigns.info addpaidsign: aliases: - aps @@ -49,17 +53,26 @@ commands: - r description: Reloads paid signs from disk usage: / - permision: paidsigns.reload + permission: paidsigns.reload permissions: paidsigns.*: + description: Alias to paidsigns.admin + default: false + children: + paidsigns.admin: true + paidsigns.admin: description: Grants all paid signs permissions default: op children: - paidsigns.create: true + paidsigns.info: true paidsigns.paymentexempt: true + paidsigns.manage: true paidsigns.reload: true + paidsigns.info: + description: Grants the permission to see command info + default: false paidsigns.manage: - description: Grants the permission to add/remove a paid sign + description: Grants the permission to add/remove/edit a paid sign default: false paidsigns.reload: description: Grants the permissions to reload the plugin