Advertisement
Guest User

MongoDB - IsoSyns

a guest
Feb 5th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. package com.parteerealms.prcore.Database;
  2.  
  3. import com.mongodb.MongoClient;
  4. import com.mongodb.client.FindIterable;
  5. import com.mongodb.client.MongoCollection;
  6. import com.mongodb.client.MongoDatabase;
  7. import com.parteerealms.prcore.Database.PlayerData.Rank;
  8. import org.bson.Document;
  9. import org.bukkit.entity.Player;
  10.  
  11. /**
  12. * @author IsoSyns
  13. */
  14. public class PRDatabase
  15. {
  16.  
  17. /**
  18. * TO UPDATE A PLAYERS RANK:
  19. * db.playerInfo.update({uuid:"uuid"}, {$set: {rank: "rank"}})
  20. */
  21.  
  22. private final MongoClient mongoClient;
  23. private final MongoDatabase database;
  24.  
  25. public PRDatabase()
  26. {
  27.  
  28. this.mongoClient = new MongoClient("localhost", 27017);
  29. this.database = mongoClient.getDatabase("players");
  30.  
  31. MongoCollection<Document> mongoCollection = database.getCollection("playerInfo");
  32. if (mongoCollection == null) {
  33. database.createCollection("playerInfo");
  34. }
  35.  
  36. // TODO: Organize collections. //
  37. }
  38.  
  39. public Document connect(Player player)
  40. {
  41. FindIterable<Document> results = database.getCollection("playerInfo").find(new Document("uuid", player.getUniqueId().toString()));
  42. if (results == null || results.first() == null || results.first().isEmpty())
  43. {
  44. Document document = new Document("uuid", player.getUniqueId().toString());
  45. document.put("rank", "default");
  46. document.put("money", 0);
  47. database.getCollection("playerInfo").insertOne(document);
  48. return document;
  49. } else if (results != null && results.first() != null && !results.first().isEmpty())
  50. {
  51. Document document = results.first();
  52. database.getCollection("playerInfo").replaceOne(results.first(), document);
  53. return document;
  54. }
  55.  
  56. return null;
  57. }
  58.  
  59. public void updatePlayer(Player uuid, int money, String serverRank)
  60. {
  61.  
  62. Document found = connect(uuid);
  63.  
  64. Document replaced = new Document();
  65. replaced.put("rank", serverRank);
  66. replaced.put("money", money);
  67.  
  68. database.getCollection("playerInfo").findOneAndReplace(found, replaced);
  69. }
  70.  
  71. public Rank getRank(Player uuid)
  72. {
  73. Rank rank = Rank.valueOf(connect(uuid).get("rank").toString().toUpperCase());
  74. return rank;
  75. }
  76.  
  77. public int getMoney(Player uuid)
  78. {
  79. int money = 0;
  80. if(connect(uuid) != null)
  81. {
  82. money = (int) connect(uuid).get("money");
  83. }
  84.  
  85. return money;
  86. }
  87.  
  88. public void injectPlayer(Player uuid)
  89. {
  90. updatePlayer(uuid, 100, "default");
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement