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.");
}
}
}
}
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;
}
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();
}
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);
}
}
Aggregations