From 2c9727e99e8bae90a2e228792cf152fd5fd179ce Mon Sep 17 00:00:00 2001 From: NK Date: Wed, 30 Nov 2022 13:43:42 +0000 Subject: [PATCH] shop --- .../nullvalkyrie/commands/ShopCommand.java | 12 +++--- .../database/DatabaseManager.java | 41 ++----------------- .../database/ShopDataManager.java | 19 +++++++++ .../database/UserDataManager.java | 34 +++++++++++++++ .../nullvalkyrie/ui/ScoreboardListener.java | 2 +- .../night/nullvalkyrie/util/FileManager.java | 1 - src/main/resources/shop.yml | 12 ------ 7 files changed, 64 insertions(+), 57 deletions(-) create mode 100644 src/main/java/me/night/nullvalkyrie/database/ShopDataManager.java delete mode 100644 src/main/resources/shop.yml diff --git a/src/main/java/me/night/nullvalkyrie/commands/ShopCommand.java b/src/main/java/me/night/nullvalkyrie/commands/ShopCommand.java index 348f75b..58c914c 100644 --- a/src/main/java/me/night/nullvalkyrie/commands/ShopCommand.java +++ b/src/main/java/me/night/nullvalkyrie/commands/ShopCommand.java @@ -1,20 +1,19 @@ package me.night.nullvalkyrie.commands; +import me.night.nullvalkyrie.database.ShopDataManager; import me.night.nullvalkyrie.items.CustomItemManager; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; -import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; +import java.util.HashMap; import java.util.List; public class ShopCommand extends Command { - private final FileConfiguration file = CustomItemManager.loadConfig("shop.yml"); - public ShopCommand() { super("7elven", new String[]{"711", "seven", "7ven"}, @@ -27,11 +26,12 @@ public class ShopCommand extends Command { public void onCommand(CommandSender sender, String[] args) { Inventory inv = Bukkit.createInventory(null, 45, ChatColor.GREEN + "7-Eleven 24/7"); int counter = 0; - for (String c : file.getKeys(false)) { - ItemStack item = CustomItemManager.produceItem(file.getString(c + ".name")).clone(); + HashMap list = ShopDataManager.getItems(); + for (String c : list.keySet()) { + ItemStack item = CustomItemManager.produceItem(c).clone(); ItemMeta itemMeta = item.getItemMeta(); List lore = itemMeta.getLore(); - lore.add("Price (BIN): " + file.getString(c + ".price")); + lore.add("Price (BIN): " + list.get(c)); itemMeta.setLore(lore); item.setItemMeta(itemMeta); inv.setItem(counter, item); diff --git a/src/main/java/me/night/nullvalkyrie/database/DatabaseManager.java b/src/main/java/me/night/nullvalkyrie/database/DatabaseManager.java index 6affb21..a52e408 100644 --- a/src/main/java/me/night/nullvalkyrie/database/DatabaseManager.java +++ b/src/main/java/me/night/nullvalkyrie/database/DatabaseManager.java @@ -1,58 +1,25 @@ package me.night.nullvalkyrie.database; import com.mongodb.client.*; -import com.mongodb.client.model.Filters; import me.night.nullvalkyrie.Main; import org.bson.Document; -import org.bson.conversions.Bson; - -import java.util.HashMap; public class DatabaseManager { - private static MongoCollection users; + public static MongoCollection users; public static MongoCollection custom_weapons; public static MongoCollection ranks; public static MongoCollection npcs; public static MongoCollection miners; - public MongoClient client; + public static MongoCollection shops; public static MongoDatabase database; public DatabaseManager() { - this.client = MongoClients.create(Main.env.get("MONGODB_URI")); - database = client.getDatabase("NullValkyrie"); + database = MongoClients.create(Main.env.get("MONGODB_URI")).getDatabase("NullValkyrie"); users = database.getCollection("users"); custom_weapons = database.getCollection("custom_weapons"); ranks = database.getCollection("ranks"); npcs = database.getCollection("npcs"); miners = database.getCollection("miners"); - } - - public static void createUserSchema(String username) { - Document document = new Document(); - document.put("Username", username); - document.put("Bank", 0); - users.insertOne(document); - } - - public void updateUserBank(String username, Number coins) { - Document document = users.find(new Document("Username", username)).first(); - if (document != null) { - Bson updated = new Document("Bank", coins); - Bson update = new Document("$set", updated); - users.updateOne(document, update); - } - } - - public static HashMap getUser(String username) { - try (MongoCursor cursor = users.find(Filters.eq("Username", username)).cursor()) { - while (cursor.hasNext()) { - Document doc = cursor.next(); - HashMap map = new HashMap<>(); - for (String key : doc.keySet()) map.put(key, doc.get(key)); - map.remove("_id"); - return map; - } - } - return null; + shops = database.getCollection("shops"); } } diff --git a/src/main/java/me/night/nullvalkyrie/database/ShopDataManager.java b/src/main/java/me/night/nullvalkyrie/database/ShopDataManager.java new file mode 100644 index 0000000..cac6c24 --- /dev/null +++ b/src/main/java/me/night/nullvalkyrie/database/ShopDataManager.java @@ -0,0 +1,19 @@ +package me.night.nullvalkyrie.database; + +import com.mongodb.client.MongoCursor; +import org.bson.Document; + +import java.util.HashMap; + +public class ShopDataManager { + public static HashMap getItems() { + HashMap list = new HashMap<>(); + try (MongoCursor cursor = DatabaseManager.shops.find().cursor()) { + while (cursor.hasNext()) { + Document doc = cursor.next(); + list.put(doc.getString("Name"), doc.getInteger("Price")); + } + return list; + } + } +} diff --git a/src/main/java/me/night/nullvalkyrie/database/UserDataManager.java b/src/main/java/me/night/nullvalkyrie/database/UserDataManager.java index eb97446..c2afb0d 100644 --- a/src/main/java/me/night/nullvalkyrie/database/UserDataManager.java +++ b/src/main/java/me/night/nullvalkyrie/database/UserDataManager.java @@ -1,4 +1,38 @@ package me.night.nullvalkyrie.database; +import com.mongodb.client.MongoCursor; +import com.mongodb.client.model.Filters; +import org.bson.Document; +import org.bson.conversions.Bson; + +import java.util.HashMap; + public class UserDataManager { + public static void createUserSchema(String username) { + Document document = new Document(); + document.put("Username", username); + document.put("Bank", 0); + DatabaseManager.users.insertOne(document); + } + public void updateUserBank(String username, Number coins) { + Document document = DatabaseManager.users.find(new Document("Username", username)).first(); + if (document != null) { + Bson updated = new Document("Bank", coins); + Bson update = new Document("$set", updated); + DatabaseManager.users.updateOne(document, update); + } + } + + public static HashMap getUser(String username) { + try (MongoCursor cursor = DatabaseManager.users.find(Filters.eq("Username", username)).cursor()) { + while (cursor.hasNext()) { + Document doc = cursor.next(); + HashMap map = new HashMap<>(); + for (String key : doc.keySet()) map.put(key, doc.get(key)); + map.remove("_id"); + return map; + } + } + return null; + } } diff --git a/src/main/java/me/night/nullvalkyrie/ui/ScoreboardListener.java b/src/main/java/me/night/nullvalkyrie/ui/ScoreboardListener.java index 8f8aa81..672f0d3 100644 --- a/src/main/java/me/night/nullvalkyrie/ui/ScoreboardListener.java +++ b/src/main/java/me/night/nullvalkyrie/ui/ScoreboardListener.java @@ -13,7 +13,7 @@ import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; -import static me.night.nullvalkyrie.database.DatabaseManager.createUserSchema; +import static me.night.nullvalkyrie.database.UserDataManager.createUserSchema; public class ScoreboardListener implements Listener { diff --git a/src/main/java/me/night/nullvalkyrie/util/FileManager.java b/src/main/java/me/night/nullvalkyrie/util/FileManager.java index 945e163..8fb543b 100644 --- a/src/main/java/me/night/nullvalkyrie/util/FileManager.java +++ b/src/main/java/me/night/nullvalkyrie/util/FileManager.java @@ -5,6 +5,5 @@ import static me.night.nullvalkyrie.items.CustomItemManager.updateYamlFilesToPlu public class FileManager { public FileManager() { updateYamlFilesToPlugin(".env"); - updateYamlFilesToPlugin("shop.yml"); } } diff --git a/src/main/resources/shop.yml b/src/main/resources/shop.yml deleted file mode 100644 index 98daf87..0000000 --- a/src/main/resources/shop.yml +++ /dev/null @@ -1,12 +0,0 @@ -SnowGun: - name: Snow Gun - price: 200 -Terminator: - name: Terminator - price: 300 -ExplosiveBow: - name: Explosive Bow - price: 250 -GrapplingHook: - name: Grappling Hook - price: 400 \ No newline at end of file