More mongodb

This commit is contained in:
samo_lego 2020-10-11 17:07:56 +02:00
parent 658d31cdfb
commit 6d79da2ca5
3 changed files with 11 additions and 8 deletions

View File

@ -120,8 +120,8 @@ public class AuthConfig {
public int port = 27017; public int port = 27017;
public boolean useSsl = true; public boolean useSsl = true;
public String databaseName = "SimpleAuthPlayerData"; public String databaseName = "SimpleAuthPlayerData";
public String password; public String password = "";
public String username; public String username = "";
} }
} }

View File

@ -34,7 +34,7 @@ public class DBHelper {
* @return false if connection is open, otherwise false * @return false if connection is open, otherwise false
*/ */
public boolean isClosed() { public boolean isClosed() {
return config.main.useMongoDB || LevelDB.isClosed(); return config.main.useMongoDB ? MongoDB.isClosed() : LevelDB.isClosed();
} }

View File

@ -7,6 +7,7 @@ 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.ReplaceOneModel; import com.mongodb.client.model.ReplaceOneModel;
import com.mongodb.client.model.UpdateOneModel;
import org.bson.Document; import org.bson.Document;
import org.iq80.leveldb.DBException; import org.iq80.leveldb.DBException;
import org.samo_lego.simpleauth.storage.PlayerCache; 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); MongoDatabase database = mongoClient.getDatabase(config.mongoDBCredentials.databaseName);
collection = database.getCollection("players"); collection = database.getCollection("players");
} }
@ -72,12 +73,10 @@ public class MongoDB {
} }
public static void saveFromCache(HashMap<String, PlayerCache> playerCacheMap) { public static void saveFromCache(HashMap<String, PlayerCache> playerCacheMap) {
List<ReplaceOneModel<Document>> list = new ArrayList<>(); List<UpdateOneModel<Document>> list = new ArrayList<>();
playerCacheMap.forEach((uuid, playerCache) -> { playerCacheMap.forEach((uuid, playerCache) -> {
JsonObject data = playerCache.toJson(); list.add(new UpdateOneModel<>(eq("UUID", uuid), new Document("password", playerCache.password)));
list.add(new ReplaceOneModel<>(eq("UUID", uuid), new Document(uuid, data.toString())));
}); });
collection.bulkWrite(list); collection.bulkWrite(list);
} }
@ -85,4 +84,8 @@ public class MongoDB {
mongoClient.close(); mongoClient.close();
return true; return true;
} }
public static boolean isClosed() {
return mongoClient == null;
}
} }