Skip to content

Commit f6567eb

Browse files
committed
v0.2.0
- Added voxelwind support - API changes (removed nukkit depended methods)
1 parent 6765a60 commit f6567eb

13 files changed

Lines changed: 222 additions & 265 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# DbLib
2-
DbLib a library for nukkit, that include ORMLite and drivers for MySQL and SQLite.
2+
DbLib a library for Nukkit and Voxelwind, that include ORMLite, Sql2o and jdbc-connectors (MySQL and SQLite).
33

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

@@ -14,6 +14,7 @@ Example project: [DbExample](https://github.com/fromgate/DbExample)
1414
* 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!
1515

1616
## Configuration
17+
Configuration file named config.yml in Nukkit and config.json in Voxelwind.
1718
```
1819
general:
1920
# Messages language

core/src/main/java/ru/nukkit/dblib/DbLib.java

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import com.j256.ormlite.logger.LocalLog;
66
import com.j256.ormlite.support.ConnectionSource;
77
import org.sql2o.Sql2o;
8+
import ru.nukkit.dblib.core.DbLibConfig;
9+
import ru.nukkit.dblib.core.Message;
810

911
import java.io.File;
1012
import java.sql.Connection;
@@ -13,13 +15,13 @@
1315

1416
public class DbLib {
1517

16-
private static Cfg config;
18+
private static DbLibConfig config;
1719
private static ConnectionSource connectionSource = null;
1820
private static Sql2o sql2o = null;
1921
private static File folder;
2022

2123

22-
public static void init (Cfg cfg, File dataFolder){
24+
public static void init(DbLibConfig cfg, File dataFolder) {
2325
config = cfg;
2426
folder = dataFolder;
2527
System.setProperty(LocalLog.LOCAL_LOG_LEVEL_PROPERTY, config.debugLog() ? "DEBUG" : "ERROR");
@@ -153,7 +155,7 @@ public static Connection getMySqlConnection(String host, int port, String databa
153155
*/
154156
public static Connection getSQLiteConnection(String fileName) {
155157
folder.mkdirs();
156-
return getSQLiteConnection(new File(folder+ File.separator + fileName));
158+
return getSQLiteConnection(new File(folder + File.separator + fileName));
157159
}
158160

159161

@@ -208,30 +210,6 @@ public static Sql2o getSql2o() {
208210
return sql2o;
209211
}
210212

211-
/**
212-
* Get jdbc-url from config section.
213-
* Config section format:
214-
* type: DBLIB # MYSQL - MySQL database, SQLITI - sqlite database, DBLIB - default (configured in DbLib)
215-
*
216-
* @param section - ConfigSection
217-
* @return - URL
218-
*/
219-
/*
220-
public static String getUrlFromConfig(ConfigSection section) {
221-
String url = DbLibPlugin.getPlugin().getDbUrl();
222-
if (section != null && !section.isEmpty()) {
223-
if (section.getString("type").equalsIgnoreCase("mysql")) {
224-
url = getMySqlUrl(section.get("mysql.host", "localhost"),
225-
section.isInt("mysql.port") ? section.getInt("mysql.port", 3306) :
226-
Integer.parseInt(section.getString("mysql.port", "3306")),
227-
section.getString("mysql.database", "db"));
228-
} else if (section.getString("type").equalsIgnoreCase("sqlite")) {
229-
url = getSqliteUrl(section.getString("file", "data.db"));
230-
}
231-
}
232-
return url;
233-
}
234-
*/
235213
/**
236214
* Get jdbc-url for MySQL file
237215
*
@@ -266,17 +244,4 @@ public static String getSqliteUrl(String fileName) {
266244
public static String getSqliteUrl(File file) {
267245
return new StringBuilder("jdbc:sqlite:").append(file.getAbsolutePath()).toString();
268246
}
269-
270-
/**
271-
* Get jdbc-url from config file, defined in secktiob key
272-
*
273-
* @param cfg - Config file
274-
* @param key - Section in config file
275-
* @return - URL
276-
*/
277-
/*
278-
public static String getUrlFromConfig(Config cfg, String key) {
279-
return getUrlFromConfig(cfg.isSection(key) ? cfg.getSection(key) : null);
280-
}
281-
*/
282247
}

core/src/main/java/ru/nukkit/dblib/Cfg.java renamed to core/src/main/java/ru/nukkit/dblib/core/DbLibConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package ru.nukkit.dblib;
1+
package ru.nukkit.dblib.core;
22

3-
public interface Cfg {
3+
public interface DbLibConfig {
44

55
String language();
66

core/src/main/java/ru/nukkit/dblib/Message.java renamed to core/src/main/java/ru/nukkit/dblib/core/Message.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ru.nukkit.dblib;
1+
package ru.nukkit.dblib.core;
22

33
import java.text.DecimalFormat;
44
import java.util.*;
@@ -340,7 +340,7 @@ public static boolean logMessage(Object... s) {
340340

341341

342342
public static Message getByName(String name) {
343-
for (Message m : values()){
343+
for (Message m : values()) {
344344
if (m.name().equalsIgnoreCase(name)) return m;
345345
}
346346
return null;

core/src/main/java/ru/nukkit/dblib/Messenger.java renamed to core/src/main/java/ru/nukkit/dblib/core/Messenger.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ru.nukkit.dblib;
1+
package ru.nukkit.dblib.core;
22

33
import java.util.Map;
44

nukkit/src/main/java/ru/nukkit/dblib/nukkit/DbLibCfg.java renamed to nukkit/src/main/java/ru/nukkit/dblib/nukkit/ConfigNukkit.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@
22

33
import cn.nukkit.plugin.Plugin;
44
import cn.nukkit.utils.SimpleConfig;
5-
import ru.nukkit.dblib.Cfg;
5+
import ru.nukkit.dblib.core.DbLibConfig;
66

77
import java.io.File;
88

9-
public class DbLibCfg extends SimpleConfig implements Cfg {
10-
public DbLibCfg(Plugin plugin) {
9+
public class ConfigNukkit extends SimpleConfig implements DbLibConfig {
10+
public ConfigNukkit(Plugin plugin) {
1111
super(plugin);
1212
}
1313

14-
@Path ("general.language")
14+
@Path("general.language")
1515
public String language = "default";
1616

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

20-
@Path ("general.debug-mode")
20+
@Path("general.debug-mode")
2121
boolean debugMode = false;
2222

2323
@Path("DbLib.use-MySQL")

nukkit/src/main/java/ru/nukkit/dblib/nukkit/DbLibPlugin.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import cn.nukkit.plugin.PluginBase;
44
import cn.nukkit.utils.TextFormat;
55
import ru.nukkit.dblib.DbLib;
6-
import ru.nukkit.dblib.Message;
6+
import ru.nukkit.dblib.core.Message;
77

88
public class DbLibPlugin extends PluginBase {
99

@@ -14,26 +14,21 @@ public static DbLibPlugin getPlugin() {
1414
return plugin;
1515
}
1616

17-
private DbLibCfg cfg;
17+
private ConfigNukkit cfg;
1818

1919
private boolean debugLog;
2020

2121
@Override
2222
public void onLoad() {
2323
plugin = this;
24-
this.cfg = new DbLibCfg(this);
24+
this.cfg = new ConfigNukkit(this);
2525

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

3333

34-
35-
36-
37-
38-
3934
}
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
package ru.nukkit.dblib.nukkit;
2+
3+
import cn.nukkit.Player;
4+
import cn.nukkit.Server;
5+
import cn.nukkit.command.CommandSender;
6+
import cn.nukkit.level.Location;
7+
import cn.nukkit.plugin.PluginBase;
8+
import cn.nukkit.utils.Config;
9+
import cn.nukkit.utils.TextFormat;
10+
import ru.nukkit.dblib.core.Messenger;
11+
12+
import java.io.File;
13+
import java.io.InputStream;
14+
import java.text.DecimalFormat;
15+
import java.util.ArrayList;
16+
import java.util.HashMap;
17+
import java.util.List;
18+
import java.util.Map;
19+
20+
import static ru.nukkit.dblib.core.Message.LNG_SAVE_FAIL;
21+
22+
public class MessengerNukkit implements Messenger {
23+
24+
25+
PluginBase plugin;
26+
27+
28+
public MessengerNukkit(PluginBase plugin) {
29+
this.plugin = plugin;
30+
}
31+
32+
@Override
33+
public String colorize(String text) {
34+
return TextFormat.colorize(text);
35+
}
36+
37+
@Override
38+
public boolean broadcast(String text) {
39+
Server.getInstance().broadcastMessage(text);
40+
return true;
41+
}
42+
43+
@Override
44+
public boolean log(String text) {
45+
plugin.getLogger().info(text);
46+
return true;
47+
}
48+
49+
@Override
50+
public String clean(String text) {
51+
return TextFormat.clean(text);
52+
}
53+
54+
@Override
55+
public boolean tip(int seconds, Object sender, final String text) {
56+
final Player player = toPlayer(sender);
57+
if (player != null) {
58+
for (int i = 0; i < seconds; i++) {
59+
Server.getInstance().getScheduler().scheduleDelayedTask(new Runnable() {
60+
public void run() {
61+
if (player.isOnline()) player.sendPopup(text);
62+
}
63+
}, 20 * i);
64+
}
65+
} else {
66+
CommandSender commandSender = toSender(sender);
67+
if (commandSender != null) commandSender.sendMessage(text);
68+
}
69+
return true;
70+
}
71+
72+
@Override
73+
public boolean tip(Object sender, String text) {
74+
Player player = toPlayer(sender);
75+
if (player != null) {
76+
player.sendTip(text);
77+
} else {
78+
CommandSender commandSender = toSender(sender);
79+
if (commandSender != null) commandSender.sendMessage(text);
80+
}
81+
return true;
82+
83+
}
84+
85+
@Override
86+
public boolean print(Object obj, String text) {
87+
CommandSender sender = toSender(obj);
88+
if (sender != null) {
89+
sender.sendMessage(text);
90+
}
91+
return true;
92+
}
93+
94+
@Override
95+
public boolean broadcast(String permission, String text) {
96+
List<Player> playerList = new ArrayList<Player>();
97+
for (Player player : Server.getInstance().getOnlinePlayers().values()) {
98+
if (permission == null || permission.isEmpty() || player.hasPermission(permission)) {
99+
player.sendMessage(text);
100+
}
101+
}
102+
return true;
103+
}
104+
105+
@Override
106+
public String toString(Object obj, boolean fullFloat) {
107+
if (obj == null) return "'null'";
108+
String s = obj.toString();
109+
DecimalFormat fmt = new DecimalFormat("####0.##");
110+
if (obj instanceof Location) {
111+
Location loc = (Location) obj;
112+
if (fullFloat)
113+
s = loc.getLevel() + "[" + loc.getX() + ", " + loc.getY() + ", " + loc.getZ() + "]";
114+
else
115+
s = loc.getLevel() + "[" + fmt.format(loc.getX()) + ", " + fmt.format(loc.getY()) + ", " + fmt.format(loc.getZ()) + "]";
116+
}
117+
return s;
118+
}
119+
120+
@SuppressWarnings("deprecation")
121+
@Override
122+
public Map<String, String> load(String language) {
123+
File f = new File(plugin.getDataFolder() + File.separator + language + ".lng");
124+
Config lng = null;
125+
if (!f.exists()) {
126+
lng = new Config(f, Config.YAML);
127+
InputStream is = plugin.getClass().getResourceAsStream("/lang/" + language + ".lng");
128+
lng.load(is);
129+
if (!f.delete()) {
130+
System.gc();
131+
f.delete();
132+
}
133+
} else lng = new Config(f, Config.YAML);
134+
135+
Map<String, String> msg = new HashMap<String, String>();
136+
for (String key : lng.getKeys(true)) {
137+
if (lng.isSection(key)) continue;
138+
msg.put(key, lng.getString(key));
139+
}
140+
return msg;
141+
}
142+
143+
@Override
144+
public void save(String language, Map<String, String> messages) {
145+
File f = new File(plugin.getDataFolder() + File.separator + language + ".lng");
146+
Config lng = new Config(f, Config.YAML);
147+
for (String key : messages.keySet())
148+
lng.set(key.toLowerCase(), messages.get(key));
149+
try {
150+
lng.save();
151+
} catch (Exception e) {
152+
LNG_SAVE_FAIL.log();
153+
}
154+
}
155+
156+
@Override
157+
public boolean isValidSender(Object send) {
158+
return (toSender(send) != null);
159+
}
160+
161+
public CommandSender toSender(Object sender) {
162+
return sender instanceof CommandSender ? (CommandSender) sender : null;
163+
}
164+
165+
public Player toPlayer(Object sender) {
166+
return sender instanceof Player ? (Player) sender : null;
167+
}
168+
169+
170+
}

0 commit comments

Comments
 (0)