From 841e2084edea104fd6137322865cf1fed05cde73 Mon Sep 17 00:00:00 2001 From: CodingMan Date: Sun, 2 Feb 2025 18:50:30 +0800 Subject: [PATCH 1/2] fix: kick error. --- build.gradle | 2 +- .../com/rock_mc/syn/event/EventListener.java | 19 +++++------- .../com/rock_mc/syn/event/WaitVerify.java | 12 ++++--- .../event/pluginevent/PluginEventSender.java | 31 +++++++++++++------ 4 files changed, 38 insertions(+), 26 deletions(-) diff --git a/build.gradle b/build.gradle index 3e74064..939d5f6 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } group = 'com.rock_mc' -version = '1.0-SNAPSHOT' +version = '1.0.1' repositories { mavenCentral() diff --git a/src/main/java/com/rock_mc/syn/event/EventListener.java b/src/main/java/com/rock_mc/syn/event/EventListener.java index 36f8a26..353b97e 100644 --- a/src/main/java/com/rock_mc/syn/event/EventListener.java +++ b/src/main/java/com/rock_mc/syn/event/EventListener.java @@ -17,6 +17,8 @@ import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.player.*; +import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionEffectType; import java.io.IOException; import java.util.List; @@ -123,6 +125,11 @@ public void onPlayerJoin(PlayerJoinEvent event) throws IOException { Location location = player.getLocation(); plugin.freezePlayerMap.put(player.getUniqueId(), location); + player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, Integer.MAX_VALUE, 0)); + + player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, Integer.MAX_VALUE, 255)); + player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, Integer.MAX_VALUE, 200)); + new WaitVerify(plugin, player).start(); } } @@ -146,18 +153,6 @@ public void onPluginKick(KickEvent event) { player.kickPlayer(event.getMessage()); } - @EventHandler - public void onPlayerMove(PlayerMoveEvent event) { - Player player = event.getPlayer(); - if (!plugin.freezePlayerMap.containsKey(player.getUniqueId())) { - return; - } - if (!((LivingEntity) player).isOnGround()) { - return; - } - event.setCancelled(true); - } - @EventHandler public void onPlayerDamage(EntityDamageEvent e) { if (e.getEntity() instanceof Player player) { diff --git a/src/main/java/com/rock_mc/syn/event/WaitVerify.java b/src/main/java/com/rock_mc/syn/event/WaitVerify.java index 2f69b3a..2ae15a7 100644 --- a/src/main/java/com/rock_mc/syn/event/WaitVerify.java +++ b/src/main/java/com/rock_mc/syn/event/WaitVerify.java @@ -7,7 +7,6 @@ import com.rock_mc.syn.log.LoggerPlugin; import org.bukkit.ChatColor; import org.bukkit.entity.Player; -import org.bukkit.event.Event; public class WaitVerify extends Thread { @@ -28,11 +27,16 @@ public WaitVerify(Syn plugin, Player newPlayer) { @Override public void run() { - LOG_PLUGIN.sendMessage(player, "女神 " + ChatColor.GOLD + Syn.APP_NAME + ChatColor.RESET + " 正守護著通往磐石的入口。她雙目炯炯有神,手持大斧,氣宇軒昂。世人若想進入磐石,就必須通過她的考驗與允許。"); - LOG_PLUGIN.sendMessage(player, "女神 " + ChatColor.GOLD + Syn.APP_NAME + ChatColor.RESET + " 問道:「你是誰?你有創世神給你的 " + ChatColor.BOLD + "" + ChatColor.GOLD + "驗證碼" + ChatColor.RESET + " 嗎?」"); +// LOG_PLUGIN.sendMessage(player, "女神 " + ChatColor.GOLD + Syn.APP_NAME + ChatColor.RESET + " 正守護著通往磐石的入口。她雙目炯炯有神,手持大斧,氣宇軒昂。世人若想進入磐石,就必須通過她的考驗與允許。"); +// LOG_PLUGIN.sendMessage(player, "女神 " + ChatColor.GOLD + Syn.APP_NAME + ChatColor.RESET + " 問道:「你是誰?你有創世神給你的 " + ChatColor.BOLD + "" + ChatColor.GOLD + "驗證碼" + ChatColor.RESET + " 嗎?」"); int failTime = plugin.dbManager.getFailedAttempts(player.getUniqueId().toString()); - LOG_PLUGIN.sendMessage(player, "女神 " + ChatColor.GOLD + Syn.APP_NAME + ChatColor.RESET + ":「最好趕快回答,你有 " + (MAX_INPUT_CODE_TIMES - (failTime - 1)) + " 次機會。」"); +// LOG_PLUGIN.sendMessage(player, "女神 " + ChatColor.GOLD + Syn.APP_NAME + ChatColor.RESET + ":「最好趕快回答,你有 " + (MAX_INPUT_CODE_TIMES - (failTime - 1)) + " 次機會。」"); + + player.sendTitle("", "女神 " + ChatColor.GOLD + Syn.APP_NAME + ChatColor.RESET + " 正守護著通往磐石的入口。她雙目炯炯有神,手持大斧,氣宇軒昂。\n世人若想進入磐石,就必須通過她的考驗與允許。", + 10, 100, 20); + player.sendTitle("", "女神 " + ChatColor.GOLD + Syn.APP_NAME + ChatColor.RESET + " 問道:「你是誰?你有創世神給你的 " + ChatColor.BOLD + "" + ChatColor.GOLD + "驗證碼" + ChatColor.RESET + " 嗎?」", + 10, 100, 20); long sleepTime = (long) (1000 * CHECK_TIME); for (int i = 0; i * CHECK_TIME < MAX_WAIT_INPUT_CODE_SECONDS; i++) { diff --git a/src/main/java/com/rock_mc/syn/event/pluginevent/PluginEventSender.java b/src/main/java/com/rock_mc/syn/event/pluginevent/PluginEventSender.java index 4702050..da8e3c3 100644 --- a/src/main/java/com/rock_mc/syn/event/pluginevent/PluginEventSender.java +++ b/src/main/java/com/rock_mc/syn/event/pluginevent/PluginEventSender.java @@ -4,6 +4,8 @@ import org.bukkit.entity.Player; import org.bukkit.event.Event; +import java.util.Objects; + public class PluginEventSender { public static void sendJoinEvent(Player player, String message) { @@ -11,18 +13,29 @@ public static void sendJoinEvent(Player player, String message) { Event joinEvent = new JoinEvent(false, player, message); Bukkit.getPluginManager().callEvent(joinEvent); } catch (java.lang.IllegalStateException e) { - Event joinEvent = new JoinEvent(true, player, message); - Bukkit.getPluginManager().callEvent(joinEvent); + + try { + Event joinEvent = new JoinEvent(true, player, message); + Bukkit.getPluginManager().callEvent(joinEvent); + } catch (java.lang.IllegalStateException e2) { + + Bukkit.getScheduler().runTask(Objects.requireNonNull(Bukkit.getPluginManager().getPlugin("Syn")), () -> { + player.sendMessage(message); + }); + } } } public static void sendKickEvent(Player player, String message) { - try { - Event kickEvent = new KickEvent(false, player, message); - Bukkit.getPluginManager().callEvent(kickEvent); - } catch (java.lang.IllegalStateException e) { - Event kickEvent = new KickEvent(true, player, message); - Bukkit.getPluginManager().callEvent(kickEvent); - } + // 確保在主線程執行踢出操作 + Bukkit.getScheduler().runTask(Objects.requireNonNull(Bukkit.getPluginManager().getPlugin("Syn")), () -> { + try { + Event kickEvent = new KickEvent(false, player, message); + Bukkit.getPluginManager().callEvent(kickEvent); + } catch (Exception e) { + // 如果事件觸發失敗,直接踢出玩家 + player.kickPlayer(message); + } + }); } } From d23b0db0169d508b62dd9db542b0aa5302833820 Mon Sep 17 00:00:00 2001 From: CodingMan Date: Fri, 21 Mar 2025 10:48:10 +0800 Subject: [PATCH 2/2] feat: remove onPlayerMoveEvent. --- build.gradle | 2 +- src/main/java/com/rock_mc/syn/api/Verify.java | 5 ++++ .../com/rock_mc/syn/event/EventListener.java | 26 ++++++++++++++++--- .../com/rock_mc/syn/event/WaitVerify.java | 2 ++ 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/build.gradle b/build.gradle index 939d5f6..5ad7c65 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } group = 'com.rock_mc' -version = '1.0.1' +version = '1.0.2' repositories { mavenCentral() diff --git a/src/main/java/com/rock_mc/syn/api/Verify.java b/src/main/java/com/rock_mc/syn/api/Verify.java index f35aea8..0a322f8 100644 --- a/src/main/java/com/rock_mc/syn/api/Verify.java +++ b/src/main/java/com/rock_mc/syn/api/Verify.java @@ -11,6 +11,7 @@ import org.bukkit.ChatColor; import org.bukkit.entity.Player; import org.bukkit.event.Event; +import org.bukkit.potion.PotionEffectType; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; @@ -105,6 +106,10 @@ public static boolean exec(Syn plugin, Logger logger, Player player, String code PluginEventSender.sendJoinEvent(player, "歡迎 " + ChatColor.YELLOW + player.getDisplayName() + ChatColor.RESET + " 全新加入!"); + player.removePotionEffect(PotionEffectType.BLINDNESS); + player.removePotionEffect(PotionEffectType.SLOW); + player.removePotionEffect(PotionEffectType.JUMP); + return true; } } diff --git a/src/main/java/com/rock_mc/syn/event/EventListener.java b/src/main/java/com/rock_mc/syn/event/EventListener.java index 353b97e..5e112d3 100644 --- a/src/main/java/com/rock_mc/syn/event/EventListener.java +++ b/src/main/java/com/rock_mc/syn/event/EventListener.java @@ -92,6 +92,18 @@ public void onPlayerLogin(PlayerLoginEvent event) throws IOException { event.disallow(PlayerLoginEvent.Result.KICK_BANNED, kickMsg); } + private void removePlayerEffect(Player player) { + player.removePotionEffect(PotionEffectType.BLINDNESS); + player.removePotionEffect(PotionEffectType.SLOW); + player.removePotionEffect(PotionEffectType.JUMP); + } + + private void addPlayerEffect(Player player) { + player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, Integer.MAX_VALUE, 0)); + player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, Integer.MAX_VALUE, 255)); + player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, Integer.MAX_VALUE, 200)); + } + @EventHandler public void onPlayerJoin(PlayerJoinEvent event) throws IOException { final Player player = event.getPlayer(); @@ -110,14 +122,23 @@ public void onPlayerJoin(PlayerJoinEvent event) throws IOException { event.setJoinMessage(LoggerPlugin.PREFIX_GAME + "玩家 " + ChatColor.GREEN + "" + ChatColor.BOLD + name + ChatColor.RESET + " 取得女神 " + ChatColor.GOLD + Syn.APP_NAME + ChatColor.RESET + " 的允許進入伺服器。"); } + removePlayerEffect(player); + LOG_PLUGIN.sendMessage(player, "女神 " + ChatColor.GOLD + Syn.APP_NAME + ChatColor.RESET + " 輕輕地在你耳邊說:\n" + welcome.get((int) (Math.random() * welcome.size()))); + } else if (player.isOp()) { plugin.dbManager.addPlayerToAllowList(uuid); + removePlayerEffect(player); + event.setJoinMessage(LoggerPlugin.PREFIX_GAME + opWelcomeMsg); LOG_PLUGIN.sendMessage(player, "女神 " + ChatColor.GOLD + Syn.APP_NAME + ChatColor.RESET + " 輕輕地在你耳邊說:\n" + welcome.get((int) (Math.random() * welcome.size()))); } else if (plugin.configManager.getConfig().getBoolean(Config.GUEST)) { + LOG_PLUGIN.logInfo("Guest mode is enabled"); + + removePlayerEffect(player); + event.setJoinMessage(LoggerPlugin.PREFIX_GAME + "訪客玩家 " + ChatColor.BOLD + name + ChatColor.RESET + " 取得女神 " + ChatColor.GOLD + Syn.APP_NAME + ChatColor.RESET + " 的暫時允許進入伺服器。"); } else { LOG_PLUGIN.logInfo("Player " + name + " is not verified, freeze player."); @@ -125,10 +146,7 @@ public void onPlayerJoin(PlayerJoinEvent event) throws IOException { Location location = player.getLocation(); plugin.freezePlayerMap.put(player.getUniqueId(), location); - player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, Integer.MAX_VALUE, 0)); - - player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, Integer.MAX_VALUE, 255)); - player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, Integer.MAX_VALUE, 200)); + addPlayerEffect(player); new WaitVerify(plugin, player).start(); } diff --git a/src/main/java/com/rock_mc/syn/event/WaitVerify.java b/src/main/java/com/rock_mc/syn/event/WaitVerify.java index 2ae15a7..a7d9f80 100644 --- a/src/main/java/com/rock_mc/syn/event/WaitVerify.java +++ b/src/main/java/com/rock_mc/syn/event/WaitVerify.java @@ -7,6 +7,7 @@ import com.rock_mc.syn.log.LoggerPlugin; import org.bukkit.ChatColor; import org.bukkit.entity.Player; +import org.bukkit.potion.PotionEffectType; public class WaitVerify extends Thread { @@ -47,6 +48,7 @@ public void run() { } if (!plugin.freezePlayerMap.containsKey(player.getUniqueId())) { + break; }