This commit is contained in:
NK 2022-11-28 22:27:00 +00:00
parent 0ba5074c30
commit df59f95c7b
2 changed files with 7 additions and 2 deletions

View file

@ -13,6 +13,7 @@ public class DatabaseManager {
private static MongoCollection<Document> custom_weapons; private static MongoCollection<Document> custom_weapons;
public static MongoCollection<Document> ranks; public static MongoCollection<Document> ranks;
public static MongoCollection<Document> npcs; public static MongoCollection<Document> npcs;
public static MongoCollection<Document> miners;
public MongoClient client; public MongoClient client;
public static MongoDatabase database; public static MongoDatabase database;
@ -23,27 +24,31 @@ public class DatabaseManager {
custom_weapons = database.getCollection("custom_weapons"); custom_weapons = database.getCollection("custom_weapons");
ranks = database.getCollection("ranks"); ranks = database.getCollection("ranks");
npcs = database.getCollection("npcs"); npcs = database.getCollection("npcs");
miners = database.getCollection("miners");
} }
public static void createUserSchema(String username) { public static void createUserSchema(String username) {
Document document = new Document(); Document document = new Document();
document.put("Username", username); document.put("Username", username);
document.put("Bank", 0); document.put("Bank", 0);
users.insertOne(document); users.insertOne(document);
} }
public void updateUserBank(String username, Number coins) { public void updateUserBank(String username, Number coins) {
Document document = users.find(new Document("Username", username)).first(); Document document = users.find(new Document("Username", username)).first();
if(document != null) { if (document != null) {
Bson updated = new Document("Bank", coins); Bson updated = new Document("Bank", coins);
Bson update = new Document("$set", updated); Bson update = new Document("$set", updated);
users.updateOne(document, update); users.updateOne(document, update);
} }
} }
public static HashMap<String, Object> getUser(String username) { public static HashMap<String, Object> getUser(String username) {
try (MongoCursor<Document> cursor = users.find(Filters.eq("Username", username)).cursor()) { try (MongoCursor<Document> cursor = users.find(Filters.eq("Username", username)).cursor()) {
while (cursor.hasNext()) { while (cursor.hasNext()) {
Document doc = cursor.next(); Document doc = cursor.next();
HashMap<String, Object> map = new HashMap<>(); HashMap<String, Object> map = new HashMap<>();
for (String key : doc.keySet()) map.put(key, doc.get(key)); for (String key : doc.keySet()) map.put(key, doc.get(key));
map.remove("_id"); map.remove("_id");
return map; return map;
} }