-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
158 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/main/java/com/gmail/picono435/picojobs/utils/MongoDBAPI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters