Files
src
build.gradle.kts
docs.md
manifest.kod
settings.gradle.kts
Code
29 3,572
src/main/java/me/zplugin/zmove/ZMove.java
package me.zplugin.zmove;

import me.zplugin.zmove.build.BuildManager;
import me.zplugin.zmove.build.BuildStorage;
import me.zplugin.zmove.command.CommandConfiguration;
import me.zplugin.zmove.command.ZMoveCommand;
import me.zplugin.zmove.command.ZMoveTabCompleter;
import me.zplugin.zmove.hook.CoreProtectHook;
import me.zplugin.zmove.item.BuildItemManager;
import me.zplugin.zmove.item.ZMToolManager;
import me.zplugin.zmove.listener.BuildItemListener;
import me.zplugin.zmove.listener.PlayerLifecycleListener;
import me.zplugin.zmove.listener.ZMToolListener;
import me.zplugin.zmove.message.MessageManager;
import me.zplugin.zmove.placeholder.ZMoveExpansion;
import me.zplugin.zmove.placement.BuildPlacementManager;
import me.zplugin.zmove.schematic.SchematicManager;
import me.zplugin.zmove.selection.SelectionManager;
import org.bukkit.Bukkit;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import io.papermc.paper.command.brigadier.Commands;
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.AtomicMoveNotSupportedException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public final class ZMove extends JavaPlugin {
    private SelectionManager selectionManager;
    private BuildStorage buildStorage;
    private BuildItemManager buildItemManager;
    private ZMToolManager toolManager;
    private BuildManager buildManager;
    private BuildPlacementManager placementManager;
    private MessageManager messageManager;
    private CommandConfiguration commandConfiguration;
    private boolean commandSummaryLogged;

    @Override
    public void onEnable() {
        try {
            preparePluginFiles();
            reloadConfig();
        } catch (IOException exception) {
            getLogger().severe("Could not prepare ZMove configuration files: " + exception.getMessage());
            Bukkit.getPluginManager().disablePlugin(this);
            return;
        }

        if (Bukkit.getPluginManager().getPlugin("FastAsyncWorldEdit") == null
                || !Bukkit.getPluginManager().isPluginEnabled("FastAsyncWorldEdit")) {
            getLogger().severe("FastAsyncWorldEdit is required. ZMove will be disabled.");
            Bukkit.getPluginManager().disablePlugin(this);
            return;
        }

        commandConfiguration = CommandConfiguration.load(this);
        messageManager = new MessageManager(this);
        selectionManager = new SelectionManager();
        buildStorage = new BuildStorage(this);
        buildItemManager = new BuildItemManager(this, messageManager);
        toolManager = new ZMToolManager(this, messageManager);
        SchematicManager schematicManager = new SchematicManager(this);
        CoreProtectHook coreProtectHook = new CoreProtectHook(this);
        buildManager = new BuildManager(this, selectionManager, buildStorage, schematicManager,
                toolManager, buildItemManager, messageManager);
        placementManager = new BuildPlacementManager(this, buildStorage, schematicManager,
                buildItemManager, coreProtectHook, messageManager);

        ZMoveCommand command = new ZMoveCommand(this, buildManager, buildStorage, toolManager,
                buildItemManager, messageManager, commandConfiguration,
                new ZMoveTabCompleter(buildStorage, commandConfiguration));
        if (commandConfiguration.hasValidMain()) {
            getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS,
                    event -> registerCommands(event.registrar(), command));
        }

        Bukkit.getPluginManager().registerEvents(new ZMToolListener(this, selectionManager,
                toolManager, messageManager), this);
        Bukkit.getPluginManager().registerEvents(new BuildItemListener(buildItemManager,
                placementManager, messageManager), this);
        Bukkit.getPluginManager().registerEvents(new PlayerLifecycleListener(selectionManager, buildManager,
                placementManager), this);
        if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
            new ZMoveExpansion(this, buildStorage, buildItemManager).register();
        }
        getLogger().info("ZMove version " + getDescription().getVersion());
        getLogger().info("Author: ZPlugins");
        getLogger().info("Description: Save, store, and place custom builds");
    }

    @Override
    public void onDisable() {
        if (selectionManager != null) selectionManager.clear();
        if (buildManager != null) buildManager.shutdown();
        if (placementManager != null) placementManager.shutdown();
    }

    public boolean reloadPluginFiles() {
        try {
            preparePluginFiles();
            reloadConfig();
        } catch (IOException exception) {
            getLogger().severe("Could not reload ZMove configuration files: " + exception.getMessage());
            return false;
        }
        if (placementManager != null) placementManager.reloadSettings();
        if (placementManager != null) placementManager.reloadCoreProtect();
        messageManager.reload();
        if (buildStorage != null) {
            try {
                buildStorage.rebuildIndex();
            } catch (IOException exception) {
                getLogger().severe("Could not rebuild the build index during reload: " + exception.getMessage());
                return false;
            }
        }
        CommandConfiguration requested = CommandConfiguration.load(this);
        if (!commandConfiguration.hasSameCommandStructure(requested)) {
            getLogger().warning("Command configuration changed.");
            getLogger().warning("A server restart is required to safely apply command-name changes.");
        }
        return true;
    }

    private void registerCommands(Commands registrar, ZMoveCommand command) {
        List<String> registeredAliases = new ArrayList<>();
        List<String> skippedAliases = new ArrayList<>();
        int conflicts = 0;
        String main = commandConfiguration.main();
        boolean primaryConflict = isRegistered(registrar, main);

        if (primaryConflict) {
            conflicts++;
            if (commandConfiguration.conflictMode().equals("disable")) {
                getLogger().severe("ERROR: Primary command /" + main + " is already registered.");
                getLogger().severe("Change commands.main in config.yml and restart the server.");
                return;
            }
            if (commandConfiguration.conflictMode().equals("warn")) {
                getLogger().warning("Primary command /" + main + " is already registered. Skipping primary command.");
            }
        } else if (registrar.register(main, "ZMove build management", command).contains(main)) {
            getLogger().fine("Registered primary command /" + main + ".");
        } else {
            conflicts++;
            getLogger().warning("Could not safely register primary command /" + main + ".");
        }

        for (String alias : commandConfiguration.aliases()) {
            if (isRegistered(registrar, alias)) {
                conflicts++;
                skippedAliases.add(alias);
                if (commandConfiguration.conflictMode().equals("warn")) {
                    getLogger().warning("Command alias /" + alias
                            + " is already registered by another command. Skipping alias.");
                }
                continue;
            }
            Set<String> result = registrar.register(alias, "ZMove build management", command);
            if (result.contains(alias)) {
                registeredAliases.add(alias);
            } else {
                conflicts++;
                skippedAliases.add(alias);
                if (commandConfiguration.conflictMode().equals("warn")) {
                    getLogger().warning("Could not safely register command alias /" + alias + ". Skipping alias.");
                }
            }
        }
        logCommandSummary(main, registeredAliases, skippedAliases, conflicts);
    }

    private static boolean isRegistered(Commands registrar, String label) {
        return registrar.getDispatcher().getRoot().getChild(label) != null;
    }

    private void logCommandSummary(String main, List<String> registeredAliases,
                                  List<String> skippedAliases, int conflicts) {
        if (!isDebugEnabled() || commandSummaryLogged) return;
        commandSummaryLogged = true;
        getLogger().info("Command system initialized.");
        getLogger().info("Primary command: /" + main);
        getLogger().info("Registered aliases: " + formatLabels(registeredAliases));
        getLogger().info("Skipped aliases: " + formatLabels(skippedAliases));
        getLogger().info("Command conflicts detected: " + conflicts);
    }

    private static String formatLabels(List<String> labels) {
        return labels.isEmpty() ? "none" : labels.stream().map(label -> "/" + label)
                .collect(java.util.stream.Collectors.joining(", "));
    }