Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.parteerealms.prcore.Database;
- import com.mongodb.MongoClient;
- import com.mongodb.client.FindIterable;
- import com.mongodb.client.MongoCollection;
- import com.mongodb.client.MongoDatabase;
- import com.parteerealms.prcore.Database.PlayerData.Rank;
- import org.bson.Document;
- import org.bukkit.entity.Player;
- /**
- * @author IsoSyns
- */
- public class PRDatabase
- {
- /**
- * TO UPDATE A PLAYERS RANK:
- * db.playerInfo.update({uuid:"uuid"}, {$set: {rank: "rank"}})
- */
- private final MongoClient mongoClient;
- private final MongoDatabase database;
- public PRDatabase()
- {
- this.mongoClient = new MongoClient("localhost", 27017);
- this.database = mongoClient.getDatabase("players");
- MongoCollection<Document> mongoCollection = database.getCollection("playerInfo");
- if (mongoCollection == null) {
- database.createCollection("playerInfo");
- }
- // TODO: Organize collections. //
- }
- public Document connect(Player player)
- {
- FindIterable<Document> results = database.getCollection("playerInfo").find(new Document("uuid", player.getUniqueId().toString()));
- if (results == null || results.first() == null || results.first().isEmpty())
- {
- Document document = new Document("uuid", player.getUniqueId().toString());
- document.put("rank", "default");
- document.put("money", 0);
- database.getCollection("playerInfo").insertOne(document);
- return document;
- } else if (results != null && results.first() != null && !results.first().isEmpty())
- {
- Document document = results.first();
- database.getCollection("playerInfo").replaceOne(results.first(), document);
- return document;
- }
- return null;
- }
- public void updatePlayer(Player uuid, int money, String serverRank)
- {
- Document found = connect(uuid);
- Document replaced = new Document();
- replaced.put("rank", serverRank);
- replaced.put("money", money);
- database.getCollection("playerInfo").findOneAndReplace(found, replaced);
- }
- public Rank getRank(Player uuid)
- {
- Rank rank = Rank.valueOf(connect(uuid).get("rank").toString().toUpperCase());
- return rank;
- }
- public int getMoney(Player uuid)
- {
- int money = 0;
- if(connect(uuid) != null)
- {
- money = (int) connect(uuid).get("money");
- }
- return money;
- }
- public void injectPlayer(Player uuid)
- {
- updatePlayer(uuid, 100, "default");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement