Basic Command System

- Implemented a basic command system.
 - Reimplemented the clear chat command.
This commit is contained in:
Ryandw11 2021-06-11 21:39:54 -07:00
parent b04124e416
commit b3f259e66a
7 changed files with 225 additions and 2 deletions

2
.gitignore vendored
View File

@ -27,3 +27,5 @@ bin/
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid* hs_err_pid*
/target

View File

@ -1,9 +1,18 @@
package me.ryandw11.ultrachat; package me.ryandw11.ultrachat;
import me.ryandw11.ultrachat.commands.ChatCommand;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.util.Objects;
/** /**
* The main class of UltraChat. * The main class of UltraChat.
*/ */
public class UltraChat extends JavaPlugin { public class UltraChat extends JavaPlugin {
@Override
public void onEnable() {
Objects.requireNonNull(getCommand("chat")).setExecutor(new ChatCommand(this));
getLogger().info("UltraChat v" + getDescription().getVersion() + " was successfully enabled.");
}
} }

View File

@ -0,0 +1,51 @@
package me.ryandw11.ultrachat.commands;
import me.ryandw11.ultrachat.UltraChat;
import me.ryandw11.ultrachat.commands.chat.ClearCommand;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
/**
* The main chat command for the plugin.
*/
public class ChatCommand implements CommandExecutor {
private final UltraChat plugin;
private final CommandHandler commandHandler;
public ChatCommand(UltraChat plugin) {
this.plugin = plugin;
this.commandHandler = new CommandHandler();
this.commandHandler.registerCommand(new ClearCommand(), "clear");
}
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String s, @NotNull String[] args) {
try {
if (args.length != 0)
return commandHandler.handleCommand(sender, cmd, s, args);
} catch (IllegalArgumentException ex) {
// Do nothing as this exception was triggered purposefully.
}
if (sender.hasPermission("ultrachat.command.help")) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
"&3=============[&2UltraChat&3]============="));
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&3Created by: &2Ryandw11"));
sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
"&3Version: &2" + plugin.getDescription().getVersion()));
sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
"&3Github wiki:&2 https://github.com/ryandw11/CustomStructures/wiki"));
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&3Commands:"));
for (SubCommand command : commandHandler.getSubCommands()) {
if (!sender.hasPermission(command.commandPermission())) continue;
sender.sendMessage(ChatColor.translateAlternateColorCodes('&',
command.helpMessage()));
}
}
return false;
}
}

View File

@ -0,0 +1,78 @@
package me.ryandw11.ultrachat.commands;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import java.util.*;
import java.util.stream.Collectors;
/**
* An extremely primitive command handler.
*/
public class CommandHandler {
private final Map<List<String>, SubCommand> commandMap;
public CommandHandler() {
commandMap = new HashMap<>();
}
/**
* Register a command.
*
* @param s The command to register.
* @param subCommand The sub command.
*/
public void registerCommand(String s, SubCommand subCommand) {
if (commandMap.containsKey(Collections.singletonList(s.toLowerCase())))
throw new IllegalArgumentException("Command already exists!");
commandMap.put(Collections.singletonList(s.toLowerCase()), subCommand);
}
/**
* Register a command.
*
* @param subCommand The sub command.
* @param args The aliases to register.
*/
public void registerCommand(SubCommand subCommand, String... args) {
List<String> list = new ArrayList<>(Arrays.asList(args));
list = list.stream().map(String::toLowerCase).collect(Collectors.toList());
if (commandMap.containsKey(list))
throw new IllegalArgumentException("Command already exists!");
commandMap.put(list, subCommand);
}
/**
* Handle a command.
*
* <p>Throws IllegalArgumentException if sub command does not exist.</p>
*
* @param sender The command sender.
* @param cmd The command.
* @param s The string.
* @param args The arguments.
* @return If the command is valid.
*/
public boolean handleCommand(CommandSender sender, Command cmd, String s, String[] args) {
if (args.length == 0)
return false;
for (Map.Entry<List<String>, SubCommand> entry : commandMap.entrySet()) {
if (entry.getKey().contains(args[0].toLowerCase())) {
String[] newArgs = new String[args.length - 1];
if (newArgs.length > 0) {
System.arraycopy(args, 1, newArgs, 0, newArgs.length);
}
return entry.getValue().subCommand(sender, cmd, s, newArgs);
}
}
throw new IllegalArgumentException("Invalid command");
}
public Collection<SubCommand> getSubCommands() {
return commandMap.values();
}
}

View File

@ -0,0 +1,30 @@
package me.ryandw11.ultrachat.commands;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
public interface SubCommand {
/**
* Get the help message for the command.
* @return The help message for the command.
*/
String helpMessage();
/**
* The permission for the command.
* @return The permission for the command.
*/
String commandPermission();
/**
* The sub command.
*
* @param sender The sender.
* @param cmd The command.
* @param s The command string.
* @param args The arguments. (Does not include the subcommand name. So args.length for `/chat color` is 0.)
* @return If the sub-command is valid.
*/
boolean subCommand(CommandSender sender, Command cmd, String s, String[] args);
}

View File

@ -0,0 +1,49 @@
package me.ryandw11.ultrachat.commands.chat;
import me.ryandw11.ultrachat.commands.SubCommand;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
/**
* This is the clear command for UltraChat.
* <p>/chat clear</p>
* <p>Permission:</p>
* <code>
* ultrachat.command.clear
* </code>
*/
public class ClearCommand implements SubCommand {
@Override
public String helpMessage() {
return "&2/chat clear &3- &bClear all messages from the chat.";
}
@Override
public String commandPermission() {
return "ultrachat.command.clear";
}
@Override
public boolean subCommand(CommandSender sender, Command cmd, String s, String[] args) {
if(!sender.hasPermission(commandPermission())) {
sender.sendMessage(ChatColor.RED + "You do not have permission for this command!");
return true;
}
for(int i = 0; i < 100; i++) {
for(Player player : Bukkit.getOnlinePlayers()) {
player.sendMessage(" ");
}
}
Bukkit.getServer().broadcastMessage(String.format("%s%s%s has cleared the chat!",
ChatColor.GOLD,
sender.getName(),
ChatColor.GREEN));
return false;
}
}

View File

@ -1,8 +1,12 @@
name: UltraChat name: UltraChat
version: 3.0.0 version: 3.0.0
main: me.ryandw11.ultrachat main: me.ryandw11.ultrachat.UltraChat
author: Ryandw11 author: Ryandw11
description: An advanced chat management plugin. description: An advanced chat management plugin.
depend: [Vault] depend: [Vault]
softdepend: [PlaceholderAPI] softdepend: [PlaceholderAPI]
api-version: 1.13 api-version: 1.13
commands:
chat:
description: The main command for Ultra Chat.
aliases: [uchat, ultrac, ultrachat]