summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPugmatt <pugmatt@gmail.com>2020-03-10 21:16:35 -0400
committerPugmatt <pugmatt@gmail.com>2020-03-10 21:16:35 -0400
commitaad99c92bd95484a8a3a7eaf9916a8fb28a57a43 (patch)
tree6091cbe6103b37fa9dc54461c20508c428cb2bf1
parentfd77605ecc8d02ad4d498111066311728db7bc35 (diff)
downloadBedrockConnect-1.4.tar.gz
BedrockConnect-1.4.tar.bz2
BedrockConnect-1.4.zip
Add "nodb" option to allow running software without the need of a database1.4
-rw-r--r--serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/BedrockConnect.java101
-rw-r--r--serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/PipePlayer.java2
-rw-r--r--serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/listeners/PacketHandler.java2
-rw-r--r--serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/sql/Data.java113
4 files changed, 145 insertions, 73 deletions
diff --git a/serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/BedrockConnect.java b/serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/BedrockConnect.java
index 149727c..2c00ac4 100644
--- a/serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/BedrockConnect.java
+++ b/serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/BedrockConnect.java
@@ -24,7 +24,10 @@ public class BedrockConnect {
public static Server server;
+ public static boolean noDB = false;
+
public static void main(String[] args) {
+ System.out.println("-= BedrockConnect =-");
paletteManager = new PaletteManager();
try {
@@ -49,56 +52,70 @@ public class BedrockConnect {
serverLimit = getArgValue(str, "server_limit");
if(str.startsWith("port="))
port = getArgValue(str, "port");
+ if(str.startsWith("nodb="))
+ noDB = true;
}
+ if(!noDB)
System.out.println("MySQL Host: " + hostname + "\n" +
"MySQL Database: " + database + "\n" +
- "MySQL User: " + username + "\n" +
- "Server Limit: " + serverLimit + "\n" +
- "Port: " + port + "\n");
-
- MySQL = new MySQL(hostname, database, username, password);
-
- connection = null;
-
- connection = MySQL.openConnection();
-
- data = new Data(serverLimit);
-
- // Keep MySQL connection alive
- Timer timer = new Timer();
- TimerTask task = new TimerTask() {
- int sec;
-
- public void run() {
- try {
- if (connection == null || connection.isClosed()) {
- connection = MySQL.openConnection();
- } else {
- if (sec == 600) {
- try {
- ResultSet rs = connection
- .createStatement()
- .executeQuery(
- "SELECT 1");
- rs.next();
- } catch (SQLException e) {
- // TODO Auto-generated
- // catch block
- e.printStackTrace();
+ "MySQL User: " + username);
+
+ System.out.println("\nServer Limit: " + serverLimit + "\n" + "Port: " + port + "\n");
+
+ if(!noDB) {
+ MySQL = new MySQL(hostname, database, username, password);
+
+ connection = null;
+
+ connection = MySQL.openConnection();
+
+ data = new Data(serverLimit);
+
+ // Keep MySQL connection alive
+ Timer timer = new Timer();
+ TimerTask task = new TimerTask() {
+ int sec;
+
+ public void run() {
+ try {
+ if (connection == null || connection.isClosed()) {
+ connection = MySQL.openConnection();
+ } else {
+ if (sec == 600) {
+ try {
+ ResultSet rs = connection
+ .createStatement()
+ .executeQuery(
+ "SELECT 1");
+ rs.next();
+ } catch (SQLException e) {
+ // TODO Auto-generated
+ // catch block
+ e.printStackTrace();
+ }
+ sec = 0;
}
- sec = 0;
}
+ } catch (SQLException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
}
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
+ sec++;
}
- sec++;
- }
- };
- timer.scheduleAtFixedRate(task, 0L, 1200L);
-
+ };
+ timer.scheduleAtFixedRate(task, 0L, 1200L);
+ } else {
+ data = new Data(serverLimit);
+ Timer timer = new Timer();
+ TimerTask task = new TimerTask() {
+ int sec;
+ public void run() {
+ sec++;
+ }
+ };
+ timer.scheduleAtFixedRate(task, 0L, 1200L);
+ }
server = new Server(port);
} catch(Exception e) {
diff --git a/serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/PipePlayer.java b/serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/PipePlayer.java
index 2fdf0b4..8cfcb6f 100644
--- a/serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/PipePlayer.java
+++ b/serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/PipePlayer.java
@@ -77,7 +77,7 @@ public class PipePlayer {
}
public void setServerList(List<String> serverList) {
- data.setValueString("servers", UIComponents.serversToFormData(serverList), uuid);
+ data.setValueString("servers", UIComponents.serversToFormData(serverList), serverList, uuid);
this.serverList = serverList;
}
diff --git a/serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/listeners/PacketHandler.java b/serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/listeners/PacketHandler.java
index f3f1a5b..f7362ef 100644
--- a/serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/listeners/PacketHandler.java
+++ b/serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/listeners/PacketHandler.java
@@ -1082,7 +1082,7 @@ public class PacketHandler implements BedrockPacketHandler {
}
public void disconnect() {
- System.out.println("Player disconnected");
+ System.out.println(name + " disconnected");
server.removePlayer(server.getPlayer(uuid));
}
diff --git a/serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/sql/Data.java b/serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/sql/Data.java
index 68c1564..be628fc 100644
--- a/serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/sql/Data.java
+++ b/serverlist-server/src/main/com/pyratron/pugmatt/bedrockconnect/sql/Data.java
@@ -1,10 +1,19 @@
package main.com.pyratron.pugmatt.bedrockconnect.sql;
+import com.nukkitx.protocol.bedrock.Bedrock;
import com.nukkitx.protocol.bedrock.BedrockServerSession;
+import com.nukkitx.protocol.bedrock.BedrockSession;
import main.com.pyratron.pugmatt.bedrockconnect.BedrockConnect;
import main.com.pyratron.pugmatt.bedrockconnect.PipePlayer;
import main.com.pyratron.pugmatt.bedrockconnect.gui.UIComponents;
-
+import org.json.simple.JSONArray;
+import org.json.simple.JSONObject;
+import org.json.simple.parser.JSONParser;
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@@ -19,10 +28,12 @@ public class Data {
public Data(String serverLimit) {
this.serverLimit = serverLimit;
- try {
- createTables();
- } catch(Exception e) {
- errorAlert(e);
+ if(!BedrockConnect.noDB) {
+ try {
+ createTables();
+ } catch (Exception e) {
+ errorAlert(e);
+ }
}
}
@@ -64,40 +75,69 @@ public class Data {
// If user exists
public void userExists(String uuid, String name, BedrockServerSession session) {
- Data db = this;
- new Thread(() -> {
- try
- {
+ if(!BedrockConnect.noDB) {
+ Data db = this;
+ new Thread(() -> {
+ try {
PreparedStatement statement = BedrockConnect.connection.prepareStatement("SELECT EXISTS(SELECT 1 FROM servers WHERE uuid = '" + uuid + "')");
statement.executeQuery();
ResultSet RS = statement.executeQuery("SELECT COUNT(*) AS total FROM servers where uuid ='" + uuid + "'");
while (RS.next()) {
- if (RS.getInt("total") > 0)
- {
+ if (RS.getInt("total") > 0) {
ResultSet rs = BedrockConnect.connection.createStatement().executeQuery("SELECT * FROM servers WHERE uuid = '" + uuid + "';");
while (rs.next()) {
if (!rs.getString("name").equals(name)) {
Basic_SQL("UPDATE servers SET name='" + name + "' WHERE uuid='" + uuid + "'");
}
PipePlayer p = getPlayer(rs, uuid, session);
- if(BedrockConnect.server == null)
- System.out.println("??");
- if(p != null)
+ if (p != null)
BedrockConnect.server.addPlayer(p);
}
- }
- else
- {
+ } else {
addNewUser(uuid, name, session);
}
}
- }
- catch (Exception e)
- {
+ } catch (Exception e) {
errorAlert(e);
session.disconnect("We had some trouble receiving your player data. Please report this to the BedrockConnect discord.");
}
- }).start();
+ }).start();
+ } else {
+ try {
+ File f = new File("players");
+ f.mkdir();
+ File plyrFile = new File("players/" + uuid + ".json");
+ if (plyrFile.createNewFile()) {
+ JSONObject jo = new JSONObject();
+ jo.put("uuid", uuid);
+ jo.put("name", name);
+ jo.put("serverLimit", serverLimit);
+ jo.put("servers", new JSONArray());
+
+ PrintWriter pw = new PrintWriter("players/" + uuid + ".json");
+ pw.write(jo.toJSONString());
+ pw.flush();
+ pw.close();
+
+ PipePlayer pl = new PipePlayer(uuid, this, session, new ArrayList<>(), Integer.parseInt(serverLimit));
+ BedrockConnect.server.addPlayer(pl);
+ } else {
+ Object obj = new JSONParser().parse(new FileReader("players/" + uuid + ".json"));
+
+ JSONObject jo = (JSONObject) obj;
+
+ int serverLimit = Integer.parseInt((String) jo.get("serverLimit"));
+
+ JSONArray servers = (JSONArray) jo.get("servers");
+
+ PipePlayer p = new PipePlayer(uuid,this, session, servers, serverLimit);
+ BedrockConnect.server.addPlayer(p);
+ }
+ } catch (Exception e) {
+ System.out.println("An error occurred.");
+ e.printStackTrace();
+ }
+ }
}
public void addNewUser(String uuid, String name, BedrockServerSession session)
@@ -120,17 +160,32 @@ public class Data {
}).start();
}
- public void setValueString(String column, String value, String uuid) {
- new Thread(() -> {
- try
- {
+ public void setValueString(String column, String value, List<String> serverList, String uuid) {
+ if(!BedrockConnect.noDB) {
+ new Thread(() -> {
+ try {
Basic_SQL("UPDATE servers SET " + column + "='" + value + "' WHERE uuid='" + uuid + "'");
- }
- catch (Exception e)
- {
+ } catch (Exception e) {
errorAlert(e);
}
- }).start();
+ }).start();
+ } else {
+ try {
+ Object obj = new JSONParser().parse(new FileReader("players/" + uuid + ".json"));
+
+ JSONObject jo = (JSONObject) obj;
+
+ jo.put("servers", serverList);
+
+ PrintWriter pw = new PrintWriter("players/" + uuid + ".json");
+ pw.write(jo.toJSONString());
+ pw.flush();
+ pw.close();
+ } catch (Exception e) {
+ System.out.println("An error occurred.");
+ e.printStackTrace();
+ }
+ }
}
public void setValueInt(String column, Integer value, String uuid) {