From ef980bd65420996eb97dd188101585bf271de38f Mon Sep 17 00:00:00 2001 From: ljacqu Date: Mon, 14 Mar 2016 20:45:46 +0100 Subject: [PATCH] #603 Delete CustomConfiguration class --- .../authme/converter/RoyalAuthConverter.java | 37 +++++--- .../authme/converter/RoyalAuthYamlReader.java | 22 ----- .../authme/settings/CustomConfiguration.java | 94 ------------------- .../properties/TestConfiguration.java | 2 - 4 files changed, 24 insertions(+), 131 deletions(-) delete mode 100644 src/main/java/fr/xephi/authme/converter/RoyalAuthYamlReader.java delete mode 100644 src/main/java/fr/xephi/authme/settings/CustomConfiguration.java diff --git a/src/main/java/fr/xephi/authme/converter/RoyalAuthConverter.java b/src/main/java/fr/xephi/authme/converter/RoyalAuthConverter.java index 42bd681df..b79657e28 100644 --- a/src/main/java/fr/xephi/authme/converter/RoyalAuthConverter.java +++ b/src/main/java/fr/xephi/authme/converter/RoyalAuthConverter.java @@ -5,35 +5,46 @@ import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.datasource.DataSource; import org.bukkit.OfflinePlayer; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; import java.io.File; +import static fr.xephi.authme.util.StringUtils.makePath; + public class RoyalAuthConverter implements Converter { + private static final String LAST_LOGIN_PATH = "timestamps.quit"; + private static final String PASSWORD_PATH = "login.password"; private final AuthMe plugin; - private final DataSource data; + private final DataSource dataSource; public RoyalAuthConverter(AuthMe plugin) { this.plugin = plugin; - this.data = plugin.getDataSource(); + this.dataSource = plugin.getDataSource(); } @Override public void run() { - for (OfflinePlayer o : plugin.getServer().getOfflinePlayers()) { + for (OfflinePlayer player : plugin.getServer().getOfflinePlayers()) { try { - String name = o.getName().toLowerCase(); - String sp = File.separator; - File file = new File("." + sp + "plugins" + sp + "RoyalAuth" + sp + "userdata" + sp + name + ".yml"); - if (data.isAuthAvailable(name)) + String name = player.getName().toLowerCase(); + File file = new File(makePath(".", "plugins", "RoyalAuth", "userdata", name + ".yml")); + + if (dataSource.isAuthAvailable(name) || !file.exists()) { continue; - if (!file.exists()) - continue; - RoyalAuthYamlReader ra = new RoyalAuthYamlReader(file); - PlayerAuth auth = new PlayerAuth(name, ra.getHash(), "127.0.0.1", ra.getLastLogin(), "your@email.com", o.getName()); - data.saveAuth(auth); + } + FileConfiguration configuration = YamlConfiguration.loadConfiguration(file); + PlayerAuth auth = PlayerAuth.builder() + .name(name) + .password(configuration.getString(PASSWORD_PATH), null) + .lastLogin(configuration.getLong(LAST_LOGIN_PATH)) + .realName(player.getName()) + .build(); + + dataSource.saveAuth(auth); } catch (Exception e) { - ConsoleLogger.logException("Error while trying to import " + o.getName() + " RoyalAuth data", e); + ConsoleLogger.logException("Error while trying to import " + player.getName() + " RoyalAuth data", e); } } } diff --git a/src/main/java/fr/xephi/authme/converter/RoyalAuthYamlReader.java b/src/main/java/fr/xephi/authme/converter/RoyalAuthYamlReader.java deleted file mode 100644 index c0437cb45..000000000 --- a/src/main/java/fr/xephi/authme/converter/RoyalAuthYamlReader.java +++ /dev/null @@ -1,22 +0,0 @@ -package fr.xephi.authme.converter; - -import fr.xephi.authme.settings.CustomConfiguration; - -import java.io.File; - -class RoyalAuthYamlReader extends CustomConfiguration { - - public RoyalAuthYamlReader(File file) { - super(file); - load(); - save(); - } - - public long getLastLogin() { - return getLong("timestamps.quit"); - } - - public String getHash() { - return getString("login.password"); - } -} diff --git a/src/main/java/fr/xephi/authme/settings/CustomConfiguration.java b/src/main/java/fr/xephi/authme/settings/CustomConfiguration.java deleted file mode 100644 index 3defffe44..000000000 --- a/src/main/java/fr/xephi/authme/settings/CustomConfiguration.java +++ /dev/null @@ -1,94 +0,0 @@ -package fr.xephi.authme.settings; - -import fr.xephi.authme.AuthMe; -import fr.xephi.authme.ConsoleLogger; -import org.bukkit.configuration.InvalidConfigurationException; -import org.bukkit.configuration.file.YamlConfiguration; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; -import java.nio.file.StandardCopyOption; - -/** - */ -public abstract class CustomConfiguration extends YamlConfiguration { - - private final File configFile; - - /** - * Constructor for CustomConfiguration. - * - * @param file the config file - */ - public CustomConfiguration(File file) { - this.configFile = file; - load(); - } - - public void load() { - try { - super.load(configFile); - } catch (FileNotFoundException e) { - ConsoleLogger.showError("Could not find " + configFile.getName() + ", creating new one..."); - reLoad(); - } catch (IOException e) { - ConsoleLogger.showError("Could not load " + configFile.getName()); - } catch (InvalidConfigurationException e) { - ConsoleLogger.showError(configFile.getName() + " is no valid configuration file"); - } - } - - public boolean reLoad() { - boolean out = true; - if (!configFile.exists()) { - out = loadResource(configFile); - } - if (out) - load(); - return out; - } - - public void save() { - try { - super.save(configFile); - } catch (IOException ex) { - ConsoleLogger.showError("Could not save config to " + configFile.getName()); - } - } - - public File getConfigFile() { - return configFile; - } - - private boolean loadResource(File file) { - if (!file.exists()) { - try { - if (!file.getParentFile().exists() && !file.getParentFile().mkdirs()) { - return false; - } - int i = file.getPath().indexOf("AuthMe"); - if (i > -1) { - String path = file.getPath().substring(i + 6).replace('\\', '/'); - InputStream is = AuthMe.class.getResourceAsStream(path); - Files.copy(is, file.toPath(), StandardCopyOption.REPLACE_EXISTING); - return true; - } - } catch (Exception e) { - ConsoleLogger.logException("Failed to load config from JAR", e); - } - } - return false; - } - - public boolean containsAll(String... paths) { - for (String path : paths) { - if (!contains(path)) { - return false; - } - } - return true; - } -} diff --git a/src/test/java/fr/xephi/authme/settings/properties/TestConfiguration.java b/src/test/java/fr/xephi/authme/settings/properties/TestConfiguration.java index 426a69ef0..1e2e5f9f4 100644 --- a/src/test/java/fr/xephi/authme/settings/properties/TestConfiguration.java +++ b/src/test/java/fr/xephi/authme/settings/properties/TestConfiguration.java @@ -5,7 +5,6 @@ import fr.xephi.authme.settings.domain.Property; import fr.xephi.authme.settings.domain.PropertyType; import fr.xephi.authme.settings.domain.SettingsClass; import fr.xephi.authme.settings.propertymap.PropertyMap; -import fr.xephi.authme.util.WrapperMock; import java.lang.reflect.Field; import java.util.List; @@ -57,7 +56,6 @@ public final class TestConfiguration implements SettingsClass { * @return The generated property map */ public static PropertyMap generatePropertyMap() { - WrapperMock.createInstance(); PropertyMap propertyMap = new PropertyMap(); for (Field field : TestConfiguration.class.getDeclaredFields()) { Object fieldValue = ReflectionTestUtils.getFieldValue(TestConfiguration.class, null, field.getName());