Advertisement
Guest User

Untitled

a guest
Feb 5th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. MongoData data;
  2.  
  3. public DBConnect(MongoData data)
  4. {
  5. this.data = data;
  6. }
  7.  
  8. private ExecutorService executor;
  9. private MongoClient mongoClient;
  10. private MongoDatabase mongoDatabase;
  11.  
  12. private MongoCollection<Document> playersCollection;
  13. private MongoCollection<Document> bansCollection;
  14.  
  15. private String host = "localhost";
  16. private int port = 27017;
  17. private String username = "minecraftroot";
  18. private String database = "minecraft";
  19. private String password = "uidh78klo0i745fgbc3hjo0";
  20.  
  21. public void connect() throws MongoException {
  22. this.executor = Executors.newSingleThreadExecutor();
  23. List<MongoCredential> mongoCredentials = Arrays.asList(
  24. MongoCredential.createCredential(this.username, this.database, this.password.toCharArray())
  25. );
  26.  
  27. this.mongoClient = new MongoClient(new ServerAddress(this.host, this.port), mongoCredentials);
  28. this.mongoDatabase = this.mongoClient.getDatabase(this.database);
  29.  
  30. this.playersCollection = this.mongoDatabase.getCollection("players");
  31. this.bansCollection = this.mongoDatabase.getCollection("bans");
  32. ProxyServer.getInstance().getConsole().sendMessage(new TextComponent("§8>> §aMongoDB §7- §2ist erfolgreich Verbunden!"));
  33. }
  34.  
  35. public void banPlayer(String uuid, String reason, long duration) {
  36. Document banDocument = new Document();
  37. banDocument.put("uuid", uuid);
  38. banDocument.put("reason", reason);
  39. banDocument.put("duration", duration);
  40.  
  41. this.bansCollection.updateOne(Filters.eq("uuid", uuid), banDocument);
  42. }
  43.  
  44. public Document getPlayerBan(String uuid) {
  45. return this.bansCollection.find(Filters.eq("uuid", uuid)).first();
  46. }
  47.  
  48. public void onJoin() {
  49. Document banDocument = this.getPlayerBan("1123-123-123-123");
  50.  
  51. if (banDocument != null) {
  52. long duration = banDocument.getLong("duration");
  53. long timeLeft = System.currentTimeMillis() - duration;
  54. String reason = banDocument.getString("reason");
  55.  
  56. System.out.println("Der Spieler ist noch " + timeLeft + "ms gebannt mit dem Grund " + reason);
  57. //Spieler disconnecten
  58. }
  59. }
  60.  
  61. public void setCoins(String uuid, int coins, Runnable callback) {
  62. this.executor.execute(() -> {
  63. Document playerDocument = this.playersCollection.find(Filters.eq("uuid", uuid)).first();
  64. playerDocument.put("coins", coins);
  65. callback.run();
  66. });
  67. }
  68.  
  69. public void addCoins(String uuid, int coins, Runnable callback) {
  70. this.executor.execute(() -> {
  71. Document playerDocument = this.playersCollection.find(Filters.eq("uuid", uuid)).first();
  72. playerDocument.put("coins", playerDocument.getInteger("coins") + coins);
  73. callback.run();
  74. });
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement