Skip to content

Commit

Permalink
修复一些账户数据处理上的错误
Browse files Browse the repository at this point in the history
  • Loading branch information
ApliNi committed Feb 13, 2024
1 parent 9848844 commit f1e2272
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 66 deletions.
1 change: 1 addition & 0 deletions src/main/java/aplini/ipacwhitelist/func/eventFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public static void runEventFunc(String cp, Player player, String playerUUID, Str
.replace(ph.playerName.ph, playerName);
// 发送消息
if(player != null){
server.getConsoleSender().sendMessage(msg);
for(Player liPlayer : server.getOnlinePlayers()){
if(!liPlayer.getName().equals(playerName)){
liPlayer.sendMessage(msg);
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/aplini/ipacwhitelist/listener/cmd/add.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static aplini.ipacwhitelist.IpacWhitelist.config;
import static aplini.ipacwhitelist.IpacWhitelist.server;
import static aplini.ipacwhitelist.IpacWhitelist.*;
import static aplini.ipacwhitelist.func.eventFunc.runEventFunc;
import static aplini.ipacwhitelist.listener.onPlayerLogin.visitPlayerList;
import static aplini.ipacwhitelist.utils.util.msg;
Expand Down Expand Up @@ -53,28 +53,33 @@ public static void cmd(CommandSender sender, String[] args){
}
// 重置参观账户
if(li.type == Type.VISIT){
li.type = Type.VISIT_CONVERT;
li.save();
// 运行参观账户转换程序
runEventFunc("whitelist.VISIT.onWhitelistAddEvent", inp.onlinePlayer, li.uuid, li.name);
li.type = Type.VISIT_CONVERT;
li.save();
sender.sendMessage(msg(config.getString("command.add.isVisit", ""), li.uuid, li.name));
return;
}
// 已添加到白名单中
// 非 NOT 类型
if(li.isExist()){
sender.sendMessage(msg(config.getString("command.add.isExist", ""), li.uuid, li.name));
return;
}
// 从已删除的数据中恢复, 如果 UUID 相同则恢复, 否则只能添加一条新数据
else if(li.uuid.equals(inp.forUUID)){
else if(Objects.equals(li.uuid, inp.forUUID)){
li.type = Type.WHITE;
li.save();
sender.sendMessage(msg(config.getString("command.add.finish", ""), li.uuid, li.name));
return;
}
// 需要创建单独的数据
// else {}
}

// 创建一条新数据
// 创建新数据
inp.pd = new PlayerData();
inp.pd.type = Type.WHITE;
inp.pd.setPlayerInfo(inp.forUUID, inp.forName);
inp.pd.save();
sender.sendMessage(msg(config.getString("command.add.finish", ""), inp.forUUID, inp.forName));
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/aplini/ipacwhitelist/listener/cmd/ban.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public static void cmd(CommandSender sender, String[] args){
return;
}
// 添加到黑名单
runEventFunc("whitelist.BAN.onWhitelistBanEvent", inp.onlinePlayer, li.uuid, li.name);
li.ban = Type.BAN;
li.save();
runEventFunc("whitelist.BAN.onWhitelistBanEvent", inp.onlinePlayer, li.uuid, li.name);
sender.sendMessage(msg(config.getString("command.ban.finish", ""), li.uuid, li.name));
return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/aplini/ipacwhitelist/listener/cmd/clear.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ else if(mode.equals("TYPE")){
String typeName = args[2].toUpperCase();

List<PlayerData> pds = (switch (typeName) {
// case "NOT" -> sql.findPlayerDataList("", Key.GET_NOT);
case "NOT" -> sql.findPlayerDataList("", Key.GET_NOT);
case "VISIT" -> sql.findPlayerDataList("", Key.GET_VISIT);
case "BAN" -> sql.findPlayerDataList("", Key.GET_BAN);
default -> null;
Expand Down Expand Up @@ -238,7 +238,7 @@ else if(args.length == 3){
}
else if(args[1].equalsIgnoreCase("TYPE")){
return List.of(
// "NOT",
"NOT",
"VISIT",
"BAN"
);
Expand Down
16 changes: 2 additions & 14 deletions src/main/java/aplini/ipacwhitelist/listener/cmd/del.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,9 @@ public static void cmd(CommandSender sender, String[] args){
sender.sendMessage(msg(config.getString("command.del.isBan", ""), li.uuid, li.name));
return;
}
// 移出白名单
runEventFunc("whitelist.WHITE.onWhitelistDelEvent", inp.onlinePlayer, li.uuid, li.name);
li.type = Type.NOT;
li.save();
runEventFunc("whitelist.WHITE.onWhitelistDelEvent", inp.onlinePlayer, li.uuid, li.name);
sender.sendMessage(msg(config.getString("command.del.finish", ""), li.uuid, li.name));
return;
}
Expand All @@ -81,18 +80,7 @@ public static List<String> tab(CommandSender sender, String[] args){
}

if(args[1].isEmpty()){
// 返回在线玩家列表
List<String> list = new ArrayList<>();
for(Player player : server.getOnlinePlayers()){
String uuid = player.getUniqueId().toString();
if(visitPlayerList.contains(uuid)){
list.add(uuid +" - "+ player.getName() +" [TYPE: VISIT] ");
}else{
list.add(uuid +" - "+ player.getName() +" [TYPE: WHITE] ");
}
}
list.add("....");
return list;
return List.of("....");
}

// 查询匹配的输入
Expand Down
103 changes: 61 additions & 42 deletions src/main/java/aplini/ipacwhitelist/listener/onPlayerLogin.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,34 +127,37 @@ public void onPlayerLoginEvent(PlayerLoginEvent event) {
// 检查所有匹配的名称, 如果 id 不同则代表不同的数据, 进行转换并删除另一个
List<PlayerData> pdsForName = sql.getPlayerDataList(null, playerName, true, true);
for(PlayerData li : pdsForName){
// 排除 id 相同的记录
if(li.id == pd.id){continue;}

// 如果 Type 和 Ban 均为 NOT 则将这条记录删除, 然后这不属于名称重复
// 用于支持通过 /wl del 删除名称重复的记录
// 处理名称重复, 但被标记为已删除的数据. 通过 /wl del 产生
if(li.type == Type.NOT && li.ban == Type.NOT){
li.delete();
// 可以选择删除或仅忽略
// li.delete();
continue;
}
// 检查是否存在名称相同, 但 UUID 不同的数据
// 主要用于解决部分同时支持正版和离线账户服务器上有时没有正确应用正版 UUID 的错误
if(config.getBoolean("whitelist.preventNameDuplication", true)){
if(!li.uuid.equals(playerUUID)){
event.disallow(KICK_OTHER, msg(config.getString("whitelist.preventNameDuplicationMsg", ""), playerUUID, playerName));
return;
}
}
// 如果 UUID 为空, 则可以合并
if(li.uuid.isEmpty()){
// 如果 pd 不在数据库中, 则使用数据库中匹配的记录
if(pd.isNull()){
pd = li;
pd.setPlayerInfo(playerUUID, playerName);
continue;
}
// 排除 id 相同的记录
if(li.id == pd.id){continue;}
// 对比和转换数据
pd.compareAndConvert(li);
// 删除多余记录
li.delete();
}
// 检查是否存在名称相同, 但 UUID 不同的数据
// 主要用于解决部分同时支持正版和离线账户服务器上有时没有正确应用正版 UUID 的错误
else if(!li.uuid.equals(playerUUID)){
if(config.getBoolean("whitelist.preventNameDuplication", true)){
event.disallow(KICK_OTHER, msg(config.getString("whitelist.preventNameDuplicationMsg", ""), playerUUID, playerName));
return;
}
}
}

// 被封禁的账户
Expand Down Expand Up @@ -186,13 +189,9 @@ public void onPlayerLoginEvent(PlayerLoginEvent event) {

// 参观账户第一次加入服务器, 创建参观账户数据
if(pd.type == Type.NOT){
pd.type = Type.VISIT;
pd.setPlayerInfo(playerUUID, playerName);
pd.save();

// 参观账户第一次登录服务器
runEventFunc("whitelist.VISIT.onNewPlayerLoginEvent", player, playerUUID, playerName);

pd.type = Type.VISIT;
plugin.getLogger().info("为新的参观账户创建数据: "+ playerName);
}

Expand All @@ -202,16 +201,27 @@ public void onPlayerLoginEvent(PlayerLoginEvent event) {
event.allow();
}

// 需要进行转换的参观账户和白名单
case VISIT_CONVERT, WHITE -> {
// 如果需要进行账户转换
if(pd.type == Type.VISIT_CONVERT){
runEventFunc("whitelist.VISIT_CONVERT.onPlayerLoginEvent", player, playerUUID, playerName);
}
// 需要进行转换的参观账户
case VISIT_CONVERT -> {
// Type = WHITE 会在玩家加入时进行
// 在登录过程中转换参观账户
runEventFunc("whitelist.VISIT_CONVERT.onPlayerLoginEvent", player, playerUUID, playerName);
event.allow();
}

// 白名单
case WHITE -> {
event.allow();
}

default -> {
plugin.getLogger().warning("出现未知的错误: 不存在有效数据的玩家登录服务器: "+ player.getUniqueId());
event.disallow(KICK_OTHER, config.getString("message.playerLoginErr", ""));
}
}

// 保存玩家数据
pd.save();
}

@EventHandler(priority = EventPriority.LOWEST) // 玩家加入服务器
Expand All @@ -232,40 +242,49 @@ public void onPlayerJoinEvent(PlayerJoinEvent event){
plugin.getLogger().info(pd.name +" 以参观模式加入服务器");
}
case VISIT_CONVERT -> {
// 如果需要进行账户转换
// 在加入过程中转换参观账户
runEventFunc("whitelist.VISIT_CONVERT.onPlayerJoinEvent", player);
// 在这里设置为白名单
pd.type = Type.WHITE;
pd.save();
// 记录在线玩家
playerList.add(pd.uuid);
}
case WHITE -> {
// 记录在线玩家
playerList.add(pd.uuid);
}
default -> {
plugin.getLogger().warning("出现未知的错误: 不存在有效数据的玩家加入服务器: "+ pd.id +": "+ pd.type.name +": "+ player.getUniqueId());
player.kickPlayer(config.getString("message.playerLoginErr", ""));
}
}
}

@EventHandler(priority = EventPriority.LOWEST) // 玩家退出
public void onPlayerQuit(PlayerQuitEvent event){
Player player = event.getPlayer();
PlayerData pd = getPlayerData(player, true);
switch(pd.type){
case VISIT -> {
// 记录在线的参观账户
visitPlayerList.remove(pd.uuid);
// 参观账户退出事件
runEventFunc("whitelist.VISIT.onPlayerQuitEvent", player, pd.uuid, pd.name);
}
case WHITE -> {
// 记录在线玩家
playerList.remove(pd.uuid);
// 玩家退出事件
runEventFunc("whitelist.WHITE.onPlayerQuitEvent", player, pd.uuid, pd.name);
CompletableFuture.runAsync(() -> {
Player player = event.getPlayer();
PlayerData pd = getPlayerData(player, true);
switch(pd.type){
case VISIT -> {
// 记录在线的参观账户
visitPlayerList.remove(pd.uuid);
// 参观账户退出事件
runEventFunc("whitelist.VISIT.onPlayerQuitEvent", player, pd.uuid, pd.name);
}
case WHITE -> {
// 记录在线玩家
playerList.remove(pd.uuid);
// 玩家退出事件
runEventFunc("whitelist.WHITE.onPlayerQuitEvent", player, pd.uuid, pd.name);
}
// 玩家可能因为 del / ban 等操作被退出服务器
// default -> {}
}
}

// 玩家退出后等待指定时间才能重新连接
playerQuitIng.add(pd.uuid);
CompletableFuture.runAsync(() -> {
// 玩家退出后等待指定时间才能重新连接
playerQuitIng.add(pd.uuid);
try {
TimeUnit.MILLISECONDS.sleep(plugin.getConfig().getInt("whitelist.repeatJoinInterval", 1200));
} catch (InterruptedException ignored) {}
Expand Down

0 comments on commit f1e2272

Please sign in to comment.