Files
src
build.gradle
settings.gradle
Code
36 2,931
src/main/java/com/yourname/securelogin/SecureLogin.java
package com.yourname.securelogin;
import com.yourname.securelogin.auth.AuthManager;
import com.yourname.securelogin.auth.BruteForceProtector;
import com.yourname.securelogin.auth.SessionManager;
import com.yourname.securelogin.commands.*;
import com.yourname.securelogin.config.ConfigManager;
import com.yourname.securelogin.data.DataStorage;
import com.yourname.securelogin.data.MySQLStorage;
import com.yourname.securelogin.data.YamlStorage;
import com.yourname.securelogin.inventory.gui.GUIListener;
import com.yourname.securelogin.inventory.gui.GUIManager;
import com.yourname.securelogin.license.LicenseManager;
import com.yourname.securelogin.listener.AuthListener;
import com.yourname.securelogin.messages.MessageManager;
import com.yourname.securelogin.totp.TOTPManager;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandExecutor;
import org.bukkit.plugin.java.JavaPlugin;
@Getter
public class SecureLogin extends JavaPlugin {
private static SecureLogin instance;
private ConfigManager configManager;
private MessageManager messageManager;
private DataStorage dataStorage;
private AuthManager authManager;
private SessionManager sessionManager;
private BruteForceProtector bruteForceProtector;
private TOTPManager totpManager;
private LicenseManager licenseManager;
private GUIManager guiManager;
@Override
public void onEnable() {
instance = this;
saveDefaultConfig();
// Core managers (no deps)
configManager = new ConfigManager(this);
messageManager = new MessageManager(this);
// License verification
licenseManager = new LicenseManager(this);
licenseManager.verify();
// Reload config after license sets premium field
configManager.reload();
// Initialize subsystem managers
sessionManager = new SessionManager(this);
bruteForceProtector = new BruteForceProtector(this);
totpManager = new TOTPManager();
// Initialize storage
setupStorage();
// Initialize auth manager
authManager = new AuthManager(this, dataStorage, sessionManager, bruteForceProtector);
// Register event listeners
getServer().getPluginManager().registerEvents(new AuthListener(this), this);
// Register commands
registerCommands();
// Register GUI system
guiManager = new GUIManager();
getServer().getPluginManager().registerEvents(new GUIListener(guiManager), this);
// Register PlaceholderAPI expansion (premium)
if (licenseManager.isPremium() && getServer().getPluginManager().getPlugin("PlaceholderAPI") != null) {
try {
Class.forName("me.clip.placeholderapi.expansion.PlaceholderExpansion");
new com.yourname.securelogin.api.PlaceholderAPIExpansion(this).register();
getLogger().info("PlaceholderAPI expansion registered.");
} catch (ClassNotFoundException e) {
getLogger().info("PlaceholderAPI not found — skipping expansion.");
}
}
// Auto-update check
if (configManager.isAutoUpdateCheck()) {
checkForUpdates();
}
getLogger().info("SecureLogin v" + getDescription().getVersion() + " enabled.");
if (licenseManager.isPremium()) {
getLogger().info("Premium features are ACTIVE.");
} else {
getLogger().info("Running in core mode.");
}
}
@Override
public void onDisable() {
if (authManager != null) {
// Save all cached data
Bukkit.getOnlinePlayers().forEach(authManager::handleQuit);
}
if (dataStorage != null) {
dataStorage.close();
}
getLogger().info("SecureLogin disabled.");
}
private void setupStorage() {
String type = configManager.getStorageType();
if (type.equalsIgnoreCase("mysql")) {
MySQLStorage mysql = new MySQLStorage(this);
boolean connected = mysql.connect(
configManager.getMysqlHost(),
configManager.getMysqlPort(),
configManager.getMysqlDatabase(),
configManager.getMysqlUsername(),
configManager.getMysqlPassword(),
configManager.isMysqlUseSSL(),
configManager.getMysqlPoolSize()
);
if (connected) {
dataStorage = mysql;
getLogger().info("Using MySQL storage.");
} else {
getLogger().warning("MySQL connection failed — falling back to YAML.");
dataStorage = new YamlStorage(this);
}
} else {
dataStorage = new YamlStorage(this);
getLogger().info("Using YAML storage.");
}
}
private void registerCommands() {
setCommandExecutor("register", new RegisterCommand(this));
setCommandExecutor("login", new LoginCommand(this));
setCommandExecutor("logout", new LogoutCommand(this));
setCommandExecutor("changepassword", new ChangePasswordCommand(this));
setCommandExecutor("securelogin", new SecureLoginAdminCommand(this));
setCommandExecutor("2fa", new TwoFACommand(this));
}
private void setCommandExecutor(String name, CommandExecutor executor) {
org.bukkit.command.PluginCommand cmd = getCommand(name);
if (cmd != null) {
cmd.setExecutor(executor);
} else {
getLogger().severe("Command '" + name + "' not found in plugin.yml!");
}
}
private void checkForUpdates() {
getLogger().info("Checking for updates...");
// Placeholder — log if update exists
getServer().getScheduler().runTaskAsynchronously(this, () -> {
try {
java.net.URL url = new java.net.URL("https://api.spigotmc.org/legacy/update.php?resource=PLACEHOLDER_ID");
java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection();
conn.setConnectTimeout(3000);
conn.setReadTimeout(3000);
try (java.io.BufferedReader reader = new java.io.BufferedReader(
new java.io.InputStreamReader(conn.getInputStream()))) {
String latest = reader.readLine();
if (latest != null && !latest.equals(getDescription().getVersion())) {
getLogger().info("Update available: v" + latest + " (current: v" + getDescription().getVersion() + ")");
}
}
conn.disconnect();
} catch (Exception e) {
getLogger().info("Could not check for updates: " + e.getMessage());
}
});
}
public static SecureLogin getInstance() {
return instance;
}
}