package com.graywolf336.trailingperspective;

import org.bukkit.plugin.java.JavaPlugin;

import com.graywolf336.trailingperspective.commands.SetTrailerHomeCommand;
import com.graywolf336.trailingperspective.commands.SwitchPerspectiveCommand;
import com.graywolf336.trailingperspective.commands.ToggleBeingTrailerCommand;
import com.graywolf336.trailingperspective.enums.Settings;
import com.graywolf336.trailingperspective.interfaces.ITrailerManager;
import com.graywolf336.trailingperspective.listeners.TrailingPerspectivePlayerListener;
import com.graywolf336.trailingperspective.managers.TrailerManager;
import com.graywolf336.trailingperspective.workers.HomeSendingWorker;
import com.graywolf336.trailingperspective.workers.SpectatorSetWorker;
import com.graywolf336.trailingperspective.workers.TrailerInformationWorker;
import com.graywolf336.trailingperspective.workers.TrailersPerspectiveWorker;

public class TrailingPerspectiveMain extends JavaPlugin {
    private boolean debug = false;
    private ITrailerManager trailerManager;

    public void onLoad() {
        this.saveDefaultConfig();
    }

    public void onEnable() {
        Settings.setPlugin(this);
        this.debug = Settings.DEBUG.asBoolean();

        this.trailerManager = new TrailerManager();
        this.getServer().getPluginManager().registerEvents(new TrailingPerspectivePlayerListener(this), this);
        this.setupCommands();
        this.setupWorkers();
    }

    public void onDisable() {
        if (this.trailerManager != null) {
            this.trailerManager.removeAllTrailers();
        }

        this.getServer().getScheduler().cancelTasks(this);
        this.trailerManager = null;
    }

    /**
     * Gets the current instance of the {@link ITrailerManager}.
     *
     * @return the trailer manager instance
     */
    public ITrailerManager getTrailerManager() {
        return this.trailerManager;
    }

    /**
     * Prints messages if the plugin is in a debugging state.
     *
     * @param colored whether the messages are colored.
     * @param msgs the messages to print.
     */
    public void debug(boolean colored, String... msgs) {
        if (this.debug) {
            for (String s : msgs) {
                if (colored) {
                    getServer().getConsoleSender().sendMessage("[TrailingPerspective] [Debug]: " + s);
                } else {
                    getLogger().info("[Debug]: " + s);
                }
            }
        }
    }

    private void setupCommands() {
        ToggleBeingTrailerCommand toggleCmd = new ToggleBeingTrailerCommand(this);
        this.getCommand("togglebeingtrailer").setExecutor(toggleCmd);
        this.getCommand("togglebeingtrailer").setTabCompleter(toggleCmd);

        SetTrailerHomeCommand homeCmd = new SetTrailerHomeCommand();
        this.getCommand("settrailerhome").setExecutor(homeCmd);
        this.getCommand("settrailerhome").setTabCompleter(homeCmd);

        SwitchPerspectiveCommand switchCmd = new SwitchPerspectiveCommand(this);
        this.getCommand("switchperspective").setExecutor(switchCmd);
        this.getCommand("switchperspective").setTabCompleter(switchCmd);
    }

    private void setupWorkers() {
        this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new SpectatorSetWorker(this), 20, 5);
        this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new TrailersPerspectiveWorker(this), 20, 10);
        this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new TrailerInformationWorker(this), 20, 10);
        this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new HomeSendingWorker(this), 20, 20);
    }
}

@@ -1092,24 +1092,81 @@
 ltConfig();%0A
+        %0A        TrailingPerspectiveAPI.setPlugin(this);%0A
     %7D%0A%0A    p

Set the plugin instance onLoad