remove bukkit commands

This commit is contained in:
expvintl 2023-10-04 16:29:04 +08:00
parent da94e4448d
commit 26af0865f7
6 changed files with 223 additions and 146 deletions

View File

@ -1,5 +1,9 @@
package expvintl.tools;
import expvintl.tools.commands.TeleportAtCommand;
import expvintl.tools.commands.auth.ChangePasswordCommand;
import expvintl.tools.commands.auth.LoginCommand;
import expvintl.tools.commands.auth.RegisterCommand;
import expvintl.tools.listeners.LoginHandler;
import expvintl.tools.utils.CommandHelper;
import org.bukkit.*;
@ -22,16 +26,17 @@ import java.util.logging.Logger;
import java.util.regex.Pattern;
public final class Tools extends JavaPlugin {
private LoginHandler loginHandler;
public static YamlConfiguration loginConfig;
public static YamlConfiguration settingsCfg;
private boolean enableLogin;
public final static Tools INSTANCE=new Tools();
public LoginHandler loginHandler;
public YamlConfiguration loginConfig;
public YamlConfiguration settingsCfg;
public boolean enableLogin;
//玩家登录数据
private final String loginDataPath =getDataFolder().getPath()+"/PlayerLoginData.yml";
public final String loginDataPath =getDataFolder().getPath()+"/PlayerLoginData.yml";
private final String settingsPath=getDataFolder().getPath()+"/settings.yml";
Logger logger=getLogger();
private Logger logger=getLogger();
//传送请求队列
private final Map<String,String> reqQueue=new HashMap<>();
public final Map<String,String> reqQueue=new HashMap<>();
private void initConfigs(){
if(!getDataFolder().exists()) getDataFolder().mkdir();
File loginData=new File(this.loginDataPath);
@ -70,7 +75,12 @@ public final class Tools extends JavaPlugin {
logger.warning(e.getMessage());
}
}
public void registerCommands(){
getCommand("l").setExecutor(new LoginCommand());
getCommand("reg").setExecutor(new RegisterCommand());
getCommand("cp").setExecutor(new ChangePasswordCommand());
getCommand("tpa").setExecutor(new TeleportAtCommand());
}
private void DisableBukkitCommands(){
try {
Field commands=SimpleCommandMap.class.getDeclaredField("knownCommands");
@ -104,6 +114,7 @@ public final class Tools extends JavaPlugin {
@Override
public void onEnable() {
logger.info("expvintl:Tools 插件已启用!");
registerCommands();
DisableBukkitCommands();
initConfigs();
}
@ -116,143 +127,6 @@ public final class Tools extends JavaPlugin {
return true;
}
switch(label) {
case "reg":
if(!enableLogin) {
sender.sendMessage("未启用登录功能!");
return true;
}
if (args.length == 2) {
if (loginHandler.isRegistered(sender.getName())||loginHandler.isLogged(sender.getName())) {
sender.sendMessage("你已经注册过了!");
return true;
}
if (!args[0].equals(args[1])) {
sender.sendMessage("两次密码不匹配!");
return true;
}
if (args[0].length() < 6 || args[0].length() > 16) {
sender.sendMessage("密码不能少于6位或大于16位!");
return true;
}
if(!Pattern.matches("[A-Za-z0-9.-_]+",args[0])){
sender.sendMessage("密码包含非法字符!\n请使用 大小写字母数字.-_等字符");
return true;
}
try {
MessageDigest md=MessageDigest.getInstance("MD5");
String passMd=new BigInteger(1,md.digest(args[0].getBytes())).toString(16);
loginConfig.set(sender.getName(), passMd);
loginConfig.save(loginDataPath);
} catch (Exception e) {
logger.warning(e.getMessage());
sender.sendMessage("密码保存失败!");
return true;
}
loginHandler.getLoggedPlayer().add(sender.getName());
sender.sendMessage("登录成功!");
Bukkit.getScheduler().cancelTasks(this);
return true;
} else {
sender.sendMessage("缺少参数!");
return false;
}
case "l":
if(!enableLogin) {
sender.sendMessage("未启用登录功能!");
return true;
}
if(args.length==1){
if(loginHandler.isLogged(sender.getName())){
sender.sendMessage("你似乎已经登录过了?");
return true;
}
String passwd=loginConfig.getString(sender.getName());
if(passwd==null){
sender.sendMessage("用户不存在!");
return true;
}
try {
MessageDigest md = MessageDigest.getInstance("MD5");
String passMd = new BigInteger(1, md.digest(args[0].getBytes())).toString(16);
if(!passwd.equals(passMd)){
sender.sendMessage("密码错误!");
return true;
}
}catch (Exception e){
sender.sendMessage("验证密码时发生内部错误!");
return true;
}
loginHandler.getLoggedPlayer().add(sender.getName());
sender.sendMessage("登录成功!");
Bukkit.getScheduler().cancelTasks(this);
return true;
}else {
sender.sendMessage("参数错误!");
return false;
}
case "cp":
if(!enableLogin) {
sender.sendMessage("未启用登录功能!");
return true;
}
if(args.length==1){
if(loginHandler.isRegistered(sender.getName())&&loginHandler.isLogged(sender.getName())){
if (args[0].length() < 6 || args[0].length() > 16) {
sender.sendMessage("密码不能少于6位或大于16位!");
return true;
}
if(!Pattern.matches("[A-Za-z0-9.-_]+",args[0])){
sender.sendMessage("密码包含非法字符!\n请使用 大小写字母数字.-_等字符");
return true;
}
try {
MessageDigest md=MessageDigest.getInstance("MD5");
String passMd=new BigInteger(1,md.digest(args[0].getBytes())).toString(16);
loginConfig.set(sender.getName(),passMd);
loginConfig.save(loginDataPath);
} catch (Exception e) {
logger.warning(e.getMessage());
sender.sendMessage("密码保存失败!");
return true;
}
sender.sendMessage("密码修改成功!");
}else{
sender.sendMessage("您未注册或登录,无权修改密码!");
}
return true;
}else{
sender.sendMessage("缺少参数!");
return false;
}
case "tpa":
if(args.length==1){
Player target=getServer().getPlayer(args[0]);
if(Objects.isNull(target)) {
sender.sendMessage(ChatColor.RED+"玩家不存在或离线!");
return true;
}
if(sender.getName().equals(args[0])){
sender.sendMessage(ChatColor.RED+"你不能传送自己!");
return true;
}
sender.sendMessage(ChatColor.GREEN+"请求已发送给 "+ target.getName());
String jsonMsg="[{\"text\":\""+sender.getName()+" 希望传送到你的位置\\n\"},{\"text\":\"[点击接受]\",\"color\":\"green\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"/tpaccept\"}},{\"text\":\" [点击拒绝]\",\"color\":\"red\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"/tpreject\"}}]";
Bukkit.dispatchCommand(Bukkit.getConsoleSender(),"tellraw "+target.getName()+" "+jsonMsg);
reqQueue.put(sender.getName(),target.getName());
Bukkit.getScheduler().scheduleSyncDelayedTask(this,()->{
//确保请求还存在 比如已接受/拒绝 这将失效
if(reqQueue.get(sender.getName())!=null) {
reqQueue.remove(sender.getName());
target.sendMessage(sender.getName() + " 的传送请求已过期!");
sender.sendMessage(target.getName() + " 的传送请求已过期!");
}
},600);
return true;
}else{
sender.sendMessage("缺少玩家参数!");
return false;
}
case "tpaccept":
//标记所有键
for(String pl:reqQueue.keySet()){

View File

@ -0,0 +1,45 @@
package expvintl.tools.commands;
import expvintl.tools.Tools;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.Objects;
public class TeleportAtCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender,Command command,String label,String[] args) {
Tools t= Tools.INSTANCE;
if(args.length==1){
Player target=Bukkit.getServer().getPlayer(args[0]);
if(Objects.isNull(target)) {
sender.sendMessage(ChatColor.RED+"玩家不存在或离线!");
return true;
}
if(sender.getName().equals(args[0])){
sender.sendMessage(ChatColor.RED+"你不能传送自己!");
return true;
}
sender.sendMessage(ChatColor.GREEN+"请求已发送给 "+ target.getName());
String jsonMsg="[{\"text\":\""+sender.getName()+" 希望传送到你的位置\\n\"},{\"text\":\"[点击接受]\",\"color\":\"green\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"/tpaccept\"}},{\"text\":\" [点击拒绝]\",\"color\":\"red\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"/tpreject\"}}]";
Bukkit.dispatchCommand(Bukkit.getConsoleSender(),"tellraw "+target.getName()+" "+jsonMsg);
t.reqQueue.put(sender.getName(),target.getName());
Bukkit.getScheduler().scheduleSyncDelayedTask(t,()->{
//确保请求还存在 比如已接受/拒绝 这将失效
if(t.reqQueue.get(sender.getName())!=null) {
t.reqQueue.remove(sender.getName());
target.sendMessage(sender.getName() + " 的传送请求已过期!");
sender.sendMessage(target.getName() + " 的传送请求已过期!");
}
},600);
return true;
}else{
sender.sendMessage("缺少玩家参数!");
return false;
}
}
}

View File

@ -0,0 +1,51 @@
package expvintl.tools.commands.auth;
import expvintl.tools.Tools;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.regex.Pattern;
public class ChangePasswordCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command,String label,String[] args) {
Tools t= Tools.INSTANCE;
if(!t.enableLogin) {
sender.sendMessage("未启用登录功能!");
return true;
}
if(args.length==1){
if(t.loginHandler.isRegistered(sender.getName())&&t.loginHandler.isLogged(sender.getName())){
if (args[0].length() < 6 || args[0].length() > 16) {
sender.sendMessage("密码不能少于6位或大于16位!");
return true;
}
if(!Pattern.matches("[A-Za-z0-9.-_]+",args[0])){
sender.sendMessage("密码包含非法字符!\n请使用 大小写字母数字.-_等字符");
return true;
}
try {
MessageDigest md=MessageDigest.getInstance("MD5");
String passMd=new BigInteger(1,md.digest(args[0].getBytes())).toString(16);
t.loginConfig.set(sender.getName(),passMd);
t.loginConfig.save(t.loginDataPath);
} catch (Exception e) {
Bukkit.getLogger().warning(e.getMessage());
sender.sendMessage("密码保存失败!");
return true;
}
sender.sendMessage("密码修改成功!");
}else{
sender.sendMessage("您未注册或登录,无权修改密码!");
}
return true;
}else{
sender.sendMessage("缺少参数!");
return false;
}
}
}

View File

@ -0,0 +1,50 @@
package expvintl.tools.commands.auth;
import expvintl.tools.Tools;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import java.math.BigInteger;
import java.security.MessageDigest;
public class LoginCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command,String label,String[] args) {
Tools t=Tools.INSTANCE;
if(!t.enableLogin) {
sender.sendMessage("未启用登录功能!");
return true;
}
if(args.length==1){
if(t.loginHandler.isLogged(sender.getName())){
sender.sendMessage("你似乎已经登录过了?");
return true;
}
String passwd=t.loginConfig.getString(sender.getName());
if(passwd==null){
sender.sendMessage("用户不存在!");
return true;
}
try {
MessageDigest md = MessageDigest.getInstance("MD5");
String passMd = new BigInteger(1, md.digest(args[0].getBytes())).toString(16);
if(!passwd.equals(passMd)){
sender.sendMessage("密码错误!");
return true;
}
}catch (Exception e){
sender.sendMessage("验证密码时发生内部错误!");
return true;
}
t.loginHandler.getLoggedPlayer().add(sender.getName());
sender.sendMessage("登录成功!");
Bukkit.getScheduler().cancelTasks(t);
return true;
}else {
sender.sendMessage("参数错误!");
return false;
}
}
}

View File

@ -0,0 +1,57 @@
package expvintl.tools.commands.auth;
import expvintl.tools.Tools;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.regex.Pattern;
public class RegisterCommand implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender,Command command,String label,String[] args) {
Tools t= Tools.INSTANCE;
if(!t.enableLogin) {
sender.sendMessage("未启用登录功能!");
return true;
}
if (args.length == 2) {
if (t.loginHandler.isRegistered(sender.getName())||t.loginHandler.isLogged(sender.getName())) {
sender.sendMessage("你已经注册过了!");
return true;
}
if (!args[0].equals(args[1])) {
sender.sendMessage("两次密码不匹配!");
return true;
}
if (args[0].length() < 6 || args[0].length() > 16) {
sender.sendMessage("密码不能少于6位或大于16位!");
return true;
}
if(!Pattern.matches("[A-Za-z0-9.-_]+",args[0])){
sender.sendMessage("密码包含非法字符!\n请使用 大小写字母数字.-_等字符");
return true;
}
try {
MessageDigest md=MessageDigest.getInstance("MD5");
String passMd=new BigInteger(1,md.digest(args[0].getBytes())).toString(16);
t.loginConfig.set(sender.getName(), passMd);
t.loginConfig.save(t.loginDataPath);
} catch (Exception e) {
Bukkit.getLogger().warning(e.getMessage());
sender.sendMessage("密码保存失败!");
return true;
}
t.loginHandler.getLoggedPlayer().add(sender.getName());
sender.sendMessage("登录成功!");
Bukkit.getScheduler().cancelTasks(t);
return true;
} else {
sender.sendMessage("缺少参数!");
return false;
}
}
}

View File

@ -16,7 +16,7 @@ import java.util.List;
public class LoginHandler implements Listener {
private final List<String> loggedPlayer=new ArrayList<>();
private final YamlConfiguration cfg= Tools.loginConfig;
private final YamlConfiguration cfg= Tools.INSTANCE.loginConfig;
private final Plugin plugin;
public LoginHandler(Plugin plugin){
this.plugin=plugin;