Arcturus-Community/src/main/java/com/eu/habbo/core/ConfigurationManager.java

238 lines
6.2 KiB
Java
Raw Normal View History

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;
public class ConfigurationManager
{
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public boolean loaded = false;
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public boolean isLoading = false;
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
private final Properties properties;
2019-03-18 02:22:00 +01:00
private final String configurationPath;
2018-07-06 15:30:00 +02:00
2019-03-18 02:22:00 +01: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-03-18 02:22:00 +01:00
public void reload()
2018-07-06 15:30:00 +02:00
{
this.isLoading = true;
this.properties.clear();
InputStream input = null;
2019-03-18 02:22:00 +01:00
try
{
File f = new File(this.configurationPath);
2018-07-06 15:30:00 +02:00
input = new FileInputStream(f);
this.properties.load(input);
2019-03-18 02:22:00 +01:00
}
catch (IOException ex)
{
Emulator.getLogging().logErrorLine("[CRITICAL] FAILED TO LOAD CONFIG FILE! (" + this.configurationPath + ")");
ex.printStackTrace();
}
finally
{
if (input != null)
{
try
{
2018-07-06 15:30:00 +02:00
input.close();
2019-03-18 02:22:00 +01:00
}
catch (IOException e)
{
2018-07-06 15:30:00 +02:00
e.printStackTrace();
}
}
}
2019-03-18 02:22:00 +01:00
if(this.loaded)
2018-07-06 15:30:00 +02:00
{
this.loadFromDatabase();
}
this.isLoading = false;
Emulator.getLogging().logStart("Configuration Manager -> Loaded!");
if (Emulator.getPluginManager() != null)
{
Emulator.getPluginManager().fireEvent(new EmulatorConfigUpdatedEvent());
}
}
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public void loadFromDatabase()
{
Emulator.getLogging().logStart("Loading configuration from database...");
long millis = System.currentTimeMillis();
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())
{
this.properties.put(set.getString("key"), set.getString("value"));
}
}
}
}
catch (SQLException e)
{
Emulator.getLogging().logSQLException(e);
}
Emulator.getLogging().logStart("Configuration -> loaded! (" + (System.currentTimeMillis() - millis) + " MS)");
}
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())
{
statement.setString(1, entry.getValue().toString());
statement.setString(2, entry.getKey().toString());
statement.executeUpdate();
}
}
catch (SQLException e)
{
Emulator.getLogging().logSQLException(e);
}
}
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +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
2018-07-06 15:30:00 +02:00
public String getValue(String key, String defaultValue)
{
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
2018-07-06 15:30:00 +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
2018-07-06 15:30:00 +02:00
public boolean getBoolean(String key, boolean defaultValue)
{
if (this.isLoading)
return defaultValue;
try
{
2019-03-18 02:22:00 +01:00
return (this.getValue(key, "0").equals("1")) || (this.getValue(key, "false").equals("true"));
2018-07-06 15:30:00 +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
2018-07-06 15:30:00 +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
2018-07-06 15:30:00 +02:00
public int getInt(String key, Integer defaultValue)
{
if (this.isLoading)
return defaultValue;
try
{
2019-03-18 02:22:00 +01:00
return Integer.parseInt(this.getValue(key, defaultValue.toString()));
2018-07-06 15:30:00 +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
2018-07-06 15:30:00 +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
2018-07-06 15:30:00 +02:00
public double getDouble(String key, Double defaultValue)
{
if (this.isLoading)
return defaultValue;
try
{
2019-03-18 02:22:00 +01:00
return Double.parseDouble(this.getValue(key, defaultValue.toString()));
2018-07-06 15:30:00 +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
2018-07-06 15:30:00 +02:00
public void update(String key, String value)
{
this.properties.setProperty(key, value);
}
public void register(String key, String value)
{
if (this.properties.getProperty(key, null) != null)
return;
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("INSERT INTO emulator_settings VALUES (?, ?)"))
{
statement.setString(1, key);
statement.setString(2, value);
statement.execute();
}
catch (SQLException e)
{
Emulator.getLogging().logSQLException(e);
}
this.update(key, value);
}
}