Skip to content

Commit

Permalink
working mongodb integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Inf1nityy committed Aug 9, 2024
1 parent 8073813 commit 54b7b7d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 19 deletions.
12 changes: 8 additions & 4 deletions src/main/java/com/nexia/base/player/PlayerDataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,23 @@ private void savePlayerData(UUID uuid) {
document.append("uuid", uuid.toString());
document.remove("data");

UpdateResult isReplaced = NexiaCore.mongoManager.getCollection(collectionName).replaceOne(Filters.eq("uuid", uuid), document);
UpdateResult isReplaced = NexiaCore.mongoManager.getCollection(collectionName).replaceOne(Filters.eq("uuid", uuid.toString()), document);

if (!isReplaced.wasAcknowledged()) {
if (isReplaced.getMatchedCount() == 0) {
NexiaCore.mongoManager.getCollection(collectionName).insertOne(document);
}
}

private <T extends SavedPlayerData> T loadPlayerData(UUID uuid, Class<T> toLoad) throws InstantiationException, IllegalAccessException {
T savedPlayerData = NexiaCore.mongoManager.getObject(collectionName, Filters.eq("uuid", uuid), toLoad);
T savedPlayerData = NexiaCore.mongoManager.getObject(collectionName, Filters.eq("uuid", uuid.toString()), toLoad);
if (savedPlayerData != null) {
return savedPlayerData;
}

return toLoad.newInstance();
try {
return toLoad.getDeclaredConstructor().newInstance();
} catch (InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
}
6 changes: 1 addition & 5 deletions src/main/java/com/nexia/core/NexiaCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,12 @@ public void onInitialize() {
logger.info("Registered commands.");

networkingHandler = new NetworkingHandler();

mongoManager = new MongoManager();
}

@Override
@SuppressWarnings("FutureReturnValueIgnored")
public void onNexusLoad(NexusAPI api, NexusServer server) {
logger.info("Connecting to database...");
mongoManager = new MongoManager();
logger.info("Connected to database.");

logger.info("Loading Nexus API...");

ServerTime.nexusServer = server;
Expand Down
26 changes: 20 additions & 6 deletions src/main/java/com/nexia/core/utilities/database/MongoManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@

@SuppressWarnings("unused")
public class MongoManager {
private final static Gson GSON = new GsonBuilder()
.registerTypeAdapter(UUID.class, TypeAdapters.UUID)
.create();
private final static Gson GSON = new GsonBuilder().create();

private final ExecutorService service;

Expand All @@ -44,13 +42,29 @@ public void openConnection() {
config.password.toCharArray()
);

String connectionString = String.format("mongodb://%s:%s@%s:%d/%s?authSource=%s",
config.username,
config.password,
config.host,
config.port,
config.database,
"admin" // Authentication database, change this if your user is created in another database
);

// Configure the MongoClientSettings
MongoClientSettings mongoClientSettings = MongoClientSettings.builder()
.credential(mongoCredential)
.applyConnectionString(new ConnectionString("mongodb://" + config.host + ":" + config.port))
.applyConnectionString(new ConnectionString(connectionString))
.build();

this.client = MongoClients.create(mongoClientSettings);
this.database = this.client.getDatabase(config.database);
try {
this.client = MongoClients.create(mongoClientSettings);
this.database = this.client.getDatabase(config.database);
System.out.println("Connection to MongoDB established successfully.");
} catch (Exception e) {
e.printStackTrace();
System.out.println("Failed to connect to MongoDB: " + e.getMessage());
}
}

public void closeConnection() {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/nexia/ffa/base/BaseFfaUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,10 @@ public void setInventory(NexiaPlayer player){
Gson gson = new Gson();
defaultInventory = gson.fromJson(defaultJson, SavableInventory.class);

String layoutPath = String.format(file + "/savedInventories/%s.json", player.getUUID());
if(new File(layoutPath).exists()) {
String layoutJson = Files.readString(Path.of(layoutPath));
layout = gson.fromJson(layoutJson, SavableInventory.class);
String savedInventory = getDataManager().get(player).savedData.get(String.class, "savedInventory");

if(!savedInventory.equals(defaultJson) && !savedInventory.isEmpty()) {
layout = gson.fromJson(savedInventory, SavableInventory.class);
}
} catch (Exception var4) {
var4.printStackTrace();
Expand Down

0 comments on commit 54b7b7d

Please sign in to comment.