From 605a82dc4bfca5d9277af2631bf48f09665a5b92 Mon Sep 17 00:00:00 2001 From: NK Date: Sun, 27 Nov 2022 00:25:47 +0000 Subject: [PATCH] change to mongodb --- .../nullvalkyrie/commands/BetaCommand.java | 2 + .../nullvalkyrie/commands/RankCommand.java | 5 +- .../database/DatabaseManager.java | 21 ++++---- .../database/ranks/RankManager.java | 52 +++++++++++++++++++ .../night/nullvalkyrie/ui/NameTagManager.java | 10 ++-- .../me/night/nullvalkyrie/ui/RankManager.java | 48 ----------------- .../nullvalkyrie/ui/ScoreboardListener.java | 9 ++-- 7 files changed, 75 insertions(+), 72 deletions(-) create mode 100644 src/main/java/me/night/nullvalkyrie/database/ranks/RankManager.java delete mode 100644 src/main/java/me/night/nullvalkyrie/ui/RankManager.java diff --git a/src/main/java/me/night/nullvalkyrie/commands/BetaCommand.java b/src/main/java/me/night/nullvalkyrie/commands/BetaCommand.java index 6fbba00..52fc228 100644 --- a/src/main/java/me/night/nullvalkyrie/commands/BetaCommand.java +++ b/src/main/java/me/night/nullvalkyrie/commands/BetaCommand.java @@ -1,6 +1,7 @@ package me.night.nullvalkyrie.commands; import me.night.nullvalkyrie.Main; +import me.night.nullvalkyrie.database.DatabaseManager; import me.night.nullvalkyrie.npc.NPCManager; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -18,6 +19,7 @@ public class BetaCommand extends Command { if (sender instanceof Player) { Player player = (Player) sender; NPCManager.createNPC(player, "&a&lNK"); + DatabaseManager.getUser("Kaly15"); } } @Override diff --git a/src/main/java/me/night/nullvalkyrie/commands/RankCommand.java b/src/main/java/me/night/nullvalkyrie/commands/RankCommand.java index ea84e2b..2fc0bd8 100644 --- a/src/main/java/me/night/nullvalkyrie/commands/RankCommand.java +++ b/src/main/java/me/night/nullvalkyrie/commands/RankCommand.java @@ -2,6 +2,7 @@ package me.night.nullvalkyrie.commands; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; +import me.night.nullvalkyrie.database.ranks.RankManager; import me.night.nullvalkyrie.ui.Rank; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -15,8 +16,6 @@ import java.util.List; import java.util.UUID; import java.util.concurrent.TimeUnit; -import static me.night.nullvalkyrie.ui.ScoreboardListener.rankManager; - public class RankCommand extends Command { public RankCommand() { @@ -45,7 +44,7 @@ public class RankCommand extends Command { OfflinePlayer target = Bukkit.getOfflinePlayer(args[0]); for (Rank rank : Rank.values()) { if (rank.name().equalsIgnoreCase(args[1])) { - rankManager.setRank(target.getUniqueId(), rank); + RankManager.setRank(target.getUniqueId(), rank); player.sendMessage(ChatColor.GREEN + "You changed " + target.getName() + "'s rank to " + rank.getDisplay()); if (target.isOnline()) { target.getPlayer().sendMessage(ChatColor.GREEN + player.getName() + " set your rank to " + rank.getDisplay()); diff --git a/src/main/java/me/night/nullvalkyrie/database/DatabaseManager.java b/src/main/java/me/night/nullvalkyrie/database/DatabaseManager.java index 6a08cbe..305db67 100644 --- a/src/main/java/me/night/nullvalkyrie/database/DatabaseManager.java +++ b/src/main/java/me/night/nullvalkyrie/database/DatabaseManager.java @@ -1,9 +1,9 @@ package me.night.nullvalkyrie.database; -import com.mongodb.MongoException; import com.mongodb.client.*; import com.mongodb.client.model.Filters; import me.night.nullvalkyrie.Main; +import me.night.nullvalkyrie.database.ranks.RankManager; import org.bson.Document; import org.bson.conversions.Bson; @@ -11,20 +11,18 @@ import java.util.HashMap; public class DatabaseManager { private static MongoCollection users; + private static MongoCollection custom_weapons; + public static MongoCollection ranks; + public MongoClient client; + public static MongoDatabase database; private Main main; public DatabaseManager(Main main) { this.main = main; - connect(); - } - public void connect() { - try (MongoClient client = MongoClients.create(Main.env.get("MONGODB_URI"))) { - - } catch (MongoException e) { - System.out.println("An error occurred when logging in to MongoDB" + e); - } - MongoClient client = MongoClients.create("mongodb+srv://cath_exe:gaeismypassion@cath-exe.iolb7.mongodb.net/NullValkyrie"); - MongoDatabase database = client.getDatabase("NullValkyrie"); + this.client = MongoClients.create(Main.env.get("MONGODB_URI")); + database = client.getDatabase("NullValkyrie"); users = database.getCollection("users"); + custom_weapons = database.getCollection("custom_weapons"); + ranks = database.getCollection("ranks"); } public static void createUserSchema(String username) { Document document = new Document(); @@ -39,7 +37,6 @@ public class DatabaseManager { 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()) { diff --git a/src/main/java/me/night/nullvalkyrie/database/ranks/RankManager.java b/src/main/java/me/night/nullvalkyrie/database/ranks/RankManager.java new file mode 100644 index 0000000..ed92726 --- /dev/null +++ b/src/main/java/me/night/nullvalkyrie/database/ranks/RankManager.java @@ -0,0 +1,52 @@ +package me.night.nullvalkyrie.database.ranks; + +import com.mongodb.client.MongoClients; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoCursor; +import com.mongodb.client.model.Filters; +import me.night.nullvalkyrie.Main; +import me.night.nullvalkyrie.database.DatabaseManager; +import me.night.nullvalkyrie.ui.Rank; +import org.bson.Document; +import org.bson.conversions.Bson; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +import java.util.UUID; + +import static me.night.nullvalkyrie.ui.ScoreboardListener.nameTagManager; + +public class RankManager { + public static void setRank(UUID uuid, Rank rank) { + Document document = DatabaseManager.ranks.find(new Document("UUID", uuid.toString())).first(); + if(document != null) { + Bson updated = new Document("Rank", rank.name()); + Bson update = new Document("$set", updated); + DatabaseManager.ranks.updateOne(document, update); + } else { + Document newDocument = new Document(); + newDocument.put("UUID", uuid.toString()); + newDocument.put("Rank", rank.name()); + DatabaseManager.ranks.insertOne(newDocument); + } + for(Player player : Bukkit.getOnlinePlayers()) { + if(player.hasPlayedBefore()) { + nameTagManager.removeTag(player); + nameTagManager.newTag(player); + } + } + } + public static Rank getRank(UUID uuid) { + try (MongoCursor cursor = DatabaseManager.ranks.find(Filters.eq("UUID", uuid.toString())).cursor()) { + while (cursor.hasNext()) { + Document doc = cursor.next(); + for (String a : doc.keySet()) { + if (a.equals("Rank")) { + return Rank.valueOf((String) doc.get(a)); + } + } + } + } + return null; + } +} diff --git a/src/main/java/me/night/nullvalkyrie/ui/NameTagManager.java b/src/main/java/me/night/nullvalkyrie/ui/NameTagManager.java index 1d5ad9c..f3008d1 100644 --- a/src/main/java/me/night/nullvalkyrie/ui/NameTagManager.java +++ b/src/main/java/me/night/nullvalkyrie/ui/NameTagManager.java @@ -1,5 +1,6 @@ package me.night.nullvalkyrie.ui; +import me.night.nullvalkyrie.database.ranks.RankManager; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.scoreboard.*; @@ -7,7 +8,7 @@ import org.bukkit.scoreboard.*; public class NameTagManager { public void setNametags(Player player) { Scoreboard newScoreboard = Bukkit.getScoreboardManager().getNewScoreboard(); - Objective obj = newScoreboard.registerNewObjective("TabList", Criteria.DUMMY, ""); + Objective obj = newScoreboard.registerNewObjective("TabList", Criteria.DUMMY, "Test"); obj.setDisplaySlot(DisplaySlot.PLAYER_LIST); player.setScoreboard(newScoreboard); for (Rank rank : Rank.values()) { @@ -16,15 +17,16 @@ public class NameTagManager { } for (Player target : Bukkit.getOnlinePlayers()) { if (player.getUniqueId() != target.getUniqueId()) { - Rank rank = ScoreboardListener.rankManager.getRank(target.getUniqueId()); + Rank rank = RankManager.getRank(target.getUniqueId()); player.getScoreboard().getTeam(rank.name()).addEntry(target.getName()); } } } public void newTag(Player player) { - Rank rank = ScoreboardListener.rankManager.getRank(player.getUniqueId()); + Rank rank = RankManager.getRank(player.getUniqueId()); for(Player target : Bukkit.getOnlinePlayers()) { - target.getScoreboard().getTeam(rank.name()).addEntry(player.getName()); + if(rank == null) target.getScoreboard().getTeam(Rank.ROOKIE.name()).addEntry(player.getName()); + else target.getScoreboard().getTeam(rank.name()).addEntry(player.getName()); } } public void removeTag(Player player) { diff --git a/src/main/java/me/night/nullvalkyrie/ui/RankManager.java b/src/main/java/me/night/nullvalkyrie/ui/RankManager.java deleted file mode 100644 index 40b8c4c..0000000 --- a/src/main/java/me/night/nullvalkyrie/ui/RankManager.java +++ /dev/null @@ -1,48 +0,0 @@ -package me.night.nullvalkyrie.ui; - -import me.night.nullvalkyrie.Main; -import org.bukkit.Bukkit; -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.entity.Player; - -import java.io.File; -import java.io.IOException; -import java.util.UUID; - -import static me.night.nullvalkyrie.ui.ScoreboardListener.nameTagManager; - -public class RankManager { - private final File file; - private final YamlConfiguration config; - public RankManager(Main main) { - if(!main.getDataFolder().exists()) { - main.getDataFolder().mkdir(); - } - file = new File(main.getDataFolder(), "ranks.yml"); - if(!file.exists()) { - try { - file.createNewFile(); - } catch (IOException e) { - e.printStackTrace(); - } - } - config = YamlConfiguration.loadConfiguration(file); - } - public void setRank(UUID uuid, Rank rank) { - config.set(uuid.toString(), rank.name()); - try { - config.save(file); - } catch (IOException e) { - e.printStackTrace(); - } - for(Player player : Bukkit.getOnlinePlayers()) { - if(player.hasPlayedBefore()) { - nameTagManager.removeTag(player); - nameTagManager.newTag(player); - } - } - } - public Rank getRank(UUID uuid) { - return Rank.valueOf(config.getString(uuid.toString())); - } -} diff --git a/src/main/java/me/night/nullvalkyrie/ui/ScoreboardListener.java b/src/main/java/me/night/nullvalkyrie/ui/ScoreboardListener.java index cce4a3f..a41bea6 100644 --- a/src/main/java/me/night/nullvalkyrie/ui/ScoreboardListener.java +++ b/src/main/java/me/night/nullvalkyrie/ui/ScoreboardListener.java @@ -1,6 +1,7 @@ package me.night.nullvalkyrie.ui; import me.night.nullvalkyrie.Main; +import me.night.nullvalkyrie.database.ranks.RankManager; import me.night.nullvalkyrie.npc.NPCManager; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -16,13 +17,11 @@ import static me.night.nullvalkyrie.database.DatabaseManager.createUserSchema; public class ScoreboardListener implements Listener { - public static RankManager rankManager; public static NameTagManager nameTagManager; private final SideBarManager sideBarManager; private final BelowNameManager belowNameManager; public ScoreboardListener(Main main) { nameTagManager = new NameTagManager(); - rankManager = new RankManager(main); sideBarManager = new SideBarManager(main); belowNameManager = new BelowNameManager(); } @@ -32,7 +31,7 @@ public class ScoreboardListener implements Listener { Player player = e.getPlayer(); if(!player.hasPlayedBefore()) { e.getPlayer().sendTitle(ChatColor.RED + "Welcome to Matrix!", ChatColor.GREEN + "LOL", 20, 100, 20); - rankManager.setRank(player.getUniqueId(), Rank.ROOKIE); + RankManager.setRank(player.getUniqueId(), Rank.ROOKIE); createUserSchema(e.getPlayer().getDisplayName()); } e.getPlayer().setPlayerListHeaderFooter(ChatColor.AQUA + "You are playing on " + ChatColor.GREEN + "127.0.0.1", ChatColor.GOLD + "Ranks, boosters, & more!" + ChatColor.AQUA + "127.0.0.1"); @@ -41,7 +40,7 @@ public class ScoreboardListener implements Listener { sideBarManager.setSideBar(player); sideBarManager.start(player); belowNameManager.setBelowName(player); - e.setJoinMessage(rankManager.getRank(e.getPlayer().getUniqueId()).getDisplay() + " " + e.getPlayer().getName() + ChatColor.WHITE + " joined the server!"); + e.setJoinMessage(RankManager.getRank(e.getPlayer().getUniqueId()).getDisplay() + " " + e.getPlayer().getName() + ChatColor.WHITE + " joined the server!"); if(NPCManager.getNPCs() == null) return; if(NPCManager.getNPCs().isEmpty()) return; NPCManager.addJoinPacket(e.getPlayer()); @@ -62,7 +61,7 @@ public class ScoreboardListener implements Listener { public void onChat(AsyncPlayerChatEvent e) { e.setCancelled(true); Player player = e.getPlayer(); - Bukkit.broadcastMessage(rankManager.getRank(player.getUniqueId()).getDisplay() + " " + player.getName() + ChatColor.WHITE + ": " + e.getMessage()); + Bukkit.broadcastMessage(me.night.nullvalkyrie.database.ranks.RankManager.getRank(player.getUniqueId()).getDisplay() + " " + player.getName() + ChatColor.WHITE + ": " + e.getMessage()); } //Death changing in sidebar