Adding methods

This commit is contained in:
samo_lego 2020-10-07 22:51:11 +02:00
parent 2098897104
commit 3feb1d19ff
4 changed files with 60 additions and 57 deletions

View File

@ -1,23 +1,11 @@
package org.samo_lego.simpleauth.storage; package org.samo_lego.simpleauth.storage;
import org.iq80.leveldb.DB;
import org.iq80.leveldb.DBException;
import org.samo_lego.simpleauth.storage.database.LevelDB; import org.samo_lego.simpleauth.storage.database.LevelDB;
import org.samo_lego.simpleauth.storage.database.MongoDB; import org.samo_lego.simpleauth.storage.database.MongoDB;
import java.io.IOException;
import static org.iq80.leveldb.impl.Iq80DBFactory.bytes;
import static org.samo_lego.simpleauth.utils.SimpleLogger.logError;
import static org.samo_lego.simpleauth.utils.SimpleLogger.logInfo;
import static org.samo_lego.simpleauth.SimpleAuth.config; import static org.samo_lego.simpleauth.SimpleAuth.config;
public class DBHelper { public class DBHelper {
private DB levelDBStore;
public DB getLevelDBStore() {
return this.levelDBStore;
}
/** /**
* Connects to the DB. * Connects to the DB.
@ -33,14 +21,8 @@ public class DBHelper {
* Closes database connection. * Closes database connection.
*/ */
public void close() { public void close() {
if (levelDBStore != null) { if(!config.main.useMongoDB)
try { LevelDB.close();
levelDBStore.close();
logInfo("Database connection closed successfully.");
} catch (Error | IOException e) {
logError(e.getMessage());
}
}
} }
/** /**
@ -49,7 +31,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 levelDBStore == null; return config.main.useMongoDB || LevelDB.isClosed();
} }
@ -61,16 +43,9 @@ public class DBHelper {
* @return true if operation was successful, otherwise false * @return true if operation was successful, otherwise false
*/ */
public boolean registerUser(String uuid, String data) { public boolean registerUser(String uuid, String data) {
try { if(config.main.useMongoDB)
if(!this.isUserRegistered(uuid)) { return MongoDB.registerUser(uuid, data);
levelDBStore.put(bytes("UUID:" + uuid), bytes("data:" + data)); return LevelDB.registerUser(uuid, data);
return true;
}
return false;
} catch (Error e) {
logError("Register error: " + e.getMessage());
return false;
}
} }
/** /**
@ -80,12 +55,7 @@ public class DBHelper {
* @return true if registered, otherwise false * @return true if registered, otherwise false
*/ */
public boolean isUserRegistered(String uuid) { public boolean isUserRegistered(String uuid) {
try { return config.main.useMongoDB ? MongoDB.isUserRegistered(uuid) : LevelDB.isUserRegistered(uuid);
return levelDBStore.get(bytes("UUID:" + uuid)) != null;
} catch (DBException e) {
logError(e.getMessage());
}
return false;
} }
/** /**
@ -94,11 +64,10 @@ public class DBHelper {
* @param uuid uuid of player to delete data for * @param uuid uuid of player to delete data for
*/ */
public void deleteUserData(String uuid) { public void deleteUserData(String uuid) {
try { if(config.main.useMongoDB)
levelDBStore.delete(bytes("UUID:" + uuid)); MongoDB.isUserRegistered(uuid);
} catch (Error e) { else
logError(e.getMessage()); LevelDB.isUserRegistered(uuid);
}
} }
/** /**
@ -108,11 +77,10 @@ public class DBHelper {
* @param data data to put inside database * @param data data to put inside database
*/ */
public void updateUserData(String uuid, String data) { public void updateUserData(String uuid, String data) {
try { if(config.main.useMongoDB)
levelDBStore.put(bytes("UUID:" + uuid), bytes("data:" + data)); MongoDB.updateUserData(uuid, data);
} catch (Error e) { else
logError(e.getMessage()); LevelDB.updateUserData(uuid, data);
}
} }
/** /**
@ -121,13 +89,7 @@ public class DBHelper {
* @param uuid uuid of the player to get data for. * @param uuid uuid of the player to get data for.
* @return data as string if player has it, otherwise empty string. * @return data as string if player has it, otherwise empty string.
*/ */
public String getData(String uuid){ public String getUserData(String uuid){
try { return config.main.useMongoDB ? MongoDB.getUserData(uuid) : LevelDB.getUserData(uuid);
if(this.isUserRegistered(uuid)) // Gets password from db and removes "data:" prefix from it
return new String(levelDBStore.get(bytes("UUID:" + uuid))).substring(5);
} catch (Error e) {
logError("Error getting data: " + e.getMessage());
}
return "";
} }
} }

View File

@ -82,7 +82,7 @@ public class PlayerCache {
this.lastAir = 300; this.lastAir = 300;
} }
String data = DB.getData(uuid); String data = DB.getUserData(uuid);
if(!data.isEmpty()) { if(!data.isEmpty()) {
// Getting (hashed) password // Getting (hashed) password
JsonObject json = gson.fromJson(data, JsonObject.class); JsonObject json = gson.fromJson(data, JsonObject.class);

View File

@ -124,7 +124,7 @@ public class LevelDB {
* @param uuid uuid of the player to get data for. * @param uuid uuid of the player to get data for.
* @return data as string if player has it, otherwise empty string. * @return data as string if player has it, otherwise empty string.
*/ */
public static String getData(String uuid){ public static String getUserData(String uuid){
try { try {
if(isUserRegistered(uuid)) // Gets password from db and removes "data:" prefix from it if(isUserRegistered(uuid)) // Gets password from db and removes "data:" prefix from it
return new String(levelDBStore.get(bytes("UUID:" + uuid))).substring(5); return new String(levelDBStore.get(bytes("UUID:" + uuid))).substring(5);

View File

@ -6,9 +6,17 @@ 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 org.bson.Document; import org.bson.Document;
import org.iq80.leveldb.DBException;
import javax.print.Doc;
import java.io.IOException;
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.SimpleAuth.config;
import static org.samo_lego.simpleauth.utils.SimpleLogger.logError;
import static org.samo_lego.simpleauth.utils.SimpleLogger.logInfo;
public class MongoDB { public class MongoDB {
private static MongoCollection<Document> collection; private static MongoCollection<Document> collection;
@ -29,4 +37,37 @@ public class MongoDB {
MongoDatabase database = mongoClient.getDatabase(config.mongoDBCredentials.databaseName); MongoDatabase database = mongoClient.getDatabase(config.mongoDBCredentials.databaseName);
collection = database.getCollection("players"); collection = database.getCollection("players");
} }
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;
}
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){
if(isUserRegistered(uuid))
return collection.find(eq("UUID", uuid)).iterator().next().getString(uuid);
return "";
}
} }