Moved command stuff to Lib to avoid depedency loops

This commit is contained in:
Norbi Peti 2016-10-28 21:23:39 +02:00
parent 435ebaac98
commit b0c145af96
3 changed files with 116 additions and 0 deletions

View file

@ -92,6 +92,12 @@
<artifactId>Towny</artifactId>
<version>master-SNAPSHOT</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.reflections/reflections -->
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.10</version>
</dependency>
</dependencies>
<organization>
<name>TBMCPlugins</name>

View file

@ -0,0 +1,84 @@
package buttondevteam.lib.chat;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Set;
import org.bukkit.plugin.java.JavaPlugin;
import org.reflections.Reflections;
import org.reflections.scanners.SubTypesScanner;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;
import buttondevteam.lib.TBMCPlayer;
public class TBMCChatAPI {
private static HashMap<String, TBMCCommandBase> commands = new HashMap<String, TBMCCommandBase>();
public static HashMap<String, TBMCCommandBase> GetCommands() {
return commands;
}
/**
* <p>
* This method adds a plugin's commands to help and sets their executor.
* </p>
* <p>
* The <u>command must be registered</u> in the caller plugin's plugin.yml. Otherwise the plugin will output a messsage to console.
* </p>
* <p>
* <i>Using this method after the server is done loading will have no effect.</i>
* </p>
*
* @param plugin
* The caller plugin
* @param acmdclass
* A command's class to get the package name for commands. The provided class's package and subpackages are scanned for commands.
*/
public static void AddCommands(JavaPlugin plugin, Class<? extends TBMCCommandBase> acmdclass) {
plugin.getLogger().info("Registering commands for " + plugin.getName());
Reflections rf = new Reflections(
new ConfigurationBuilder().setUrls(ClasspathHelper.forClassLoader(plugin.getClass().getClassLoader()))
.addClassLoader(plugin.getClass().getClassLoader()).addScanners(new SubTypesScanner())
.filterInputsBy((String pkg) -> pkg.contains(acmdclass.getPackage().getName())));
Set<Class<? extends TBMCCommandBase>> cmds = rf.getSubTypesOf(TBMCCommandBase.class);
for (Class<? extends TBMCCommandBase> cmd : cmds) {
try {
if (Modifier.isAbstract(cmd.getModifiers()))
continue;
TBMCCommandBase c = cmd.newInstance();
c.plugin = plugin;
commands.put(c.GetCommandPath(), c);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
/**
* <p>
* Add player information for {@link PlayerInfoCommand}. Only mods can see the given information.
* </p>
*
* @param player
* @param infoline
*/
public void AddPlayerInfoForMods(TBMCPlayer player, String infoline) {
// TODO
}
/**
* <p>
* Add player information for hover text at {@link ChatProcessing}. Every online player can see the given information.
* </p>
*
* @param player
* @param infoline
*/
public void AddPlayerInfoForHover(TBMCPlayer player, String infoline) {
// TODO
}
}

View file

@ -0,0 +1,26 @@
package buttondevteam.lib.chat;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
public abstract class TBMCCommandBase {
public TBMCCommandBase() {
}
public abstract String[] GetHelpText(String alias);
public abstract boolean OnCommand(CommandSender sender, String alias, String[] args);
public abstract String GetCommandPath();
public abstract boolean GetPlayerOnly();
public abstract boolean GetModOnly();
Plugin plugin; // Used By TBMCChatAPI
public Plugin getPlugin() { // Used by CommandCaller (ButtonChat)
return plugin;
}
}