重写权限系统和完善权限管理
1. 添加 /qq 绑定QQ号 2. 输出信息时要求绑定qq
This commit is contained in:
		@@ -8,6 +8,7 @@ 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.minecraft.command.CommandQQ;
 | 
			
		||||
 | 
			
		||||
public class MinecraftPlugin extends JavaPlugin {
 | 
			
		||||
 | 
			
		||||
@@ -17,9 +18,14 @@ public class MinecraftPlugin extends JavaPlugin {
 | 
			
		||||
	protected final MinecraftMessage mcevent = new MinecraftMessage(this);
 | 
			
		||||
	protected final TencentMessage tencentevent = new TencentMessage(this);
 | 
			
		||||
	
 | 
			
		||||
	protected final CommandQQ cmdqq = new CommandQQ(this);
 | 
			
		||||
	
 | 
			
		||||
	@Override
 | 
			
		||||
	public void onEnable() {
 | 
			
		||||
		nativebot.onEnable();
 | 
			
		||||
		
 | 
			
		||||
		getCommand("qq").setExecutor(cmdqq);
 | 
			
		||||
		
 | 
			
		||||
		nativebot.getBot().register(tencentevent);
 | 
			
		||||
		getServer().getPluginManager().registerEvents(mcevent, this);
 | 
			
		||||
		super.onEnable();
 | 
			
		||||
 
 | 
			
		||||
@@ -10,6 +10,8 @@ import cc.moecraft.icq.command.interfaces.IcqCommand;
 | 
			
		||||
import cc.moecraft.icq.event.IcqListener;
 | 
			
		||||
import cc.moecraft.icq.sender.IcqHttpApi;
 | 
			
		||||
import cc.moecraft.logger.environments.ColorSupportLevel;
 | 
			
		||||
import ren.taske.nativebot.bot.permission.Permission;
 | 
			
		||||
import ren.taske.nativebot.bot.permission.PermissionManager;
 | 
			
		||||
import ren.taske.nativebot.commons.Config;
 | 
			
		||||
 | 
			
		||||
public class Bot extends Thread {
 | 
			
		||||
@@ -64,6 +66,8 @@ public class Bot extends Thread {
 | 
			
		||||
	 * But wait! Did you {@code register} the commands?
 | 
			
		||||
	 */
 | 
			
		||||
	public void run() {
 | 
			
		||||
		PermissionManager.init();
 | 
			
		||||
		Permission.lock();
 | 
			
		||||
		bot.startBot();
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
 
 | 
			
		||||
@@ -40,7 +40,7 @@ public class Chatting {
 | 
			
		||||
			return;
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
		if(!ut.hasPermission(NODE_CHATTING_TENCENT, true)) {
 | 
			
		||||
		if(!ut.hasPermission(NODE_CHATTING_TENCENT)) {
 | 
			
		||||
			evt.respond(MessageLib.getUnauthorizedMessage(user));
 | 
			
		||||
			return;
 | 
			
		||||
		}
 | 
			
		||||
@@ -74,7 +74,14 @@ public class Chatting {
 | 
			
		||||
			return;
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
		if(!um.hasPermission(NODE_CHATTING_MINECRAFT, true)) {
 | 
			
		||||
		if(um.getTencentId() == -1L) {
 | 
			
		||||
			evt.getPlayer().sendMessage("Please set your Tencent userid first!");
 | 
			
		||||
			return;
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
		UserTencent ut = UserTencent.of(um.getTencentId());
 | 
			
		||||
		
 | 
			
		||||
		if(!ut.hasPermission(NODE_CHATTING_MINECRAFT)) {
 | 
			
		||||
			evt.getPlayer().sendMessage("You're Unauthorized!");
 | 
			
		||||
			return;
 | 
			
		||||
		}
 | 
			
		||||
 
 | 
			
		||||
@@ -29,10 +29,10 @@ public abstract class CommandBase implements EverywhereCommand {
 | 
			
		||||
	@Override
 | 
			
		||||
	public String run(EventMessage event, User sender, String command, ArrayList<String> args) {
 | 
			
		||||
		long userid = sender.getId();
 | 
			
		||||
		if(node == null || node.equals("") || UserTencent.of(userid).hasPermission(node) || UserTencent.of(userid).hasPermission(Reference.NODE_OP)) {
 | 
			
		||||
		if(node == null || node.equals("") || UserTencent.of(userid).hasPermission(Reference.NODE_OP) || UserTencent.of(userid).hasPermission(node)) {
 | 
			
		||||
			return execute(event, sender, userid, command, args);
 | 
			
		||||
		} else {
 | 
			
		||||
			return MessageLib.getUnauthorizedMessage(sender);
 | 
			
		||||
			return MessageLib.getUnauthorizedMessage(sender)+"CB";
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
 
 | 
			
		||||
@@ -5,6 +5,7 @@ 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.bot.permission.PermissionManager;
 | 
			
		||||
import ren.taske.nativebot.commons.Reference;
 | 
			
		||||
import ren.taske.nativebot.core.profile.UserTencent;
 | 
			
		||||
import ren.taske.nativebot.util.MessageUtils;
 | 
			
		||||
@@ -32,7 +33,12 @@ public class CommandPermission extends CommandBase {
 | 
			
		||||
				UserTencent u = UserTencent.of(userid);
 | 
			
		||||
				
 | 
			
		||||
				if(args.size() == 2) {
 | 
			
		||||
					boolean exists = PermissionManager.has(nodename);
 | 
			
		||||
					if(exists) {
 | 
			
		||||
						message = nodename + " = " + u.hasPermission(nodename);
 | 
			
		||||
					} else {
 | 
			
		||||
						message = nodename + " is NOT registered!";
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
				
 | 
			
		||||
				if(args.size() > 2) {
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,50 @@
 | 
			
		||||
package ren.taske.nativebot.bot.permission;
 | 
			
		||||
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
 | 
			
		||||
import ren.taske.nativebot.core.NativeBot;
 | 
			
		||||
 | 
			
		||||
public class Permission {
 | 
			
		||||
 | 
			
		||||
	private static final HashMap<String, Permission> PERMS = new HashMap<>();
 | 
			
		||||
	
 | 
			
		||||
	private static boolean lock;
 | 
			
		||||
	
 | 
			
		||||
	protected final String name;
 | 
			
		||||
	protected final boolean def;
 | 
			
		||||
	
 | 
			
		||||
	private Permission(String name) {
 | 
			
		||||
		this(name, false);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	private Permission(String name, boolean def) {
 | 
			
		||||
		this.name = name;
 | 
			
		||||
		this.def  = def;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public static Permission of(String name) {
 | 
			
		||||
		return of(name, false);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public static Permission of(String name, boolean def) {
 | 
			
		||||
		if(!PERMS.containsKey(name)) {
 | 
			
		||||
			if(lock) return null;
 | 
			
		||||
			PERMS.put(name, new Permission(name, def));
 | 
			
		||||
			NativeBot.logger().info("Register Permission Node "+name + " (def: " + def + ")");
 | 
			
		||||
		}
 | 
			
		||||
		return PERMS.get(name);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public static void lock() {
 | 
			
		||||
		lock = true;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public String getName() {
 | 
			
		||||
		return this.name;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public boolean getDefault() {
 | 
			
		||||
		return this.def;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,30 @@
 | 
			
		||||
package ren.taske.nativebot.bot.permission;
 | 
			
		||||
 | 
			
		||||
import ren.taske.nativebot.bot.chatting.Chatting;
 | 
			
		||||
import ren.taske.nativebot.commons.Reference;
 | 
			
		||||
 | 
			
		||||
public class PermissionManager {
 | 
			
		||||
 | 
			
		||||
	public static boolean add(String node) {
 | 
			
		||||
		return add(node, false);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public static boolean add(String node, boolean def) {
 | 
			
		||||
		return !(Permission.of(node, def) == null);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public static boolean has(String node) {
 | 
			
		||||
		return !(Permission.of(node) == null);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	// Add defaults
 | 
			
		||||
	public static void init() {
 | 
			
		||||
		
 | 
			
		||||
		add(Reference.NODE_OP);
 | 
			
		||||
		
 | 
			
		||||
		add(Chatting.NODE_CHATTING_TENCENT, true);
 | 
			
		||||
		add(Chatting.NODE_CHATTING_MINECRAFT, true);
 | 
			
		||||
		
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
}
 | 
			
		||||
@@ -47,8 +47,13 @@ public class NativeBot {
 | 
			
		||||
		for(String cmd : bot.getCommands()) logger.info("[C] "+cmd);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	@SuppressWarnings("deprecation")
 | 
			
		||||
	public void onDisable() {
 | 
			
		||||
		
 | 
			
		||||
		try {
 | 
			
		||||
			bot.stop();
 | 
			
		||||
		} catch(Exception e) {
 | 
			
		||||
			logger.warning(e.getMessage());
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -56,4 +56,17 @@ public class UserMinecraft extends User {
 | 
			
		||||
		data.save();
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public static final String _TENCENT_UID = "tencent.uid";
 | 
			
		||||
	
 | 
			
		||||
	public void setTencentId(long uid) {
 | 
			
		||||
		data.setLong(_TENCENT_UID, uid);
 | 
			
		||||
		data.save();
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public long getTencentId() {
 | 
			
		||||
		data.setDefault(_TENCENT_UID, -1L);
 | 
			
		||||
		data.save();
 | 
			
		||||
		return data.getLong(_TENCENT_UID, -1L);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -3,6 +3,7 @@ package ren.taske.nativebot.core.profile;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
 | 
			
		||||
import ren.taske.data.SimpleDataStorage;
 | 
			
		||||
import ren.taske.nativebot.bot.permission.Permission;
 | 
			
		||||
import ren.taske.nativebot.commons.Reference;
 | 
			
		||||
 | 
			
		||||
public class UserTencent extends User {
 | 
			
		||||
@@ -11,11 +12,22 @@ public class UserTencent extends User {
 | 
			
		||||
	
 | 
			
		||||
	protected final SimpleDataStorage data;
 | 
			
		||||
	
 | 
			
		||||
	public static final UserTencent NONE = new UserTencent(-1L) {
 | 
			
		||||
		@Override
 | 
			
		||||
		public boolean hasPermission(String node) {
 | 
			
		||||
			return false;
 | 
			
		||||
		}
 | 
			
		||||
	};
 | 
			
		||||
	
 | 
			
		||||
	private UserTencent(long userid) {
 | 
			
		||||
		super(Long.toString(userid));
 | 
			
		||||
		data = new SimpleDataStorage(Reference.getTencentProfile(userid));
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	static {
 | 
			
		||||
		PROFILES.put(-1L, NONE);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public static UserTencent of(long userid) {
 | 
			
		||||
		if(!PROFILES.containsKey(userid)) {
 | 
			
		||||
			UserTencent user = new UserTencent(userid);
 | 
			
		||||
@@ -40,16 +52,14 @@ public class UserTencent extends User {
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public boolean hasPermission(String node) {
 | 
			
		||||
		return hasPermission(node, false);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public boolean hasPermission(String node, boolean defaultVal) {
 | 
			
		||||
		if(node != null) {
 | 
			
		||||
			data.setDefault(node, defaultVal);
 | 
			
		||||
		Permission perm = Permission.of(node);
 | 
			
		||||
		if(perm != null) {
 | 
			
		||||
			data.setDefault(node, perm.getDefault());
 | 
			
		||||
			data.save();
 | 
			
		||||
		}
 | 
			
		||||
			return data.getBoolean(node, false);
 | 
			
		||||
		}
 | 
			
		||||
		return false;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public void setPermission(String node, boolean val) {
 | 
			
		||||
		data.setBoolean(node, val);
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,58 @@
 | 
			
		||||
package ren.taske.nativebot.minecraft.command;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.ChatColor;
 | 
			
		||||
import org.bukkit.command.Command;
 | 
			
		||||
import org.bukkit.command.CommandExecutor;
 | 
			
		||||
import org.bukkit.command.CommandSender;
 | 
			
		||||
 | 
			
		||||
import ren.taske.data.util.ParseUtil;
 | 
			
		||||
import ren.taske.nativebot.MinecraftPlugin;
 | 
			
		||||
import ren.taske.nativebot.commons.Reference;
 | 
			
		||||
import ren.taske.nativebot.core.profile.UserMinecraft;
 | 
			
		||||
import ren.taske.nativebot.core.profile.UserTencent;
 | 
			
		||||
 | 
			
		||||
public class CommandQQ implements CommandExecutor {
 | 
			
		||||
 | 
			
		||||
	protected final MinecraftPlugin plugin;
 | 
			
		||||
	
 | 
			
		||||
	public CommandQQ(MinecraftPlugin plugin) {
 | 
			
		||||
		this.plugin = plugin;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	@Override
 | 
			
		||||
	public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
 | 
			
		||||
		
 | 
			
		||||
		if(args.length == 1) {
 | 
			
		||||
			UserMinecraft um = UserMinecraft.of(sender.getName());
 | 
			
		||||
			Long qqid = ParseUtil.parseLong(args[0]);
 | 
			
		||||
			if(qqid != null) {
 | 
			
		||||
				if(um.getTencentId() == -1L) {
 | 
			
		||||
					um.setTencentId(qqid);
 | 
			
		||||
					sender.sendMessage("Your Tencent userid now is " + qqid);
 | 
			
		||||
				} else {
 | 
			
		||||
					sender.sendMessage("You have set your Tencent userid!");
 | 
			
		||||
				}
 | 
			
		||||
				return true;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
		if(args.length == 2) {
 | 
			
		||||
			UserMinecraft um = UserMinecraft.of(args[1]);
 | 
			
		||||
			UserTencent ut = UserTencent.of(um.getTencentId());
 | 
			
		||||
			Long qqid = ParseUtil.parseLong(args[0]);
 | 
			
		||||
			
 | 
			
		||||
			if(ut.hasPermission(Reference.NODE_OP)) {
 | 
			
		||||
				if(qqid != null) {
 | 
			
		||||
					um.setTencentId(qqid);
 | 
			
		||||
					sender.sendMessage(ChatColor.GOLD+um.getUserId()+ChatColor.RESET+"'s Tencent userid now is "+qqid);
 | 
			
		||||
				}
 | 
			
		||||
			} else {
 | 
			
		||||
				sender.sendMessage("You have no permission!");
 | 
			
		||||
			}
 | 
			
		||||
			return true;
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
		return false;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
}
 | 
			
		||||
@@ -1,3 +1,10 @@
 | 
			
		||||
name: NativeBot
 | 
			
		||||
main: ren.taske.nativebot.MinecraftPlugin
 | 
			
		||||
version: 1.0.0
 | 
			
		||||
 | 
			
		||||
commands:
 | 
			
		||||
  qq:
 | 
			
		||||
    description: set your qq userid
 | 
			
		||||
    usage: /qq <QQNumber> [MinecraftName]
 | 
			
		||||
    permission: nativebot.qq
 | 
			
		||||
    permission-message: You don't have permission!
 | 
			
		||||
		Reference in New Issue
	
	Block a user