From 6d79da2ca58eb97c185102ff4b8bc74a62bc4172 Mon Sep 17 00:00:00 2001 From: samo_lego <34912839+samolego@users.noreply.github.com> Date: Sun, 11 Oct 2020 17:07:56 +0200 Subject: [PATCH] More mongodb --- .../samo_lego/simpleauth/storage/AuthConfig.java | 4 ++-- .../org/samo_lego/simpleauth/storage/DBHelper.java | 2 +- .../simpleauth/storage/database/MongoDB.java | 13 ++++++++----- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/samo_lego/simpleauth/storage/AuthConfig.java b/src/main/java/org/samo_lego/simpleauth/storage/AuthConfig.java index 242bdc3..1c7b60c 100644 --- a/src/main/java/org/samo_lego/simpleauth/storage/AuthConfig.java +++ b/src/main/java/org/samo_lego/simpleauth/storage/AuthConfig.java @@ -120,8 +120,8 @@ public class AuthConfig { public int port = 27017; public boolean useSsl = true; public String databaseName = "SimpleAuthPlayerData"; - public String password; - public String username; + public String password = ""; + public String username = ""; } } diff --git a/src/main/java/org/samo_lego/simpleauth/storage/DBHelper.java b/src/main/java/org/samo_lego/simpleauth/storage/DBHelper.java index af49b07..2ec26cf 100644 --- a/src/main/java/org/samo_lego/simpleauth/storage/DBHelper.java +++ b/src/main/java/org/samo_lego/simpleauth/storage/DBHelper.java @@ -34,7 +34,7 @@ public class DBHelper { * @return false if connection is open, otherwise false */ public boolean isClosed() { - return config.main.useMongoDB || LevelDB.isClosed(); + return config.main.useMongoDB ? MongoDB.isClosed() : LevelDB.isClosed(); } diff --git a/src/main/java/org/samo_lego/simpleauth/storage/database/MongoDB.java b/src/main/java/org/samo_lego/simpleauth/storage/database/MongoDB.java index 3798a8d..925185c 100644 --- a/src/main/java/org/samo_lego/simpleauth/storage/database/MongoDB.java +++ b/src/main/java/org/samo_lego/simpleauth/storage/database/MongoDB.java @@ -7,6 +7,7 @@ import com.mongodb.client.MongoClients; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import com.mongodb.client.model.ReplaceOneModel; +import com.mongodb.client.model.UpdateOneModel; import org.bson.Document; import org.iq80.leveldb.DBException; import org.samo_lego.simpleauth.storage.PlayerCache; @@ -35,7 +36,7 @@ public class MongoDB { ) );*/ - mongoClient = MongoClients.create(String.format("mongodb://%s:%s", config.mongoDBCredentials.host, config.mongoDBCredentials.port)); + mongoClient = MongoClients.create(String.format("mongodb://%s:%d", config.mongoDBCredentials.host, config.mongoDBCredentials.port)); MongoDatabase database = mongoClient.getDatabase(config.mongoDBCredentials.databaseName); collection = database.getCollection("players"); } @@ -72,12 +73,10 @@ public class MongoDB { } public static void saveFromCache(HashMap playerCacheMap) { - List> list = new ArrayList<>(); + List> list = new ArrayList<>(); playerCacheMap.forEach((uuid, playerCache) -> { - JsonObject data = playerCache.toJson(); - list.add(new ReplaceOneModel<>(eq("UUID", uuid), new Document(uuid, data.toString()))); + list.add(new UpdateOneModel<>(eq("UUID", uuid), new Document("password", playerCache.password))); }); - collection.bulkWrite(list); } @@ -85,4 +84,8 @@ public class MongoDB { mongoClient.close(); return true; } + + public static boolean isClosed() { + return mongoClient == null; + } }