Skip to content

Commit

Permalink
完成重构!
Browse files Browse the repository at this point in the history
  • Loading branch information
ApliNi committed Aug 29, 2023
1 parent c1bcab8 commit 53e05d2
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 187 deletions.
73 changes: 30 additions & 43 deletions src/main/java/aplini/ipacwhitelist/Listener/CommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

import static aplini.ipacwhitelist.IpacWhitelist.allowJoin;
import static aplini.ipacwhitelist.util.EventFunc.startVisitConvertFunc;
import static aplini.ipacwhitelist.util.SQL.*;
import static aplini.ipacwhitelist.util.SQL.whileDataForList;
import static aplini.ipacwhitelist.util.Util.ifIsUUID32toUUID36;

public class CommandHandler implements Listener, CommandExecutor, TabCompleter {
private static IpacWhitelist plugin;
Expand Down Expand Up @@ -78,7 +79,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
}

// 获取玩家数据
String inpData = args[1];
String inpData = ifIsUUID32toUUID36(args[1]);
PlayerData pd = Util.getPlayerData(inpData);
// 输入数据错误
if(pd == null){
Expand All @@ -88,7 +89,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command

// 如果已被封禁
if(pd.Ban == Type.BAN){
sender.sendMessage(plugin.getConfig().getString("message.command.ban-exist", "").replace("%player%", pd.Name));
sender.sendMessage(plugin.getConfig().getString("message.command.err-ban", "").replace("%player%", pd.Name));
return true;
}

Expand Down Expand Up @@ -150,40 +151,36 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
}

// 获取玩家数据
String inpData = args[1];
String inpData = ifIsUUID32toUUID36(args[1]);
PlayerData pd = Util.getPlayerData(inpData);
// 输入数据错误
if(pd == null){
sender.sendMessage(plugin.getConfig().getString("message.command.err-length", ""));
return true;
}

// 检查这个玩家是否存在
if(pd.Type == Type.NOT){
sender.sendMessage(plugin.getConfig().getString("message.command.err-note-exist", "").replace("%player%", inpData));
return true;
}

// 是否被封禁
if(pd.Ban == Type.BAN){
sender.sendMessage(plugin.getConfig().getString("message.command.err-ban", ""));
return true;
}

// 检查这个玩家是否存在
if(pd.Type == Type.NOT){
sender.sendMessage(plugin.getConfig().getString("message.command.err-note-exist", "").replace("%player%", inpData));
return true;
}

// 删除
pd.Type = Type.NOT;
Type state = pd.save();
pd.save();

if(state != Type.ERROR){
// 踢出玩家
Player player = Bukkit.getPlayer(pd.Name);
if(plugin.getConfig().getBoolean("whitelist.kick-on-del") && player != null){
player.kickPlayer(plugin.getConfig().getString("message.join.not", "").replace("%player%", player.getName()));
}
sender.sendMessage(plugin.getConfig().getString("message.command.del", "").replace("%player%", inpData));
}else{
sender.sendMessage(plugin.getConfig().getString("message.command.err", ""));
// 踢出玩家
Player player = Bukkit.getPlayer(pd.Name);
if(plugin.getConfig().getBoolean("whitelist.kick-on-del") && player != null){
player.kickPlayer(plugin.getConfig().getString("message.join.not", "").replace("%player%", player.getName()));
}
sender.sendMessage(plugin.getConfig().getString("message.command.del", "").replace("%player%", inpData));
return true;
}

Expand All @@ -200,7 +197,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
}

// 获取玩家数据
String inpData = args[1];
String inpData = ifIsUUID32toUUID36(args[1]);
PlayerData pd = Util.getPlayerData(inpData);
// 输入数据错误
if(pd == null){
Expand All @@ -216,19 +213,14 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command

// ban
pd.Ban = Type.BAN;
Type state = pd.save();
pd.save();

if(state != Type.ERROR){
// 踢出玩家
Player player = Bukkit.getPlayer(pd.Name);
if(plugin.getConfig().getBoolean("whitelist.kick-on-ban") && player != null){
player.kickPlayer(plugin.getConfig().getString("message.join.ban", "").replace("%player%", player.getName()));
}
sender.sendMessage(plugin.getConfig().getString("message.command.ban", "").replace("%player%", inpData));
}else{
sender.sendMessage(plugin.getConfig().getString("message.command.err", ""));
// 踢出玩家
Player player = Bukkit.getPlayer(pd.Name);
if(plugin.getConfig().getBoolean("whitelist.kick-on-ban") && player != null){
player.kickPlayer(plugin.getConfig().getString("message.join.ban", "").replace("%player%", player.getName()));
}

sender.sendMessage(plugin.getConfig().getString("message.command.ban", "").replace("%player%", inpData));
return true;
}

Expand All @@ -245,16 +237,16 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
}

// 获取玩家数据
String inpData = args[1];
String inpData = ifIsUUID32toUUID36(args[1]);
PlayerData pd = Util.getPlayerData(inpData);
// 输入数据错误
if(pd == null){
sender.sendMessage(plugin.getConfig().getString("message.command.err-length", ""));
return true;
}

// 检查这个玩家是否存在
if(pd.Type == Type.NOT){
// 检查这个玩家是否存在, 这里不能使用 NOT 来检查, 因为账户可能在 Type=NOT 时被封禁
if(pd.ID != -1){
sender.sendMessage(plugin.getConfig().getString("message.command.err-note-exist", "").replace("%player%", inpData));
return true;
}
Expand All @@ -267,14 +259,9 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command

// unban
pd.Ban = Type.NOT_BAN;
Type state = pd.save();

if(state != Type.ERROR){
sender.sendMessage(plugin.getConfig().getString("message.command.unban", "").replace("%player%", inpData));
}else{
sender.sendMessage(plugin.getConfig().getString("message.command.err", ""));
}
pd.save();

sender.sendMessage(plugin.getConfig().getString("message.command.unban", "").replace("%player%", inpData));
return true;
}

Expand All @@ -291,7 +278,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
}

// 获取玩家数据
String inpData = args[1];
String inpData = ifIsUUID32toUUID36(args[1]);
PlayerData pd = Util.getPlayerData(inpData);
// 输入数据错误
if(pd == null){
Expand Down
30 changes: 19 additions & 11 deletions src/main/java/aplini/ipacwhitelist/Listener/onPlayerJoin.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aplini.ipacwhitelist.Listener;

import aplini.ipacwhitelist.IpacWhitelist;
import aplini.ipacwhitelist.util.PlayerData;
import aplini.ipacwhitelist.util.SQL;
import aplini.ipacwhitelist.util.Type;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -72,8 +73,8 @@ public void onPlayerLogin(PlayerLoginEvent event) {
}

// 白名单逻辑
Type state = SQL.isInWhitelisted(event.getPlayer());
switch(state){
PlayerData pd = SQL.isInWhitelisted(event.getPlayer());
switch(pd.__whitelistedState){

case NOT, VISIT -> { // 不存在 / 参观账户
// 检查用户名
Expand All @@ -86,8 +87,9 @@ public void onPlayerLogin(PlayerLoginEvent event) {
// 是否启用参观账户
if(plugin.getConfig().getBoolean("visit.enable", false)){
// 如果是新账户, 则需要运行 onNewVisitPlayerLoginEvent
if(state == Type.NOT){
onVisitPlayerJoin.onNewVisitPlayerLoginEvent(event);
if(pd.__whitelistedState == Type.NOT){
// 参观账户的数据在这里创建...
onVisitPlayerJoin.onNewVisitPlayerLoginEvent(event, pd);
}else{
onVisitPlayerJoin.onVisitPlayerLoginEvent(event);
}
Expand All @@ -106,12 +108,14 @@ public void onPlayerLogin(PlayerLoginEvent event) {
event.setResult(PlayerLoginEvent.Result.KICK_OTHER);
return;
}
if(state == Type.VISIT_CONVERT){
// 如果是待转换的参观账户
if(pd.__whitelistedState == Type.VISIT_CONVERT){
// 运行 wl-add-convert
Player player = event.getPlayer();
startVisitConvertFunc(plugin, player, "visit.wl-add-convert.command");
// 修改 Type 为 WHITE, 同时更新时间
SQL.addPlayer(player, Type.WHITE);
// 修改 Type 为 WHITE
pd.Type = Type.WHITE;
pd.save();
}
// 通过白名单, 无需处理
// else if(state == Type.WHITE) event.setResult(PlayerLoginEvent.Result.ALLOWED); // 可能其他插件需要拒绝玩家加入
Expand Down Expand Up @@ -140,14 +144,18 @@ public void onPlayerLogin(PlayerLoginEvent event) {

@EventHandler(priority = EventPriority.MONITOR) // 玩家退出
public void onPlayerQuit(PlayerQuitEvent event) {
// 保存断开连接的玩家的uuid
UUID playerUUID = event.getPlayer().getUniqueId();
playerDisconnectList.add(playerUUID);

// 定时移除这个uuid
// 更新玩家的最后连接时间
PlayerData pd = SQL.getPlayerData(Type.DATA_UUID, playerUUID.toString());
pd.Time = -3;
pd.save();

// 玩家退出后等待指定时间才能重新连接
playerDisconnectList.add(playerUUID);
CompletableFuture.runAsync(() -> {
try {
TimeUnit.MILLISECONDS.sleep(plugin.getConfig().getInt("whitelist.playerDisconnectToReconnectMinTime", 1500));
TimeUnit.MILLISECONDS.sleep(plugin.getConfig().getInt("whitelist.playerDisconnectToReconnectMinTime", 1000));
} catch (InterruptedException ignored) {}
playerDisconnectList.remove(playerUUID);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package aplini.ipacwhitelist.Listener;

import aplini.ipacwhitelist.IpacWhitelist;
import aplini.ipacwhitelist.util.SQL;
import aplini.ipacwhitelist.util.PlayerData;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
Expand Down Expand Up @@ -29,11 +29,14 @@ public onVisitPlayerJoin(IpacWhitelist plugin){
}

// 参观账户第一次登录服务器
public static void onNewVisitPlayerLoginEvent(PlayerLoginEvent event) {
public static void onNewVisitPlayerLoginEvent(PlayerLoginEvent event, PlayerData pd) {
if(ifForbiddenJoin(event)){return;}

// 将这个玩家以参观账户的身份添加到数据库中
SQL.addPlayer(event.getPlayer(), VISIT);
// 创建参观账户
pd.setPlayerInfo(event.getPlayer());
pd.Type = VISIT;
pd.Time = -3;
pd.save();
getLogger().info("[IpacWhitelist] 为新的参观账户创建数据: %s ".formatted(event.getPlayer().getName()));

// 参观账户事件程序
Expand Down
29 changes: 23 additions & 6 deletions src/main/java/aplini/ipacwhitelist/util/PlayerData.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package aplini.ipacwhitelist.util;

import org.bukkit.entity.Player;

import static aplini.ipacwhitelist.util.Type.NOT;
import static aplini.ipacwhitelist.util.Type.NOT_BAN;
import static aplini.ipacwhitelist.util.Util.ifIsUUID32toUUID36;
Expand All @@ -10,10 +12,12 @@ public class PlayerData {
public int ID = -1;
public Type Type = NOT;
public Type Ban = NOT_BAN;
public String UUID = null;
public String Name = null;
public String UUID = "";
public String Name = "";
public long Time = -1;

public Type __whitelistedState = null;


// 数据是否为空
public boolean isNull(){
Expand All @@ -24,14 +28,27 @@ public boolean isNull(){
public void addPlayerAuto(String inpData){
inpData = ifIsUUID32toUUID36(inpData);
if(inpData.length() == 36){ // uuid
UUID = inpData;
this.UUID = inpData;
}else if(inpData.length() <= 16){ // name
Name = inpData;
this.Name = inpData;
}
}

// 设置玩家数据
public void setPlayerInfo(Player player){
this.UUID = player.getUniqueId().toString();
this.Name = player.getName();
}

// 保存数据到数据库
public Type save(){
return SQL.setPlayerData(Name, UUID, Time, Type, Ban);
public void save(){
SQL.savePlayerData(this);
// return SQL.setPlayerData(Name, UUID, Time, Type, Ban);
}

// 设置白名单状态, 并返回 this
public PlayerData whitelistedState(Type type){
this.__whitelistedState = type;
return this;
}
}
Loading

0 comments on commit 53e05d2

Please sign in to comment.