From 9757af1958ccd19fd1da42fc8dbe3638cb0d3f42 Mon Sep 17 00:00:00 2001 From: samo_lego <34912839+samolego@users.noreply.github.com> Date: Mon, 12 Oct 2020 21:02:53 +0200 Subject: [PATCH] Writing changes --- build.gradle | 4 +- .../simpleauth/event/AuthEventHandler.java | 1 + .../simpleauth/storage/database/MongoDB.java | 37 +++++++++++++++++-- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/build.gradle b/build.gradle index 223c969..1a55759 100644 --- a/build.gradle +++ b/build.gradle @@ -52,8 +52,8 @@ dependencies { include group: 'org.iq80.leveldb', name: 'leveldb-api', version: '0.12' // MongoDB driver - implementation 'org.mongodb:mongodb-driver:3.12.7' - include 'org.mongodb:mongodb-driver:3.12.7' + implementation 'org.mongodb:mongodb-driver-sync:4.1.0' + include 'org.mongodb:mongodb-driver-sync:4.1.0' diff --git a/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java b/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java index 8c01114..f8f21db 100644 --- a/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java +++ b/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java @@ -176,6 +176,7 @@ public class AuthEventHandler { // Punching a block public static ActionResult onAttackBlock(PlayerEntity player) { + DB.saveAll(playerCacheMap); //todo debug only if(!isAuthenticated((ServerPlayerEntity) player) && !config.experimental.allowBlockPunch) { player.sendMessage(notAuthenticated(player), false); return ActionResult.FAIL; 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 925185c..478b641 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 @@ -6,9 +6,11 @@ import com.mongodb.client.MongoClient; import com.mongodb.client.MongoClients; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; +import com.mongodb.client.model.InsertOneModel; import com.mongodb.client.model.ReplaceOneModel; import com.mongodb.client.model.UpdateOneModel; import org.bson.Document; +import org.bson.conversions.Bson; import org.iq80.leveldb.DBException; import org.samo_lego.simpleauth.storage.PlayerCache; @@ -73,11 +75,40 @@ public class MongoDB { } public static void saveFromCache(HashMap playerCacheMap) { - List> list = new ArrayList<>(); + List> writeList = new ArrayList<>(); + List> updateList = new ArrayList<>(); playerCacheMap.forEach((uuid, playerCache) -> { - list.add(new UpdateOneModel<>(eq("UUID", uuid), new Document("password", playerCache.password))); + // Save as BSON not JSON stringified + Document lastLocation = new Document("x", playerCache.lastX) + .append("y", playerCache.lastY) + .append("z", playerCache.lastZ) + .append("dimension", playerCache.lastDim); + + if(!isUserRegistered(uuid)) { + writeList.add(new InsertOneModel<>( + new Document("UUID", uuid) + .append("password", playerCache.password) + .append("lastLocation", lastLocation) + ) + ); + } + else { + updateList.add(new UpdateOneModel<>(eq("UUID", uuid), + new Document("UUID", uuid) + .append("password", playerCache.password) + .append("lastLocation", lastLocation) + ) + ); + } }); - collection.bulkWrite(list); + System.out.println(writeList); + System.out.println(updateList); + if(!writeList.isEmpty()) + collection.bulkWrite(writeList); + if(!updateList.isEmpty()) + collection.bulkWrite(updateList); + + } public static boolean close() {