auth system

This commit is contained in:
Gitea 2023-01-10 03:18:21 +08:00
parent b14b3b2e0c
commit 8d63abd40d
3 changed files with 208 additions and 42 deletions

View File

@ -0,0 +1,80 @@
package expvintl.tools;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.*;
import java.util.ArrayList;
import java.util.List;
public class LoginHandler implements Listener {
private List<String> loggedPlayer=new ArrayList<>();
private YamlConfiguration cfg=Tools.loginConfig;
@EventHandler
public void onBlockBreak(BlockBreakEvent event){
if(!isLogged(event.getPlayer().getName())) event.setCancelled(true);
}
@EventHandler
public void onBlockPlace(BlockPlaceEvent event){
if(!isLogged(event.getPlayer().getName())) event.setCancelled(true);
}
@EventHandler
public void onPlayerMove(PlayerMoveEvent event){
if(!isLogged(event.getPlayer().getName())) event.setCancelled(true);
}
@EventHandler
public void onPlayerChat(AsyncPlayerChatEvent event){
if(!isLogged(event.getPlayer().getName())) event.setCancelled(true);
}
@EventHandler
public void onPlayerDropItem(PlayerDropItemEvent event){
if(!isLogged(event.getPlayer().getName())) event.setCancelled(true);
}
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event){
if(!isLogged(event.getPlayer().getName())) event.setCancelled(true);
}
@EventHandler
public void onPlayerBedEnter(PlayerBedEnterEvent event){
if(!isLogged(event.getPlayer().getName())) event.setCancelled(true);
}
@EventHandler
public void onInventoryClick(InventoryClickEvent event){
if(!isLogged(event.getWhoClicked().getName())) event.setCancelled(true);
}
@EventHandler
public void onPlayerCommand(PlayerCommandPreprocessEvent event){
if(!isLogged(event.getPlayer().getName())) {
if (!event.getMessage().startsWith("/l") && !event.getMessage().startsWith("/reg")) {
event.setCancelled(true);
}
}
}
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event){
if(isLogged(event.getPlayer().getName())){
loggedPlayer.remove(event.getPlayer().getName());
}
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event){
if(isRegistered(event.getPlayer().getName())) {
event.getPlayer().sendMessage("请使用 \"/l 密码\" 来登录!");
}else{
event.getPlayer().sendMessage("您似乎没有注册,请使用 \"/reg 密码 确认密码\" 来注册!");
}
}
public List<String> getLoggedPlayer(){
return loggedPlayer;
}
public boolean isRegistered(String name){
return cfg.getString(name)!=null;
}
public boolean isLogged(String name){
return loggedPlayer.contains(name);
}
}

View File

@ -3,61 +3,146 @@ package expvintl.tools;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
import java.util.Objects;
import java.util.Random;
import java.util.logging.Logger;
import java.util.regex.Pattern;
public final class Tools extends JavaPlugin {
private LoginHandler loginHandler;
public static YamlConfiguration loginConfig;
private final String cfgPath=getDataFolder().getPath()+"/PlayerLoginData.yml";
Logger logger=getLogger();
@Override
public void onEnable() {
// Plugin startup logic
logger.info("Plugin Enabled!");
loginHandler=new LoginHandler();
getServer().getPluginManager().registerEvents(loginHandler,this);
}
@Override
public void onLoad() {
if(!getDataFolder().exists()) getDataFolder().mkdir();
File conf=new File(cfgPath);
if(!conf.exists()) {
try {
if(!conf.createNewFile()) return;
} catch (IOException e) {
logger.warning(e.getMessage());
}
}
try {
loginConfig = new YamlConfiguration();
loginConfig.load(cfgPath);
logger.info("已加载配置!");
} catch (Exception e) {
logger.warning(e.getMessage());
}
}
@Override
public boolean onCommand(CommandSender sender,Command command,String label,String[] args) {
Player player=this.getServer().getPlayer(sender.getName());
if(Objects.isNull(player)){
sender.sendMessage("玩家不存在!");
return false;
sender.sendMessage("玩家不存在!");
return true;
}
if(label.equals("kill")){
if(args.length>0){
sender.sendMessage(args[0]+" 死了");
switch(label) {
case "reg":
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;
}
loginConfig.set(sender.getName(), args[0]);
try {
loginConfig.save(cfgPath);
} catch (IOException e) {
logger.warning(e.getMessage());
sender.sendMessage("密码保存失败!");
return true;
}
loginHandler.getLoggedPlayer().add(sender.getName());
sender.sendMessage("登录成功!");
return true;
} else {
sender.sendMessage("缺少参数!");
return false;
}
case "l":
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;
}
if(!passwd.equals(args[0])){
sender.sendMessage("密码错误!");
return true;
}
loginHandler.getLoggedPlayer().add(sender.getName());
sender.sendMessage("登录成功!");
return true;
}else {
sender.sendMessage("参数错误!");
return false;
}
case "kill":
if(args.length>0){
sender.sendMessage(args[0]+" 死了");
return true;
}
player.setHealth(0);
return true;
}
player.setHealth(0);
}
if(label.equals("tp")){
if(args.length==4){
try {
double x = Double.parseDouble(args[1]);
double y = Double.parseDouble(args[2]);
double z = Double.parseDouble(args[3]);
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));
}catch (NumberFormatException f) {
sender.sendMessage("错误的数字格式!");
case "tp":
if(args.length==4){
try {
double x = Double.parseDouble(args[1]);
double y = Double.parseDouble(args[2]);
double z = Double.parseDouble(args[3]);
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));
}catch (NumberFormatException f) {
sender.sendMessage("错误的数字格式!");
}
}else if(args.length==3) {
try {
double x = Double.parseDouble(args[0]);
double y = Double.parseDouble(args[1]);
double z = Double.parseDouble(args[2]);
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));
} catch (NumberFormatException f) {
sender.sendMessage("错误的数字格式!");
}
}else{
sender.sendMessage("缺少应有的坐标参数!");
return false;
}
}else if(args.length==3) {
try {
double x = Double.parseDouble(args[0]);
double y = Double.parseDouble(args[1]);
double z = Double.parseDouble(args[2]);
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));
} catch (NumberFormatException f) {
sender.sendMessage("错误的数字格式!");
}
}else{
sender.sendMessage("缺少应有的坐标参数!");
return false;
}
return true;
}
return true;
}
@ -66,9 +151,4 @@ public final class Tools extends JavaPlugin {
public void onDisable() {
// Plugin shutdown logic
}
public Player getRandomOnlinePlayer(){
int r=new Random().nextInt(getServer().getOnlinePlayers().size());
return (Player)getServer().getOnlinePlayers().toArray()[r];
}
}

View File

@ -6,8 +6,14 @@ authors: [ expvintl ]
description: Tools
commands:
kill: #指令名
description: "kill self" #指令的注解
usage: /kill
description: "杀死自己" #指令的注解
usage: "用法: /kill"
tp:
description: "teleport"
usage: /tp <玩家名> <x> <y> <z>
description: "传送到某个位置"
usage: "用法: /tp <玩家名> <x> <y> <z>"
reg:
description: "注册用户名"
usage: "用法: /reg 密码 确认密码"
l:
description: "登录账户"
usage: "用法: /l 密码"