Command system finished with #21!

This commit is contained in:
Norbi Peti 2016-06-25 10:48:24 +02:00
parent 718f43e303
commit cc574c3450
9 changed files with 59 additions and 21 deletions

View file

@ -246,7 +246,7 @@ public class PlayerListener implements Listener {
@EventHandler
public void onPlayerChat(AsyncPlayerChatEvent event) {
if (event.isCancelled()) // TODO: Change FactionChat to /tellraw
if (event.isCancelled())
return;
event.setCancelled(ChatProcessing.ProcessChat(event.getPlayer(),
event.getMessage()));

View file

@ -3,6 +3,7 @@ package io.github.norbipeti.thebuttonmcchat.commands;
import io.github.norbipeti.thebuttonmcchat.PluginMain;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Set;
@ -87,7 +88,7 @@ public class CommandCaller implements CommandExecutor {
TBMCCommandBase cmd = commands.get(path);
int argc = 0;
while (cmd == null && path.contains("/")) {
path = path.substring(0, path.indexOf('/'));
path = path.substring(0, path.lastIndexOf('/'));
argc++;
cmd = commands.get(path);
System.out.println(path);
@ -100,12 +101,31 @@ public class CommandCaller implements CommandExecutor {
"§cInternal error: Command not registered to CommandCaller");
return true;
}
if (cmd.GetPlayerOnly() && sender == Bukkit.getConsoleSender()) {
sender.sendMessage("§cOnly ingame players can use this command.");
return true;
}
if (!cmd.OnCommand(
sender,
alias,
(args.length > 0 ? Arrays.copyOfRange(args, argc,
args.length - 1) : args)))
(args.length > 0 ? Arrays.copyOfRange(args, args.length - argc,
args.length) : args)))
sender.sendMessage(cmd.GetHelpText(alias));
return true;
}
public static String[] GetSubCommands(TBMCCommandBase command) {
ArrayList<String> cmds = new ArrayList<String>();
cmds.add("§6---- Subcommands ----");
for (TBMCCommandBase cmd : CommandCaller.GetCommands().values()) {
if (cmd.GetCommandPath().startsWith(command.GetCommandPath() + "/")) {
int ind = cmd.GetCommandPath().indexOf('/',
command.GetCommandPath().length() + 2);
if (ind >= 0)
continue;
cmds.add(cmd.GetCommandPath().replace('/', ' '));
}
}
return cmds.toArray(new String[cmds.size()]);
}
}

View file

@ -21,7 +21,7 @@ public final class HelpCommand extends UCommandBase {
sender.sendMessage(new String[] {
"§6---- TBMC Help ----",
"Do /u help <topic> for more info",
"Alternatively, you can do /u help <commandname> [subcommands] for more info about a command",
"Do /u help <commandname> [subcommands] for more info about a command",
"Topics:",
"flairs: The flairs are the numbers near your name",
"commands: See all the commands from this plugin",
@ -32,12 +32,11 @@ public final class HelpCommand extends UCommandBase {
sender.sendMessage(new String[] { "§6---- About flairs ----", "" }); // TODO
else if (args[0].equalsIgnoreCase("commands")) {
ArrayList<String> text = new ArrayList<String>();
int i = 0;
text.set(i++, "§6---- Command list ----");
text.add("§6---- Command list ----");
for (TBMCCommandBase cmd : CommandCaller.GetCommands().values())
if (!cmd.GetCommandPath().contains("/"))
text.set(i++, "/" + cmd.GetCommandPath());
sender.sendMessage((String[]) text.toArray());
text.add("/" + cmd.GetCommandPath());
sender.sendMessage(text.toArray(new String[text.size()]));
} else {
String path = args[0];
for (int i = 1; i < args.length; i++)
@ -57,4 +56,9 @@ public final class HelpCommand extends UCommandBase {
public String GetUCommandPath() {
return "help";
}
@Override
public boolean GetPlayerOnly() {
return false;
}
}

View file

@ -1,5 +1,8 @@
package io.github.norbipeti.thebuttonmcchat.commands.ucmds;
import java.util.ArrayList;
import io.github.norbipeti.thebuttonmcchat.commands.CommandCaller;
import io.github.norbipeti.thebuttonmcchat.commands.TBMCCommandBase;
import org.bukkit.command.CommandSender;
@ -8,8 +11,7 @@ public final class UCommand extends TBMCCommandBase {
@Override
public String[] GetHelpText(String alias) {
return new String[] { "§6---- U commands ----",
"Subcommands: help, accept, ignore, admin" }; // TODO
return CommandCaller.GetSubCommands(this);
}
@Override

View file

@ -8,10 +8,10 @@ public abstract class UCommandBase extends TBMCCommandBase {
@Override
public String GetCommandPath() {
return "u/" + GetUCommandPath(); //TODO: This for others
return "u/" + GetUCommandPath();
}
public abstract String GetUCommandPath(); // TODO: Help for /u commands
public abstract String GetUCommandPath();
@Override
public boolean GetPlayerOnly() {

View file

@ -1,5 +1,6 @@
package io.github.norbipeti.thebuttonmcchat.commands.ucmds.admin;
import io.github.norbipeti.thebuttonmcchat.commands.CommandCaller;
import io.github.norbipeti.thebuttonmcchat.commands.ucmds.UCommandBase;
import org.bukkit.command.CommandSender;
@ -8,13 +9,11 @@ public final class AdminCommand extends UCommandBase {
@Override
public String[] GetHelpText(String alias) {
return new String[] { "§6---- Admin ----",
"These commands are for mods only.", "Subcommands: reload, " }; // TODO
return CommandCaller.GetSubCommands(this);
}
@Override
public boolean OnCommand(CommandSender sender, String alias,
String[] args) {
public boolean OnCommand(CommandSender sender, String alias, String[] args) {
return false;
}
@ -22,4 +21,9 @@ public final class AdminCommand extends UCommandBase {
public String GetUCommandPath() {
return "admin";
}
@Override
public boolean GetPlayerOnly() {
return false;
}
}

View file

@ -4,7 +4,7 @@ import io.github.norbipeti.thebuttonmcchat.commands.ucmds.UCommandBase;
public abstract class AdminCommandBase extends UCommandBase {
public abstract String[] GetHelpText(String alias);
public abstract String[] GetHelpText(String alias); //TODO: Require permissionű
@Override
public String GetUCommandPath() {

View file

@ -1,5 +1,6 @@
package io.github.norbipeti.thebuttonmcchat.commands.ucmds.announce;
import io.github.norbipeti.thebuttonmcchat.commands.CommandCaller;
import io.github.norbipeti.thebuttonmcchat.commands.ucmds.UCommandBase;
import org.bukkit.command.CommandSender;
@ -8,8 +9,7 @@ public class AnnounceCommand extends UCommandBase {
@Override
public String[] GetHelpText(String alias) {
return new String[] { "§6---- Announce ----",
"Subcommands: add, settime, remove, list, edit" }; // TODO
return CommandCaller.GetSubCommands(this);
}
@Override
@ -22,4 +22,8 @@ public class AnnounceCommand extends UCommandBase {
return "announce";
}
@Override
public boolean GetPlayerOnly() {
return false;
}
}

View file

@ -8,9 +8,13 @@ public abstract class AnnounceCommandBase extends UCommandBase {
@Override
public String GetUCommandPath() {
return "announce";
return "announce/" + GetAnnounceCommandPath();
}
public abstract String GetAnnounceCommandPath();
@Override
public boolean GetPlayerOnly() {
return false;
}
}