CustomWeaponsDataManager.java (5723B)
1 package me.night.nullvalkyrie.database; 2 3 import com.mongodb.client.MongoCursor; 4 import com.mongodb.client.model.Filters; 5 import org.bson.Document; 6 import org.bukkit.Material; 7 8 import java.util.ArrayList; 9 import java.util.HashMap; 10 import java.util.List; 11 12 @SuppressWarnings("unchecked") 13 public class CustomWeaponsDataManager { 14 public HashMap<String, Object> getWeapon(String itemName) { 15 HashMap<String, Object> item = new HashMap<>(); 16 try (MongoCursor<Document> cursor = new DatabaseManager().getCustomWeaponsDB().find(Filters.eq("Name", itemName)).cursor()) { 17 while (cursor.hasNext()) { 18 Document doc = cursor.next(); 19 String name = doc.getString("Name"); 20 Document lore = (Document) doc.get("Lore"); 21 Document ability = (Document) lore.get("Ability"); 22 Document properties = (Document) lore.get("Properties"); 23 HashMap<String, HashMap<String, Object>> lores = new HashMap<>(); 24 HashMap<String, Object> abi = new HashMap<>(); 25 HashMap<String, Object> prop = new HashMap<>(); 26 abi.put("Name", ability.getString("Name")); 27 List<String> details = new ArrayList<>(); 28 if (ability.get("Details") != null) details.addAll((List<String>) ability.get("Details")); 29 abi.put("Details", details); 30 for (String a : properties.keySet()) prop.put(a, properties.get(a)); 31 lores.put("Ability", abi); 32 lores.put("Properties", prop); 33 Document enchants = (Document) doc.get("Enchants"); 34 Document attributes = (Document) doc.get("Attributes"); 35 HashMap<String, Object> ench = new HashMap<>(); 36 HashMap<String, Object> attr = new HashMap<>(); 37 for (String a : enchants.keySet()) ench.put(a, enchants.get(a)); 38 for (String a : attributes.keySet()) attr.put(a, attributes.get(a)); 39 Document pdc = (Document) doc.get("PDC"); 40 HashMap<String, Object> pdcdata = new HashMap<>(); 41 if (pdc != null) for (String a : pdc.keySet()) pdcdata.put(a, pdc.get(a)); 42 Document recipe = (Document) doc.get("Recipes"); 43 HashMap<String, Object> recipes = new HashMap<>(); 44 if (recipe != null) { 45 Document ing = (Document) recipe.get("Ingredients"); 46 HashMap<String, String> ingredients = new HashMap<>(); 47 for (String i : ing.keySet()) 48 ingredients.put(i, ing.getString(i)); 49 List<String> shapes = new ArrayList<>(); 50 if (recipe.get("Shapes") != null) shapes.addAll((List<String>) recipe.get("Shapes")); 51 recipes.put("Shape", shapes); 52 recipes.put("Amount", recipe.getInteger("Amount")); 53 recipes.put("Ingredients", ingredients); 54 } 55 item.put("Name", name); 56 item.put("Material", Material.matchMaterial(doc.getString("Material"))); 57 item.put("Type", doc.getString("Type")); 58 item.put("Rarity", doc.getString("Rarity")); 59 item.put("Lore", lores); 60 item.put("Enchants", ench); 61 item.put("Attributes", attr); 62 item.put("PDC", pdcdata); 63 item.put("Recipes", recipes); 64 } 65 return item; 66 } 67 } 68 69 public static HashMap<String, Object> getWeapons() { 70 HashMap<String, Object> list = new HashMap<>(); 71 try (MongoCursor<Document> cursor = new DatabaseManager().getCustomWeaponsDB().find().cursor()) { 72 while (cursor.hasNext()) { 73 Document doc = cursor.next(); 74 HashMap<String, Object> item = new HashMap<>(); 75 String name = doc.getString("Name"); 76 Document lore = (Document) doc.get("Lore"); 77 Document ability = (Document) lore.get("Ability"); 78 Document properties = (Document) lore.get("Properties"); 79 HashMap<String, HashMap<String, Object>> lores = new HashMap<>(); 80 HashMap<String, Object> abi = new HashMap<>(); 81 HashMap<String, Object> prop = new HashMap<>(); 82 abi.put("Name", ability.getString("Name")); 83 List<String> details = new ArrayList<>(); 84 if (ability.get("Details") != null) details.addAll((List<String>) ability.get("Details")); 85 abi.put("Details", details); 86 for (String a : properties.keySet()) prop.put(a, properties.get(a)); 87 lores.put("Ability", abi); 88 lores.put("Properties", prop); 89 Document enchants = (Document) doc.get("Enchants"); 90 Document attributes = (Document) doc.get("Attributes"); 91 HashMap<String, Object> ench = new HashMap<>(); 92 HashMap<String, Object> attr = new HashMap<>(); 93 for (String a : enchants.keySet()) ench.put(a, enchants.get(a)); 94 for (String a : attributes.keySet()) attr.put(a, attributes.get(a)); 95 item.put("Name", name); 96 item.put("Material", Material.matchMaterial(doc.getString("Material"))); 97 item.put("Type", doc.getString("Type")); 98 item.put("Rarity", doc.getString("Rarity")); 99 item.put("Lore", lores); 100 item.put("Enchants", ench); 101 item.put("Attributes", attr); 102 list.put(name, item); 103 } 104 return list; 105 } 106 } 107 }