Skip to content

Commit

Permalink
将 .setResult() 换成 .disallow()
Browse files Browse the repository at this point in the history
  • Loading branch information
ApliNi committed Sep 2, 2023
1 parent 8b0d259 commit e01bf97
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
51 changes: 27 additions & 24 deletions src/main/java/aplini/ipacwhitelist/Listener/onPlayerJoin.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ public void onPlayerLogin(PlayerLoginEvent event) {

// 服务器启动等待
if(!allowJoin){
event.setKickMessage(plugin.getConfig().getString("message.join.starting", ""));
event.setResult(PlayerLoginEvent.Result.KICK_OTHER);
event.disallow(PlayerLoginEvent.Result.KICK_OTHER,
plugin.getConfig().getString("message.join.starting", ""));
return;
}

// 玩家 UUID
UUID playerUUID = event.getPlayer().getUniqueId();
// 玩家是否在断开连接的列表中
if(playerDisconnectList.contains(playerUUID)){
event.setKickMessage(plugin.getConfig().getString("message.join.limiter-reconnection-time", ""));
event.setResult(PlayerLoginEvent.Result.KICK_OTHER);
event.disallow(PlayerLoginEvent.Result.KICK_OTHER,
plugin.getConfig().getString("message.join.limiter-reconnection-time", ""));
return;
}

Expand All @@ -65,10 +65,10 @@ public void onPlayerLogin(PlayerLoginEvent event) {
}
if(inBlacklist){
getLogger().info("[IpacWhitelist] %s 在IP黑名单中: %s".formatted(playerName, playerIP));
event.setKickMessage(plugin.getConfig().getString("message.join.ban-ip", "")
.replace("%player%",playerName)
.replace("%ip%", playerIP));
event.setResult(PlayerLoginEvent.Result.KICK_BANNED);
event.disallow(PlayerLoginEvent.Result.KICK_BANNED,
plugin.getConfig().getString("message.join.ban-ip", "")
.replace("%player%",playerName)
.replace("%ip%", playerIP));
return;
}

Expand All @@ -79,9 +79,9 @@ public void onPlayerLogin(PlayerLoginEvent event) {
case NOT, VISIT -> { // 不存在 / 参观账户
// 检查用户名
if(!Pattern.matches(plugin.getConfig().getString("whitelist.name-rule-visit", ".*"), playerName)){
event.setKickMessage(plugin.getConfig().getString("message.join.err-name-visit", "")
.replace("%player%", playerName));
event.setResult(PlayerLoginEvent.Result.KICK_OTHER);
event.disallow(PlayerLoginEvent.Result.KICK_OTHER,
plugin.getConfig().getString("message.join.err-name-visit", "")
.replace("%player%", playerName));
return;
}
// 是否启用参观账户
Expand All @@ -95,17 +95,18 @@ public void onPlayerLogin(PlayerLoginEvent event) {
}
}else{
getLogger().info("[IpacWhitelist] %s 不在白名单中".formatted(event.getPlayer().getName()));
event.setKickMessage(plugin.getConfig().getString("message.join.not", "").replace("%player%", playerName));
event.setResult(PlayerLoginEvent.Result.KICK_WHITELIST);
event.disallow(PlayerLoginEvent.Result.KICK_WHITELIST,
plugin.getConfig().getString("message.join.not", "")
.replace("%player%", playerName));
}
}

case WHITE, VISIT_CONVERT -> { // 白名单 / 正在将 VISIT 转换为 WHITE
// 检查用户名
if(!Pattern.matches(plugin.getConfig().getString("whitelist.name-rule", ".*"), playerName)){
event.setKickMessage(plugin.getConfig().getString("message.join.err-name", "")
.replace("%player%", playerName));
event.setResult(PlayerLoginEvent.Result.KICK_OTHER);
event.disallow(PlayerLoginEvent.Result.KICK_OTHER,
plugin.getConfig().getString("message.join.err-name", "")
.replace("%player%", playerName));
return;
}
// 如果是待转换的参观账户
Expand All @@ -117,26 +118,28 @@ public void onPlayerLogin(PlayerLoginEvent event) {
pd.Type = Type.WHITE;
pd.save();
}
// 通过白名单, 无需处理
// else if(state == Type.WHITE) event.setResult(PlayerLoginEvent.Result.ALLOWED); // 可能其他插件需要拒绝玩家加入
// 通过白名单, 允许登录
event.allow();
}

case WHITE_EXPIRED -> { // 白名单已过期
getLogger().info("[IpacWhitelist] %s 白名单已过期".formatted(playerName));
event.setKickMessage(plugin.getConfig().getString("message.join.expired", "").replace("%player%", playerName));
event.setResult(PlayerLoginEvent.Result.KICK_WHITELIST);
event.disallow(PlayerLoginEvent.Result.KICK_WHITELIST,
plugin.getConfig().getString("message.join.expired", "")
.replace("%player%", playerName));
}

case BAN -> { // 黑名单
getLogger().info("[IpacWhitelist] %s 在黑名单中".formatted(playerName));
event.setKickMessage(plugin.getConfig().getString("message.join.ban", "").replace("%player%", playerName));
event.setResult(PlayerLoginEvent.Result.KICK_BANNED);
event.disallow(PlayerLoginEvent.Result.KICK_BANNED,
plugin.getConfig().getString("message.join.ban", "")
.replace("%player%", playerName));
}

default -> { // 内部错误
getLogger().warning("[IpacWhitelist] %s 触发内部错误".formatted(playerName));
event.setKickMessage(plugin.getConfig().getString("message.join.err", ""));
event.setResult(PlayerLoginEvent.Result.KICK_OTHER);
event.disallow(PlayerLoginEvent.Result.KICK_OTHER,
plugin.getConfig().getString("message.join.err", ""));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,17 @@ public static boolean ifForbiddenJoin(PlayerLoginEvent event) {
String Hostname = event.getHostname();
if(!plugin.getConfig().getStringList("visit.limit-hostname.list").contains(Hostname)){
getLogger().info("[IpacWhitelist] %s 参观账户使用非法主机名: %s".formatted(event.getPlayer().getName(), Hostname));
event.setKickMessage(plugin.getConfig().getString("message.visit.illegal-hostname", "")
.replace("%player%", event.getPlayer().getName()));
event.setResult(PlayerLoginEvent.Result.KICK_WHITELIST);
event.disallow(PlayerLoginEvent.Result.KICK_WHITELIST,
plugin.getConfig().getString("message.visit.illegal-hostname", "")
.replace("%player%", event.getPlayer().getName()));
return true;
}
}

// 参观账户队列已满
if(visitList.size() == plugin.getConfig().getInt("visit.max-visit-player")){
event.setKickMessage(plugin.getConfig().getString("message.visit.full", ""));
event.setResult(PlayerLoginEvent.Result.KICK_FULL);
event.disallow(PlayerLoginEvent.Result.KICK_FULL,
plugin.getConfig().getString("message.visit.full", ""));
return true;
}

Expand Down

0 comments on commit e01bf97

Please sign in to comment.