添加md5加密

This commit is contained in:
root 2023-04-17 14:29:45 +08:00
parent cf0c5d2dcb
commit 99fa769314
2 changed files with 83 additions and 61 deletions

View File

@ -69,11 +69,11 @@ public class LoginHandler implements Listener {
@EventHandler @EventHandler
public void onPlayerJoin(PlayerJoinEvent event){ public void onPlayerJoin(PlayerJoinEvent event){
if(isRegistered(event.getPlayer().getName())) { if(isRegistered(event.getPlayer().getName())) {
event.getPlayer().sendMessage("请使用 \"/l 密码\" 来登录!"); event.getPlayer().sendMessage("请使用 \"/l 密码\" 来登录!");
}else{ }else{
event.getPlayer().sendMessage("您似乎没有注册,请使用 \"/reg 密码 确认密码\" 来注册!"); event.getPlayer().sendMessage("您似乎没有注册,请使用 \"/reg 密码 确认密码\" 来注册!");
} }
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin,()-> event.getPlayer().kickPlayer("登录验证超时!"),1200L); Bukkit.getScheduler().scheduleSyncDelayedTask(plugin,()-> event.getPlayer().kickPlayer("登录验证超时!"),1200L);
} }
public List<String> getLoggedPlayer(){ public List<String> getLoggedPlayer(){
return loggedPlayer; return loggedPlayer;

View File

@ -1,7 +1,6 @@
package expvintl.tools; package expvintl.tools;
import org.bukkit.Bukkit; import org.bukkit.*;
import org.bukkit.Location;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
@ -10,6 +9,8 @@ import org.bukkit.plugin.java.JavaPlugin;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
@ -19,13 +20,15 @@ import java.util.regex.Pattern;
public final class Tools extends JavaPlugin { public final class Tools extends JavaPlugin {
private LoginHandler loginHandler; private LoginHandler loginHandler;
public static YamlConfiguration loginConfig; public static YamlConfiguration loginConfig;
//玩家登录数据
private final String cfgPath=getDataFolder().getPath()+"/PlayerLoginData.yml"; private final String cfgPath=getDataFolder().getPath()+"/PlayerLoginData.yml";
Logger logger=getLogger(); Logger logger=getLogger();
//传送请求队列
private final Map<String,String> reqQueue=new HashMap<>(); private final Map<String,String> reqQueue=new HashMap<>();
@Override @Override
public void onEnable() { public void onEnable() {
// Plugin startup logic // Plugin startup logic
logger.info("Plugin Enabled!"); logger.info("expvintl:Tools 插件已启用!");
loginHandler=new LoginHandler(this); loginHandler=new LoginHandler(this);
getServer().getPluginManager().registerEvents(loginHandler,this); getServer().getPluginManager().registerEvents(loginHandler,this);
} }
@ -44,7 +47,7 @@ public final class Tools extends JavaPlugin {
try { try {
loginConfig = new YamlConfiguration(); loginConfig = new YamlConfiguration();
loginConfig.load(cfgPath); loginConfig.load(cfgPath);
logger.info("已加载配置!"); logger.info("已加载配置!");
} catch (Exception e) { } catch (Exception e) {
logger.warning(e.getMessage()); logger.warning(e.getMessage());
} }
@ -54,98 +57,110 @@ public final class Tools extends JavaPlugin {
public boolean onCommand(CommandSender sender,Command command,String label,String[] args) { public boolean onCommand(CommandSender sender,Command command,String label,String[] args) {
Player player=this.getServer().getPlayer(sender.getName()); Player player=this.getServer().getPlayer(sender.getName());
if(Objects.isNull(player)){ if(Objects.isNull(player)){
sender.sendMessage("玩家不存在!"); sender.sendMessage("玩家不存在!");
return true; return true;
} }
switch(label) { switch(label) {
case "reg": case "reg":
if (args.length == 2) { if (args.length == 2) {
if (loginHandler.isRegistered(sender.getName())||loginHandler.isLogged(sender.getName())) { if (loginHandler.isRegistered(sender.getName())||loginHandler.isLogged(sender.getName())) {
sender.sendMessage("你已经注册过了!"); sender.sendMessage("你已经注册过了!");
return true; return true;
} }
if (!args[0].equals(args[1])) { if (!args[0].equals(args[1])) {
sender.sendMessage("两次密码不匹配!"); sender.sendMessage("两次密码不匹配!");
return true; return true;
} }
if (args[0].length() < 6 || args[0].length() > 16) { if (args[0].length() < 6 || args[0].length() > 16) {
sender.sendMessage("密码不能少于6位或大于16位!"); sender.sendMessage("密码不能少于6位或大于16位!");
return true; return true;
} }
if(!Pattern.matches("[A-Za-z0-9.-_]+",args[0])){ if(!Pattern.matches("[A-Za-z0-9.-_]+",args[0])){
sender.sendMessage("密码包含非法字符!\n请使用 大小写字母数字.-_等字符"); sender.sendMessage("密码包含非法字符!\n请使用 大小写字母数字.-_等字符");
return true; return true;
} }
loginConfig.set(sender.getName(), args[0]);
try { 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(cfgPath); loginConfig.save(cfgPath);
} catch (IOException e) { } catch (Exception e) {
logger.warning(e.getMessage()); logger.warning(e.getMessage());
sender.sendMessage("密码保存失败!"); sender.sendMessage("密码保存失败!");
return true; return true;
} }
loginHandler.getLoggedPlayer().add(sender.getName()); loginHandler.getLoggedPlayer().add(sender.getName());
sender.sendMessage("登录成功!"); sender.sendMessage("登录成功!");
Bukkit.getScheduler().cancelTasks(this); Bukkit.getScheduler().cancelTasks(this);
return true; return true;
} else { } else {
sender.sendMessage("缺少参数!"); sender.sendMessage("缺少参数!");
return false; return false;
} }
case "l": case "l":
if(args.length==1){ if(args.length==1){
if(loginHandler.isLogged(sender.getName())){ if(loginHandler.isLogged(sender.getName())){
sender.sendMessage("你似乎已经登录过了?"); sender.sendMessage("你似乎已经登录过了?");
return true; return true;
} }
String passwd=loginConfig.getString(sender.getName()); String passwd=loginConfig.getString(sender.getName());
if(passwd==null){ if(passwd==null){
sender.sendMessage("用户不存在!"); sender.sendMessage("用户不存在!");
return true; return true;
} }
if(!passwd.equals(args[0])){ try {
sender.sendMessage("密码错误!"); 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; return true;
} }
loginHandler.getLoggedPlayer().add(sender.getName()); loginHandler.getLoggedPlayer().add(sender.getName());
sender.sendMessage("登录成功!"); sender.sendMessage("登录成功!");
Bukkit.getScheduler().cancelTasks(this); Bukkit.getScheduler().cancelTasks(this);
return true; return true;
}else { }else {
sender.sendMessage("参数错误!"); sender.sendMessage("参数错误!");
return false; return false;
} }
case "cp": case "cp":
if(args.length==1){ if(args.length==1){
if(loginHandler.isRegistered(sender.getName())&&loginHandler.isLogged(sender.getName())){ if(loginHandler.isRegistered(sender.getName())&&loginHandler.isLogged(sender.getName())){
if (args[0].length() < 6 || args[0].length() > 16) { if (args[0].length() < 6 || args[0].length() > 16) {
sender.sendMessage("密码不能少于6位或大于16位!"); sender.sendMessage("密码不能少于6位或大于16位!");
return true; return true;
} }
if(!Pattern.matches("[A-Za-z0-9.-_]+",args[0])){ if(!Pattern.matches("[A-Za-z0-9.-_]+",args[0])){
sender.sendMessage("密码包含非法字符!\n请使用 大小写字母数字.-_等字符"); sender.sendMessage("密码包含非法字符!\n请使用 大小写字母数字.-_等字符");
return true; return true;
} }
loginConfig.set(sender.getName(),args[0]);
try { 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(cfgPath); loginConfig.save(cfgPath);
} catch (IOException e) { } catch (Exception e) {
logger.warning(e.getMessage()); logger.warning(e.getMessage());
sender.sendMessage("密码保存失败!"); sender.sendMessage("密码保存失败!");
return true; return true;
} }
sender.sendMessage("密码修改成功!"); sender.sendMessage("密码修改成功!");
}else{ }else{
sender.sendMessage("您未注册或登录,无权修改密码!"); sender.sendMessage("您未注册或登录,无权修改密码!");
} }
return true; return true;
}else{ }else{
sender.sendMessage("缺少参数!"); sender.sendMessage("缺少参数!");
return false; return false;
} }
case "kill": case "kill":
if(args.length>0){ if(args.length>0){
sender.sendMessage(args[0]+" 死了"); sender.sendMessage(args[0]+" 死了");
return true; return true;
} }
player.setHealth(0); player.setHealth(0);
@ -157,9 +172,9 @@ public final class Tools extends JavaPlugin {
double y = Double.parseDouble(args[2]); double y = Double.parseDouble(args[2]);
double z = Double.parseDouble(args[3]); double z = Double.parseDouble(args[3]);
player.teleport(new Location(player.getWorld(),x,y,z)); player.teleport(new Location(player.getWorld(),x,y,z));
getServer().broadcastMessage(String.format("%s 传送到了 X:%.2f Y:%.2f Z:%.2f",player.getName(),x,y,z)); getServer().broadcastMessage(String.format("%s 传送到了 X:%.2f Y:%.2f Z:%.2f",player.getName(),x,y,z));
}catch (NumberFormatException f) { }catch (NumberFormatException f) {
sender.sendMessage("错误的数字格式!"); sender.sendMessage("错误的数字格式!");
} }
}else if(args.length==3) { }else if(args.length==3) {
try { try {
@ -167,12 +182,12 @@ public final class Tools extends JavaPlugin {
double y = Double.parseDouble(args[1]); double y = Double.parseDouble(args[1]);
double z = Double.parseDouble(args[2]); double z = Double.parseDouble(args[2]);
player.teleport(new Location(player.getWorld(), x, y, z)); player.teleport(new Location(player.getWorld(), x, y, z));
getServer().broadcastMessage(String.format("%s 传送到了 X:%.2f Y:%.2f Z:%.2f", player.getName(), x, y, z)); getServer().broadcastMessage(String.format("%s 传送到了 X:%.2f Y:%.2f Z:%.2f", player.getName(), x, y, z));
} catch (NumberFormatException f) { } catch (NumberFormatException f) {
sender.sendMessage("错误的数字格式!"); sender.sendMessage("错误的数字格式!");
} }
}else{ }else{
sender.sendMessage("缺少应有的坐标参数!"); sender.sendMessage("缺少应有的坐标参数!");
return false; return false;
} }
return true; return true;
@ -181,65 +196,72 @@ public final class Tools extends JavaPlugin {
if(args.length==1){ if(args.length==1){
Player target=getServer().getPlayer(args[0]); Player target=getServer().getPlayer(args[0]);
if(Objects.isNull(target)) { if(Objects.isNull(target)) {
sender.sendMessage("玩家不存在或离线!"); sender.sendMessage(ChatColor.RED+"玩家不存在或离线!");
return true; return true;
} }
sender.sendMessage("请求已发送给 "+ target.getName()); if(sender.getName().equals(args[0])){
String jsonMsg="[{\"text\":\""+sender.getName()+" 希望传送到你的位置\\n\"},{\"text\":\"[点击接受]\",\"color\":\"green\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"/tpaccept\"}},{\"text\":\" [点击拒绝]\",\"color\":\"red\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"/tpreject\"}}]"; 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); Bukkit.dispatchCommand(Bukkit.getConsoleSender(),"tellraw "+target.getName()+" "+jsonMsg);
reqQueue.put(sender.getName(),target.getName()); reqQueue.put(sender.getName(),target.getName());
Bukkit.getScheduler().scheduleSyncDelayedTask(this,()->{ Bukkit.getScheduler().scheduleSyncDelayedTask(this,()->{
reqQueue.remove(sender.getName()); //确保请求还存在 比如已接受/拒绝 这将失效
target.sendMessage(sender.getName()+" 的传送请求已过期!"); if(reqQueue.get(sender.getName())!=null) {
sender.sendMessage(target.getName()+" 的传送请求已过期!"); reqQueue.remove(sender.getName());
target.sendMessage(sender.getName() + " 的传送请求已过期!");
sender.sendMessage(target.getName() + " 的传送请求已过期!");
}
},600); },600);
return true; return true;
}else{ }else{
sender.sendMessage("缺少玩家参数!"); sender.sendMessage("缺少玩家参数!");
return false; return false;
} }
case "tpaccept": case "tpaccept":
//标记所有键 //标记所有键
for(String pl:reqQueue.keySet()){ for(String pl:reqQueue.keySet()){
//确定队列中存在传送目标玩家 //确定队列中存在传送目标玩家
if(reqQueue.get(pl).equals(sender.getName())){ if(reqQueue.get(pl).equals(sender.getName())){
Player tpSender=Bukkit.getPlayer(pl); Player tpSender=Bukkit.getPlayer(pl);
if(Objects.isNull(tpSender)){ if(Objects.isNull(tpSender)){
sender.sendMessage("请求发送者已离线或不存在!"); sender.sendMessage("请求发送者已离线或不存在!");
return true; return true;
} }
//传送玩家 //传送玩家
tpSender.teleport(player); tpSender.teleport(player);
sender.sendMessage("传送中..."); sender.sendMessage("传送中...");
tpSender.sendMessage("传送中..."); tpSender.sendMessage("传送中...");
//从队列中删除 //从队列中删除
reqQueue.remove(pl); reqQueue.remove(pl);
return true; return true;
} }
} }
sender.sendMessage("没有传送请求或请求已过期!"); sender.sendMessage("没有传送请求或请求已过期!");
return true; return true;
case "tpreject": case "tpreject":
//标记所有键 //标记所有键
for(String pl:reqQueue.keySet()){ for(String pl:reqQueue.keySet()){
//确定队列中存在传送目标玩家 //确定队列中存在传送目标玩家
if(reqQueue.get(pl).equals(sender.getName())){ if(reqQueue.get(pl).equals(sender.getName())){
Player s=Bukkit.getPlayer(pl); Player s=Bukkit.getPlayer(pl);
if(Objects.isNull(s)) if(Objects.isNull(s))
{ {
sender.sendMessage("已拒绝传送请求!"); sender.sendMessage("已拒绝传送请求!");
//从队列中删除 //从队列中删除
reqQueue.remove(pl); reqQueue.remove(pl);
return true; return true;
} }
sender.sendMessage("已拒绝传送请求!"); sender.sendMessage("已拒绝传送请求!");
s.sendMessage(sender.getName()+" 拒绝了您的传送请求!"); s.sendMessage(sender.getName()+" 拒绝了您的传送请求!");
//从队列中删除 //从队列中删除
reqQueue.remove(pl); reqQueue.remove(pl);
return true; return true;
} }
} }
sender.sendMessage("没有传送请求或请求已过期!"); sender.sendMessage("没有传送请求或请求已过期!");
return true; return true;
} }
return true; return true;
@ -248,6 +270,6 @@ public final class Tools extends JavaPlugin {
@Override @Override
public void onDisable() { public void onDisable() {
// Plugin shutdown logic // Plugin shutdown logic
getServer().broadcastMessage("插件已被重载,请重新登录或注册!"); getServer().broadcastMessage(ChatColor.GOLD+"插件已被重载,请重新登录或注册!");
} }
} }