WorldAliasSettings doesn't require Plan as variable anymore

This commit is contained in:
Rsl1122 2017-12-12 10:39:12 +02:00
parent 890b2a318f
commit 706b8f1c8d
8 changed files with 48 additions and 126 deletions

View File

@ -8,7 +8,7 @@ import com.djrapitops.plugin.api.config.Config;
import com.djrapitops.plugin.api.config.ConfigNode;
import com.djrapitops.plugin.api.utility.log.Log;
import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.systems.store.config.ConfigSystem;
import java.io.IOException;
import java.util.HashMap;
@ -21,17 +21,6 @@ import java.util.Map;
*/
public class WorldAliasSettings {
private final Plan plugin;
/**
* Constructor.
*
* @param plugin Current instance of Plan.
*/
public WorldAliasSettings(Plan plugin) {
this.plugin = plugin;
}
/**
* Used to get all World aliases in the config
*
@ -48,7 +37,7 @@ public class WorldAliasSettings {
}
private ConfigNode getAliasSection() {
Config config = plugin.getMainConfig();
Config config = ConfigSystem.getInstance().getConfig();
return config.getConfigNode(Settings.WORLD_ALIASES.getPath());
}

View File

@ -1,104 +0,0 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package main.java.com.djrapitops.plan.systems;
import com.djrapitops.plugin.api.Benchmark;
import com.djrapitops.plugin.api.Check;
import com.djrapitops.plugin.api.utility.log.Log;
import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.api.exceptions.DatabaseInitException;
import main.java.com.djrapitops.plan.api.exceptions.PlanEnableException;
import main.java.com.djrapitops.plan.database.Database;
import main.java.com.djrapitops.plan.database.databases.MySQLDB;
import main.java.com.djrapitops.plan.database.databases.SQLiteDB;
import main.java.com.djrapitops.plan.settings.Settings;
import main.java.com.djrapitops.plan.settings.locale.Locale;
import main.java.com.djrapitops.plan.settings.locale.Msg;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Set;
/**
* //TODO Class Javadoc Comment
*
* @author Rsl1122
*/
public class DatabaseSystem implements SubSystem {
private Database db;
private Set<Database> databases;
@Override
public void init() throws PlanEnableException {
try {
Benchmark.start("Init Database");
Log.info(Locale.get(Msg.ENABLE_DB_INIT).toString());
initDatabase();
Log.info(Locale.get(Msg.ENABLE_DB_INFO).parse(db.getConfigName()));
Benchmark.stop("Systems", "Init Database");
} catch (DatabaseInitException e) {
throw new PlanEnableException(db.getConfigName() + "-Database failed to initialize", e);
}
}
private void initDatabase() throws DatabaseInitException {
if (Check.isBungeeAvailable()) {
db = new MySQLDB();
} else {
// Enables database on Bukkit
// Bukkit supports SQLite.
initPlanDatabase();
}
db.init();
}
private void initPlanDatabase() throws DatabaseInitException {
databases = new HashSet<>();
databases.add(new MySQLDB());
databases.add(new SQLiteDB());
String dbType = Settings.DB_TYPE.toString().toLowerCase().trim();
db = getActiveDatabase(dbType);
}
public Set<Database> getDatabases() {
return databases;
}
public void setDatabases(Set<Database> databases) {
this.databases = databases;
}
@Override
public void close() {
try {
if (db != null) {
db.close();
}
} catch (SQLException e) {
Log.toLog(this.getClass().getName(), e);
}
}
public static DatabaseSystem getInstance() {
return Systems.getInstance().databaseSystem;
}
public Database getActiveDatabase() {
return db;
}
public static Database getActiveDatabase(String dbName) throws DatabaseInitException {
for (Database database : DatabaseSystem.getInstance().getDatabases()) {
String dbConfigName = database.getConfigName();
if (Verify.equalsIgnoreCase(dbName, dbConfigName)) {
database.init();
return database;
}
}
throw new DatabaseInitException(Locale.get(Msg.ENABLE_FAIL_WRONG_DB) + " " + dbName);
}
}

View File

@ -49,7 +49,7 @@ public class PlanGamemodeChangeListener implements Listener {
String gameMode = event.getNewGameMode().name();
String worldName = p.getWorld().getName();
new WorldAliasSettings(plugin).addWorld(worldName);
new WorldAliasSettings().addWorld(worldName);
Optional<Session> cachedSession = plugin.getDataCache().getCachedSession(uuid);
cachedSession.ifPresent(session -> session.changeState(worldName, gameMode, time));

View File

@ -31,7 +31,7 @@ public class PlanWorldChangeListener implements Listener {
String gameMode = p.getGameMode().name();
long time = MiscUtils.getTime();
new WorldAliasSettings(plugin).addWorld(worldName);
new WorldAliasSettings().addWorld(worldName);
Optional<Session> cachedSession = plugin.getDataCache().getCachedSession(uuid);
cachedSession.ifPresent(session -> session.changeState(worldName, gameMode, time));

View File

@ -26,7 +26,11 @@ public class FileSystem implements SubSystem {
private File configFile;
public FileSystem(IPlan plugin) {
dataFolder = plugin.getDataFolder();
this(plugin.getDataFolder());
}
public FileSystem(File dataFolder) {
this.dataFolder = dataFolder;
}
public static FileSystem getInstance() {

View File

@ -1,12 +1,11 @@
package main.java.com.djrapitops.plan.utilities.html.graphs.pie;
import main.java.com.djrapitops.plan.Plan;
import main.java.com.djrapitops.plan.data.time.GMTimes;
import main.java.com.djrapitops.plan.data.time.WorldTimes;
import main.java.com.djrapitops.plan.settings.Settings;
import main.java.com.djrapitops.plan.settings.WorldAliasSettings;
import main.java.com.djrapitops.plan.settings.theme.ThemeVal;
import main.java.com.djrapitops.plan.settings.theme.Theme;
import main.java.com.djrapitops.plan.settings.theme.ThemeVal;
import main.java.com.djrapitops.plan.utilities.comparators.PieSliceComparator;
import java.util.*;
@ -64,14 +63,14 @@ public class WorldPie {
}
private static Map<String, Long> transformToAliases(Map<String, Long> playtimePerWorld) {
WorldAliasSettings aliasSettings = new WorldAliasSettings(Plan.getInstance());
WorldAliasSettings aliasSettings = new WorldAliasSettings();
Map<String, String> aliases = aliasSettings.getAliases();
return transformToAliases(playtimePerWorld, aliases);
}
public static Map<String, Long> transformToAliases(Map<String, Long> playtimePerWorld, Map<String, String> aliases) {
// TODO Optimization is possible
WorldAliasSettings aliasSettings = new WorldAliasSettings(Plan.getInstance());
WorldAliasSettings aliasSettings = new WorldAliasSettings();
Map<String, Long> playtimePerAlias = new HashMap<>();
for (Map.Entry<String, Long> entry : playtimePerWorld.entrySet()) {
@ -121,7 +120,7 @@ public class WorldPie {
private static Map<String, GMTimes> transformToGMAliases(Map<String, GMTimes> gmTimesMap) {
// TODO Optimization is possible
WorldAliasSettings aliasSettings = new WorldAliasSettings(Plan.getInstance());
WorldAliasSettings aliasSettings = new WorldAliasSettings();
Map<String, String> aliases = aliasSettings.getAliases();
Map<String, GMTimes> gmTimesPerAlias = new HashMap<>();

View File

@ -105,7 +105,7 @@ public class SessionsTableCreator {
}
public static String getLongestWorldPlayed(Session session) {
WorldAliasSettings aliasSettings = new WorldAliasSettings(Plan.getInstance());
WorldAliasSettings aliasSettings = new WorldAliasSettings();
Map<String, String> aliases = aliasSettings.getAliases();
if (session.getSessionEnd() == -1) {
return "Current: " + aliases.get(session.getWorldTimes().getCurrentWorld());

View File

@ -0,0 +1,34 @@
/*
* Licence is provided in the jar as license.yml also here:
* https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml
*/
package test.java.utils.testsystem;
import main.java.com.djrapitops.plan.systems.store.config.ConfigSystem;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* //TODO Class Javadoc Comment
*
* @author Rsl1122
*/
public class TestConfigSystem extends ConfigSystem {
@Override
protected void copyDefaults() throws IOException {
File file = new File(getClass().getResource("/config.yml").getPath());
Scanner scanner = new Scanner(new FileInputStream(file));
List<String> lines = new ArrayList<>();
while (scanner.hasNextLine()) {
lines.add(scanner.nextLine());
}
config.copyDefaults(lines);
}
}