Del all files
This commit is contained in:
@@ -1,14 +0,0 @@
|
||||
package fr.xephi.authme.initialization;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Annotation for specifying the plugin's data folder.
|
||||
*/
|
||||
@Target({ElementType.PARAMETER, ElementType.FIELD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface DataFolder {
|
||||
}
|
||||
@@ -1,101 +0,0 @@
|
||||
package fr.xephi.authme.initialization;
|
||||
|
||||
import fr.xephi.authme.ConsoleLogger;
|
||||
import fr.xephi.authme.data.auth.PlayerCache;
|
||||
import fr.xephi.authme.datasource.CacheDataSource;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.datasource.DataSourceType;
|
||||
import fr.xephi.authme.datasource.MariaDB;
|
||||
import fr.xephi.authme.datasource.MySQL;
|
||||
import fr.xephi.authme.datasource.PostgreSqlDataSource;
|
||||
import fr.xephi.authme.datasource.SQLite;
|
||||
import fr.xephi.authme.datasource.mysqlextensions.MySqlExtensionsFactory;
|
||||
import fr.xephi.authme.output.ConsoleLoggerFactory;
|
||||
import fr.xephi.authme.service.BukkitService;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.DatabaseSettings;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Provider;
|
||||
import java.io.File;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* Creates the AuthMe data source.
|
||||
*/
|
||||
public class DataSourceProvider implements Provider<DataSource> {
|
||||
|
||||
private static final int SQLITE_MAX_SIZE = 4000;
|
||||
|
||||
private final ConsoleLogger logger = ConsoleLoggerFactory.get(DataSourceProvider.class);
|
||||
|
||||
@Inject
|
||||
@DataFolder
|
||||
private File dataFolder;
|
||||
@Inject
|
||||
private Settings settings;
|
||||
@Inject
|
||||
private BukkitService bukkitService;
|
||||
@Inject
|
||||
private PlayerCache playerCache;
|
||||
@Inject
|
||||
private MySqlExtensionsFactory mySqlExtensionsFactory;
|
||||
|
||||
DataSourceProvider() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataSource get() {
|
||||
try {
|
||||
return createDataSource();
|
||||
} catch (Exception e) {
|
||||
logger.logException("Could not create data source:", e);
|
||||
throw new IllegalStateException("Error during initialization of data source", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the data source.
|
||||
*
|
||||
* @return the constructed data source
|
||||
* @throws SQLException when initialization of a SQL data source failed
|
||||
*/
|
||||
private DataSource createDataSource() throws SQLException {
|
||||
DataSourceType dataSourceType = settings.getProperty(DatabaseSettings.BACKEND);
|
||||
DataSource dataSource;
|
||||
switch (dataSourceType) {
|
||||
case MYSQL:
|
||||
dataSource = new MySQL(settings, mySqlExtensionsFactory);
|
||||
break;
|
||||
case MARIADB:
|
||||
dataSource = new MariaDB(settings, mySqlExtensionsFactory);
|
||||
break;
|
||||
case POSTGRESQL:
|
||||
dataSource = new PostgreSqlDataSource(settings, mySqlExtensionsFactory);
|
||||
break;
|
||||
case SQLITE:
|
||||
dataSource = new SQLite(settings, dataFolder);
|
||||
break;
|
||||
default:
|
||||
throw new UnsupportedOperationException("Unknown data source type '" + dataSourceType + "'");
|
||||
}
|
||||
|
||||
if (settings.getProperty(DatabaseSettings.USE_CACHING)) {
|
||||
dataSource = new CacheDataSource(dataSource, playerCache);
|
||||
}
|
||||
if (DataSourceType.SQLITE.equals(dataSourceType)) {
|
||||
checkDataSourceSize(dataSource);
|
||||
}
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
private void checkDataSourceSize(DataSource dataSource) {
|
||||
bukkitService.runTaskAsynchronously(() -> {
|
||||
int accounts = dataSource.getAccountsRegistered();
|
||||
if (accounts >= SQLITE_MAX_SIZE) {
|
||||
logger.warning("YOU'RE USING THE SQLITE DATABASE WITH "
|
||||
+ accounts + "+ ACCOUNTS; FOR BETTER PERFORMANCE, PLEASE UPGRADE TO MYSQL!!");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package fr.xephi.authme.initialization;
|
||||
|
||||
/**
|
||||
* Common interface for types which have data that becomes outdated
|
||||
* and that can be cleaned up periodically.
|
||||
*
|
||||
* @see fr.xephi.authme.task.CleanupTask
|
||||
*/
|
||||
public interface HasCleanup {
|
||||
|
||||
/**
|
||||
* Performs the cleanup action.
|
||||
*/
|
||||
void performCleanup();
|
||||
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
package fr.xephi.authme.initialization;
|
||||
|
||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
||||
import fr.xephi.authme.data.auth.PlayerCache;
|
||||
import fr.xephi.authme.data.limbo.LimboService;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.service.BukkitService;
|
||||
import fr.xephi.authme.service.ValidationService;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.SpawnLoader;
|
||||
import fr.xephi.authme.settings.properties.RestrictionSettings;
|
||||
import fr.xephi.authme.util.PlayerUtils;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Saves all players' data when the plugin shuts down.
|
||||
*/
|
||||
public class OnShutdownPlayerSaver {
|
||||
|
||||
@Inject
|
||||
private BukkitService bukkitService;
|
||||
@Inject
|
||||
private Settings settings;
|
||||
@Inject
|
||||
private ValidationService validationService;
|
||||
@Inject
|
||||
private DataSource dataSource;
|
||||
@Inject
|
||||
private SpawnLoader spawnLoader;
|
||||
@Inject
|
||||
private PlayerCache playerCache;
|
||||
@Inject
|
||||
private LimboService limboService;
|
||||
|
||||
OnShutdownPlayerSaver() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the data of all online players.
|
||||
*/
|
||||
public void saveAllPlayers() {
|
||||
for (Player player : bukkitService.getOnlinePlayers()) {
|
||||
savePlayer(player);
|
||||
}
|
||||
}
|
||||
|
||||
private void savePlayer(Player player) {
|
||||
String name = player.getName().toLowerCase(Locale.ROOT);
|
||||
if (PlayerUtils.isNpc(player) || validationService.isUnrestricted(name)) {
|
||||
return;
|
||||
}
|
||||
if (limboService.hasLimboPlayer(name)) {
|
||||
limboService.restoreData(player);
|
||||
} else {
|
||||
saveLoggedinPlayer(player);
|
||||
}
|
||||
playerCache.removePlayer(name);
|
||||
}
|
||||
|
||||
private void saveLoggedinPlayer(Player player) {
|
||||
if (settings.getProperty(RestrictionSettings.SAVE_QUIT_LOCATION)) {
|
||||
Location loc = spawnLoader.getPlayerLocationOrSpawn(player);
|
||||
PlayerAuth auth = PlayerAuth.builder()
|
||||
.name(player.getName().toLowerCase(Locale.ROOT))
|
||||
.realName(player.getName())
|
||||
.location(loc).build();
|
||||
dataSource.updateQuitLoc(auth);
|
||||
// TODO: send an update when a messaging service will be implemented (QUITLOC)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,114 +0,0 @@
|
||||
package fr.xephi.authme.initialization;
|
||||
|
||||
import fr.xephi.authme.AuthMe;
|
||||
import fr.xephi.authme.ConsoleLogger;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import fr.xephi.authme.message.MessageKey;
|
||||
import fr.xephi.authme.message.Messages;
|
||||
import fr.xephi.authme.output.ConsoleFilter;
|
||||
import fr.xephi.authme.output.ConsoleLoggerFactory;
|
||||
import fr.xephi.authme.output.Log4JFilter;
|
||||
import fr.xephi.authme.service.BukkitService;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.properties.DatabaseSettings;
|
||||
import fr.xephi.authme.settings.properties.EmailSettings;
|
||||
import fr.xephi.authme.settings.properties.PluginSettings;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.bstats.bukkit.Metrics;
|
||||
import org.bstats.charts.SimplePie;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static fr.xephi.authme.service.BukkitService.TICKS_PER_MINUTE;
|
||||
import static fr.xephi.authme.settings.properties.EmailSettings.RECALL_PLAYERS;
|
||||
|
||||
/**
|
||||
* Contains actions such as migrations that should be performed on startup.
|
||||
*/
|
||||
public class OnStartupTasks {
|
||||
|
||||
private static ConsoleLogger consoleLogger = ConsoleLoggerFactory.get(OnStartupTasks.class);
|
||||
|
||||
@Inject
|
||||
private DataSource dataSource;
|
||||
@Inject
|
||||
private Settings settings;
|
||||
@Inject
|
||||
private BukkitService bukkitService;
|
||||
@Inject
|
||||
private Messages messages;
|
||||
|
||||
OnStartupTasks() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends bstats metrics.
|
||||
*
|
||||
* @param plugin the plugin instance
|
||||
* @param settings the settings
|
||||
*/
|
||||
public static void sendMetrics(AuthMe plugin, Settings settings) {
|
||||
final Metrics metrics = new Metrics(plugin, 164);
|
||||
|
||||
metrics.addCustomChart(new SimplePie("messages_language",
|
||||
() -> settings.getProperty(PluginSettings.MESSAGES_LANGUAGE)));
|
||||
metrics.addCustomChart(new SimplePie("database_backend",
|
||||
() -> settings.getProperty(DatabaseSettings.BACKEND).toString()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up the console filter if enabled.
|
||||
*
|
||||
* @param logger the plugin logger
|
||||
*/
|
||||
public static void setupConsoleFilter(Logger logger) {
|
||||
// Try to set the log4j filter
|
||||
try {
|
||||
Class.forName("org.apache.logging.log4j.core.filter.AbstractFilter");
|
||||
setLog4JFilter();
|
||||
} catch (ClassNotFoundException | NoClassDefFoundError e) {
|
||||
// log4j is not available
|
||||
consoleLogger.info("You're using Minecraft 1.6.x or older, Log4J support will be disabled");
|
||||
ConsoleFilter filter = new ConsoleFilter();
|
||||
logger.setFilter(filter);
|
||||
Bukkit.getLogger().setFilter(filter);
|
||||
Logger.getLogger("Minecraft").setFilter(filter);
|
||||
}
|
||||
}
|
||||
|
||||
// Set the console filter to remove the passwords
|
||||
private static void setLog4JFilter() {
|
||||
org.apache.logging.log4j.core.Logger logger;
|
||||
logger = (org.apache.logging.log4j.core.Logger) LogManager.getRootLogger();
|
||||
logger.addFilter(new Log4JFilter());
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts a task that regularly reminds players without a defined email to set their email,
|
||||
* if enabled.
|
||||
*/
|
||||
public void scheduleRecallEmailTask() {
|
||||
if (!settings.getProperty(RECALL_PLAYERS)) {
|
||||
return;
|
||||
}
|
||||
bukkitService.runTaskTimerAsynchronously(new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
List<String> loggedPlayersWithEmptyMail = dataSource.getLoggedPlayersWithEmptyMail();
|
||||
bukkitService.runTask(() -> {
|
||||
for (String playerWithoutMail : loggedPlayersWithEmptyMail) {
|
||||
Player player = bukkitService.getPlayerExact(playerWithoutMail);
|
||||
if (player != null) {
|
||||
messages.send(player, MessageKey.ADD_EMAIL_MESSAGE);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}, 1, TICKS_PER_MINUTE * settings.getProperty(EmailSettings.DELAY_RECALL));
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package fr.xephi.authme.initialization;
|
||||
|
||||
/**
|
||||
* Interface for reloadable entities.
|
||||
*
|
||||
* @see fr.xephi.authme.command.executable.authme.ReloadCommand
|
||||
*/
|
||||
public interface Reloadable {
|
||||
|
||||
/**
|
||||
* Performs the reload action.
|
||||
*/
|
||||
void reload();
|
||||
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package fr.xephi.authme.initialization;
|
||||
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
|
||||
/**
|
||||
* Interface for classes that keep a local copy of certain settings.
|
||||
*
|
||||
* @see fr.xephi.authme.command.executable.authme.ReloadCommand
|
||||
*/
|
||||
public interface SettingsDependent {
|
||||
|
||||
/**
|
||||
* Performs a reload with the provided settings instance.
|
||||
*
|
||||
* @param settings the settings instance
|
||||
*/
|
||||
void reload(Settings settings);
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
package fr.xephi.authme.initialization;
|
||||
|
||||
import ch.jalu.configme.configurationdata.ConfigurationData;
|
||||
import ch.jalu.configme.resource.PropertyResource;
|
||||
import fr.xephi.authme.service.yaml.YamlFileResourceProvider;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
import fr.xephi.authme.settings.SettingsMigrationService;
|
||||
import fr.xephi.authme.settings.properties.AuthMeSettingsRetriever;
|
||||
import fr.xephi.authme.util.FileUtils;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Provider;
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* Initializes the settings.
|
||||
*/
|
||||
public class SettingsProvider implements Provider<Settings> {
|
||||
|
||||
@Inject
|
||||
@DataFolder
|
||||
private File dataFolder;
|
||||
@Inject
|
||||
private SettingsMigrationService migrationService;
|
||||
|
||||
SettingsProvider() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the plugin's settings.
|
||||
*
|
||||
* @return the settings instance, or null if it could not be constructed
|
||||
*/
|
||||
@Override
|
||||
public Settings get() {
|
||||
File configFile = new File(dataFolder, "config.yml");
|
||||
if (!configFile.exists()) {
|
||||
FileUtils.create(configFile);
|
||||
}
|
||||
PropertyResource resource = YamlFileResourceProvider.loadFromFile(configFile);
|
||||
ConfigurationData configurationData = AuthMeSettingsRetriever.buildConfigurationData();
|
||||
return new Settings(dataFolder, resource, migrationService, configurationData);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,95 +0,0 @@
|
||||
package fr.xephi.authme.initialization;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import fr.xephi.authme.AuthMe;
|
||||
import fr.xephi.authme.datasource.DataSource;
|
||||
import org.bukkit.scheduler.BukkitScheduler;
|
||||
import org.bukkit.scheduler.BukkitWorker;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* Waits for asynchronous tasks to complete before closing the data source
|
||||
* so the plugin can shut down properly.
|
||||
*/
|
||||
public class TaskCloser implements Runnable {
|
||||
|
||||
private final BukkitScheduler scheduler;
|
||||
private final Logger logger;
|
||||
private final AuthMe plugin;
|
||||
private final DataSource dataSource;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param plugin the plugin instance
|
||||
* @param dataSource the data source (nullable)
|
||||
*/
|
||||
public TaskCloser(AuthMe plugin, DataSource dataSource) {
|
||||
this.scheduler = plugin.getServer().getScheduler();
|
||||
this.logger = plugin.getLogger();
|
||||
this.plugin = plugin;
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
List<Integer> pendingTasks = getPendingTasks();
|
||||
logger.log(Level.INFO, "Waiting for {0} tasks to finish", pendingTasks.size());
|
||||
int progress = 0;
|
||||
|
||||
//one minute + some time checking the running state
|
||||
int tries = 60;
|
||||
while (!pendingTasks.isEmpty()) {
|
||||
if (tries <= 0) {
|
||||
logger.log(Level.INFO, "Async tasks times out after to many tries {0}", pendingTasks);
|
||||
break;
|
||||
}
|
||||
|
||||
try {
|
||||
sleep();
|
||||
} catch (InterruptedException ignored) {
|
||||
Thread.currentThread().interrupt();
|
||||
break;
|
||||
}
|
||||
|
||||
for (Iterator<Integer> iterator = pendingTasks.iterator(); iterator.hasNext(); ) {
|
||||
int taskId = iterator.next();
|
||||
if (!scheduler.isCurrentlyRunning(taskId)) {
|
||||
iterator.remove();
|
||||
progress++;
|
||||
logger.log(Level.INFO, "Progress: {0} / {1}", new Object[]{progress, pendingTasks.size()});
|
||||
}
|
||||
}
|
||||
|
||||
tries--;
|
||||
}
|
||||
|
||||
if (dataSource != null) {
|
||||
dataSource.closeConnection();
|
||||
}
|
||||
}
|
||||
|
||||
/** Makes the current thread sleep for one second. */
|
||||
@VisibleForTesting
|
||||
void sleep() throws InterruptedException {
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
|
||||
private List<Integer> getPendingTasks() {
|
||||
List<Integer> pendingTasks = new ArrayList<>();
|
||||
//returns only the async tasks
|
||||
for (BukkitWorker pendingTask : scheduler.getActiveWorkers()) {
|
||||
if (pendingTask.getOwner().equals(plugin)
|
||||
//it's not a periodic task
|
||||
&& !scheduler.isQueued(pendingTask.getTaskId())) {
|
||||
pendingTasks.add(pendingTask.getTaskId());
|
||||
}
|
||||
}
|
||||
return pendingTasks;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user