Files
src
build.gradle
README.md
Code
26 2,245
src/main/java/com/auluckyblockparty/AuLuckyBlockParty.java
package com.auluckyblockparty;
import com.auluckyblockparty.license.LicenseCommand;
import com.auluckyblockparty.license.LicensedCommandExecutor;
import com.auluckyblockparty.license.LicenseManager;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.java.JavaPlugin;
public class AuLuckyBlockParty extends JavaPlugin {
private LicenseManager licenseManager;
// Guards against loadFeatures() being called more than once.
private boolean featuresLoaded = false;
@Override
public void onEnable() {
// ── Step 1: Create the data folder and save license.yml only ──────────
if (!getDataFolder().exists()) {
getDataFolder().mkdirs();
}
saveLicenseConfig();
// ── Step 2: Bootstrap the license subsystem ───────────────────────────
licenseManager = new LicenseManager(this);
// Register the license-reload command immediately — it must always work.
PluginCommand adminCmd = getCommand("admin");
if (adminCmd != null) {
LicenseCommand licenseCommand = new LicenseCommand(this, licenseManager);
adminCmd.setExecutor(licenseCommand);
adminCmd.setTabCompleter(licenseCommand);
}
// ── Step 3: Async license check — no other files loaded until valid ───
// Auto-gate all feature commands immediately, before the async check finishes.
// This prevents a tiny race window where a command could fire before the
// async verification returns its result.
autoGateAllCommands();
licenseManager.verifyAsync(
/* onValid */ this::loadFeatures,
/* onInvalid */ this::disableFeatures
);
}
@Override
public void onDisable() {
// Clean shutdown — cancel any tasks, close resources, etc.
getLogger().info("AuLuckyBlockParty has been disabled.");
}
// ─────────────────────────────────────────────────────────────────────────
// Feature gate
// ─────────────────────────────────────────────────────────────────────────
/**
* Called exactly once when the license first becomes valid (either on startup
* or after a successful /admin license-reload while previously invalid).
* Loads config.yml and data.yml and registers all plugin internals.
*/
public void loadFeatures() {
if (featuresLoaded) return;
featuresLoaded = true;
getLogger().info("License validated. Loading plugin features...");
// ── Load configuration files ──────────────────────────────────────────
saveDefaultConfig(); // config.yml
saveResource("data.yml", false); // data.yml (only if absent)
// ── Register events ───────────────────────────────────────────────────
// Example: getServer().getPluginManager().registerEvents(new YourListener(this), this);
// ── Register feature commands ─────────────────────────────────────────
// Assign the real executor; autoGateAllCommands() already wrapped every
// command with LicensedCommandExecutor on startup, so we must set the
// real executor THROUGH the existing wrapper rather than bypassing it.
// Use registerLicensedCommand() which always produces a clean single wrap.
//
// Example:
// registerLicensedCommand("luckyblock", new LuckyBlockCommand(this));
getLogger().info("AuLuckyBlockParty is fully enabled.");
}
private void disableFeatures() {
getLogger().warning(
"[AuLuckyBlockParty] Error: Invalid license or connection failure. Features disabled.");
// Plugin stays enabled; only internal mechanisms are inactive.
}
// ─────────────────────────────────────────────────────────────────────────
// Helpers
// ─────────────────────────────────────────────────────────────────────────
/**
* Registers a command executor behind the license gate.
* If the command was already auto-gated by autoGateAllCommands(), this replaces
* the inner delegate instead of stacking a second wrapper on top.
*
* @param commandName name as declared in plugin.yml (without the slash)
* @param executor the real executor to delegate to when license is valid
*/
public void registerLicensedCommand(String commandName, CommandExecutor executor) {
PluginCommand cmd = getCommand(commandName);
if (cmd == null) {
getLogger().warning("Command '" + commandName + "' is not declared in plugin.yml.");
return;
}
// Always create a fresh wrapper with the real executor — avoids double-wrapping.
cmd.setExecutor(new LicensedCommandExecutor(licenseManager, executor));
}
/**
* Automatically wraps ALL commands declared in plugin.yml (except "admin") with
* LicensedCommandExecutor. This is called immediately in onEnable() so that even
* commands whose real executors haven't been set yet are gated — they will block
* with the license error message until loadFeatures() sets the real executor
* (which itself is wrapped).
*
* This means developers NEVER need to remember to call registerLicensedCommand();
* every new command added to plugin.yml is automatically protected.
*/
private void autoGateAllCommands() {
if (getDescription().getCommands() == null) return;
for (String commandName : getDescription().getCommands().keySet()) {
if (commandName.equalsIgnoreCase("admin")) continue; // license-reload must always work
PluginCommand cmd = getCommand(commandName);
if (cmd == null) continue;
CommandExecutor current = cmd.getExecutor();
// Only wrap if not already wrapped (prevents double-wrapping on re-registration)
if (!(current instanceof LicensedCommandExecutor)) {
cmd.setExecutor(new LicensedCommandExecutor(licenseManager, current));
}
}
}
public LicenseManager getLicenseManager() {
return licenseManager;
}
// ─────────────────────────────────────────────────────────────────────────
// license.yml bootstrap
// ─────────────────────────────────────────────────────────────────────────
private void saveLicenseConfig() {
java.io.File licenseFile = new java.io.File(getDataFolder(), "license.yml");
if (!licenseFile.exists()) {
saveResource("license.yml", false);
}
}
}