Skip to content

Commit

Permalink
Added MongoDB storage method
Browse files Browse the repository at this point in the history
  • Loading branch information
Picono435 committed Aug 19, 2020
1 parent 75233d2 commit d60c605
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 23 deletions.
2 changes: 1 addition & 1 deletion dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.gmail.picono435</groupId>
<artifactId>picojobs</artifactId>
<version>1.0-alpha-1</version>
<version>1.0-alpha-1a</version>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,10 @@
<version>1.7</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>4.1.0</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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"));

Expand All @@ -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();
Expand All @@ -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"));

Expand All @@ -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();
Expand All @@ -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();
}
}
79 changes: 79 additions & 0 deletions src/main/java/com/gmail/picono435/picojobs/utils/MongoDBAPI.java
Original file line number Diff line number Diff line change
@@ -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<Document> 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<String> getAllUsers() {
List<String> uuids = new ArrayList<String>();
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();
}
}
8 changes: 6 additions & 2 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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://<user>:<password>@mydatabases-host.mongodb.net/"
database: "mcserver"
collection: "jobplayers"

#################################################################################
# #
Expand Down

0 comments on commit d60c605

Please sign in to comment.