No chat to show

This project's files are all here to explore.

Files
src
build.gradle
README.md
Code
26 2,587
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 com.niloy.auluckyblockparty.*;
import com.niloy.auluckyblockparty.command.LvpCommand;
import com.niloy.auluckyblockparty.listener.BlockInteractListener;
import com.niloy.auluckyblockparty.listener.VoteListener;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.java.JavaPlugin;

@Getter
public class AuLuckyBlockParty extends JavaPlugin {

    private LicenseManager licenseManager;

    // Feature managers
    private ConfigManager configManager;
    private LuckyBlockManager luckyBlockManager;

    // 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.
        if (featuresLoaded && luckyBlockManager != null) {
            luckyBlockManager.shutdownAll();
        }
        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 every luckyblocks/<id>.yml, and registers all
     * plugin internals.
     */
    public void loadFeatures() {
        if (featuresLoaded) return;
        featuresLoaded = true;

        getLogger().info("License validated. Loading plugin features...");

        // Load configuration files - only when license is valid
        saveDefaultConfig();

        // Initialize managers
        configManager = new ConfigManager(this);
        luckyBlockManager = new LuckyBlockManager(this);

        getLogger().info("Loaded " + luckyBlockManager.getAll().size() + " lucky block(s): "
                + String.join(", ", luckyBlockManager.getAll().keySet()));

        // Register feature commands
        registerLicensedCommand("lvp", new LvpCommand(this));

        // Register event listeners
        Bukkit.getPluginManager().registerEvents(new BlockInteractListener(this), this);

        // Hook Votifier if present
        if (Bukkit.getPluginManager().getPlugin("Votifier") != null
                || Bukkit.getPluginManager().getPlugin("NuVotifier") != null) {
            Bukkit.getPluginManager().registerEvents(new VoteListener(this), this);
            getLogger().info("Votifier hooked successfully.");
        }

        // Hook PlaceholderAPI if present
        if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
            new AuLuckyBlockPartyPlaceholder(this).register();
            getLogger().info("PlaceholderAPI hooked successfully.");
        }

        getLogger().info("AuLuckyBlockParty is fully enabled.");
    }

    /**
     * Unloads all features when license becomes invalid.
     * Saves data, cleans up resources, and nullifies all managers.
     */
    public void unloadFeatures() {
        if (!featuresLoaded) return;

        getLogger().warning("License is invalid. Unloading all features...");

        // Save data and clean up every lucky block before shutdown
        if (luckyBlockManager != null) {
            luckyBlockManager.shutdownAll();
        }

        // Nullify all managers - config.yml and luckyblocks/*.yml no longer accessible
        configManager = null;
        luckyBlockManager = null;

        featuresLoaded = false;
        getLogger().warning("All features have been disabled. Config and data files are no longer loaded.");
    }

    private void disableFeatures() {
        unloadFeatures();
    }

    // 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));

        // Set tab completer if executor supports it
        if (executor instanceof org.bukkit.command.TabExecutor) {
            cmd.setTabCompleter((org.bukkit.command.TabExecutor) 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);
        }
    }