This commit is contained in:
NK 2022-11-30 13:43:42 +00:00
parent ff2aec18c1
commit 2c9727e99e
7 changed files with 64 additions and 57 deletions

View file

@ -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<String, Integer> list = ShopDataManager.getItems();
for (String c : list.keySet()) {
ItemStack item = CustomItemManager.produceItem(c).clone();
ItemMeta itemMeta = item.getItemMeta();
List<String> 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);

View file

@ -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<Document> users;
public static MongoCollection<Document> users;
public static MongoCollection<Document> custom_weapons;
public static MongoCollection<Document> ranks;
public static MongoCollection<Document> npcs;
public static MongoCollection<Document> miners;
public MongoClient client;
public static MongoCollection<Document> 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<String, Object> getUser(String username) {
try (MongoCursor<Document> cursor = users.find(Filters.eq("Username", username)).cursor()) {
while (cursor.hasNext()) {
Document doc = cursor.next();
HashMap<String, Object> 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");
}
}

View file

@ -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<String, Integer> getItems() {
HashMap<String, Integer> list = new HashMap<>();
try (MongoCursor<Document> cursor = DatabaseManager.shops.find().cursor()) {
while (cursor.hasNext()) {
Document doc = cursor.next();
list.put(doc.getString("Name"), doc.getInteger("Price"));
}
return list;
}
}
}

View file

@ -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<String, Object> getUser(String username) {
try (MongoCursor<Document> cursor = DatabaseManager.users.find(Filters.eq("Username", username)).cursor()) {
while (cursor.hasNext()) {
Document doc = cursor.next();
HashMap<String, Object> map = new HashMap<>();
for (String key : doc.keySet()) map.put(key, doc.get(key));
map.remove("_id");
return map;
}
}
return null;
}
}

View file

@ -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 {

View file

@ -5,6 +5,5 @@ import static me.night.nullvalkyrie.items.CustomItemManager.updateYamlFilesToPlu
public class FileManager {
public FileManager() {
updateYamlFilesToPlugin(".env");
updateYamlFilesToPlugin("shop.yml");
}
}

View file

@ -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