Skip to content

Commit

Permalink
v0.2.0
Browse files Browse the repository at this point in the history
- Added voxelwind support
- API changes (removed nukkit depended methods)
  • Loading branch information
fromgate committed Sep 13, 2016
1 parent 6765a60 commit f6567eb
Show file tree
Hide file tree
Showing 13 changed files with 222 additions and 265 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# DbLib
DbLib a library for nukkit, that include ORMLite and drivers for MySQL and SQLite.
DbLib a library for Nukkit and Voxelwind, that include ORMLite, Sql2o and jdbc-connectors (MySQL and SQLite).

[Download at nukkit.ru](http://nukkit.ru/resources/dblib.14/)

Expand All @@ -14,6 +14,7 @@ Example project: [DbExample](https://github.com/fromgate/DbExample)
* Organizes universal data storage for all plugins, that uses DbLib. Server owner must configure DbLib once and all plugins that use DbLib will work fine!

## Configuration
Configuration file named config.yml in Nukkit and config.json in Voxelwind.
```
general:
# Messages language
Expand Down
45 changes: 5 additions & 40 deletions core/src/main/java/ru/nukkit/dblib/DbLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.j256.ormlite.logger.LocalLog;
import com.j256.ormlite.support.ConnectionSource;
import org.sql2o.Sql2o;
import ru.nukkit.dblib.core.DbLibConfig;
import ru.nukkit.dblib.core.Message;

import java.io.File;
import java.sql.Connection;
Expand All @@ -13,13 +15,13 @@

public class DbLib {

private static Cfg config;
private static DbLibConfig config;
private static ConnectionSource connectionSource = null;
private static Sql2o sql2o = null;
private static File folder;


public static void init (Cfg cfg, File dataFolder){
public static void init(DbLibConfig cfg, File dataFolder) {
config = cfg;
folder = dataFolder;
System.setProperty(LocalLog.LOCAL_LOG_LEVEL_PROPERTY, config.debugLog() ? "DEBUG" : "ERROR");
Expand Down Expand Up @@ -153,7 +155,7 @@ public static Connection getMySqlConnection(String host, int port, String databa
*/
public static Connection getSQLiteConnection(String fileName) {
folder.mkdirs();
return getSQLiteConnection(new File(folder+ File.separator + fileName));
return getSQLiteConnection(new File(folder + File.separator + fileName));
}


Expand Down Expand Up @@ -208,30 +210,6 @@ public static Sql2o getSql2o() {
return sql2o;
}

/**
* Get jdbc-url from config section.
* Config section format:
* type: DBLIB # MYSQL - MySQL database, SQLITI - sqlite database, DBLIB - default (configured in DbLib)
*
* @param section - ConfigSection
* @return - URL
*/
/*
public static String getUrlFromConfig(ConfigSection section) {
String url = DbLibPlugin.getPlugin().getDbUrl();
if (section != null && !section.isEmpty()) {
if (section.getString("type").equalsIgnoreCase("mysql")) {
url = getMySqlUrl(section.get("mysql.host", "localhost"),
section.isInt("mysql.port") ? section.getInt("mysql.port", 3306) :
Integer.parseInt(section.getString("mysql.port", "3306")),
section.getString("mysql.database", "db"));
} else if (section.getString("type").equalsIgnoreCase("sqlite")) {
url = getSqliteUrl(section.getString("file", "data.db"));
}
}
return url;
}
*/
/**
* Get jdbc-url for MySQL file
*
Expand Down Expand Up @@ -266,17 +244,4 @@ public static String getSqliteUrl(String fileName) {
public static String getSqliteUrl(File file) {
return new StringBuilder("jdbc:sqlite:").append(file.getAbsolutePath()).toString();
}

/**
* Get jdbc-url from config file, defined in secktiob key
*
* @param cfg - Config file
* @param key - Section in config file
* @return - URL
*/
/*
public static String getUrlFromConfig(Config cfg, String key) {
return getUrlFromConfig(cfg.isSection(key) ? cfg.getSection(key) : null);
}
*/
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ru.nukkit.dblib;
package ru.nukkit.dblib.core;

public interface Cfg {
public interface DbLibConfig {

String language();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.nukkit.dblib;
package ru.nukkit.dblib.core;

import java.text.DecimalFormat;
import java.util.*;
Expand Down Expand Up @@ -340,7 +340,7 @@ public static boolean logMessage(Object... s) {


public static Message getByName(String name) {
for (Message m : values()){
for (Message m : values()) {
if (m.name().equalsIgnoreCase(name)) return m;
}
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ru.nukkit.dblib;
package ru.nukkit.dblib.core;

import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@

import cn.nukkit.plugin.Plugin;
import cn.nukkit.utils.SimpleConfig;
import ru.nukkit.dblib.Cfg;
import ru.nukkit.dblib.core.DbLibConfig;

import java.io.File;

public class DbLibCfg extends SimpleConfig implements Cfg {
public DbLibCfg(Plugin plugin) {
public class ConfigNukkit extends SimpleConfig implements DbLibConfig {
public ConfigNukkit(Plugin plugin) {
super(plugin);
}

@Path ("general.language")
@Path("general.language")
public String language = "default";

@Path ("general.save-translation")
@Path("general.save-translation")
public boolean saveLanguage = false;

@Path ("general.debug-mode")
@Path("general.debug-mode")
boolean debugMode = false;

@Path("DbLib.use-MySQL")
Expand Down
13 changes: 4 additions & 9 deletions nukkit/src/main/java/ru/nukkit/dblib/nukkit/DbLibPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import cn.nukkit.plugin.PluginBase;
import cn.nukkit.utils.TextFormat;
import ru.nukkit.dblib.DbLib;
import ru.nukkit.dblib.Message;
import ru.nukkit.dblib.core.Message;

public class DbLibPlugin extends PluginBase {

Expand All @@ -14,26 +14,21 @@ public static DbLibPlugin getPlugin() {
return plugin;
}

private DbLibCfg cfg;
private ConfigNukkit cfg;

private boolean debugLog;

@Override
public void onLoad() {
plugin = this;
this.cfg = new DbLibCfg(this);
this.cfg = new ConfigNukkit(this);

this.cfg.load();
this.cfg.save();
Message.init("DbLib", new NukkitMessenger(this), cfg.language, cfg.debugMode, cfg.saveLanguage);
Message.init("DbLib", new MessengerNukkit(this), cfg.language, cfg.debugMode, cfg.saveLanguage);
DbLib.init(cfg, this.getDataFolder());
getLogger().info(TextFormat.colorize("&eDbLib " + this.getDescription().getVersion() + " created by fromgate for nukkit.ru"));
}







}
170 changes: 170 additions & 0 deletions nukkit/src/main/java/ru/nukkit/dblib/nukkit/MessengerNukkit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package ru.nukkit.dblib.nukkit;

import cn.nukkit.Player;
import cn.nukkit.Server;
import cn.nukkit.command.CommandSender;
import cn.nukkit.level.Location;
import cn.nukkit.plugin.PluginBase;
import cn.nukkit.utils.Config;
import cn.nukkit.utils.TextFormat;
import ru.nukkit.dblib.core.Messenger;

import java.io.File;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static ru.nukkit.dblib.core.Message.LNG_SAVE_FAIL;

public class MessengerNukkit implements Messenger {


PluginBase plugin;


public MessengerNukkit(PluginBase plugin) {
this.plugin = plugin;
}

@Override
public String colorize(String text) {
return TextFormat.colorize(text);
}

@Override
public boolean broadcast(String text) {
Server.getInstance().broadcastMessage(text);
return true;
}

@Override
public boolean log(String text) {
plugin.getLogger().info(text);
return true;
}

@Override
public String clean(String text) {
return TextFormat.clean(text);
}

@Override
public boolean tip(int seconds, Object sender, final String text) {
final Player player = toPlayer(sender);
if (player != null) {
for (int i = 0; i < seconds; i++) {
Server.getInstance().getScheduler().scheduleDelayedTask(new Runnable() {
public void run() {
if (player.isOnline()) player.sendPopup(text);
}
}, 20 * i);
}
} else {
CommandSender commandSender = toSender(sender);
if (commandSender != null) commandSender.sendMessage(text);
}
return true;
}

@Override
public boolean tip(Object sender, String text) {
Player player = toPlayer(sender);
if (player != null) {
player.sendTip(text);
} else {
CommandSender commandSender = toSender(sender);
if (commandSender != null) commandSender.sendMessage(text);
}
return true;

}

@Override
public boolean print(Object obj, String text) {
CommandSender sender = toSender(obj);
if (sender != null) {
sender.sendMessage(text);
}
return true;
}

@Override
public boolean broadcast(String permission, String text) {
List<Player> playerList = new ArrayList<Player>();
for (Player player : Server.getInstance().getOnlinePlayers().values()) {
if (permission == null || permission.isEmpty() || player.hasPermission(permission)) {
player.sendMessage(text);
}
}
return true;
}

@Override
public String toString(Object obj, boolean fullFloat) {
if (obj == null) return "'null'";
String s = obj.toString();
DecimalFormat fmt = new DecimalFormat("####0.##");
if (obj instanceof Location) {
Location loc = (Location) obj;
if (fullFloat)
s = loc.getLevel() + "[" + loc.getX() + ", " + loc.getY() + ", " + loc.getZ() + "]";
else
s = loc.getLevel() + "[" + fmt.format(loc.getX()) + ", " + fmt.format(loc.getY()) + ", " + fmt.format(loc.getZ()) + "]";
}
return s;
}

@SuppressWarnings("deprecation")
@Override
public Map<String, String> load(String language) {
File f = new File(plugin.getDataFolder() + File.separator + language + ".lng");
Config lng = null;
if (!f.exists()) {
lng = new Config(f, Config.YAML);
InputStream is = plugin.getClass().getResourceAsStream("/lang/" + language + ".lng");
lng.load(is);
if (!f.delete()) {
System.gc();
f.delete();
}
} else lng = new Config(f, Config.YAML);

Map<String, String> msg = new HashMap<String, String>();
for (String key : lng.getKeys(true)) {
if (lng.isSection(key)) continue;
msg.put(key, lng.getString(key));
}
return msg;
}

@Override
public void save(String language, Map<String, String> messages) {
File f = new File(plugin.getDataFolder() + File.separator + language + ".lng");
Config lng = new Config(f, Config.YAML);
for (String key : messages.keySet())
lng.set(key.toLowerCase(), messages.get(key));
try {
lng.save();
} catch (Exception e) {
LNG_SAVE_FAIL.log();
}
}

@Override
public boolean isValidSender(Object send) {
return (toSender(send) != null);
}

public CommandSender toSender(Object sender) {
return sender instanceof CommandSender ? (CommandSender) sender : null;
}

public Player toPlayer(Object sender) {
return sender instanceof Player ? (Player) sender : null;
}


}
Loading

0 comments on commit f6567eb

Please sign in to comment.