2018-07-06 15:30:00 +02:00
|
|
|
package com.eu.habbo.core;
|
|
|
|
|
|
|
|
import com.eu.habbo.Emulator;
|
|
|
|
import com.eu.habbo.plugin.events.emulator.EmulatorConfigUpdatedEvent;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.sql.*;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Properties;
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
public class ConfigurationManager {
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
private final Properties properties;
|
|
|
|
private final String configurationPath;
|
2018-07-06 15:30:00 +02:00
|
|
|
public boolean loaded = false;
|
|
|
|
public boolean isLoading = false;
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
public ConfigurationManager(String configurationPath) throws Exception {
|
2018-07-06 15:30:00 +02:00
|
|
|
this.properties = new Properties();
|
2019-03-18 02:22:00 +01:00
|
|
|
this.configurationPath = configurationPath;
|
2018-07-06 15:30:00 +02:00
|
|
|
this.reload();
|
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
public void reload() {
|
2018-07-06 15:30:00 +02:00
|
|
|
this.isLoading = true;
|
|
|
|
this.properties.clear();
|
|
|
|
|
|
|
|
InputStream input = null;
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
try {
|
2019-03-18 02:22:00 +01:00
|
|
|
File f = new File(this.configurationPath);
|
2018-07-06 15:30:00 +02:00
|
|
|
input = new FileInputStream(f);
|
|
|
|
this.properties.load(input);
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
} catch (IOException ex) {
|
2019-03-18 02:22:00 +01:00
|
|
|
Emulator.getLogging().logErrorLine("[CRITICAL] FAILED TO LOAD CONFIG FILE! (" + this.configurationPath + ")");
|
|
|
|
ex.printStackTrace();
|
2019-05-26 20:14:53 +02:00
|
|
|
} finally {
|
|
|
|
if (input != null) {
|
|
|
|
try {
|
2018-07-06 15:30:00 +02:00
|
|
|
input.close();
|
2019-05-26 20:14:53 +02:00
|
|
|
} catch (IOException e) {
|
2018-07-06 15:30:00 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
if (this.loaded) {
|
2018-07-06 15:30:00 +02:00
|
|
|
this.loadFromDatabase();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.isLoading = false;
|
|
|
|
Emulator.getLogging().logStart("Configuration Manager -> Loaded!");
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
if (Emulator.getPluginManager() != null) {
|
2018-07-06 15:30:00 +02:00
|
|
|
Emulator.getPluginManager().fireEvent(new EmulatorConfigUpdatedEvent());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
public void loadFromDatabase() {
|
2018-07-06 15:30:00 +02:00
|
|
|
Emulator.getLogging().logStart("Loading configuration from database...");
|
|
|
|
|
|
|
|
long millis = System.currentTimeMillis();
|
2019-05-26 20:14:53 +02:00
|
|
|
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); Statement statement = connection.createStatement()) {
|
|
|
|
if (statement.execute("SELECT * FROM emulator_settings")) {
|
|
|
|
try (ResultSet set = statement.getResultSet()) {
|
|
|
|
while (set.next()) {
|
2018-07-06 15:30:00 +02:00
|
|
|
this.properties.put(set.getString("key"), set.getString("value"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-26 20:14:53 +02:00
|
|
|
} catch (SQLException e) {
|
2018-07-06 15:30:00 +02:00
|
|
|
Emulator.getLogging().logSQLException(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
Emulator.getLogging().logStart("Configuration -> loaded! (" + (System.currentTimeMillis() - millis) + " MS)");
|
|
|
|
}
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
public void saveToDatabase() {
|
|
|
|
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("UPDATE emulator_settings SET `value` = ? WHERE `key` = ? LIMIT 1")) {
|
|
|
|
for (Map.Entry<Object, Object> entry : this.properties.entrySet()) {
|
2018-07-06 15:30:00 +02:00
|
|
|
statement.setString(1, entry.getValue().toString());
|
|
|
|
statement.setString(2, entry.getKey().toString());
|
|
|
|
statement.executeUpdate();
|
|
|
|
}
|
2019-05-26 20:14:53 +02:00
|
|
|
} catch (SQLException e) {
|
2018-07-06 15:30:00 +02:00
|
|
|
Emulator.getLogging().logSQLException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
public String getValue(String key) {
|
2019-03-18 02:22:00 +01:00
|
|
|
return this.getValue(key, "");
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
public String getValue(String key, String defaultValue) {
|
2018-07-06 15:30:00 +02:00
|
|
|
if (this.isLoading)
|
|
|
|
return defaultValue;
|
|
|
|
|
|
|
|
if (!this.properties.containsKey(key)) {
|
|
|
|
Emulator.getLogging().logErrorLine("[CONFIG] Key not found: " + key);
|
|
|
|
}
|
|
|
|
return this.properties.getProperty(key, defaultValue);
|
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
public boolean getBoolean(String key) {
|
2019-03-18 02:22:00 +01:00
|
|
|
return this.getBoolean(key, false);
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
public boolean getBoolean(String key, boolean defaultValue) {
|
2018-07-06 15:30:00 +02:00
|
|
|
if (this.isLoading)
|
|
|
|
return defaultValue;
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
try {
|
2019-03-18 02:22:00 +01:00
|
|
|
return (this.getValue(key, "0").equals("1")) || (this.getValue(key, "false").equals("true"));
|
2019-05-26 20:14:53 +02:00
|
|
|
} catch (Exception e) {
|
2019-03-18 02:22:00 +01:00
|
|
|
Emulator.getLogging().logErrorLine("Failed to parse key " + key + " with value " + this.getValue(key) + " to type boolean.");
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
public int getInt(String key) {
|
2019-03-18 02:22:00 +01:00
|
|
|
return this.getInt(key, 0);
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
public int getInt(String key, Integer defaultValue) {
|
2018-07-06 15:30:00 +02:00
|
|
|
if (this.isLoading)
|
|
|
|
return defaultValue;
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
try {
|
2019-03-18 02:22:00 +01:00
|
|
|
return Integer.parseInt(this.getValue(key, defaultValue.toString()));
|
2019-05-26 20:14:53 +02:00
|
|
|
} catch (Exception e) {
|
2019-03-18 02:22:00 +01:00
|
|
|
Emulator.getLogging().logErrorLine("Failed to parse key " + key + " with value " + this.getValue(key) + " to type integer.");
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
public double getDouble(String key) {
|
2019-03-18 02:22:00 +01:00
|
|
|
return this.getDouble(key, 0.0);
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
public double getDouble(String key, Double defaultValue) {
|
2018-07-06 15:30:00 +02:00
|
|
|
if (this.isLoading)
|
|
|
|
return defaultValue;
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
try {
|
2019-03-18 02:22:00 +01:00
|
|
|
return Double.parseDouble(this.getValue(key, defaultValue.toString()));
|
2019-05-26 20:14:53 +02:00
|
|
|
} catch (Exception e) {
|
2019-03-18 02:22:00 +01:00
|
|
|
Emulator.getLogging().logErrorLine("Failed to parse key " + key + " with value " + this.getValue(key) + " to type double.");
|
2018-07-06 15:30:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
|
2018-07-08 23:32:00 +02:00
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
public void update(String key, String value) {
|
2018-07-06 15:30:00 +02:00
|
|
|
this.properties.setProperty(key, value);
|
|
|
|
}
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
public void register(String key, String value) {
|
2018-07-06 15:30:00 +02:00
|
|
|
if (this.properties.getProperty(key, null) != null)
|
|
|
|
return;
|
|
|
|
|
2019-05-26 20:14:53 +02:00
|
|
|
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("INSERT INTO emulator_settings VALUES (?, ?)")) {
|
2018-07-06 15:30:00 +02:00
|
|
|
statement.setString(1, key);
|
|
|
|
statement.setString(2, value);
|
|
|
|
statement.execute();
|
2019-05-26 20:14:53 +02:00
|
|
|
} catch (SQLException e) {
|
2018-07-06 15:30:00 +02:00
|
|
|
Emulator.getLogging().logSQLException(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.update(key, value);
|
|
|
|
}
|
|
|
|
}
|