权限系统和转发系统

1. 添加转发系统
2. 添加权限系统
3. 添加管理权限的命令 /perm
This commit is contained in:
Taskeren 2019-06-06 23:58:08 +08:00
parent 3df286ebbf
commit 1f6f293b58
11 changed files with 285 additions and 8 deletions

View File

@ -1,23 +1,46 @@
package ren.taske.nativebot; package ren.taske.nativebot;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import cc.moecraft.icq.event.events.message.EventMessage;
import ren.taske.nativebot.bot.chatting.Chatting;
import ren.taske.nativebot.bot.chatting.MinecraftMessage;
import ren.taske.nativebot.bot.chatting.TencentMessage;
import ren.taske.nativebot.core.NativeBot; import ren.taske.nativebot.core.NativeBot;
public class MinecraftPlugin extends JavaPlugin { public class MinecraftPlugin extends JavaPlugin {
protected final NativeBot core = new NativeBot(this); protected final NativeBot nativebot = new NativeBot(this);
protected final Chatting chatting = new Chatting(this);
protected final MinecraftMessage mcevent = new MinecraftMessage(this);
protected final TencentMessage tencentevent = new TencentMessage(this);
@Override @Override
public void onEnable() { public void onEnable() {
core.onEnable(); nativebot.onEnable();
nativebot.getBot().register(tencentevent);
getServer().getPluginManager().registerEvents(mcevent, this);
super.onEnable(); super.onEnable();
} }
@Override @Override
public void onDisable() { public void onDisable() {
core.onDisable(); nativebot.onDisable();
super.onDisable(); super.onDisable();
} }
public NativeBot getBot() {
return this.nativebot;
}
public void onTencentMessage(EventMessage evt) {
chatting.onTencentMessage(evt);
}
public void onMinecraftMessage(AsyncPlayerChatEvent evt) {
chatting.onMinecraftMessage(evt);
}
} }

View File

@ -7,6 +7,7 @@ import cc.moecraft.icq.PicqBotX;
import cc.moecraft.icq.PicqConfig; import cc.moecraft.icq.PicqConfig;
import cc.moecraft.icq.command.CommandManager; import cc.moecraft.icq.command.CommandManager;
import cc.moecraft.icq.command.interfaces.IcqCommand; import cc.moecraft.icq.command.interfaces.IcqCommand;
import cc.moecraft.icq.event.IcqListener;
import cc.moecraft.icq.sender.IcqHttpApi; import cc.moecraft.icq.sender.IcqHttpApi;
import cc.moecraft.logger.environments.ColorSupportLevel; import cc.moecraft.logger.environments.ColorSupportLevel;
import ren.taske.nativebot.commons.Config; import ren.taske.nativebot.commons.Config;
@ -49,6 +50,15 @@ public class Bot extends Thread {
register(cmds.toArray(new IcqCommand[0])); register(cmds.toArray(new IcqCommand[0]));
} }
/**
* Add event listener for bot
* @param listeners listeners
*/
public void register(IcqListener...listeners) {
bot.getEventManager().registerListeners(listeners);
}
/** /**
* To start the bot. <br> * To start the bot. <br>
* But wait! Did you {@code register} the commands? * But wait! Did you {@code register} the commands?

View File

@ -0,0 +1,109 @@
package ren.taske.nativebot.bot.chatting;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import cc.moecraft.icq.event.events.message.EventMessage;
import cc.moecraft.icq.user.User;
import cc.moecraft.icq.utils.CQUtils;
import ren.taske.nativebot.MinecraftPlugin;
import ren.taske.nativebot.commons.Config;
import ren.taske.nativebot.core.NativeBot;
import ren.taske.nativebot.core.profile.UserMinecraft;
import ren.taske.nativebot.core.profile.UserTencent;
import ren.taske.nativebot.util.MessageLib;
public class Chatting {
protected final MinecraftPlugin plugin;
protected final NativeBot nativebot;
public Chatting(MinecraftPlugin plugin) {
this.plugin = plugin;
this.nativebot = plugin.getBot();
}
public static final String NODE_CHATTING_TENCENT = "chatting.tencent";
public static final String NODE_CHATTING_MINECRAFT = "chatting.minecraft";
/**
* To handle messages from Tencent
* @param evt PicqBotX message event
*/
public void onTencentMessage(EventMessage evt) {
User user = evt.getSender();
long userid = user.getId();
UserTencent ut = UserTencent.of(userid);
String message = evt.getMessage();
String username = user.getInfo().getNickname();
if(!isPrefixed(message)) {
return;
}
if(!ut.hasPermission(NODE_CHATTING_TENCENT, true)) {
evt.respond(MessageLib.getUnauthorizedMessage(user));
return;
}
if(isPrefixed(message)) {
message = removePrefix(message);
message = CQUtils.removeCqCode(message);
StringBuffer sb = new StringBuffer();
sb.append("<").append(username).append("> ").append("\u00a7r");
sb.append(message);
plugin.getServer().broadcastMessage(sb.toString());
}
}
/**
* To handle messages from Minecraft
* @param evt Minecraft chatting event
*/
public void onMinecraftMessage(AsyncPlayerChatEvent evt) {
String message = evt.getMessage();
String username = evt.getPlayer().getName();
UserMinecraft um = UserMinecraft.of(username);
if(!isPrefixed(message)) {
return;
}
if(!um.hasPermission(NODE_CHATTING_MINECRAFT, true)) {
evt.getPlayer().sendMessage("You're Unauthorized!");
return;
}
if(isPrefixed(message)) {
message = removePrefix(message);
evt.setMessage(message);
StringBuffer sb = new StringBuffer();
sb.append("\uff3b").append(username).append("\uff3d ");
sb.append(message);
plugin.getBot().getBot().sendGroupMessage(Config.group_id, sb.toString());
}
}
public static boolean isPrefixed(String message) {
for(String prefix : Config.chatting_prefixes) {
if(message.startsWith(prefix)) return true;
}
return false;
}
public static String removePrefix(String message) {
return message.length() < 1 ? "" : message.substring(1);
}
}

View File

@ -0,0 +1,22 @@
package ren.taske.nativebot.bot.chatting;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import ren.taske.nativebot.MinecraftPlugin;
public class MinecraftMessage implements Listener {
protected final MinecraftPlugin plugin;
public MinecraftMessage(MinecraftPlugin plugin) {
this.plugin = plugin;
}
@EventHandler
public void message(AsyncPlayerChatEvent evt) {
plugin.onMinecraftMessage(evt);
}
}

View File

@ -0,0 +1,21 @@
package ren.taske.nativebot.bot.chatting;
import cc.moecraft.icq.event.EventHandler;
import cc.moecraft.icq.event.IcqListener;
import cc.moecraft.icq.event.events.message.EventMessage;
import ren.taske.nativebot.MinecraftPlugin;
public class TencentMessage extends IcqListener {
protected final MinecraftPlugin plugin;
public TencentMessage(MinecraftPlugin plugin) {
this.plugin = plugin;
}
@EventHandler
public void message(EventMessage evt) {
plugin.onTencentMessage(evt);
}
}

View File

@ -18,7 +18,9 @@ public class CommandAbout extends CommandBase {
"(command with * requires OP_PERMISSION_NODE)", "(command with * requires OP_PERMISSION_NODE)",
"/about[/bot|/help] - show this notice", "/about[/bot|/help] - show this notice",
"/op - query if you're operator", "/op - query if you're operator",
"/op* [userid] - set user as operator"); "/op* [uid] - set user as operator",
"/perm* [uid] [node] - get the value of the node",
"/perm* [uid] [node] [true/false] - set the value of the node");
} }
} }

View File

@ -0,0 +1,57 @@
package ren.taske.nativebot.bot.command;
import java.util.ArrayList;
import cc.moecraft.icq.event.events.message.EventMessage;
import cc.moecraft.icq.user.User;
import ren.taske.data.util.ParseUtil;
import ren.taske.nativebot.commons.Reference;
import ren.taske.nativebot.core.profile.UserTencent;
import ren.taske.nativebot.util.MessageUtils;
public class CommandPermission extends CommandBase {
public CommandPermission() {
super("perm", Reference.NODE_OP, "permission");
}
@Override
public String execute(EventMessage evt, User user, long userid, String command, ArrayList<String> args) {
String message = "";
if(args.size() < 2) {
message = "Wrong Arguments!";
}
if(args.size() >= 2) {
String username = args.get(0);
String nodename = args.get(1);
Long uid = ParseUtil.parseLong(username);
if(uid != null) {
UserTencent u = UserTencent.of(userid);
if(args.size() == 2) {
message = nodename + " = " + u.hasPermission(nodename);
}
if(args.size() > 2) {
boolean value = ParseUtil.parseBoolean(args.get(2));
u.setPermission(nodename, value);
StringBuffer sb = new StringBuffer();
sb.append(nodename).append("(").append(uid).append(")");
sb.append(" set to ").append(value);
message = sb.toString();
}
} else {
message = "NumberFormatException!";
}
}
return MessageUtils.retAt(userid, message);
}
}

View File

@ -17,6 +17,10 @@ public class Config {
public static String url_out; public static String url_out;
public static String[] prefixes; public static String[] prefixes;
// The Chatting Settings
public static long group_id;
public static String[] chatting_prefixes;
public static void refresh() { public static void refresh() {
port_in = cfg.getInt("in", "constructor", 25560, 0, 65535, "The port for receiving messages"); port_in = cfg.getInt("in", "constructor", 25560, 0, 65535, "The port for receiving messages");
@ -24,6 +28,9 @@ public class Config {
url_out = cfg.getString("url", "constructor", "127.0.0.1", "The url for sending message to HttpApi"); url_out = cfg.getString("url", "constructor", "127.0.0.1", "The url for sending message to HttpApi");
prefixes = cfg.getStringList("prefixes", "constructor", new String[] {"/"}, "The prefixes of commands in Tencent"); prefixes = cfg.getStringList("prefixes", "constructor", new String[] {"/"}, "The prefixes of commands in Tencent");
group_id = cfg.get("chatting", "group", "139971220").getLong(0L);
chatting_prefixes = cfg.getStringList("prefixes", "chatting", new String[] {"!", "\uff01"}, "The prefixes of chatting");
cfg.save(); cfg.save();
} }

View File

@ -8,6 +8,7 @@ import cc.moecraft.icq.command.interfaces.IcqCommand;
import ren.taske.nativebot.bot.Bot; import ren.taske.nativebot.bot.Bot;
import ren.taske.nativebot.bot.command.CommandAbout; import ren.taske.nativebot.bot.command.CommandAbout;
import ren.taske.nativebot.bot.command.CommandOperator; import ren.taske.nativebot.bot.command.CommandOperator;
import ren.taske.nativebot.bot.command.CommandPermission;
import ren.taske.nativebot.util.ClassUtils; import ren.taske.nativebot.util.ClassUtils;
public class NativeBot { public class NativeBot {
@ -17,13 +18,13 @@ public class NativeBot {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private static final Class<IcqCommand>[] COMMAND_CLASSES = new Class[] { private static final Class<IcqCommand>[] COMMAND_CLASSES = new Class[] {
CommandAbout.class, CommandAbout.class,
CommandOperator.class CommandOperator.class,
CommandPermission.class
}; };
protected final JavaPlugin plugin; protected final JavaPlugin plugin;
protected final Bot bot; protected final Bot bot;
public NativeBot(JavaPlugin plugin) { public NativeBot(JavaPlugin plugin) {
this.plugin = plugin; this.plugin = plugin;
this.bot = new Bot(); this.bot = new Bot();
@ -36,6 +37,10 @@ public class NativeBot {
return logger; return logger;
} }
public Bot getBot() {
return this.bot;
}
public void onEnable() { public void onEnable() {
bot.register(ClassUtils.instantiate(COMMAND_CLASSES)); bot.register(ClassUtils.instantiate(COMMAND_CLASSES));
bot.start(); bot.start();

View File

@ -36,7 +36,24 @@ public class UserMinecraft extends User {
@Override @Override
public void reload() { public void reload() {
data.save();
}
public boolean hasPermission(String node) {
return hasPermission(node, false);
}
public boolean hasPermission(String node, boolean defaultVal) {
if(node != null) {
data.setDefault(node, defaultVal);
data.save();
}
return data.getBoolean(node, false);
}
public void setPermission(String node, boolean val) {
data.setBoolean(node, val);
data.save();
} }
} }

View File

@ -40,8 +40,12 @@ public class UserTencent extends User {
} }
public boolean hasPermission(String node) { public boolean hasPermission(String node) {
return hasPermission(node, false);
}
public boolean hasPermission(String node, boolean defaultVal) {
if(node != null) { if(node != null) {
data.setDefault(node, false); data.setDefault(node, defaultVal);
data.save(); data.save();
} }
return data.getBoolean(node, false); return data.getBoolean(node, false);