Alpha mongodb support

This commit is contained in:
samo_lego 2020-10-13 20:37:14 +02:00
parent 9757af1958
commit 3b4e9f9561
6 changed files with 68 additions and 97 deletions

View File

@ -220,7 +220,7 @@ public class SimpleAuth implements DedicatedServerModInitializer {
// Marking player as not authenticated
String uuid = convertUuid(player);
playerCacheMap.put(uuid, new PlayerCache(uuid, player));
playerCacheMap.put(uuid, PlayerCache.fromJson(player, DB.getUserData(uuid)));
playerCacheMap.get(uuid).isAuthenticated = false;
// Teleporting player to spawn to hide its position

View File

@ -177,7 +177,7 @@ public class AuthCommand {
Entity sender = source.getEntity();
THREADPOOL.submit(() -> {
DB.deleteUserData(uuid);
SimpleAuth.playerCacheMap.put(uuid, new PlayerCache(uuid, null));
SimpleAuth.playerCacheMap.put(uuid, new PlayerCache(null));
});
if(sender != null)
@ -205,7 +205,7 @@ public class AuthCommand {
playerCache = playerCacheMap.get(uuid);
}
else {
playerCache = new PlayerCache(uuid, null);
playerCache = new PlayerCache(null);
}
playerCacheMap.put(uuid, playerCache);
@ -238,7 +238,7 @@ public class AuthCommand {
playerCache = playerCacheMap.get(uuid);
}
else {
playerCache = new PlayerCache(uuid, null);
playerCache = new PlayerCache(null);
}
playerCacheMap.put(uuid, playerCache);

View File

@ -176,7 +176,6 @@ 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;

View File

@ -47,7 +47,8 @@ public class DBHelper {
*/
public boolean registerUser(String uuid, String data) {
if(config.main.useMongoDB)
return MongoDB.registerUser(uuid, data);
//return MongoDB.registerUser(uuid, data);
System.out.println("Not implemented yet.");
return LevelDB.registerUser(uuid, data);
}
@ -81,7 +82,8 @@ public class DBHelper {
*/
public void updateUserData(String uuid, String data) {
if(config.main.useMongoDB)
MongoDB.updateUserData(uuid, data);
//MongoDB.updateUserData(uuid, data);
System.out.println("Not implemented yet.");
else
LevelDB.updateUserData(uuid, data);
}

View File

@ -1,11 +1,12 @@
package org.samo_lego.simpleauth.storage;
import com.google.gson.*;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import net.minecraft.block.Blocks;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText;
import static org.samo_lego.simpleauth.SimpleAuth.DB;
import java.util.Objects;
import static org.samo_lego.simpleauth.SimpleAuth.config;
import static org.samo_lego.simpleauth.utils.SimpleLogger.logInfo;
@ -57,21 +58,21 @@ public class PlayerCache {
private static final Gson gson = new Gson();
public PlayerCache(String uuid, ServerPlayerEntity player) {
if(DB.isClosed())
return;
public PlayerCache(ServerPlayerEntity player) {
if(config.experimental.debugMode)
logInfo("Creating cache for " + Objects.requireNonNull(player).getName());
this.isAuthenticated = false;
this.loginTries = 0;
if(player != null) {
if(config.experimental.debugMode)
logInfo("Creating cache for " + player.getName());
this.lastIp = player.getIp();
this.lastAir = player.getAir();
this.wasOnFire = player.isOnFire();
this.wasInPortal = player.getBlockState().getBlock().equals(Blocks.NETHER_PORTAL);
this.lastAir = player.getAir();
// Setting position cache
this.lastDim = String.valueOf(player.getEntityWorld().getRegistryKey().getValue());
this.wasInPortal = player.getBlockState().getBlock().equals(Blocks.NETHER_PORTAL);
this.lastX = player.getX();
this.lastY = player.getY();
this.lastZ = player.getZ();
@ -82,61 +83,52 @@ public class PlayerCache {
this.lastAir = 300;
}
String data = DB.getUserData(uuid);
if(!data.isEmpty()) {
// Getting (hashed) password
JsonObject json = gson.fromJson(data, JsonObject.class);
JsonElement passwordElement = json.get("password");
if(passwordElement instanceof JsonNull) {
if(player != null) {
player.sendMessage(new LiteralText(config.lang.corruptedPlayerData), false);
}
this.isRegistered = false;
this.password = "";
if(config.experimental.debugMode)
logInfo("Password for " + uuid + " is null! Marking as not registered.");
this.password = "";
this.isRegistered = false;
}
else {
this.password = passwordElement.getAsString();
this.isRegistered = true;
}
// We should check the DB for saved coords
if(config.main.spawnOnJoin) {
try {
JsonElement lastLoc = json.get("lastLocation");
if (lastLoc != null) {
// Getting DB coords
JsonObject lastLocation = gson.fromJson(lastLoc.getAsString(), JsonObject.class);
this.lastDim = lastLocation.get("dim").isJsonNull() ? config.worldSpawn.dimension : lastLocation.get("dim").getAsString();
this.lastX = lastLocation.get("x").isJsonNull() ? config.worldSpawn.x : lastLocation.get("x").getAsDouble();
this.lastY = lastLocation.get("y").isJsonNull() ? config.worldSpawn.y : lastLocation.get("y").getAsDouble();
this.lastZ = lastLocation.get("z").isJsonNull() ? config.worldSpawn.z : lastLocation.get("z").getAsDouble();
// Removing location data from DB
json.remove("lastLocation");
DB.updateUserData(uuid, json.toString());
}
} catch (JsonSyntaxException ignored) {
// Player didn't have any coords in db to tp to
}
}
}
else {
this.isRegistered = false;
this.password = "";
}
this.isAuthenticated = false;
this.loginTries = 0;
if(config.experimental.debugMode)
logInfo("Cache created. Registered: " + this.isRegistered + ", hashed password: " + this.password);
logInfo("New player cache created.");
}
public static PlayerCache fromJson(String json) {
public static PlayerCache fromJson(ServerPlayerEntity player, String json) {
if(json.isEmpty()) {
// Player doesn't have data yet
return new PlayerCache(player);
}
if(config.experimental.debugMode)
logInfo("Creating cache for " + Objects.requireNonNull(player).getName());
// Parsing data from DB
PlayerCache playerCache = gson.fromJson(json, PlayerCache.class);
System.out.println(playerCache);
playerCache.loginTries = 0;
if(playerCache.password != null && !playerCache.password.isEmpty()) {
playerCache.isRegistered = true;
}
else {
// Not registered
playerCache.isRegistered = false;
playerCache.password = "";
}
if(player != null) {
playerCache.lastIp = player.getIp();
playerCache.wasInPortal = player.getBlockState().getBlock().equals(Blocks.NETHER_PORTAL);
playerCache.lastAir = player.getAir();
playerCache.wasOnFire = player.isOnFire();
// Setting position cache
playerCache.lastDim = String.valueOf(player.getEntityWorld().getRegistryKey().getValue());
playerCache.lastX = player.getX();
playerCache.lastY = player.getY();
playerCache.lastZ = player.getZ();
}
else {
playerCache.wasInPortal = false;
playerCache.lastAir = 300;
playerCache.wasOnFire = false;
}
return playerCache;
}

View File

@ -1,17 +1,13 @@
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.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;
import java.util.ArrayList;
@ -20,7 +16,6 @@ import java.util.List;
import static com.mongodb.client.model.Filters.eq;
import static org.samo_lego.simpleauth.SimpleAuth.config;
import static org.samo_lego.simpleauth.utils.SimpleLogger.logError;
public class MongoDB {
private static MongoCollection<Document> collection;
@ -44,39 +39,24 @@ public class MongoDB {
}
public static boolean isUserRegistered(String uuid) {
try {
return collection.find(eq("UUID", uuid)).iterator().hasNext();
} catch (DBException e) {
logError(e.getMessage());
}
return false;
}
public static boolean registerUser(String uuid, String data) {
if(!isUserRegistered(uuid)) {
collection.insertOne(
new Document(uuid, data)
);
return true;
}
return false;
return collection.find(eq("UUID", uuid)).iterator().hasNext();
}
public static void deleteUserData(String uuid) {
collection.deleteOne(eq("UUID", uuid));
}
public static void updateUserData(String uuid, String data) {
collection.updateOne(eq("UUID", uuid), new Document(uuid, data));
}
public static String getUserData(String uuid){
return isUserRegistered(uuid) ? collection.find(eq("UUID", uuid)).iterator().next().getString(uuid) : "";
if(isUserRegistered(uuid)) {
Document data = collection.find(eq("UUID", uuid)).iterator().next();
return data.toJson();
}
return "";
}
public static void saveFromCache(HashMap<String, PlayerCache> playerCacheMap) {
List<InsertOneModel<Document>> writeList = new ArrayList<>();
List<UpdateOneModel<Document>> updateList = new ArrayList<>();
List<ReplaceOneModel<Document>> updateList = new ArrayList<>();
playerCacheMap.forEach((uuid, playerCache) -> {
// Save as BSON not JSON stringified
Document lastLocation = new Document("x", playerCache.lastX)
@ -93,7 +73,7 @@ public class MongoDB {
);
}
else {
updateList.add(new UpdateOneModel<>(eq("UUID", uuid),
updateList.add(new ReplaceOneModel<>(eq("UUID", uuid),
new Document("UUID", uuid)
.append("password", playerCache.password)
.append("lastLocation", lastLocation)
@ -101,8 +81,6 @@ public class MongoDB {
);
}
});
System.out.println(writeList);
System.out.println(updateList);
if(!writeList.isEmpty())
collection.bulkWrite(writeList);
if(!updateList.isEmpty())