MinerDataManager.java (2554B)
1 package me.night.nullvalkyrie.database; 2 3 import com.mongodb.client.MongoCursor; 4 import me.night.nullvalkyrie.entities.miners.CryptoMiner; 5 import me.night.nullvalkyrie.entities.miners.MinerType; 6 import org.bson.Document; 7 import org.bson.conversions.Bson; 8 import org.bukkit.Bukkit; 9 import org.bukkit.Location; 10 11 import java.util.HashMap; 12 13 public class MinerDataManager { 14 public static void setMiner(String name, MinerType type, int level, double rate, boolean enabled, long lastclaim, Location location) { 15 Document newDocument = new Document(); 16 newDocument.put("ID", new DatabaseManager().getMinersDB().countDocuments() + 1); 17 newDocument.put("Name", name); 18 newDocument.put("Material", type.getName()); 19 newDocument.put("Level", level); 20 newDocument.put("Rate", rate); 21 newDocument.put("Enabled", enabled); 22 newDocument.put("LastClaim", lastclaim); 23 newDocument.put("x", location.getX()); 24 newDocument.put("y", location.getY()); 25 newDocument.put("z", location.getZ()); 26 new DatabaseManager().getMinersDB().insertOne(newDocument); 27 } 28 29 public static void setLastClaim(String name) { 30 Document document = new DatabaseManager().getMinersDB().find(new Document("Name", name)).first(); 31 if (document != null) { 32 Bson updated = new Document("LastClaim", System.currentTimeMillis()); 33 Bson update = new Document("$set", updated); 34 new DatabaseManager().getMinersDB().updateOne(document, update); 35 } 36 } 37 38 public static long getLastClaim(long id) { 39 Document doc = new DatabaseManager().getMinersDB().find(new Document("ID", id)).first(); 40 if (doc != null) { 41 for (String key : doc.keySet()) { 42 if (key.equals("LastClaim")) return (long) doc.get(key); 43 } 44 } 45 return 0; 46 } 47 48 public static HashMap<Long, CryptoMiner> getMiners() { 49 HashMap<Long, CryptoMiner> list = new HashMap<>(); 50 try (MongoCursor<Document> cursor = new DatabaseManager().getMinersDB().find().cursor()) { 51 while (cursor.hasNext()) { 52 Document doc = cursor.next(); 53 list.put(doc.getLong("ID"), new CryptoMiner(doc.getString("Name"), MinerType.getByName(doc.getString("Material")), doc.getInteger("Level"), doc.getDouble("Rate"), doc.getLong("LastClaim"), new Location(Bukkit.getWorld("world"), doc.getDouble("x"), doc.getDouble("y"), doc.getDouble("z")))); 54 } 55 return list; 56 } 57 } 58 59 } 60