Skip to content

Commit

Permalink
v0.0.6
Browse files Browse the repository at this point in the history
- Small update of config operations
  • Loading branch information
fromgate committed Feb 29, 2016
1 parent aec47a2 commit 2c357eb
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 62 deletions.
73 changes: 12 additions & 61 deletions src/main/java/ru/nukkit/dblib/DbLibPlugin.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package ru.nukkit.dblib;

import cn.nukkit.plugin.PluginBase;
import cn.nukkit.utils.Config;
import cn.nukkit.utils.TextFormat;
import com.j256.ormlite.logger.LocalLog;
import com.j256.ormlite.support.ConnectionSource;
import ru.nukkit.dblib.util.DbLibCfg;
import ru.nukkit.dblib.util.Message;

import java.io.File;
import java.io.IOException;

public class DbLibPlugin extends PluginBase {

ConnectionSource connectionSource = null;
Expand All @@ -19,72 +16,26 @@ static DbLibPlugin getPlugin() {
return plugin;
}

private boolean dbUseMySQL;
private String dbFileName;

private String dbMySqlUrl;
private int dbMySqlPort;
private String dbMySqlDatabase;
private String dbMySqlUsername;
private String dbMySqlPassword;
private DbLibCfg cfg;

private boolean debugLog;


@Override
public void onEnable(){
plugin = this;
this.cfg = new DbLibCfg(this);
this.cfg.load();
this.cfg.save();
Message.init(this);
getLogger().info(TextFormat.YELLOW+"DbLib "+this.getDescription().getVersion()+" created by fromgate for nukkit.ru");
initDb();
}


private void load(){
this.getDataFolder().mkdirs();
File f = new File(this.getDataFolder(),"config.yml");
if (!f.exists()) try {
f.createNewFile();
} catch (IOException e) {
}
Config cfg = new Config(f,Config.YAML);
this.dbUseMySQL = cfg.getBoolean("DbLib.use-MySQL",false);
this.debugLog = cfg.getBoolean("DbLib.ORMLite-debug",false);

this.dbFileName = cfg.getString("SQLite.file-name",new File(this.getDataFolder().getParent()).getParent()+File.separator+"nukkit.db");

this.dbMySqlUrl = cfg.getString("MySQL.host",cfg.getString("MySQL.url","localhost"));
this.dbMySqlPort = cfg.getInt("MySQL.port",3306);
this.dbMySqlDatabase = cfg.getString("MySQL.database","db");
this.dbMySqlUsername = cfg.getString("MySQL.username","nukkit");
this.dbMySqlPassword = cfg.getString("MySQL.password","tikkun");
}
private void save(){
this.getDataFolder().mkdirs();
File f = new File(this.getDataFolder(),"config.yml");
if (f.exists()) f.delete();
Config cfg = new Config(f,Config.YAML);
cfg.set("DbLib.use-MySQL",this.dbUseMySQL);
cfg.set("DbLib.ORMLite-debug",this.debugLog);

cfg.set("SQLite.file-name",this.dbFileName);

cfg.set("MySQL.host",this.dbMySqlUrl);
cfg.set("MySQL.port",this.dbMySqlPort);
cfg.set("MySQL.database",this.dbMySqlDatabase);
cfg.set("MySQL.username",this.dbMySqlUsername);
cfg.set("MySQL.password",this.dbMySqlPassword);

cfg.save();
}

private void initDb(){
load();
save();
System.setProperty(LocalLog.LOCAL_LOG_LEVEL_PROPERTY, this.debugLog ? "DEBUG" : "ERROR");
String dbUrl = this.getDbUrl();
Message.URL_LOG.log("NOCOLOR",dbUrl,this.dbMySqlUsername);
this.connectionSource = DbLib.getConnectionSource(this.getDbUrl(),this.dbMySqlUsername,this.dbMySqlPassword);
Message.URL_LOG.log("NOCOLOR",dbUrl,this.cfg.dbMySqlUsername);
this.connectionSource = DbLib.getConnectionSource(this.getDbUrl(),this.cfg.dbMySqlUsername,this.cfg.dbMySqlPassword);
}

ConnectionSource getDefaultConnection() {
Expand All @@ -93,14 +44,14 @@ ConnectionSource getDefaultConnection() {

String getDbUrl() {
StringBuilder sb = new StringBuilder("jdbc:");
if (this.dbUseMySQL) {
if (this.cfg.dbUseMySQL) {
sb.append("mysql://");
sb.append(this.dbMySqlUrl);
sb.append(":").append(this.dbMySqlPort);
sb.append("/").append(this.dbMySqlDatabase);
sb.append(this.cfg.dbMySqlUrl);
sb.append(":").append(this.cfg.dbMySqlPort);
sb.append("/").append(this.cfg.dbMySqlDatabase);
} else {
sb.append("sqlite:");
sb.append(this.dbFileName);
sb.append(this.cfg.dbFileName);
}
return sb.toString();
}
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/ru/nukkit/dblib/util/DbLibCfg.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ru.nukkit.dblib.util;

import cn.nukkit.plugin.Plugin;
import cn.nukkit.utils.SimpleConfig;

public class DbLibCfg extends SimpleConfig {
public DbLibCfg(Plugin plugin) {
super(plugin);
}

@Path(value = "DbLib.use-MySQL")
public boolean dbUseMySQL;

@Path(value = "SQLite.file-name")
public String dbFileName;

@Path(value = "MySQL.host")
public String dbMySqlUrl;

@SimpleConfig.Path(value ="MySQL.port")
public int dbMySqlPort;

@SimpleConfig.Path(value ="MySQL.database")
public String dbMySqlDatabase;

@SimpleConfig.Path(value ="MySQL.username")
public String dbMySqlUsername;

@SimpleConfig.Path(value = "MySQL.password")
public String dbMySqlPassword;

@SimpleConfig.Path(value ="DbLib.ORMLite-debug")
public boolean debugLog;

}
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: DbLib
main: ru.nukkit.dblib.DbLibPlugin
version: "0.0.5"
version: "0.0.6"
author: fromgate, nukkit.ru
api: ["1.0.0"]
description: SQLite/MySQL library for Nukkit plugins

0 comments on commit 2c357eb

Please sign in to comment.