diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml index dba7b459..a4315c6b 100644 --- a/dependency-reduced-pom.xml +++ b/dependency-reduced-pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.gmail.picono435 picojobs - 1.0-alpha-1 + 1.0-alpha-1a src/main/java diff --git a/pom.xml b/pom.xml index cd06c1f9..b625d997 100644 --- a/pom.xml +++ b/pom.xml @@ -121,5 +121,10 @@ 1.7 compile + + org.mongodb + mongodb-driver-sync + 4.1.0 + \ No newline at end of file diff --git a/src/main/java/com/gmail/picono435/picojobs/managers/StorageManager.java b/src/main/java/com/gmail/picono435/picojobs/managers/StorageManager.java index f5aa624c..97f71bcc 100644 --- a/src/main/java/com/gmail/picono435/picojobs/managers/StorageManager.java +++ b/src/main/java/com/gmail/picono435/picojobs/managers/StorageManager.java @@ -13,12 +13,17 @@ import com.gmail.picono435.picojobs.api.PicoJobsAPI; import com.gmail.picono435.picojobs.api.StorageMethod; import com.gmail.picono435.picojobs.utils.FileCreator; +import com.gmail.picono435.picojobs.utils.MongoDBAPI; import com.gmail.picono435.picojobs.utils.MySQLAPI; public class StorageManager { public StorageMethod storageMethod = StorageMethod.YAML; + /* + * GET DATA METHODS + */ + // GENERAL public void getData() { storageMethod = StorageMethod.getStorageMethod(PicoJobsPlugin.getPlugin().getConfig().getConfigurationSection("storage").getString("storage-method")); @@ -34,19 +39,17 @@ public void getData() { return; } + if(storageMethod == StorageMethod.MONGODB) { + PicoJobsPlugin.sendConsoleMessage(ChatColor.AQUA + "[PicoJobs] Using the MongoDB storage method."); + getDataInMongoDB(); + return; + } + PicoJobsPlugin.sendConsoleMessage(ChatColor.YELLOW + "[PicoJobs] We did not find any storage method with that name. Using YAML storage method as default."); getDataInConfig(); } - private void getDataInMySQL() { - MySQLAPI api = new MySQLAPI(); - api.startConnection(); - for(String uuid : api.getAllUsers()) { - PicoJobsPlugin.playersdata.put(UUID.fromString(uuid), api.getFromDB(uuid)); - } - api.close(); - } - + // YAML private void getDataInConfig() { FileCreator.createDataFile(); FileConfiguration data = FileCreator.getData(); @@ -64,6 +67,30 @@ private void getDataInConfig() { } } + // MYSQL + private void getDataInMySQL() { + MySQLAPI api = new MySQLAPI(); + api.startConnection(); + for(String uuid : api.getAllUsers()) { + PicoJobsPlugin.playersdata.put(UUID.fromString(uuid), api.getFromDB(uuid)); + } + api.close(); + } + + // MYSQL + private void getDataInMongoDB() { + MongoDBAPI api = new MongoDBAPI(); + api.startConnection(); + for(String uuid : api.getAllUsers()) { + PicoJobsPlugin.playersdata.put(UUID.fromString(uuid), api.getFromDB(uuid)); + } + api.close(); + } + + /* + * SAVE DATA METHODS + */ + // GENERAL public void saveData() { storageMethod = StorageMethod.getStorageMethod(PicoJobsPlugin.getPlugin().getConfig().getConfigurationSection("storage").getString("storage-method")); @@ -79,21 +106,17 @@ public void saveData() { return; } + if(storageMethod == StorageMethod.MONGODB) { + PicoJobsPlugin.sendConsoleMessage(ChatColor.AQUA + "[PicoJobs] Using the MongoDB storage method."); + saveInMongoDB(); + return; + } + PicoJobsPlugin.sendConsoleMessage(ChatColor.YELLOW + "[PicoJobs] We did not find any storage method with that name. Using YAML storage method as default."); saveInConfig(); } - private void saveInMySQL() { - MySQLAPI api = new MySQLAPI(); - api.startConnection(); - api.deleteMysqlRecords(); - for(UUID uuid : PicoJobsPlugin.playersdata.keySet()) { - JobPlayer jp = PicoJobsPlugin.playersdata.get(uuid); - api.addINDB(uuid.toString(), jp.getJob().getName(), jp.getMethod(), jp.getMethodLevel(), jp.getSalary(), jp.isWorking()); - } - api.close(); - } - + // YAML private void saveInConfig() { if(FileCreator.getDataFile() != null) { FileCreator.getDataFile().delete(); @@ -120,4 +143,28 @@ private void saveInConfig() { e.printStackTrace(); } } + + // MYSQL + private void saveInMySQL() { + MySQLAPI api = new MySQLAPI(); + api.startConnection(); + api.deleteMysqlRecords(); + for(UUID uuid : PicoJobsPlugin.playersdata.keySet()) { + JobPlayer jp = PicoJobsPlugin.playersdata.get(uuid); + api.addINDB(uuid.toString(), jp.getJob().getName(), jp.getMethod(), jp.getMethodLevel(), jp.getSalary(), jp.isWorking()); + } + api.close(); + } + + // MYSQL + private void saveInMongoDB() { + MongoDBAPI api = new MongoDBAPI(); + api.startConnection(); + api.deleteAllDocuments(); + for(UUID uuid : PicoJobsPlugin.playersdata.keySet()) { + JobPlayer jp = PicoJobsPlugin.playersdata.get(uuid); + api.addINDB(uuid.toString(), jp.getJob().getName(), jp.getMethod(), jp.getMethodLevel(), jp.getSalary(), jp.isWorking()); + } + api.close(); + } } diff --git a/src/main/java/com/gmail/picono435/picojobs/utils/MongoDBAPI.java b/src/main/java/com/gmail/picono435/picojobs/utils/MongoDBAPI.java new file mode 100644 index 00000000..5f27d604 --- /dev/null +++ b/src/main/java/com/gmail/picono435/picojobs/utils/MongoDBAPI.java @@ -0,0 +1,79 @@ +package com.gmail.picono435.picojobs.utils; + +import java.util.ArrayList; +import java.util.List; + +import org.bson.Document; +import org.bukkit.configuration.ConfigurationSection; + +import com.gmail.picono435.picojobs.PicoJobsPlugin; +import com.gmail.picono435.picojobs.api.Job; +import com.gmail.picono435.picojobs.api.JobPlayer; +import com.gmail.picono435.picojobs.api.PicoJobsAPI; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoDatabase; + +public class MongoDBAPI { + + private ConfigurationSection conf; + + private MongoCollection jobPlayers; + private MongoDatabase mcserverdb; + private MongoClient client; + + public boolean startConnection() { + conf = PicoJobsPlugin.getInstance().getConfig().getConfigurationSection("storage").getConfigurationSection("mongodb"); + openConnection(); + return true; + } + + public boolean openConnection(){ + client = MongoClients.create(conf.getString("URI")); + + mcserverdb = client.getDatabase(conf.getString("database")); + jobPlayers = mcserverdb.getCollection(conf.getString("collection")); + return true; + } + + public void addINDB(final String uuid, final String job, final double method, final double level, final double salary, final boolean isWorking) { + Document obj = new Document("uuid", uuid); + obj.put("job", job); + obj.put("method", method); + obj.put("level", level); + obj.put("salary", salary); + obj.put("isWorking", isWorking); + + jobPlayers.insertOne(obj); + } + + public JobPlayer getFromDB(String uuid) { + Document query = new Document("uuid", uuid); + Document obj = jobPlayers.find(query).first(); + + Job job = PicoJobsAPI.getJobsManager().getJob(obj.getString("job")); + double method = obj.getDouble("method"); + double level = obj.getDouble("level"); + double salary = obj.getDouble("salary"); + boolean isWorking = obj.getBoolean("isWorking"); + + return new JobPlayer(job, method, level, salary, isWorking); + } + + public List getAllUsers() { + List uuids = new ArrayList(); + for(Document doc : jobPlayers.find()) { + uuids.add((String) doc.get("uuid")); + } + return uuids; + } + + public void deleteAllDocuments() { + jobPlayers.deleteMany(new Document()); + } + + public void close() { + client.close(); + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 77d879b9..559da295 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -31,7 +31,7 @@ storage: # Here is a list of storage methods that you can choose: # YAML - Save the data in a YAML file inside the plugin directory # MySQL - Save the data in a MySQL database - # MongoDB - Save the data in a MongoDB database [SOON] + # MongoDB - Save the data in a MongoDB database storage-method: YAML # MySQL configuration, required only if the storage method is MySQL mysql: @@ -43,7 +43,11 @@ storage: tablename: "jobplayers" # MongoDB configuration, required only if the storage method is MongoDB mongodb: - uri: "mongodb+srv:://MongoDBURI" + # Here you need to put the MongoDB Connection URI + # For more information check https://docs.mongodb.com/manual/reference/connection-string/ + URI: "mongodb+srv://:@mydatabases-host.mongodb.net/" + database: "mcserver" + collection: "jobplayers" ################################################################################# # #