Search in sources :

Example 1 with TownyInitException

use of com.palmergames.bukkit.towny.exceptions.initialization.TownyInitException in project Towny by TownyAdvanced.

the class Towny method onEnable.

@Override
public void onEnable() {
    Bukkit.getLogger().info("====================      Towny      ========================");
    townyUniverse = TownyUniverse.getInstance();
    // Setup static classes
    BukkitTools.initialize(this);
    TownyTimerHandler.initialize(this);
    TownyEconomyHandler.initialize(this);
    TownyFormatter.initialize();
    PlayerCacheUtil.initialize(this);
    TownyPerms.initialize(this);
    InviteHandler.initialize(this);
    try {
        // Load the foundation of Towny, containing config, locales, database.
        loadFoundation(false);
        // Check for plugins that we use or we develop.
        // N.B. Includes the hook for TownyChat
        checkPlugins();
        // Make sure the timers are stopped for a reset, then started.
        cycleTimers();
        // Reset the player cache.
        resetCache();
        // Check for plugin updates if the Minecraft version is still supported.
        if (isMinecraftVersionStillSupported())
            TownyUpdateChecker.checkForUpdates(this);
        // Initialize SpawnUtil only after the Translation class has figured out a language.
        // N.B. Important that localization loaded correctly for this step.
        SpawnUtil.initialize(this);
        // Setup bukkit command interfaces
        registerSpecialCommands();
        registerCommands();
        // Add custom metrics charts.
        addMetricsCharts();
    } catch (TownyInitException tie) {
        addError(tie.getError());
        getLogger().log(Level.SEVERE, tie.getMessage(), tie);
    }
    // NOTE: Runs regardless if Towny errors out!
    // Important for safe mode.
    adventure = BukkitAudiences.create(this);
    // If we aren't going to enter safe mode, do the following:
    if (!isError() && TownySettings.isTownyUpdating(getVersion())) {
        printChangelogToConsole();
        // Update config with new version.
        TownySettings.setLastRunVersion(getVersion());
        // Save database.
        townyUniverse.getDataSource().saveAll();
        // cleanup() updates SQL schema for any changes.
        townyUniverse.getDataSource().cleanup();
    }
    // Hence, these if checks.
    if (!isError(TownyInitException.TownyError.MAIN_CONFIG) && !isError(TownyInitException.TownyError.PERMISSIONS)) {
        // Register all child permissions for ranks
        TownyPerms.registerPermissionNodes();
    }
    registerEvents();
    Bukkit.getLogger().info("=============================================================");
    if (isError()) {
        plugin.getLogger().warning("[WARNING] - ***** SAFE MODE ***** " + version);
    } else {
        plugin.getLogger().info("Version: " + version + " - Plugin Enabled");
    }
    Bukkit.getLogger().info("=============================================================");
    if (!isError()) {
        // Re login anyone online. (In case of plugin reloading)
        for (Player player : BukkitTools.getOnlinePlayers()) if (player != null) {
            // Test and kick any players with invalid names.
            if (player.getName().contains(" ")) {
                player.kickPlayer("Invalid name!");
                return;
            }
            // Perform login code in it's own thread to update Towny data.
            if (BukkitTools.scheduleSyncDelayedTask(new OnPlayerLogin(this, player), 0L) == -1) {
                TownyMessaging.sendErrorMsg("Could not schedule OnLogin.");
            }
        }
    }
}
Also used : Player(org.bukkit.entity.Player) OnPlayerLogin(com.palmergames.bukkit.towny.tasks.OnPlayerLogin) TownyInitException(com.palmergames.bukkit.towny.exceptions.initialization.TownyInitException)

Example 2 with TownyInitException

use of com.palmergames.bukkit.towny.exceptions.initialization.TownyInitException in project Towny by TownyAdvanced.

the class TownyUniverse method loadDatabase.

/**
 * Loads the database into memory.
 *
 * @param loadDbType - load setting from the config.
 * @return true when the database will load.
 */
private boolean loadDatabase(String loadDbType) {
    long startTime = System.currentTimeMillis();
    /*
         * Select the datasource.
         */
    switch(loadDbType.toLowerCase()) {
        case "ff":
        case "flatfile":
            {
                this.dataSource = new TownyFlatFileSource(towny, this);
                break;
            }
        case "mysql":
            {
                this.dataSource = new TownySQLSource(towny, this);
                break;
            }
        default:
            {
                throw new TownyInitException("Database: Database.yml unsupported load format: " + loadDbType, TownyInitException.TownyError.DATABASE_CONFIG);
            }
    }
    /*
         * Load the actual database.
         */
    if (!dataSource.loadAll())
        throw new TownyInitException("Database: Failed to load database.", TownyInitException.TownyError.DATABASE);
    long time = System.currentTimeMillis() - startTime;
    towny.getLogger().info("Database: Loaded in " + time + "ms.");
    // TODO: remove this when we're using UUIDs directly in the database.
    towny.getLogger().info("Database: " + TownySettings.getUUIDPercent() + " of residents have stored UUIDs.");
    // Throw Event.
    Bukkit.getPluginManager().callEvent(new TownyLoadedDatabaseEvent());
    // Congratulations the Database loaded.
    return true;
}
Also used : TownySQLSource(com.palmergames.bukkit.towny.db.TownySQLSource) TownyLoadedDatabaseEvent(com.palmergames.bukkit.towny.event.TownyLoadedDatabaseEvent) TownyFlatFileSource(com.palmergames.bukkit.towny.db.TownyFlatFileSource) TownyInitException(com.palmergames.bukkit.towny.exceptions.initialization.TownyInitException)

Example 3 with TownyInitException

use of com.palmergames.bukkit.towny.exceptions.initialization.TownyInitException in project Towny by TownyAdvanced.

the class TownyPerms method loadPerms.

/**
 * Load the townyperms.yml file.
 * If it doesn't exist create it from the resource file in the jar.
 *
 * @param permsYMLPath - Path to townyperms.yml in the data directory.
 * @throws TownyInitException - Thrown when if we fail to copy or load townyperms.yml.
 */
public static void loadPerms(@NotNull Path permsYMLPath) {
    try {
        InputStream resource = Towny.class.getResourceAsStream("/townyperms.yml");
        if (resource == null) {
            throw new TownyInitException("Could not find 'townyperms.yml' in the JAR.", TownyInitException.TownyError.PERMISSIONS);
        }
        Files.copy(resource, permsYMLPath);
    } catch (FileAlreadyExistsException ignored) {
    // Expected behaviour
    } catch (IOException e) {
        throw new TownyInitException("Could not copy townyperms.yml from JAR to '" + permsYMLPath + "'.", TownyInitException.TownyError.PERMISSIONS, e);
    }
    // read the townyperms.yml into memory
    perms = new CommentedConfiguration(permsYMLPath);
    if (!perms.load()) {
        throw new TownyInitException("Could not read townyperms.yml", TownyInitException.TownyError.PERMISSIONS);
    }
    groupPermsMap.clear();
    buildGroupPermsMap();
    checkForVitalGroups();
    buildComments();
    perms.save();
    /*
		 * Only do this once as we are really only interested in Towny perms.
		 */
    collectPermissions();
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) InputStream(java.io.InputStream) CommentedConfiguration(com.palmergames.bukkit.config.CommentedConfiguration) IOException(java.io.IOException) TownyInitException(com.palmergames.bukkit.towny.exceptions.initialization.TownyInitException)

Example 4 with TownyInitException

use of com.palmergames.bukkit.towny.exceptions.initialization.TownyInitException in project Towny by TownyAdvanced.

the class Towny method registerSpecialCommands.

// https://www.spigotmc.org/threads/small-easy-register-command-without-plugin-yml.38036/
private void registerSpecialCommands() {
    List<Command> commands = new ArrayList<>(4);
    commands.add(new AcceptCommand(TownySettings.getAcceptCommand()));
    commands.add(new DenyCommand(TownySettings.getDenyCommand()));
    commands.add(new ConfirmCommand(TownySettings.getConfirmCommand()));
    commands.add(new CancelCommand(TownySettings.getCancelCommand()));
    try {
        final Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap");
        bukkitCommandMap.setAccessible(true);
        CommandMap commandMap = (CommandMap) bukkitCommandMap.get(Bukkit.getServer());
        commandMap.registerAll("towny", commands);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        throw new TownyInitException("An issue has occured while registering custom commands.", TownyInitException.TownyError.OTHER, e);
    }
}
Also used : ArrayList(java.util.ArrayList) ConfirmCommand(com.palmergames.bukkit.towny.command.commandobjects.ConfirmCommand) TownyInitException(com.palmergames.bukkit.towny.exceptions.initialization.TownyInitException) Field(java.lang.reflect.Field) DenyCommand(com.palmergames.bukkit.towny.command.commandobjects.DenyCommand) CancelCommand(com.palmergames.bukkit.towny.command.commandobjects.CancelCommand) ResidentCommand(com.palmergames.bukkit.towny.command.ResidentCommand) DenyCommand(com.palmergames.bukkit.towny.command.commandobjects.DenyCommand) TownyAdminCommand(com.palmergames.bukkit.towny.command.TownyAdminCommand) TownCommand(com.palmergames.bukkit.towny.command.TownCommand) TownyCommand(com.palmergames.bukkit.towny.command.TownyCommand) InviteCommand(com.palmergames.bukkit.towny.command.InviteCommand) TownyWorldCommand(com.palmergames.bukkit.towny.command.TownyWorldCommand) Command(org.bukkit.command.Command) CancelCommand(com.palmergames.bukkit.towny.command.commandobjects.CancelCommand) PlotCommand(com.palmergames.bukkit.towny.command.PlotCommand) ConfirmCommand(com.palmergames.bukkit.towny.command.commandobjects.ConfirmCommand) NationCommand(com.palmergames.bukkit.towny.command.NationCommand) AcceptCommand(com.palmergames.bukkit.towny.command.commandobjects.AcceptCommand) AcceptCommand(com.palmergames.bukkit.towny.command.commandobjects.AcceptCommand) CommandMap(org.bukkit.command.CommandMap)

Aggregations

TownyInitException (com.palmergames.bukkit.towny.exceptions.initialization.TownyInitException)4 CommentedConfiguration (com.palmergames.bukkit.config.CommentedConfiguration)1 InviteCommand (com.palmergames.bukkit.towny.command.InviteCommand)1 NationCommand (com.palmergames.bukkit.towny.command.NationCommand)1 PlotCommand (com.palmergames.bukkit.towny.command.PlotCommand)1 ResidentCommand (com.palmergames.bukkit.towny.command.ResidentCommand)1 TownCommand (com.palmergames.bukkit.towny.command.TownCommand)1 TownyAdminCommand (com.palmergames.bukkit.towny.command.TownyAdminCommand)1 TownyCommand (com.palmergames.bukkit.towny.command.TownyCommand)1 TownyWorldCommand (com.palmergames.bukkit.towny.command.TownyWorldCommand)1 AcceptCommand (com.palmergames.bukkit.towny.command.commandobjects.AcceptCommand)1 CancelCommand (com.palmergames.bukkit.towny.command.commandobjects.CancelCommand)1 ConfirmCommand (com.palmergames.bukkit.towny.command.commandobjects.ConfirmCommand)1 DenyCommand (com.palmergames.bukkit.towny.command.commandobjects.DenyCommand)1 TownyFlatFileSource (com.palmergames.bukkit.towny.db.TownyFlatFileSource)1 TownySQLSource (com.palmergames.bukkit.towny.db.TownySQLSource)1 TownyLoadedDatabaseEvent (com.palmergames.bukkit.towny.event.TownyLoadedDatabaseEvent)1 OnPlayerLogin (com.palmergames.bukkit.towny.tasks.OnPlayerLogin)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1