Moved CommandCaller and org.reflections here

This commit is contained in:
Norbi Peti 2016-12-09 18:11:07 +01:00
parent 9661cd3405
commit d5c9882009
2 changed files with 126 additions and 8 deletions

35
pom.xml
View file

@ -36,12 +36,31 @@
<target>1.8</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> <includes>
<include>com.github.TBMCPlugins.ButtonLib:ButtonLib</include> </includes>
</artifactSet> <pluginExecution> <action> <execute /> </action> </pluginExecution>
</configuration> </execution> </executions> </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>
<includes>
<include>org.reflections:reflections</include>
</includes>
</artifactSet>
<pluginExecution>
<action>
<execute />
</action>
</pluginExecution>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
@ -107,7 +126,7 @@
<dependency>
<groupId>com.github.milkbowl</groupId> <!-- net.milkbowl.vault -->
<artifactId>VaultAPI</artifactId>
<version>master-SNAPSHOT</version> <!-- 1.6 -->
<version>master-SNAPSHOT</version> <!-- 1.6 -->
<scope>provided</scope>
</dependency>
</dependencies>
@ -125,7 +144,7 @@
<properties>
<!-- github server corresponds to entry in ~/.m2/settings.xml -->
<github.global.server>github</github.global.server>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<scm>
<url>https://github.com/TBMCPlugins/mvn-repo</url>

View file

@ -0,0 +1,99 @@
package buttondevteam.core;
import java.util.Arrays;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.chat.TBMCChatAPI;
import buttondevteam.lib.chat.TBMCCommandBase;
public class CommandCaller implements CommandExecutor {
private static final String REGISTER_ERROR_MSG = "An error occured while registering commands";
private CommandCaller() {
}
private static CommandCaller instance;
public static void RegisterCommand(TBMCCommandBase cmd) {
if (instance == null)
instance = new CommandCaller();
if (cmd.GetCommandPath() == null) {
TBMCCoreAPI.SendException(REGISTER_ERROR_MSG,
new Exception("Command " + cmd.getClass().getSimpleName() + " has no command path!"));
return;
}
if (cmd.getPlugin() == null) {
TBMCCoreAPI.SendException(REGISTER_ERROR_MSG,
new Exception("Command " + cmd.GetCommandPath() + " has no plugin!"));
return;
}
int i;
String topcmd;
if ((i = (topcmd = cmd.GetCommandPath()).indexOf(' ')) != -1) // Get top-level command
topcmd = cmd.GetCommandPath().substring(0, i);
{
PluginCommand pc = ((JavaPlugin) cmd.getPlugin()).getCommand(topcmd);
if (pc == null)
TBMCCoreAPI.SendException(REGISTER_ERROR_MSG, new Exception("Top level command " + topcmd
+ " not registered in plugin.yml for plugin: " + cmd.getPlugin().getName()));
else
pc.setExecutor(instance);
}
}
@Override
public boolean onCommand(CommandSender sender, Command command, String alias, String[] args) {
String path = command.getName().toLowerCase();
for (String arg : args)
path += " " + arg;
TBMCCommandBase cmd = TBMCChatAPI.GetCommands().get(path);
int argc = 0;
while (cmd == null && path.contains(" ")) {
path = path.substring(0, path.lastIndexOf(' '));
argc++;
cmd = TBMCChatAPI.GetCommands().get(path);
}
if (cmd == null) {
String[] subcmds = TBMCChatAPI.GetSubCommands(path, sender);
if (subcmds.length > 0)
sender.sendMessage(subcmds);
else {
final String errormsg = "§cYou don't have access to any of this command's subcommands.";
sender.sendMessage(errormsg);
if (!(sender instanceof ConsoleCommandSender))
Bukkit.getConsoleSender().sendMessage(errormsg);
}
return true;
}
if (cmd.GetModOnly() && !MainPlugin.permission.has(sender, "tbmc.admin")) {
sender.sendMessage("§cYou need to be a mod to use this command.");
return true;
}
if (cmd.GetPlayerOnly() && !(sender instanceof Player)) {
sender.sendMessage("§cOnly ingame players can use this command.");
return true;
}
final String[] cmdargs = args.length > 0 ? Arrays.copyOfRange(args, args.length - argc, args.length) : args;
try {
if (!cmd.OnCommand(sender, alias, cmdargs)) {
if (cmd.GetHelpText(alias) == null) {
sender.sendMessage("Wrong usage, but there's no help text! Error is being reported to devs.");
throw new NullPointerException("GetHelpText is null for comand /" + cmd.GetCommandPath());
} else
sender.sendMessage(cmd.GetHelpText(alias));
}
} catch (Exception e) {
TBMCCoreAPI.SendException("Failed to execute command /" + cmd.GetCommandPath() + " with arguments "
+ Arrays.toString(cmdargs), e);
}
return true;
}
}