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

135 lines
3.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 java.sql.*;
import java.util.Properties;
public class TextsManager
{
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
private final Properties texts;
public TextsManager()
{
long millis = System.currentTimeMillis();
this.texts = new Properties();
try
{
this.reload();
Emulator.getLogging().logStart("Texts Manager -> Loaded! (" + (System.currentTimeMillis() - millis) + " MS)");
}
catch (Exception e)
{
e.printStackTrace();
}
}
2018-07-08 23:32:00 +02:00
2018-07-06 15:30:00 +02:00
public void reload() throws Exception
{
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); Statement statement = connection.createStatement(); ResultSet set = statement.executeQuery("SELECT * FROM emulator_texts"))
{
while(set.next())
{
if(this.texts.containsKey(set.getString("key")))
{
this.texts.setProperty(set.getString("key"), set.getString("value"));
}
else
{
this.texts.put(set.getString("key"), set.getString("value"));
}
}
}
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.texts.containsKey(key)) {
Emulator.getLogging().logErrorLine("[TEXTS] Text key not found: " + key);
}
return this.texts.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)
{
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)
{
Emulator.getLogging().logErrorLine(e);
}
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)
{
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)
{
Emulator.getLogging().logErrorLine(e);
}
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.texts.setProperty(key, value);
}
public void register(String key, String value)
{
if (this.texts.getProperty(key, null) != null)
return;
try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("INSERT INTO emulator_texts VALUES (?, ?)"))
{
statement.setString(1, key);
statement.setString(2, value);
statement.execute();
}
catch (SQLException e)
{
Emulator.getLogging().logSQLException(e);
}
this.update(key, value);
}
}