Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MongoData data;
- public DBConnect(MongoData data)
- {
- this.data = data;
- }
- private ExecutorService executor;
- private MongoClient mongoClient;
- private MongoDatabase mongoDatabase;
- private MongoCollection<Document> playersCollection;
- private MongoCollection<Document> bansCollection;
- private String host = "localhost";
- private int port = 27017;
- private String username = "minecraftroot";
- private String database = "minecraft";
- private String password = "uidh78klo0i745fgbc3hjo0";
- public void connect() throws MongoException {
- this.executor = Executors.newSingleThreadExecutor();
- List<MongoCredential> mongoCredentials = Arrays.asList(
- MongoCredential.createCredential(this.username, this.database, this.password.toCharArray())
- );
- this.mongoClient = new MongoClient(new ServerAddress(this.host, this.port), mongoCredentials);
- this.mongoDatabase = this.mongoClient.getDatabase(this.database);
- this.playersCollection = this.mongoDatabase.getCollection("players");
- this.bansCollection = this.mongoDatabase.getCollection("bans");
- ProxyServer.getInstance().getConsole().sendMessage(new TextComponent("§8>> §aMongoDB §7- §2ist erfolgreich Verbunden!"));
- }
- public void banPlayer(String uuid, String reason, long duration) {
- Document banDocument = new Document();
- banDocument.put("uuid", uuid);
- banDocument.put("reason", reason);
- banDocument.put("duration", duration);
- this.bansCollection.updateOne(Filters.eq("uuid", uuid), banDocument);
- }
- public Document getPlayerBan(String uuid) {
- return this.bansCollection.find(Filters.eq("uuid", uuid)).first();
- }
- public void onJoin() {
- Document banDocument = this.getPlayerBan("1123-123-123-123");
- if (banDocument != null) {
- long duration = banDocument.getLong("duration");
- long timeLeft = System.currentTimeMillis() - duration;
- String reason = banDocument.getString("reason");
- System.out.println("Der Spieler ist noch " + timeLeft + "ms gebannt mit dem Grund " + reason);
- //Spieler disconnecten
- }
- }
- public void setCoins(String uuid, int coins, Runnable callback) {
- this.executor.execute(() -> {
- Document playerDocument = this.playersCollection.find(Filters.eq("uuid", uuid)).first();
- playerDocument.put("coins", coins);
- callback.run();
- });
- }
- public void addCoins(String uuid, int coins, Runnable callback) {
- this.executor.execute(() -> {
- Document playerDocument = this.playersCollection.find(Filters.eq("uuid", uuid)).first();
- playerDocument.put("coins", playerDocument.getInteger("coins") + coins);
- callback.run();
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement