Fixed Reflections exception and added AddCommand
With a beatiful piece of code.
This commit is contained in:
parent
16ec9a3788
commit
7131021a86
1 changed files with 38 additions and 6 deletions
|
@ -1,7 +1,9 @@
|
||||||
package buttondevteam.lib.chat;
|
package buttondevteam.lib.chat;
|
||||||
|
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
@ -11,6 +13,7 @@ import org.reflections.scanners.SubTypesScanner;
|
||||||
import org.reflections.util.ClasspathHelper;
|
import org.reflections.util.ClasspathHelper;
|
||||||
import org.reflections.util.ConfigurationBuilder;
|
import org.reflections.util.ConfigurationBuilder;
|
||||||
|
|
||||||
|
import buttondevteam.lib.TBMCCoreAPI;
|
||||||
import buttondevteam.lib.TBMCPlayer;
|
import buttondevteam.lib.TBMCPlayer;
|
||||||
|
|
||||||
public class TBMCChatAPI {
|
public class TBMCChatAPI {
|
||||||
|
@ -53,10 +56,10 @@ public class TBMCChatAPI {
|
||||||
*/
|
*/
|
||||||
public static void AddCommands(JavaPlugin plugin, Class<? extends TBMCCommandBase> acmdclass) {
|
public static void AddCommands(JavaPlugin plugin, Class<? extends TBMCCommandBase> acmdclass) {
|
||||||
plugin.getLogger().info("Registering commands for " + plugin.getName());
|
plugin.getLogger().info("Registering commands for " + plugin.getName());
|
||||||
Reflections rf = new Reflections(
|
Reflections rf = new Reflections(new ConfigurationBuilder()
|
||||||
new ConfigurationBuilder().setUrls(ClasspathHelper.forClassLoader(plugin.getClass().getClassLoader()))
|
.setUrls(ClasspathHelper.forPackage(acmdclass.getPackage().getName(),
|
||||||
.addClassLoader(plugin.getClass().getClassLoader()).addScanners(new SubTypesScanner())
|
plugin.getClass().getClassLoader()))
|
||||||
.filterInputsBy((String pkg) -> pkg.contains(acmdclass.getPackage().getName())));
|
.addClassLoader(plugin.getClass().getClassLoader()).addScanners(new SubTypesScanner()));
|
||||||
Set<Class<? extends TBMCCommandBase>> cmds = rf.getSubTypesOf(TBMCCommandBase.class);
|
Set<Class<? extends TBMCCommandBase>> cmds = rf.getSubTypesOf(TBMCCommandBase.class);
|
||||||
for (Class<? extends TBMCCommandBase> cmd : cmds) {
|
for (Class<? extends TBMCCommandBase> cmd : cmds) {
|
||||||
try {
|
try {
|
||||||
|
@ -66,13 +69,42 @@ public class TBMCChatAPI {
|
||||||
c.plugin = plugin;
|
c.plugin = plugin;
|
||||||
commands.put(c.GetCommandPath(), c);
|
commands.put(c.GetCommandPath(), c);
|
||||||
} catch (InstantiationException e) {
|
} catch (InstantiationException e) {
|
||||||
e.printStackTrace();
|
TBMCCoreAPI.SendException("An error occured while registering command " + cmd.getName(), e);
|
||||||
} catch (IllegalAccessException e) {
|
} catch (IllegalAccessException e) {
|
||||||
e.printStackTrace();
|
TBMCCoreAPI.SendException("An error occured while registering command " + cmd.getName(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* This method adds a plugin's command to help and sets it's 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 thecmdclass
|
||||||
|
* The command's class to create it (because why let you create the command class)
|
||||||
|
*/
|
||||||
|
public static void AddCommand(JavaPlugin plugin, Class<? extends TBMCCommandBase> thecmdclass, Object... params) {
|
||||||
|
plugin.getLogger().info("Registering command " + thecmdclass.getName() + " for " + plugin.getName());
|
||||||
|
try {
|
||||||
|
TBMCCommandBase c = thecmdclass
|
||||||
|
.getConstructor(Arrays.stream(params).map(p -> p.getClass()).toArray(Class[]::new))
|
||||||
|
.newInstance(params);
|
||||||
|
c.plugin = plugin;
|
||||||
|
commands.put(c.GetCommandPath(), c);
|
||||||
|
} catch (Exception e) {
|
||||||
|
TBMCCoreAPI.SendException("An error occured while registering command " + thecmdclass.getName(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
* Add player information for {@link PlayerInfoCommand}. Only mods can see the given information.
|
* Add player information for {@link PlayerInfoCommand}. Only mods can see the given information.
|
||||||
|
|
Loading…
Reference in a new issue