Adds in-game command info, and fixes some permission bugs

This commit is contained in:
2022-08-05 14:28:03 +02:00
parent e5dfc1ccf6
commit 337c4573e0
4 changed files with 103 additions and 3 deletions

View File

@ -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());

View File

@ -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 <p>The player which sent the command</p>
*/
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 <p>The command to get information about</p>
* @param sender <p>The sender asking to see command info</p>
*/
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("<command>",
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<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label,
@NotNull String[] args) {
return new ArrayList<>();
}
}