New command system is getting ready

This commit is contained in:
Norbi Peti 2016-06-24 19:02:45 +02:00
parent b312f2358e
commit 2d7447a46f
5 changed files with 150 additions and 120 deletions

127
pom.xml
View file

@ -1,61 +1,68 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>TheButtonMCPlugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>The Button Minecraft Plugin</name>
<description>The Button Minecraft Plugin</description>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>classworlds:classworlds</exclude>
<exclude>junit:junit</exclude>
<exclude>jmock:*</exclude>
<exclude>*:xml-apis</exclude>
<exclude>org.apache.maven:lib:tests</exclude>
<exclude>log4j:log4j:jar:</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<groupId>tk.sznp</groupId>
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
</repositories>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>TheButtonMCPlugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>The Button Minecraft Plugin</name>
<description>The Button Minecraft Plugin</description>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>classworlds:classworlds</exclude>
<exclude>junit:junit</exclude>
<exclude>jmock:*</exclude>
<exclude>*:xml-apis</exclude>
<exclude>org.apache.maven:lib:tests</exclude>
<exclude>log4j:log4j:jar:</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<groupId>tk.sznp</groupId>
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.10</version>
</dependency>
</dependencies>
</project>

View file

@ -0,0 +1,76 @@
package io.github.norbipeti.thebuttonmcchat.commands;
import io.github.norbipeti.thebuttonmcchat.PluginMain;
import java.util.HashMap;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.reflections.Reflections;
public class CommandCaller implements CommandExecutor {
private CommandCaller() {
}
private static HashMap<String, TBMCCommandBase> commands = new HashMap<String, TBMCCommandBase>();
private static HashMap<String, TBMCSubCommandBase> subcommands = new HashMap<String, TBMCSubCommandBase>();
public static void RegisterCommands(PluginMain plugin) {
System.out.println("Registering commands...");
Reflections rf = new Reflections(
"io.github.norbipeti.thebuttonmcchat.commands");
Set<Class<? extends TBMCCommandBase>> cmds = rf
.getSubTypesOf(TBMCCommandBase.class);
for (Class<? extends TBMCCommandBase> cmd : cmds) {
try {
TBMCCommandBase c = cmd.newInstance();
commands.put(c.GetCommandName(), c);
plugin.getCommand(c.GetCommandName()).setExecutor(c);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
System.out.println("Registering subcommands...");
Set<Class<? extends TBMCSubCommandBase>> subcmds = rf
.getSubTypesOf(TBMCSubCommandBase.class);
for (Class<? extends TBMCSubCommandBase> subcmd : subcmds) {
try {
TBMCSubCommandBase sc = subcmd.newInstance();
subcommands.put(sc.GetCommandPath(), sc);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
System.out.println("Done registering");
}
@Override
public boolean onCommand(CommandSender sender, Command command,
String alias, String[] args) {
// TODO: Test if path exists, if not,
// go up one level, and
// finally fallback to args.length==0
String path = command.getName();
for (String arg : args)
path += "/" + arg;
TBMCSubCommandBase cmd = subcommands.get(path);
while (cmd == null && path.contains("/"))
path = path.substring(0, path.indexOf('/'));
if (cmd == null) {
sender.sendMessage("§cInternal error: Subcommand not registered to CommandCaller");
if (sender != Bukkit.getConsoleSender())
Bukkit.getConsoleSender()
.sendMessage(
"§cInternal error: Subcommand not registered to CommandCaller");
return true;
}
return true;
}
}

View file

@ -13,47 +13,9 @@ import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public abstract class TBMCCommandBase implements CommandExecutor {
public static void RegisterCommands(PluginMain plugin) {
TBMCCommandBase cmd = new UCommand();
plugin.getCommand(cmd.GetCommandName()).setExecutor(cmd);
cmd = new OOCCommand();
plugin.getCommand(cmd.GetCommandName()).setExecutor(cmd);
cmd = new UnlolCommand();
plugin.getCommand(cmd.GetCommandName()).setExecutor(cmd);
cmd = new MWikiCommand();
plugin.getCommand(cmd.GetCommandName()).setExecutor(cmd);
cmd = new TableflipCommand();
plugin.getCommand(cmd.GetCommandName()).setExecutor(cmd);
cmd = new UnflipCommand();
plugin.getCommand(cmd.GetCommandName()).setExecutor(cmd);
cmd = new ChatonlyCommand();
plugin.getCommand(cmd.GetCommandName()).setExecutor(cmd);
cmd = new ShrugCommand();
plugin.getCommand(cmd.GetCommandName()).setExecutor(cmd);
}
private static HashMap<String, TBMCCommandBase> commands = new HashMap<String, TBMCCommandBase>();
public static HashMap<String, TBMCCommandBase> GetCommands() {
return commands;
}
public abstract class TBMCCommandBase {
public TBMCCommandBase() {
commands.put(GetCommandName(), this);
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String alias,
String[] args) {
if (GetPlayerOnly() && !(sender instanceof Player)) {
sender.sendMessage("§cError: You must be a player to use this command.");
return true;
}
if (!OnCommand(sender, alias, args))
sender.sendMessage(GetHelpText(alias));
return true;
}
public abstract String[] GetHelpText(String alias);
@ -61,7 +23,7 @@ public abstract class TBMCCommandBase implements CommandExecutor {
public abstract boolean OnCommand(CommandSender sender, String alias,
String[] args);
public abstract String GetCommandName();
public abstract String GetCommandPath();
public abstract boolean GetPlayerOnly();
}

View file

@ -14,11 +14,7 @@ public final class UCommand extends UCommandBase {
@Override
public boolean OnUCommand(CommandSender sender, String alias, String[] args) {
if (args.length == 0)
return false; // TODO: Forward call to the correct handler
if (!TBMCCommandBase.GetCommands().containsKey(args[0]))
return false;
TBMCCommandBase cmd = TBMCCommandBase.GetCommands().get(args[0]); //Subcommand
return false;
}
@Override

View file

@ -5,32 +5,21 @@ import java.util.Arrays;
import org.bukkit.command.CommandSender;
import io.github.norbipeti.thebuttonmcchat.commands.TBMCCommandBase;
import io.github.norbipeti.thebuttonmcchat.commands.TBMCSubCommandBase;
public abstract class UCommandBase extends TBMCCommandBase {
public abstract String[] GetHelpText(String alias);
@Override
public boolean OnCommand(CommandSender sender, String alias, String[] args) {
if (args.length == 0)
return false;
return OnUCommand(sender, alias,
Arrays.copyOfRange(args, 1, args.length));
public String GetCommandPath() {
return "u/" + GetUCommandPath();
}
public abstract boolean OnUCommand(CommandSender sender, String alias,
String[] args);
@Override
public String GetCommandName() {
return "u";
}
public abstract String GetUCommandName(); // TODO: Help for /u commands
public abstract String GetUCommandPath(); // TODO: Help for /u commands
@Override
public boolean GetPlayerOnly() {
return true;
}
}