Code improvement

This commit is contained in:
HaHaWTH 2023-11-23 22:19:23 +08:00
parent 265ea66894
commit 6b5e0b82ba

View File

@ -23,6 +23,7 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -45,6 +46,7 @@ import static org.bukkit.Bukkit.getServer;
public class GuiCaptchaHandler implements Listener { public class GuiCaptchaHandler implements Listener {
//define AuthMeApi //define AuthMeApi
private final AuthMeApi authmeApi = AuthMeApi.getInstance(); private final AuthMeApi authmeApi = AuthMeApi.getInstance();
StringBuilder sb = new StringBuilder();
@Inject @Inject
private BukkitService bukkitService; private BukkitService bukkitService;
@Inject @Inject
@ -72,12 +74,13 @@ public class GuiCaptchaHandler implements Listener {
Random howManyRandom = new Random(); Random howManyRandom = new Random();
int howLongIsRandomString = (howManyRandom.nextInt(3) + 1); int howLongIsRandomString;
public GuiCaptchaHandler() { public GuiCaptchaHandler() {
} }
protected List<String> whiteList = AuthMe.settings.getProperty(SecuritySettings.GUI_CAPTCHA_COUNTRY_WHITELIST); protected List<String> whiteList = AuthMe.settings.getProperty(SecuritySettings.GUI_CAPTCHA_COUNTRY_WHITELIST);
private boolean isBedrockPlayer(UUID uuid) { private boolean isBedrockPlayer(UUID uuid) {
return settings.getProperty(HooksSettings.HOOK_FLOODGATE_PLAYER) && settings.getProperty(SecuritySettings.GUI_CAPTCHA_BE_COMPATIBILITY) && org.geysermc.floodgate.api.FloodgateApi.getInstance().isFloodgateId(uuid) && getServer().getPluginManager().getPlugin("floodgate") != null; return settings.getProperty(HooksSettings.HOOK_FLOODGATE_PLAYER) && settings.getProperty(SecuritySettings.GUI_CAPTCHA_BE_COMPATIBILITY) && org.geysermc.floodgate.api.FloodgateApi.getInstance().isFloodgateId(uuid) && getServer().getPluginManager().getPlugin("floodgate") != null;
} }
@ -88,6 +91,27 @@ public class GuiCaptchaHandler implements Listener {
ProtocolLibrary.getProtocolManager().removePacketListener(chatPacketListener); ProtocolLibrary.getProtocolManager().removePacketListener(chatPacketListener);
} }
@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerLogin(PlayerLoginEvent event) {
bukkitService.runTaskAsynchronously(() -> {
randomString = "";
howLongIsRandomString = (howManyRandom.nextInt(3) + 1);
sb = new StringBuilder();
for (int i = 0; i < howLongIsRandomString; i++) {
//生成随机索引号
int index = randomItemSet.nextInt(randomSet.length());
// 从字符串中获取由索引 index 指定的字符
char randomChar = randomSet.charAt(index);
// 将字符追加到字符串生成器
sb.append(randomChar);
}
});
}
@EventHandler @EventHandler
public void onInventoryClick(InventoryClickEvent event) { public void onInventoryClick(InventoryClickEvent event) {
if (event.getWhoClicked() instanceof Player) { if (event.getWhoClicked() instanceof Player) {
@ -121,18 +145,18 @@ public class GuiCaptchaHandler implements Listener {
return; return;
} }
bukkitService.runTaskAsynchronously(() -> { bukkitService.runTaskAsynchronously(() -> {
StringBuilder sb = new StringBuilder(); // StringBuilder sb = new StringBuilder();
howLongIsRandomString = (howManyRandom.nextInt(3) + 1); // howLongIsRandomString = (howManyRandom.nextInt(3) + 1);
for (int i = 0; i < howLongIsRandomString; i++) { // for (int i = 0; i < howLongIsRandomString; i++) {
//生成随机索引号 // //生成随机索引号
int index = randomItemSet.nextInt(randomSet.length()); // int index = randomItemSet.nextInt(randomSet.length());
//
// 从字符串中获取由索引 index 指定的字符 // // 从字符串中获取由索引 index 指定的字符
char randomChar = randomSet.charAt(index); // char randomChar = randomSet.charAt(index);
//
// 将字符追加到字符串生成器 // // 将字符追加到字符串生成器
sb.append(randomChar); // sb.append(randomChar);
} // }
bukkitService.runTask(() -> { bukkitService.runTask(() -> {
randomString = sb.toString(); randomString = sb.toString();