MongoDB null values

Discussion in 'Plugin Development' started by AstroCoder, Sep 10, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    AstroCoder

    Hi guys im having problems with my mongodb object this is what happen: I setup 3 classes (mongo manager, client data and cliente manager) and when i join this set the values to null instead to username and cannot retrieve coins when i set it to something.

    ClientData class
    Code:
    import com.mongodb.client.FindIterable;
    import com.mongodb.client.model.Filters;
    import net.union.hub.engine.sql.MongoManager;
    import org.bson.Document;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.UUID;
    
    public class ClientData {
        private final FindIterable<Document> find;
        public int id;
        private String uuid;
        private String lastName;
        private String username;
        private int level;
        private int exp;
        private int joins;
        private int coins;
        private int pearls;
        private long firstLoginDate;
        private long lastLoginDate;
        private List<UUID> friends;
        private Document document;
    
        public ClientData(String uuid) {
            this.uuid = uuid;
            this.friends = new ArrayList();
            this.find = MongoManager.get().getPlayers().find(Filters.eq("uuid", uuid));
            this.document = this.find.first();
        }
    
        public static ClientData getPlayer(String uuid) {
            return new ClientData(uuid);
        }
    
        public static ClientData getPlayerByName(String name) {
            FindIterable<Document> find = MongoManager.get().getPlayers().find(Filters.eq("lastName", name));
            Document doc = find.first();
    
            if (doc == null) {
                return null;
            }
    
            return new ClientData((String) doc.get("uuid"));
        }
    
    
        public void save() {
            Document doc = new Document("uuid", this.uuid).append("lastName", this.lastName).append("userName", this.username).append("firstLoginDate", Long.valueOf(this.firstLoginDate)).append("lastLoginDate", Long.valueOf(this.lastLoginDate)).append("level", Integer.valueOf(this.level)).append("exp", Integer.valueOf(this.exp)).append("joins", Integer.valueOf(this.joins)).append("coins", Integer.valueOf(this.coins)).append("pearls", Integer.valueOf(this.pearls));
    
            MongoManager.get().getPlayers().updateOne(Filters.eq("uuid", this.uuid), new Document("$set", doc));
        }
    
        public void create() {
            if (this.document != null) {
                retrieve();
                return;
            }
    
            Player player = Bukkit.getPlayer(UUID.fromString(this.uuid));
    
            this.document = new Document("uuid", this.uuid).append("lastName", player.getDisplayName())
                    .append("userName", player.getDisplayName())
                    .append("firstLoginDate", System.currentTimeMillis())
                    .append("lastLoginDate", System.currentTimeMillis())
                    .append("level", 0)
                    .append("exp", 0).append("joins", 0)
                    .append("coins", 0).append("pearls", 0);
            MongoManager.get().getPlayers().insertOne(this.document);
            retrieve();
        }
    
        public void retrieve() {
            setLastName((String) this.document.get("lastName"));
            setUsername((String) this.document.get("userName"));
            setFirstLoginDate((Long) this.document.get("firstLoginDate"));
            setLastLoginDate((Long) this.document.get("lastLoginDate"));
            setLevel(this.document.getInteger("level"));
            setExp(this.document.getInteger("exp"));
            setJoins(this.document.getInteger("joins"));
            setCoins(this.document.getInteger("coins"));
            setPearls(this.document.getInteger("pearls"));
        }
    
        public int getId() {
            return this.id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getLastName() {
            return lastName;
        }
    
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    
        public long getFirstLoginDate() {
            return this.firstLoginDate;
        }
    
        public void setFirstLoginDate(long firstLoginDate) {
            this.firstLoginDate = firstLoginDate;
        }
    
        public long getLastLoginDate() {
            return this.lastLoginDate;
        }
    
        public void setLastLoginDate(long lastLoginDate) {
            this.lastLoginDate = lastLoginDate;
        }
    
        public FindIterable<Document> getFind() {
            return this.find;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String name) {
            this.username = name;
        }
    
        public String getUuid() {
            return this.uuid;
        }
    
        public void setUuid(String uuid) {
            this.uuid = uuid;
        }
    
        public int getLevel() {
            if(level <= 0)
                return 0;
            return this.level;
        }
    
        public void setLevel(int level) {
            this.level = level;
        }
    
        public int getExp() {
            if(exp <= 0)
                return 0;
    
            return this.exp;
        }
    
        public void setExp(int exp) {
            this.exp = exp;
        }
    
        public int getJoins() {
            if(joins <= 0)
                return 0;
            return this.joins;
        }
    
        public void setJoins(int joins) {
            this.joins = joins;
        }
    
        public int getCoins() {
            if(exp <= 0)
                return 0;
            return this.coins;
        }
    
        public void setCoins(int coins) {
            this.coins = coins;
        }
    
        public int getPearls() {
            if(pearls <= 0)
                return 0;
            return this.pearls;
        }
    
        public void setPearls(int pearls) {
            this.pearls = pearls;
        }
    
        public List<UUID> getFriends() {
            return this.friends;
        }
    
        public void setFriends(List<UUID> friends) {
            this.friends = friends;
        }
    
        public void addFriend(UUID friend) {
            this.friends.add(friend);
        }
    
        public void addJoin(int joins) {
            this.joins += joins;
        }
    }
    Mongo Manager
    Code:
    import com.mongodb.MongoClient;
    import com.mongodb.MongoCredential;
    import com.mongodb.ServerAddress;
    import com.mongodb.client.MongoCollection;
    import com.mongodb.client.MongoDatabase;
    import org.bson.Document;
    
    import java.util.ArrayList;
    
    
    public class MongoManager {
        private static MongoManager ins = new MongoManager();
    
        private MongoClient client;
        private MongoDatabase db;
        private MongoCollection<Document> players;
    
        public static MongoManager get() {
            return ins;
        }
    
        public void init() {
            ServerAddress addr = new ServerAddress("127.0.0.1", 27017);
            ArrayList credentials = new ArrayList();
            credentials.add(MongoCredential.createCredential("union", "admin", "union16".toCharArray()));
            this.client = new MongoClient(addr, credentials);
            this.players = this.client.getDatabase("admin").getCollection("clientdata");
        }
    
        public void disconnect() {
            this.client.close();
        }
    
        public MongoClient getClient() {
            return this.client;
        }
    
        public MongoCollection<Document> getPlayers() {
            return this.players;
        }
    
        public MongoDatabase getDatabase() {
            return this.db;
        }
    }
    ClientManager
    Code:
    import org.bukkit.entity.Player;
    
    
    public class ClientManager {
        public static ClientManager get = new ClientManager();
    
        public static ClientManager get() {
            return get;
        }
    
    
        public ClientData getPlayer(String uuid) {
            return ClientData.getPlayer(uuid);
        }
    
        public ClientData getPlayerByName(String name) {
            return ClientData.getPlayerByName(name);
        }
    
        public void save(Player p) {
            getPlayer(p.getPlayer().getUniqueId().toString()).save();
        }
    }
     
  2. Offline

    Zombie_Striker

    @AstroCoder
    Have you debugged? What line is supposed to set the username? Does that line work? Does the method ever get called?
     
  3. Offline

    AstroCoder

    Yes i've debugged.

    This is where i set all the values on mongodb
    Code:
    this.document = new Document("uuid", this.uuid).append("lastName", player.getDisplayName())
                    .append("userName", player.getDisplayName())
                    .append("firstLoginDate", System.currentTimeMillis())
                    .append("lastLoginDate", System.currentTimeMillis())
                    .append("level", 0)
                    .append("exp", 0).append("joins", 0)
                    .append("coins", 0).append("pearls", 0);
    And here is where i retrieve the mongodb values and set to an object
    Code:
        public void retrieve() {
            setLastName((String) this.document.get("lastName"));
            setUsername((String) this.document.get("userName"));
            setFirstLoginDate((Long) this.document.get("firstLoginDate"));
            setLastLoginDate((Long) this.document.get("lastLoginDate"));
            setLevel(this.document.getInteger("level"));
            setExp(this.document.getInteger("exp"));
            setJoins(this.document.getInteger("joins"));
            setCoins(this.document.getInteger("coins"));
            setPearls(this.document.getInteger("pearls"));
        }
    All lines work but i've made a command with this line to check the user stats, this is the line that return null instead of the username/stats
    Code:
    switch (args[0]) {
                    case "get":
                        if (args.length == 1) {
                            p.sendMessage("§cYou need to specify player");
                            return true;
                        }
    
                        Player target = Bukkit.getPlayer(args[1]);
                        ClientData cd = ClientManager.get().getPlayer(target.getUniqueId().toString());
    
                        p.sendMessage("§7---------------[ Stats ]---------------");
                        p.sendMessage("§aUUID: §e" + cd.getUuid());
                        p.sendMessage("§aLastName: §e" + cd.getLastName());
                        p.sendMessage("§aUserName: §e" + cd.getUsername());
                        p.sendMessage("§aJoins: §e" + cd.getJoins());
                        p.sendMessage("§aLast join: §e" + cd.getLastLoginDate());
                        p.sendMessage("§aFirst join: §e" + cd.getFirstLoginDate());
                        p.sendMessage("§aCoins: §e" + cd.getCoins());
                        p.sendMessage("§aPearls: §e" + cd.getPearls());
                        p.sendMessage("§aLevel: §e" + cd.getLevel());
                        p.sendMessage("§aCurrent EXP: §e" + cd.getExp());
                        break;
                    case "set": //Debug
                        ClientData _cd = ClientManager.get().getPlayer(p.getUniqueId().toString());
                        _cd.setCoins(100);
                }
    [​IMG]
     
    Last edited: Sep 10, 2016
  4. Offline

    Zombie_Striker

    @AstroCoder
    Does the username and lastname get stored correctly? Are they set to anything right after the document has been created?
     
  5. Offline

    AstroCoder

    Yap they are stored correctly, after the doc has been created i call the retrieve function to set the object values to the same values setted on doc creation
     
Thread Status:
Not open for further replies.

Share This Page