Create project

master
落雨楓 5 months ago
commit 4b98de5140

19
.gitignore vendored

@ -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

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="FacetManager">
<facet type="minecraft" name="Minecraft">
<configuration>
<autoDetectTypes>
<platformType>SPIGOT</platformType>
</autoDetectTypes>
</configuration>
</facet>
</component>
</module>

@ -0,0 +1,98 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.isekai.mcp</groupId>
<artifactId>isekai-authme-floodgate</artifactId>
<version>1.1-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spigot.version>1.14.2-R0.1-SNAPSHOT</spigot.version>
<floodgate.version>2.2.0-SNAPSHOT</floodgate.version>
<authme.version>5.6.0-SNAPSHOT</authme.version>
</properties>
<build>
<finalName>isekai-authme-floodgate</finalName>
<resources>
<resource>
<directory>src/main/resources/</directory>
</resource>
</resources>
<plugins>
<!-- Clean the target folder content -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<!-- Include resource files -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.0</version>
</plugin>
<!-- Maven Java compiler -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- Generate a jar containing classes and resources -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots</url>
</repository>
<repository>
<id>opencollab-snapshot</id>
<url>https://repo.opencollab.dev/maven-snapshots/</url>
</repository>
<repository>
<id>codemc-repo</id>
<url>https://repo.codemc.org/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<!-- Spigot API, http://www.spigotmc.org/ or http://bukkit.org/ -->
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>${spigot.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.geysermc.floodgate</groupId>
<artifactId>api</artifactId>
<version>${floodgate.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>fr.xephi</groupId>
<artifactId>authme</artifactId>
<version>${authme.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

@ -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.
* <p>
* 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;
}
}

@ -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);
}
}

@ -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();
}
}

@ -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", "错误");
}
}

@ -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();
}
}

@ -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();
}
}

@ -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);
}*/
}
}

@ -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);
}
}
}
}

@ -0,0 +1,7 @@
name: IsekaiAuthMeFloodgate
main: cn.isekai.mcp.authmeFloodgate.PluginMain
api-version: 1.14
version: 1.1
softdepend:
- AuthMe
- floodgate
Loading…
Cancel
Save