From 0718d8ec0300ac2ae3d098e257fb1e45672c1260 Mon Sep 17 00:00:00 2001 From: samo_lego <34912839+samolego@users.noreply.github.com> Date: Thu, 8 Oct 2020 18:39:18 +0200 Subject: [PATCH] Compiling fixes --- .../org/samo_lego/simpleauth/SimpleAuth.java | 30 ++--------------- .../simpleauth/storage/DBHelper.java | 19 ++++++++--- .../simpleauth/storage/PlayerCache.java | 21 ++++++++++++ .../simpleauth/storage/database/LevelDB.java | 30 +++++++++++++---- .../simpleauth/storage/database/MongoDB.java | 33 ++++++++++++++----- 5 files changed, 85 insertions(+), 48 deletions(-) diff --git a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java index 5331f12..537bbdd 100644 --- a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java +++ b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java @@ -1,6 +1,5 @@ package org.samo_lego.simpleauth; -import com.google.gson.JsonObject; import net.fabricmc.api.DedicatedServerModInitializer; import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; @@ -16,15 +15,14 @@ import net.minecraft.util.math.BlockPos; import net.minecraft.util.registry.Registry; import net.minecraft.util.registry.RegistryKey; import net.minecraft.world.World; -import org.iq80.leveldb.WriteBatch; import org.samo_lego.simpleauth.commands.*; import org.samo_lego.simpleauth.event.AuthEventHandler; import org.samo_lego.simpleauth.event.entity.player.*; import org.samo_lego.simpleauth.event.item.DropItemCallback; import org.samo_lego.simpleauth.event.item.TakeItemCallback; import org.samo_lego.simpleauth.storage.AuthConfig; -import org.samo_lego.simpleauth.storage.PlayerCache; import org.samo_lego.simpleauth.storage.DBHelper; +import org.samo_lego.simpleauth.storage.PlayerCache; import java.io.File; import java.io.FileReader; @@ -38,7 +36,6 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; -import static org.iq80.leveldb.impl.Iq80DBFactory.bytes; import static org.samo_lego.simpleauth.utils.CarpetHelper.isPlayerCarpetFake; import static org.samo_lego.simpleauth.utils.SimpleLogger.logError; import static org.samo_lego.simpleauth.utils.SimpleLogger.logInfo; @@ -136,30 +133,7 @@ public class SimpleAuth implements DedicatedServerModInitializer { */ private void onStopServer(MinecraftServer server) { logInfo("Shutting down SimpleAuth."); - - WriteBatch batch = DB.getLevelDBStore().createWriteBatch(); - // Updating player data. - playerCacheMap.forEach((uuid, playerCache) -> { - JsonObject data = new JsonObject(); - data.addProperty("password", playerCache.password); - - JsonObject lastLocation = new JsonObject(); - lastLocation.addProperty("dim", playerCache.lastDim); - lastLocation.addProperty("x", playerCache.lastX); - lastLocation.addProperty("y", playerCache.lastY); - lastLocation.addProperty("z", playerCache.lastZ); - - data.addProperty("lastLocation", lastLocation.toString()); - - batch.put(bytes("UUID:" + uuid), bytes("data:" + data.toString())); - }); - try { - // Writing and closing batch - DB.getLevelDBStore().write(batch); - batch.close(); - } catch (IOException e) { - logError("Error saving player data! " + e.getMessage()); - } + DB.saveAll(playerCacheMap); // Closing threads try { 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 d8c4919..af49b07 100644 --- a/src/main/java/org/samo_lego/simpleauth/storage/DBHelper.java +++ b/src/main/java/org/samo_lego/simpleauth/storage/DBHelper.java @@ -3,7 +3,10 @@ package org.samo_lego.simpleauth.storage; import org.samo_lego.simpleauth.storage.database.LevelDB; import org.samo_lego.simpleauth.storage.database.MongoDB; +import java.util.HashMap; + import static org.samo_lego.simpleauth.SimpleAuth.config; +import static org.samo_lego.simpleauth.utils.SimpleLogger.logInfo; public class DBHelper { @@ -21,8 +24,8 @@ public class DBHelper { * Closes database connection. */ public void close() { - if(!config.main.useMongoDB) - LevelDB.close(); + if(config.main.useMongoDB && MongoDB.close() || LevelDB.close()) + logInfo("Database connection closed successfully."); } /** @@ -65,9 +68,9 @@ public class DBHelper { */ public void deleteUserData(String uuid) { if(config.main.useMongoDB) - MongoDB.isUserRegistered(uuid); + MongoDB.deleteUserData(uuid); else - LevelDB.isUserRegistered(uuid); + LevelDB.deleteUserData(uuid); } /** @@ -92,4 +95,12 @@ public class DBHelper { public String getUserData(String uuid){ return config.main.useMongoDB ? MongoDB.getUserData(uuid) : LevelDB.getUserData(uuid); } + + public void saveAll(HashMap playerCacheMap) { + // Saving player data. + if(config.main.useMongoDB) + MongoDB.saveFromCache(playerCacheMap); + else + LevelDB.saveFromCache(playerCacheMap); + } } \ No newline at end of file diff --git a/src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java b/src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java index b7469c0..86d9b20 100644 --- a/src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java +++ b/src/main/java/org/samo_lego/simpleauth/storage/PlayerCache.java @@ -133,4 +133,25 @@ public class PlayerCache { if(config.experimental.debugMode) logInfo("Cache created. Registered: " + this.isRegistered + ", hashed password: " + this.password); } + + public static PlayerCache fromJson(String json) { + PlayerCache playerCache = gson.fromJson(json, PlayerCache.class); + System.out.println(playerCache); + return playerCache; + } + + public JsonObject toJson() { + JsonObject cacheJson = new JsonObject(); + cacheJson.addProperty("password", this.password); + + JsonObject lastLocation = new JsonObject(); + lastLocation.addProperty("dim", this.lastDim); + lastLocation.addProperty("x", this.lastX); + lastLocation.addProperty("y", this.lastY); + lastLocation.addProperty("z", this.lastZ); + + cacheJson.addProperty("lastLocation", lastLocation.toString()); + + return cacheJson; + } } diff --git a/src/main/java/org/samo_lego/simpleauth/storage/database/LevelDB.java b/src/main/java/org/samo_lego/simpleauth/storage/database/LevelDB.java index a9ee8ce..cc4befc 100644 --- a/src/main/java/org/samo_lego/simpleauth/storage/database/LevelDB.java +++ b/src/main/java/org/samo_lego/simpleauth/storage/database/LevelDB.java @@ -1,25 +1,24 @@ package org.samo_lego.simpleauth.storage.database; +import com.google.gson.JsonObject; import org.iq80.leveldb.DB; import org.iq80.leveldb.DBException; import org.iq80.leveldb.Options; +import org.iq80.leveldb.WriteBatch; import org.samo_lego.simpleauth.SimpleAuth; +import org.samo_lego.simpleauth.storage.PlayerCache; import java.io.File; import java.io.IOException; +import java.util.HashMap; import static org.iq80.leveldb.impl.Iq80DBFactory.bytes; import static org.iq80.leveldb.impl.Iq80DBFactory.factory; import static org.samo_lego.simpleauth.utils.SimpleLogger.logError; -import static org.samo_lego.simpleauth.utils.SimpleLogger.logInfo; public class LevelDB { private static DB levelDBStore; - public static DB getLevelDBStore() { - return levelDBStore; - } - /** * Connects to the LevelDB. */ @@ -35,15 +34,16 @@ public class LevelDB { /** * Closes database connection. */ - public static void close() { + public static boolean close() { if (levelDBStore != null) { try { levelDBStore.close(); - logInfo("Database connection closed successfully."); + return true; } catch (Error | IOException e) { logError(e.getMessage()); } } + return false; } /** @@ -133,4 +133,20 @@ public class LevelDB { } return ""; } + + public static void saveFromCache(HashMap playerCacheMap) { + WriteBatch batch = levelDBStore.createWriteBatch(); + // Updating player data. + playerCacheMap.forEach((uuid, playerCache) -> { + JsonObject data = playerCache.toJson(); + batch.put(bytes("UUID:" + uuid), bytes("data:" + data.toString())); + }); + try { + // Writing and closing batch + levelDBStore.write(batch); + batch.close(); + } catch (IOException e) { + logError("Error saving player data! " + e.getMessage()); + } + } } 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 d051d2a..3798a8d 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 @@ -1,25 +1,27 @@ package org.samo_lego.simpleauth.storage.database; +import com.google.gson.JsonObject; 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.ReplaceOneModel; import org.bson.Document; import org.iq80.leveldb.DBException; +import org.samo_lego.simpleauth.storage.PlayerCache; - -import javax.print.Doc; -import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; import static com.mongodb.client.model.Filters.eq; -import static org.iq80.leveldb.impl.Iq80DBFactory.bytes; import static org.samo_lego.simpleauth.SimpleAuth.config; import static org.samo_lego.simpleauth.utils.SimpleLogger.logError; -import static org.samo_lego.simpleauth.utils.SimpleLogger.logInfo; public class MongoDB { private static MongoCollection collection; + private static MongoClient mongoClient; public static void initialize() { /*mongoClient = MongoClients.create( @@ -33,7 +35,7 @@ public class MongoDB { ) );*/ - MongoClient mongoClient = MongoClients.create(String.format("mongodb://%s:%s", config.mongoDBCredentials.host, config.mongoDBCredentials.port)); + mongoClient = MongoClients.create(String.format("mongodb://%s:%s", config.mongoDBCredentials.host, config.mongoDBCredentials.port)); MongoDatabase database = mongoClient.getDatabase(config.mongoDBCredentials.databaseName); collection = database.getCollection("players"); } @@ -66,8 +68,21 @@ public class MongoDB { } public static String getUserData(String uuid){ - if(isUserRegistered(uuid)) - return collection.find(eq("UUID", uuid)).iterator().next().getString(uuid); - return ""; + return isUserRegistered(uuid) ? collection.find(eq("UUID", uuid)).iterator().next().getString(uuid) : ""; + } + + public static void saveFromCache(HashMap playerCacheMap) { + List> list = new ArrayList<>(); + playerCacheMap.forEach((uuid, playerCache) -> { + JsonObject data = playerCache.toJson(); + list.add(new ReplaceOneModel<>(eq("UUID", uuid), new Document(uuid, data.toString()))); + }); + + collection.bulkWrite(list); + } + + public static boolean close() { + mongoClient.close(); + return true; } }