Made the commands easier to use

Display the command list when the user uses the base command in addition
to /hd help. Move the plugin information to the subcommand /hd version
This commit is contained in:
kmecpp 2018-07-20 14:32:49 -04:00
parent cae74ee88d
commit fc0840c9d3
3 changed files with 113 additions and 115 deletions

View File

@ -4,13 +4,17 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import com.gmail.filoghost.holographicdisplays.HolographicDisplays;
import com.gmail.filoghost.holographicdisplays.commands.Colors;
import com.gmail.filoghost.holographicdisplays.commands.CommandValidator;
import com.gmail.filoghost.holographicdisplays.commands.Strings;
import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand.SubCommandType;
import com.gmail.filoghost.holographicdisplays.commands.main.subs.AddlineCommand;
import com.gmail.filoghost.holographicdisplays.commands.main.subs.AlignCommand;
import com.gmail.filoghost.holographicdisplays.commands.main.subs.CopyCommand;
@ -18,7 +22,6 @@ import com.gmail.filoghost.holographicdisplays.commands.main.subs.CreateCommand;
import com.gmail.filoghost.holographicdisplays.commands.main.subs.DeleteCommand;
import com.gmail.filoghost.holographicdisplays.commands.main.subs.EditCommand;
import com.gmail.filoghost.holographicdisplays.commands.main.subs.FixCommand;
import com.gmail.filoghost.holographicdisplays.commands.main.subs.HelpCommand;
import com.gmail.filoghost.holographicdisplays.commands.main.subs.InfoCommand;
import com.gmail.filoghost.holographicdisplays.commands.main.subs.InsertlineCommand;
import com.gmail.filoghost.holographicdisplays.commands.main.subs.ListCommand;
@ -30,16 +33,17 @@ import com.gmail.filoghost.holographicdisplays.commands.main.subs.ReloadCommand;
import com.gmail.filoghost.holographicdisplays.commands.main.subs.RemovelineCommand;
import com.gmail.filoghost.holographicdisplays.commands.main.subs.SetlineCommand;
import com.gmail.filoghost.holographicdisplays.commands.main.subs.TeleportCommand;
import com.gmail.filoghost.holographicdisplays.commands.main.subs.VersionCommand;
import com.gmail.filoghost.holographicdisplays.exception.CommandException;
import com.gmail.filoghost.holographicdisplays.util.Utils;
public class HologramsCommandHandler implements CommandExecutor {
private List<HologramSubCommand> subCommands;
public HologramsCommandHandler() {
subCommands = Utils.newList();
registerSubCommand(new AddlineCommand());
registerSubCommand(new CreateCommand());
registerSubCommand(new DeleteCommand());
@ -51,45 +55,69 @@ public class HologramsCommandHandler implements CommandExecutor {
registerSubCommand(new AlignCommand());
registerSubCommand(new CopyCommand());
registerSubCommand(new ReloadCommand());
registerSubCommand(new VersionCommand());
registerSubCommand(new FixCommand());
registerSubCommand(new RemovelineCommand());
registerSubCommand(new SetlineCommand());
registerSubCommand(new InsertlineCommand());
registerSubCommand(new ReadtextCommand());
registerSubCommand(new ReadimageCommand());
registerSubCommand(new InfoCommand());
registerSubCommand(new HelpCommand(this));
}
public void registerSubCommand(HologramSubCommand subCommand) {
subCommands.add(subCommand);
}
public List<HologramSubCommand> getSubCommands() {
return new ArrayList<HologramSubCommand>(subCommands);
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (args.length == 0) {
sender.sendMessage(Colors.PRIMARY_SHADOW + "Server is running " + Colors.PRIMARY + "Holographic Displays " + Colors.PRIMARY_SHADOW + "v" + HolographicDisplays.getInstance().getDescription().getVersion() + " by " + Colors.PRIMARY + "filoghost");
if (sender.hasPermission(Strings.BASE_PERM + "help")) {
sender.sendMessage(Colors.PRIMARY_SHADOW + "Commands: " + Colors.PRIMARY + "/" + label + " help");
if (args.length == 0 || args[0].equalsIgnoreCase("help")) {
sender.sendMessage("");
sender.sendMessage(Strings.formatTitle("Holographic Displays Commands"));
for (HologramSubCommand subCommand : subCommands) {
if (subCommand.getType() == SubCommandType.GENERIC) {
String usage = "/" + label + " " + subCommand.getName() + (subCommand.getPossibleArguments().length() > 0 ? " " + subCommand.getPossibleArguments() : "");
if (CommandValidator.isPlayerSender(sender)) {
List<String> help = Utils.newList();
help.add(Colors.PRIMARY + usage);
for (String tutLine : subCommand.getTutorial()) {
help.add(Colors.SECONDARY_SHADOW + tutLine);
}
HolographicDisplays.getNMSManager()
.newFancyMessage(usage)
.color(ChatColor.AQUA)
.suggest(usage)
.tooltip(Utils.join(help, "\n"))
.send((Player) sender);
} else {
sender.sendMessage(Colors.PRIMARY + usage);
}
}
}
if (CommandValidator.isPlayerSender(sender)) {
sendHoverTip(sender);
}
return true;
}
for (HologramSubCommand subCommand : subCommands) {
if (subCommand.isValidTrigger(args[0])) {
if (!subCommand.hasPermission(sender)) {
sender.sendMessage(Colors.ERROR + "You don't have permission.");
return true;
}
if (args.length - 1 >= subCommand.getMinimumArguments()) {
try {
subCommand.execute(sender, label, Arrays.copyOfRange(args, 1, args.length));
@ -99,12 +127,26 @@ public class HologramsCommandHandler implements CommandExecutor {
} else {
sender.sendMessage(Colors.ERROR + "Usage: /" + label + " " + subCommand.getName() + " " + subCommand.getPossibleArguments());
}
return true;
}
}
sender.sendMessage(Colors.ERROR + "Unknown sub-command. Type \"/" + label + " help\" for a list of commands.");
return true;
}
public static void sendHoverTip(CommandSender sender) {
sender.sendMessage("");
HolographicDisplays.getNMSManager().newFancyMessage("TIP").style(ChatColor.BOLD).color(ChatColor.YELLOW)
.then(" Try to ").color(ChatColor.GRAY)
.then("hover").color(ChatColor.WHITE).style(ChatColor.ITALIC, ChatColor.UNDERLINE)
.tooltip(ChatColor.LIGHT_PURPLE + "Hover on the commands to get info about them.")
.then(" or ").color(ChatColor.GRAY)
.then("click").color(ChatColor.WHITE).style(ChatColor.ITALIC, ChatColor.UNDERLINE)
.tooltip(ChatColor.LIGHT_PURPLE + "Click on the commands to insert them in the chat.")
.then(" on the commands!").color(ChatColor.GRAY)
.send((Player) sender);
}
}

View File

@ -1,96 +0,0 @@
package com.gmail.filoghost.holographicdisplays.commands.main.subs;
import java.util.List;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import com.gmail.filoghost.holographicdisplays.HolographicDisplays;
import com.gmail.filoghost.holographicdisplays.commands.Colors;
import com.gmail.filoghost.holographicdisplays.commands.CommandValidator;
import com.gmail.filoghost.holographicdisplays.commands.Strings;
import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand;
import com.gmail.filoghost.holographicdisplays.commands.main.HologramsCommandHandler;
import com.gmail.filoghost.holographicdisplays.exception.CommandException;
import com.gmail.filoghost.holographicdisplays.util.Utils;
public class HelpCommand extends HologramSubCommand {
private HologramsCommandHandler mainCommandHandler;
public HelpCommand(HologramsCommandHandler mainCommandHandler) {
super("help");
setPermission(Strings.BASE_PERM + "help");
this.mainCommandHandler = mainCommandHandler;
}
@Override
public String getPossibleArguments() {
return "";
}
@Override
public int getMinimumArguments() {
return 0;
}
@Override
public void execute(CommandSender sender, String label, String[] args) throws CommandException {
sender.sendMessage("");
sender.sendMessage(Strings.formatTitle("Holographic Displays Commands"));
for (HologramSubCommand subCommand : mainCommandHandler.getSubCommands()) {
if (subCommand.getType() == SubCommandType.GENERIC) {
String usage = "/" + label + " " + subCommand.getName() + (subCommand.getPossibleArguments().length() > 0 ? " " + subCommand.getPossibleArguments() : "");
if (CommandValidator.isPlayerSender(sender)) {
List<String> help = Utils.newList();
help.add(Colors.PRIMARY + usage);
for (String tutLine : subCommand.getTutorial()) {
help.add(Colors.SECONDARY_SHADOW + tutLine);
}
HolographicDisplays.getNMSManager().newFancyMessage(usage)
.color(ChatColor.AQUA)
.suggest(usage)
.tooltip(Utils.join(help, "\n"))
.send((Player) sender);
} else {
sender.sendMessage(Colors.PRIMARY + usage);
}
}
}
if (CommandValidator.isPlayerSender(sender)) {
sendHoverTip(sender);
}
}
public static void sendHoverTip(CommandSender sender) {
sender.sendMessage("");
HolographicDisplays.getNMSManager().newFancyMessage("TIP").style(ChatColor.BOLD).color(ChatColor.YELLOW)
.then(" Try to ").color(ChatColor.GRAY)
.then("hover").color(ChatColor.WHITE).style(ChatColor.ITALIC, ChatColor.UNDERLINE)
.tooltip(ChatColor.LIGHT_PURPLE + "Hover on the commands to get info about them.")
.then(" or ").color(ChatColor.GRAY)
.then("click").color(ChatColor.WHITE).style(ChatColor.ITALIC, ChatColor.UNDERLINE)
.tooltip(ChatColor.LIGHT_PURPLE + "Click on the commands to insert them in the chat.")
.then(" on the commands!").color(ChatColor.GRAY)
.send((Player) sender);
}
@Override
public List<String> getTutorial() {
return null;
}
@Override
public SubCommandType getType() {
return SubCommandType.HIDDEN;
}
}

View File

@ -0,0 +1,52 @@
package com.gmail.filoghost.holographicdisplays.commands.main.subs;
import java.util.Arrays;
import java.util.List;
import org.bukkit.command.CommandSender;
import com.gmail.filoghost.holographicdisplays.HolographicDisplays;
import com.gmail.filoghost.holographicdisplays.commands.Colors;
import com.gmail.filoghost.holographicdisplays.commands.Strings;
import com.gmail.filoghost.holographicdisplays.commands.main.HologramSubCommand;
import com.gmail.filoghost.holographicdisplays.exception.CommandException;
public class VersionCommand extends HologramSubCommand {
public VersionCommand() {
super("version");
setPermission(Strings.BASE_PERM + "version");
}
@Override
public String getPossibleArguments() {
return "";
}
@Override
public int getMinimumArguments() {
return 0;
}
@Override
public void execute(CommandSender sender, String label, String[] args) throws CommandException {
sender.sendMessage("");
sender.sendMessage(Colors.PRIMARY_SHADOW + "Server is running " + Colors.PRIMARY + "Holographic Displays " + Colors.PRIMARY_SHADOW + "v" + HolographicDisplays.getInstance().getDescription().getVersion() + " by " + Colors.PRIMARY + "filoghost");
if (sender.hasPermission(Strings.BASE_PERM + "help")) {
sender.sendMessage(Colors.PRIMARY_SHADOW + "Commands: " + Colors.PRIMARY + "/" + label + " help");
}
}
@Override
public List<String> getTutorial() {
return Arrays.asList("Displays plugin information.");
}
@Override
public SubCommandType getType() {
return SubCommandType.GENERIC;
}
}