Writing changes

This commit is contained in:
samo_lego 2020-10-12 21:02:53 +02:00
parent 6d79da2ca5
commit 9757af1958
3 changed files with 37 additions and 5 deletions

View File

@ -52,8 +52,8 @@ dependencies {
include group: 'org.iq80.leveldb', name: 'leveldb-api', version: '0.12' include group: 'org.iq80.leveldb', name: 'leveldb-api', version: '0.12'
// MongoDB driver // MongoDB driver
implementation 'org.mongodb:mongodb-driver:3.12.7' implementation 'org.mongodb:mongodb-driver-sync:4.1.0'
include 'org.mongodb:mongodb-driver:3.12.7' include 'org.mongodb:mongodb-driver-sync:4.1.0'

View File

@ -176,6 +176,7 @@ public class AuthEventHandler {
// Punching a block // Punching a block
public static ActionResult onAttackBlock(PlayerEntity player) { public static ActionResult onAttackBlock(PlayerEntity player) {
DB.saveAll(playerCacheMap); //todo debug only
if(!isAuthenticated((ServerPlayerEntity) player) && !config.experimental.allowBlockPunch) { if(!isAuthenticated((ServerPlayerEntity) player) && !config.experimental.allowBlockPunch) {
player.sendMessage(notAuthenticated(player), false); player.sendMessage(notAuthenticated(player), false);
return ActionResult.FAIL; return ActionResult.FAIL;

View File

@ -6,9 +6,11 @@ import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients; import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.InsertOneModel;
import com.mongodb.client.model.ReplaceOneModel; import com.mongodb.client.model.ReplaceOneModel;
import com.mongodb.client.model.UpdateOneModel; import com.mongodb.client.model.UpdateOneModel;
import org.bson.Document; import org.bson.Document;
import org.bson.conversions.Bson;
import org.iq80.leveldb.DBException; import org.iq80.leveldb.DBException;
import org.samo_lego.simpleauth.storage.PlayerCache; import org.samo_lego.simpleauth.storage.PlayerCache;
@ -73,11 +75,40 @@ public class MongoDB {
} }
public static void saveFromCache(HashMap<String, PlayerCache> playerCacheMap) { public static void saveFromCache(HashMap<String, PlayerCache> playerCacheMap) {
List<UpdateOneModel<Document>> list = new ArrayList<>(); List<InsertOneModel<Document>> writeList = new ArrayList<>();
List<UpdateOneModel<Document>> updateList = new ArrayList<>();
playerCacheMap.forEach((uuid, playerCache) -> { 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() { public static boolean close() {