forked from sorceress/EasyAuth
Compiling fixes
This commit is contained in:
parent
3feb1d19ff
commit
0718d8ec03
|
@ -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 {
|
||||
|
|
|
@ -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<String, PlayerCache> playerCacheMap) {
|
||||
// Saving player data.
|
||||
if(config.main.useMongoDB)
|
||||
MongoDB.saveFromCache(playerCacheMap);
|
||||
else
|
||||
LevelDB.saveFromCache(playerCacheMap);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<String, PlayerCache> 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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<Document> 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<String, PlayerCache> playerCacheMap) {
|
||||
List<ReplaceOneModel<Document>> 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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue