[+] 添加控制台执行指令 /console 和修改指令注册
This commit is contained in:
parent
7af12650ea
commit
a3b81d4344
@ -22,6 +22,8 @@ public class MinecraftPlugin extends JavaPlugin {
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
nativebotJavaPlugin = this;
|
||||
|
||||
nativebot.onEnable();
|
||||
|
||||
getCommand("qq").setExecutor(cmdqq);
|
||||
@ -49,4 +51,6 @@ public class MinecraftPlugin extends JavaPlugin {
|
||||
chatting.onMinecraftMessage(evt);
|
||||
}
|
||||
|
||||
public static JavaPlugin nativebotJavaPlugin;
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,59 @@
|
||||
package ren.taske.nativebot.bot.command;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import cc.moecraft.icq.event.events.message.EventMessage;
|
||||
import cc.moecraft.icq.user.User;
|
||||
import ren.taske.nativebot.MinecraftPlugin;
|
||||
import ren.taske.nativebot.commons.Reference;
|
||||
|
||||
public class CommandConsole extends CommandBase {
|
||||
|
||||
public CommandConsole() {
|
||||
super("console", Reference.NODE_OP, "$");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String execute(EventMessage evt, User user, long userid, String command, ArrayList<String> args) {
|
||||
String message = "";
|
||||
String cmd = merge(args);
|
||||
Boolean flag = callSyncDispatch(Bukkit.getServer().getConsoleSender(), cmd);
|
||||
if(flag == null) {
|
||||
return "Exception!";
|
||||
}
|
||||
if(flag) {
|
||||
message = "Done!";
|
||||
} else {
|
||||
message = "Fail!";
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
public static String merge(ArrayList<String> strs) {
|
||||
String ret = "";
|
||||
for(String str : strs) {
|
||||
ret += str + " ";
|
||||
}
|
||||
return ret.trim();
|
||||
}
|
||||
|
||||
public static Boolean callSyncDispatch(CommandSender sender, String command) {
|
||||
try {
|
||||
return Bukkit.getScheduler().callSyncMethod(MinecraftPlugin.nativebotJavaPlugin, new Callable<Boolean>() {
|
||||
@Override
|
||||
public Boolean call() throws Exception {
|
||||
return Bukkit.dispatchCommand(Bukkit.getServer().getConsoleSender(), command);
|
||||
}
|
||||
}).get();
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -4,22 +4,18 @@ import java.util.logging.Logger;
|
||||
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import cc.moecraft.icq.command.interfaces.IcqCommand;
|
||||
import ren.taske.nativebot.bot.Bot;
|
||||
import ren.taske.nativebot.bot.command.CommandAbout;
|
||||
import ren.taske.nativebot.bot.command.CommandOperator;
|
||||
import ren.taske.nativebot.bot.command.CommandPermission;
|
||||
import ren.taske.nativebot.util.ClassUtils;
|
||||
|
||||
public class NativeBot {
|
||||
|
||||
protected static Logger logger;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static final Class<IcqCommand>[] COMMAND_CLASSES = new Class[] {
|
||||
CommandAbout.class,
|
||||
CommandOperator.class,
|
||||
CommandPermission.class
|
||||
private static final String[] COMMANDS = new String[] {
|
||||
"CommandAbout",
|
||||
"CommandOperator",
|
||||
"CommandPermission",
|
||||
"CommandConsole"
|
||||
};
|
||||
|
||||
protected final JavaPlugin plugin;
|
||||
@ -42,7 +38,7 @@ public class NativeBot {
|
||||
}
|
||||
|
||||
public void onEnable() {
|
||||
bot.register(ClassUtils.instantiate(COMMAND_CLASSES));
|
||||
bot.register(ClassUtils.instantiate(COMMANDS));
|
||||
bot.start();
|
||||
for(String cmd : bot.getCommands()) logger.info("[C] "+cmd);
|
||||
}
|
||||
@ -56,4 +52,18 @@ public class NativeBot {
|
||||
}
|
||||
}
|
||||
|
||||
public static void bigWarning(String str, Object...format) {
|
||||
logger.warning("***********************************************");
|
||||
logger.warning(String.format(str, format));
|
||||
logger.warning("***********************************************");
|
||||
}
|
||||
|
||||
public static void bigWarning(String[] strs, Object...format) {
|
||||
String s = "";
|
||||
for(String str : strs) {
|
||||
s += str + "\n";
|
||||
}
|
||||
bigWarning(s, format);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -9,13 +9,31 @@ import ren.taske.nativebot.core.NativeBot;
|
||||
|
||||
public class ClassUtils {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static List<IcqCommand> instantiate(Class<IcqCommand>...classes) {
|
||||
List<IcqCommand> ret = Lists.newArrayList();
|
||||
for(Class<IcqCommand> clazz : classes) {
|
||||
public static List<IcqCommand> instantiate(String...names) {
|
||||
List<Class<?>> classes = Lists.<Class<?>>newArrayList();
|
||||
for(String name : names) {
|
||||
Class<?> cls;
|
||||
try {
|
||||
IcqCommand cmd = clazz.newInstance();
|
||||
cls = Class.forName("ren.taske.nativebot.bot.command."+name);
|
||||
classes.add(cls);
|
||||
} catch (Exception e) {
|
||||
NativeBot.logger().warning("Unable to load "+name);
|
||||
}
|
||||
}
|
||||
return instantiate(classes);
|
||||
}
|
||||
|
||||
public static List<IcqCommand> instantiate(List<Class<?>> classes) {
|
||||
List<IcqCommand> ret = Lists.newArrayList();
|
||||
for(Class<?> clazz : classes) {
|
||||
try {
|
||||
Object obj = clazz.newInstance();
|
||||
if(obj instanceof IcqCommand) {
|
||||
IcqCommand cmd = (IcqCommand) obj;
|
||||
ret.add(cmd);
|
||||
} else {
|
||||
NativeBot.logger().warning(String.format("%s is NOT a IcqCommand", clazz.getName()));
|
||||
}
|
||||
} catch(Exception e) {
|
||||
NativeBot.logger().warning("Error when loading IcqCommand "+clazz.getName());
|
||||
NativeBot.logger().warning(e.getMessage());
|
||||
|
Loading…
Reference in New Issue
Block a user