NPCDataManager.java (2489B)
1 package me.night.nullvalkyrie.database; 2 3 import com.mongodb.client.MongoCursor; 4 import me.night.nullvalkyrie.entities.npcs.NPCManager; 5 import org.bson.Document; 6 7 import java.util.ArrayList; 8 import java.util.HashMap; 9 import java.util.List; 10 11 12 public class NPCDataManager { 13 public static void setNPC(String name, double x, double y, double z, int pitch, int yaw, String world, String texture, String signature) { 14 Document document = new DatabaseManager().getNPCsDB().find(new Document("Name", name)).first(); 15 if (document != null) { 16 System.out.println("A NPC with this name already exist"); 17 } else { 18 Document newDocument = new Document(); 19 newDocument.put("Name", name); 20 newDocument.put("x", x); 21 newDocument.put("y", y); 22 newDocument.put("z", z); 23 newDocument.put("pitch", pitch); 24 newDocument.put("yaw", yaw); 25 newDocument.put("world", world); 26 newDocument.put("texture", texture); 27 newDocument.put("signature", signature); 28 new DatabaseManager().getNPCsDB().insertOne(newDocument); 29 } 30 } 31 32 public static void reloadNPC() { 33 List<HashMap<String, Object>> npcList = new ArrayList<>(); 34 try (MongoCursor<Document> cursor = new DatabaseManager().getNPCsDB().find().cursor()) { 35 while (cursor.hasNext()) { 36 Document document = cursor.next(); 37 HashMap<String, Object> npc = new HashMap<>(); 38 String name = document.getString("Name"); 39 double x = document.getDouble("x"); 40 double y = document.getDouble("y"); 41 double z = document.getDouble("z"); 42 int pitch = document.getInteger("pitch"); 43 int yaw = document.getInteger("yaw"); 44 String world = document.getString("world"); 45 String texture = document.getString("texture"); 46 String signature = document.getString("signature"); 47 npc.put("name", name); 48 npc.put("x", x); 49 npc.put("y", y); 50 npc.put("z", z); 51 npc.put("pitch", pitch); 52 npc.put("yaw", yaw); 53 npc.put("world", world); 54 npc.put("texture", texture); 55 npc.put("signature", signature); 56 npcList.add(npc); 57 } 58 } 59 NPCManager.reloadNPC(npcList); 60 } 61 }