史诗级大饼,请叫我黑饼王。
This commit is contained in:
@@ -1,10 +1,23 @@
|
||||
package me.skymc.taboolib.commands.internal;
|
||||
|
||||
import me.skymc.taboolib.fileutils.FileUtils;
|
||||
import me.skymc.taboolib.listener.TListener;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.server.PluginEnableEvent;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-05-23 2:43
|
||||
*/
|
||||
public class TBaseCommand {
|
||||
@TListener(register = "onLoad")
|
||||
public class TBaseCommand implements Listener {
|
||||
|
||||
void onLoad() {
|
||||
registerCommands();
|
||||
}
|
||||
|
||||
/**
|
||||
* 向服务端注册 BaseMainCommand 类
|
||||
@@ -16,4 +29,43 @@ public class TBaseCommand {
|
||||
public static BaseMainCommand registerCommand(String command, BaseMainCommand baseMainCommand) {
|
||||
return BaseMainCommand.createCommandExecutor(command, baseMainCommand);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册所有插件的所有 TCommand 命令
|
||||
*/
|
||||
public static void registerCommands() {
|
||||
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
|
||||
try {
|
||||
registerCommand(plugin);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册插件的所有 TCommand 命令
|
||||
*
|
||||
* @param plugin 插件
|
||||
*/
|
||||
public static void registerCommand(Plugin plugin) {
|
||||
for (Class pluginClass : FileUtils.getClasses(plugin)) {
|
||||
if (BaseMainCommand.class.isAssignableFrom(pluginClass) && pluginClass.isAnnotationPresent(TCommand.class)) {
|
||||
TCommand tCommand = (TCommand) pluginClass.getAnnotation(TCommand.class);
|
||||
try {
|
||||
registerCommand(tCommand.name(), (BaseMainCommand) pluginClass.newInstance());
|
||||
} catch (InstantiationException | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onEnable(PluginEnableEvent e) {
|
||||
try {
|
||||
registerCommand(e.getPlugin());
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package me.skymc.taboolib.commands.internal;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-08-23 20:34
|
||||
*/
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface TCommand {
|
||||
|
||||
String name();
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user