From 4b98de5140a5c85df63ee6d75dacda95e57c89fc Mon Sep 17 00:00:00 2001 From: Hyperzlib Date: Tue, 6 Aug 2024 22:47:33 +0800 Subject: [PATCH] Create project --- .gitignore | 19 ++++ isekai-authme-floodgate.iml | 12 +++ pom.xml | 98 +++++++++++++++++++ .../mcp/authmeFloodgate/AuthMeHook.java | 62 ++++++++++++ .../mcp/authmeFloodgate/PluginLogger.java | 27 +++++ .../mcp/authmeFloodgate/PluginMain.java | 42 ++++++++ .../mcp/authmeFloodgate/form/AlertForm.java | 41 ++++++++ .../mcp/authmeFloodgate/form/LoginForm.java | 73 ++++++++++++++ .../authmeFloodgate/form/RegisterForm.java | 79 +++++++++++++++ .../listener/AuthMeListener.java | 37 +++++++ .../listener/BukkitListener.java | 79 +++++++++++++++ src/main/resources/plugin.yml | 7 ++ 12 files changed, 576 insertions(+) create mode 100644 .gitignore create mode 100644 isekai-authme-floodgate.iml create mode 100644 pom.xml create mode 100644 src/main/java/cn/isekai/mcp/authmeFloodgate/AuthMeHook.java create mode 100644 src/main/java/cn/isekai/mcp/authmeFloodgate/PluginLogger.java create mode 100644 src/main/java/cn/isekai/mcp/authmeFloodgate/PluginMain.java create mode 100644 src/main/java/cn/isekai/mcp/authmeFloodgate/form/AlertForm.java create mode 100644 src/main/java/cn/isekai/mcp/authmeFloodgate/form/LoginForm.java create mode 100644 src/main/java/cn/isekai/mcp/authmeFloodgate/form/RegisterForm.java create mode 100644 src/main/java/cn/isekai/mcp/authmeFloodgate/listener/AuthMeListener.java create mode 100644 src/main/java/cn/isekai/mcp/authmeFloodgate/listener/BukkitListener.java create mode 100644 src/main/resources/plugin.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..54abab5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,19 @@ +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +# https://github.com/takari/maven-wrapper#usage-without-binary-jar +.mvn/wrapper/maven-wrapper.jar + +# Eclipse m2e generated files +# Eclipse Core +.project +# JDT-specific (Eclipse Java Development Tools) +.classpath + +.idea \ No newline at end of file diff --git a/isekai-authme-floodgate.iml b/isekai-authme-floodgate.iml new file mode 100644 index 0000000..fa63d4b --- /dev/null +++ b/isekai-authme-floodgate.iml @@ -0,0 +1,12 @@ + + + + + + + SPIGOT + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..ce1a3e1 --- /dev/null +++ b/pom.xml @@ -0,0 +1,98 @@ + + + 4.0.0 + + cn.isekai.mcp + isekai-authme-floodgate + 1.1-SNAPSHOT + + + 17 + 15 + UTF-8 + 1.14.2-R0.1-SNAPSHOT + 2.2.0-SNAPSHOT + 5.6.0-SNAPSHOT + + + + isekai-authme-floodgate + + + src/main/resources/ + + + + + + + org.apache.maven.plugins + maven-clean-plugin + 3.2.0 + + + + org.apache.maven.plugins + maven-resources-plugin + 3.3.0 + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.6.0 + + 1.8 + 1.8 + + + + + org.apache.maven.plugins + maven-jar-plugin + 3.3.0 + + + + + + + spigot-repo + https://hub.spigotmc.org/nexus/content/repositories/snapshots + + + opencollab-snapshot + https://repo.opencollab.dev/maven-snapshots/ + + + codemc-repo + https://repo.codemc.org/repository/maven-public/ + + + + + + + org.spigotmc + spigot-api + ${spigot.version} + provided + + + + org.geysermc.floodgate + api + ${floodgate.version} + provided + + + fr.xephi + authme + ${authme.version} + provided + + + + \ No newline at end of file diff --git a/src/main/java/cn/isekai/mcp/authmeFloodgate/AuthMeHook.java b/src/main/java/cn/isekai/mcp/authmeFloodgate/AuthMeHook.java new file mode 100644 index 0000000..8100028 --- /dev/null +++ b/src/main/java/cn/isekai/mcp/authmeFloodgate/AuthMeHook.java @@ -0,0 +1,62 @@ +package cn.isekai.mcp.authmeFloodgate; + +import fr.xephi.authme.api.v3.AuthMeApi; +import org.bukkit.entity.Player; + +/** + * AuthMe hook - class that handles talking with the AuthMe plugin. + *

+ * It is a good idea to wrap all of your interactions with another plugin + * into a separate class; this way you can keep track of the third-party + * methods you use and you can easily change them or disable them, + * e.g. when AuthMe is disabled. + */ +public class AuthMeHook { + private static AuthMeHook instance; + + private AuthMeApi authMeApi = null; + + public static AuthMeHook getInstance() { + if (instance == null) { + instance = new AuthMeHook(); + } + return instance; + } + + public void initializeAuthMeHook() { + authMeApi = AuthMeApi.getInstance(); + } + + public void removeAuthMeHook() { + authMeApi = null; + } + + public boolean isHookActive() { + return authMeApi != null; + } + + public boolean isNameRegistered(String name) { + return authMeApi != null && authMeApi.isRegistered(name); + } + + public boolean isAuthenticated(Player player) { + return authMeApi != null && authMeApi.isAuthenticated(player); + } + + public boolean register(Player player, String password) { + if (authMeApi == null) return false; + authMeApi.forceRegister(player, password, true); + return true; + } + + public boolean checkPassword(Player player, String password) { + return authMeApi != null && authMeApi.checkPassword(player.getName(), password); + } + + public boolean login(Player player) { + if (authMeApi == null) return false; + authMeApi.forceLogin(player); + return true; + } + +} diff --git a/src/main/java/cn/isekai/mcp/authmeFloodgate/PluginLogger.java b/src/main/java/cn/isekai/mcp/authmeFloodgate/PluginLogger.java new file mode 100644 index 0000000..03beb12 --- /dev/null +++ b/src/main/java/cn/isekai/mcp/authmeFloodgate/PluginLogger.java @@ -0,0 +1,27 @@ +package cn.isekai.mcp.authmeFloodgate; + +import java.util.logging.Logger; + +public class PluginLogger { + private static Logger logger; + + private PluginLogger() { + // Class only has static methods and should not be instantiated + } + + public static void setLogger(Logger logger) { + PluginLogger.logger = logger; + } + + public static void info(String message) { + logger.info(message); + } + + public static void warning(String message) { + logger.warning(message); + } + + public static void severe(String message) { + logger.severe(message); + } +} diff --git a/src/main/java/cn/isekai/mcp/authmeFloodgate/PluginMain.java b/src/main/java/cn/isekai/mcp/authmeFloodgate/PluginMain.java new file mode 100644 index 0000000..7dfe03a --- /dev/null +++ b/src/main/java/cn/isekai/mcp/authmeFloodgate/PluginMain.java @@ -0,0 +1,42 @@ +package cn.isekai.mcp.authmeFloodgate; + +import cn.isekai.mcp.authmeFloodgate.listener.AuthMeListener; +import cn.isekai.mcp.authmeFloodgate.listener.BukkitListener; +import org.bukkit.event.Listener; +import org.bukkit.plugin.PluginManager; +import org.bukkit.plugin.java.JavaPlugin; + +public class PluginMain extends JavaPlugin { + private Listener authMeListener; + private AuthMeHook authMeHook; + + @Override + public void onEnable() { + PluginLogger.setLogger(getLogger()); + + final PluginManager pluginManager = getServer().getPluginManager(); + + BukkitListener listener = new BukkitListener(this); + pluginManager.registerEvents(listener, this); + + authMeHook = AuthMeHook.getInstance(); + + if (pluginManager.isPluginEnabled("AuthMe") && pluginManager.isPluginEnabled("floodgate")) { + registerAuthMeComponents(); + } + } + + public void registerAuthMeComponents() { + PluginLogger.info("Initialize AuthMe Hook"); + authMeHook.initializeAuthMeHook(); + if (authMeListener == null) { + authMeListener = new AuthMeListener(); + getServer().getPluginManager().registerEvents(authMeListener, this); + } + } + + public void removeAuthMeHook() { + PluginLogger.info("Remove AuthMe Hook"); + authMeHook.removeAuthMeHook(); + } +} diff --git a/src/main/java/cn/isekai/mcp/authmeFloodgate/form/AlertForm.java b/src/main/java/cn/isekai/mcp/authmeFloodgate/form/AlertForm.java new file mode 100644 index 0000000..556e872 --- /dev/null +++ b/src/main/java/cn/isekai/mcp/authmeFloodgate/form/AlertForm.java @@ -0,0 +1,41 @@ +package cn.isekai.mcp.authmeFloodgate.form; + +import cn.isekai.mcp.authmeFloodgate.AuthMeHook; +import org.bukkit.entity.Player; +import org.geysermc.cumulus.form.SimpleForm; +import org.geysermc.floodgate.api.FloodgateApi; + +import java.util.UUID; + +public class AlertForm { + FloodgateApi floodgateApi; + Player player; + UUID playerId; + + public AlertForm(Player player) { + this.player = player; + this.playerId = player.getUniqueId(); + + floodgateApi = FloodgateApi.getInstance(); + } + + public void alert(String message, String title) { + SimpleForm form = SimpleForm.builder() + .title(title) + .content(message) + .build(); + floodgateApi.sendForm(playerId, form); + } + + public void alert(String message) { + alert(message, "提示"); + } + + public void alertError(String message, String title) { + alert("§c" + message + "§0", title); + } + + public void alertError(String message) { + alert("§c" + message + "§0", "错误"); + } +} diff --git a/src/main/java/cn/isekai/mcp/authmeFloodgate/form/LoginForm.java b/src/main/java/cn/isekai/mcp/authmeFloodgate/form/LoginForm.java new file mode 100644 index 0000000..13242b0 --- /dev/null +++ b/src/main/java/cn/isekai/mcp/authmeFloodgate/form/LoginForm.java @@ -0,0 +1,73 @@ +package cn.isekai.mcp.authmeFloodgate.form; + +import cn.isekai.mcp.authmeFloodgate.AuthMeHook; +import cn.isekai.mcp.authmeFloodgate.PluginLogger; +import org.bukkit.entity.Player; +import org.geysermc.cumulus.form.CustomForm; +import org.geysermc.cumulus.form.SimpleForm; +import org.geysermc.floodgate.api.FloodgateApi; +import org.geysermc.floodgate.api.player.FloodgatePlayer; + +import java.util.Objects; +import java.util.UUID; + +public class LoginForm { + FloodgateApi floodgateApi; + AuthMeHook authMeHook; + + Player player; + FloodgatePlayer floodgatePlayer; + UUID playerId; + + public LoginForm(Player player) { + this.player = player; + playerId = player.getUniqueId(); + + floodgateApi = FloodgateApi.getInstance(); + authMeHook = AuthMeHook.getInstance(); + + floodgatePlayer = floodgateApi.getPlayer(playerId); + } + + private void showLoginForm(String message) { + CustomForm.Builder registerFormBuilder = CustomForm.builder(); + registerFormBuilder.title("登录") + .label("欢迎来到异世界百科!请登录账号"); + + if (message != null) { + registerFormBuilder.label("§c" + message + "§0"); + } else { + registerFormBuilder.label(""); + } + + registerFormBuilder.input("密码", "") + .validResultHandler(response -> handleLogin(response.next())); + + floodgatePlayer.sendForm(registerFormBuilder.build()); + } + + private void showLoginForm() { + this.showLoginForm(null); + } + + private void handleLogin(Object password) { + if (password == null) { + showLoginForm("输入信息错误"); + return; + } + String passwordStr = password.toString(); + + if (!authMeHook.checkPassword(player, passwordStr)) { + showLoginForm("密码错误"); + return; + } + + if (!authMeHook.login(player)) { + new AlertForm(player).alertError("无法在服务器上登录,请查看聊天窗口的错误提示"); + } + } + + public void show() { + this.showLoginForm(); + } +} diff --git a/src/main/java/cn/isekai/mcp/authmeFloodgate/form/RegisterForm.java b/src/main/java/cn/isekai/mcp/authmeFloodgate/form/RegisterForm.java new file mode 100644 index 0000000..4ea6d6e --- /dev/null +++ b/src/main/java/cn/isekai/mcp/authmeFloodgate/form/RegisterForm.java @@ -0,0 +1,79 @@ +package cn.isekai.mcp.authmeFloodgate.form; + +import cn.isekai.mcp.authmeFloodgate.AuthMeHook; +import org.bukkit.entity.Player; +import org.geysermc.cumulus.form.CustomForm; +import org.geysermc.cumulus.form.SimpleForm; +import org.geysermc.floodgate.api.FloodgateApi; + +import java.util.UUID; + +public class RegisterForm { + FloodgateApi floodgateApi; + AuthMeHook authMeHook; + + Player player; + UUID playerId; + + public RegisterForm(Player player) { + this.player = player; + this.playerId = player.getUniqueId(); + + floodgateApi = FloodgateApi.getInstance(); + authMeHook = AuthMeHook.getInstance(); + } + + private void showWelcome() { + SimpleForm welcomeForm = SimpleForm.builder() + .title("提示") + .content("欢迎来到异世界百科!") + .button("立即注册") + .validResultHandler(response -> showRegisterForm()) + .build(); + + floodgateApi.sendForm(playerId, welcomeForm); + } + + private void showRegisterForm(String message) { + CustomForm.Builder registerFormBuilder = CustomForm.builder(); + registerFormBuilder.title("注册"); + + if (message != null) { + registerFormBuilder.label("§c" + message + "§0"); + } else { + registerFormBuilder.label(""); + } + + registerFormBuilder.input("密码", "") + .input("再次输入密码", "") + .validResultHandler(response -> handleRegister(response.next(), response.next())); + + floodgateApi.sendForm(playerId, registerFormBuilder.build()); + } + + private void handleRegister(Object password, Object repeatPassword) { + if (password == null || repeatPassword == null) { + showRegisterForm("输入信息错误"); + return; + } + + String passwordStr = password.toString(); + String repeatPasswordStr = repeatPassword.toString(); + + if (!passwordStr.equals(repeatPasswordStr)) { + showRegisterForm("两次输入的密码不同"); + } + + if (!authMeHook.register(player, passwordStr)) { + new AlertForm(player).alertError("无法在服务器上注册,请查看聊天窗口的错误提示"); + } + } + + private void showRegisterForm() { + this.showRegisterForm(null); + } + + public void show() { + this.showWelcome(); + } +} diff --git a/src/main/java/cn/isekai/mcp/authmeFloodgate/listener/AuthMeListener.java b/src/main/java/cn/isekai/mcp/authmeFloodgate/listener/AuthMeListener.java new file mode 100644 index 0000000..d2fefdf --- /dev/null +++ b/src/main/java/cn/isekai/mcp/authmeFloodgate/listener/AuthMeListener.java @@ -0,0 +1,37 @@ +package cn.isekai.mcp.authmeFloodgate.listener; + +import cn.isekai.mcp.authmeFloodgate.AuthMeHook; +import cn.isekai.mcp.authmeFloodgate.PluginLogger; +import fr.xephi.authme.events.AuthMeAsyncPreLoginEvent; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.geysermc.cumulus.form.CustomForm; +import org.geysermc.cumulus.form.SimpleForm; +import org.geysermc.floodgate.api.FloodgateApi; + +import java.util.UUID; + +public class AuthMeListener implements Listener { + FloodgateApi floodgateApi; + AuthMeHook authMeHook; + + public AuthMeListener() { + floodgateApi = FloodgateApi.getInstance(); + authMeHook = AuthMeHook.getInstance(); + } + + @EventHandler(priority = EventPriority.LOW) + public void onLogin(AuthMeAsyncPreLoginEvent event) { + /*UUID playerId = event.getPlayer().getUniqueId(); + if (floodgateApi.isFloodgatePlayer(playerId)) { + SimpleForm welcomeForm = SimpleForm.builder() + .title("欢迎来到异世界百科") + .content("欢迎来到异世界百科服务器!") + .button("关闭") + .build(); + + floodgateApi.sendForm(playerId, welcomeForm); + }*/ + } +} \ No newline at end of file diff --git a/src/main/java/cn/isekai/mcp/authmeFloodgate/listener/BukkitListener.java b/src/main/java/cn/isekai/mcp/authmeFloodgate/listener/BukkitListener.java new file mode 100644 index 0000000..ec1343f --- /dev/null +++ b/src/main/java/cn/isekai/mcp/authmeFloodgate/listener/BukkitListener.java @@ -0,0 +1,79 @@ +package cn.isekai.mcp.authmeFloodgate.listener; + +import cn.isekai.mcp.authmeFloodgate.AuthMeHook; +import cn.isekai.mcp.authmeFloodgate.PluginLogger; +import cn.isekai.mcp.authmeFloodgate.PluginMain; +import cn.isekai.mcp.authmeFloodgate.form.LoginForm; +import cn.isekai.mcp.authmeFloodgate.form.RegisterForm; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.server.PluginDisableEvent; +import org.bukkit.event.server.PluginEnableEvent; +import org.geysermc.floodgate.api.FloodgateApi; + +import java.util.UUID; + +public class BukkitListener implements Listener { + + private final PluginMain pluginMain; + FloodgateApi floodgateApi; + AuthMeHook authMeHook; + + public BukkitListener(PluginMain pluginMain) { + this.pluginMain = pluginMain; + floodgateApi = FloodgateApi.getInstance(); + authMeHook = AuthMeHook.getInstance(); + } + + /** + * Checks if AuthMe has been enabled and hooks into it in such a case. + * + * @param event the plugin enable event to process + */ + @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) + public void onPluginEnable(PluginEnableEvent event) { + if ("AuthMe".equals(event.getPlugin().getName())) { + pluginMain.registerAuthMeComponents(); + } + } + + /** + * Checks if AuthMe has been disabled and unhooks from it in such a case. + * + * @param event the plugin disable event to process + */ + @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) + public void onPluginDisable(PluginDisableEvent event) { + if ("AuthMe".equals(event.getPlugin().getName())) { + pluginMain.removeAuthMeHook(); + } + } + + @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) + public void onJoin(PlayerJoinEvent event) { + final Player player = event.getPlayer(); + UUID playerId = event.getPlayer().getUniqueId(); + if (floodgateApi.isFloodgatePlayer(playerId)) { + if (!authMeHook.isNameRegistered(player.getName())) { + Bukkit.getScheduler().scheduleSyncDelayedTask(pluginMain, () -> { + // 显示注册表单 + RegisterForm registerForm = new RegisterForm(player); + registerForm.show(); + }, 120); + } else if(!authMeHook.isAuthenticated(player)) { + Bukkit.getScheduler().scheduleSyncDelayedTask(pluginMain, () -> { + // 显示登录表单 + /*LoginForm loginForm = new LoginForm(player); + loginForm.show();*/ + // 自动登录 + authMeHook.login(player); + player.sendMessage("§6已通过 Microsoft 账号自动登录。"); + }, 40); + } + } + } +} \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..231462f --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,7 @@ +name: IsekaiAuthMeFloodgate +main: cn.isekai.mcp.authmeFloodgate.PluginMain +api-version: 1.14 +version: 1.1 +softdepend: + - AuthMe + - floodgate \ No newline at end of file