From d4d4ef75e07800483a50731570b26fa4cf2c0467 Mon Sep 17 00:00:00 2001 From: NK Date: Sat, 3 Dec 2022 14:02:36 +0000 Subject: [PATCH] changing money amount in ssscoreboard --- .../nullvalkyrie/commands/BetaCommand.java | 2 ++ .../database/UserDataManager.java | 33 +++++++++++-------- .../me/night/nullvalkyrie/npc/NPCEvents.java | 2 +- .../nullvalkyrie/ui/ScoreboardListener.java | 13 ++------ .../night/nullvalkyrie/ui/SideBarManager.java | 24 +++++++------- 5 files changed, 37 insertions(+), 37 deletions(-) diff --git a/src/main/java/me/night/nullvalkyrie/commands/BetaCommand.java b/src/main/java/me/night/nullvalkyrie/commands/BetaCommand.java index 453ec37..f7145cc 100644 --- a/src/main/java/me/night/nullvalkyrie/commands/BetaCommand.java +++ b/src/main/java/me/night/nullvalkyrie/commands/BetaCommand.java @@ -1,5 +1,6 @@ package me.night.nullvalkyrie.commands; +import me.night.nullvalkyrie.database.UserDataManager; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -14,6 +15,7 @@ public class BetaCommand extends Command { @Override public void onCommand(CommandSender sender, String[] args) { if (sender instanceof Player player) { + UserDataManager.updateUserBank(player.getUniqueId().toString(), 10); } } diff --git a/src/main/java/me/night/nullvalkyrie/database/UserDataManager.java b/src/main/java/me/night/nullvalkyrie/database/UserDataManager.java index 9951f2e..8f8e21d 100644 --- a/src/main/java/me/night/nullvalkyrie/database/UserDataManager.java +++ b/src/main/java/me/night/nullvalkyrie/database/UserDataManager.java @@ -1,38 +1,45 @@ package me.night.nullvalkyrie.database; -import com.mongodb.client.MongoCursor; -import com.mongodb.client.model.Filters; +import me.night.nullvalkyrie.ui.ScoreboardListener; import org.bson.Document; import org.bson.conversions.Bson; import java.util.HashMap; public class UserDataManager { - public static void createUserSchema(String uuid) { + public static void createUserBank(String uuid) { Document document = new Document(); document.put("UUID", uuid); document.put("Bank", 0); DatabaseManager.getUsersDB().insertOne(document); } - public void updateUserBank(String uuid, Number coins) { + public static void updateUserBank(String uuid, Integer coins) { Document document = DatabaseManager.getUsersDB().find(new Document("UUID", uuid)).first(); if (document != null) { - Bson updated = new Document("Bank", coins); + Integer coinsBefore = document.getInteger("Bank"); + Bson updated = new Document("Bank", coins + coinsBefore); Bson update = new Document("$set", updated); DatabaseManager.getUsersDB().updateOne(document, update); + ScoreboardListener.sideBarManager.addBank(uuid); + } else { + Document doc = new Document(); + doc.put("UUID", uuid); + doc.put("Bank", coins); + DatabaseManager.getUsersDB().insertOne(doc); + ScoreboardListener.sideBarManager.addBank(uuid); } + + } public static HashMap getUser(String uuid) { - try (MongoCursor cursor = DatabaseManager.getUsersDB().find(Filters.eq("UUID", uuid)).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; - } + Document document = DatabaseManager.getUsersDB().find(new Document("UUID", uuid)).first(); + if (document != null) { + HashMap map = new HashMap<>(); + for (String key : document.keySet()) map.put(key, document.get(key)); + map.remove("_id"); + return map; } return null; } diff --git a/src/main/java/me/night/nullvalkyrie/npc/NPCEvents.java b/src/main/java/me/night/nullvalkyrie/npc/NPCEvents.java index 1d146a9..38a2280 100644 --- a/src/main/java/me/night/nullvalkyrie/npc/NPCEvents.java +++ b/src/main/java/me/night/nullvalkyrie/npc/NPCEvents.java @@ -15,7 +15,7 @@ public class NPCEvents implements Listener { @EventHandler public void onClick(RightClickNPC e) { Player player = e.getPlayer(); - if (e.getNPC().getBukkitEntity().getName().equalsIgnoreCase(Util.color("&a&lNK"))) { + if (e.getNPC().getBukkitEntity().getName().equalsIgnoreCase(Util.color("&1&lRB18"))) { player.sendMessage(Util.color("Hi")); } } diff --git a/src/main/java/me/night/nullvalkyrie/ui/ScoreboardListener.java b/src/main/java/me/night/nullvalkyrie/ui/ScoreboardListener.java index d38c1cc..6b7411b 100644 --- a/src/main/java/me/night/nullvalkyrie/ui/ScoreboardListener.java +++ b/src/main/java/me/night/nullvalkyrie/ui/ScoreboardListener.java @@ -8,17 +8,16 @@ import org.bukkit.ChatColor; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; -import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; -import static me.night.nullvalkyrie.database.UserDataManager.createUserSchema; +import static me.night.nullvalkyrie.database.UserDataManager.createUserBank; public class ScoreboardListener implements Listener { public static NameTagManager nameTagManager; - private final SideBarManager sideBarManager; + public static SideBarManager sideBarManager; private final BelowNameManager belowNameManager; public ScoreboardListener(Main main) { @@ -33,7 +32,7 @@ public class ScoreboardListener implements Listener { if (!player.hasPlayedBefore()) { e.getPlayer().sendTitle(ChatColor.RED + "Welcome to Vanadium!", ChatColor.GREEN + "LOL", 20, 100, 20); RankDataManager.setRank(player.getUniqueId(), Rank.ROOKIE); - createUserSchema(e.getPlayer().getUniqueId().toString()); + createUserBank(e.getPlayer().getUniqueId().toString()); } 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"); nameTagManager.setNametags(player); @@ -63,10 +62,4 @@ public class ScoreboardListener implements Listener { Player player = e.getPlayer(); Bukkit.broadcastMessage(RankDataManager.getRank(player.getUniqueId()).getDisplay() + " " + player.getName() + ChatColor.WHITE + ": " + e.getMessage()); } - - //Death changing in sidebar - @EventHandler - public void onDeath(PlayerDeathEvent e) { - if (e.getEntity().getPlayer() == null) sideBarManager.changeOnDeath(e.getEntity().getPlayer()); - } } diff --git a/src/main/java/me/night/nullvalkyrie/ui/SideBarManager.java b/src/main/java/me/night/nullvalkyrie/ui/SideBarManager.java index 3a8f27a..71487c1 100644 --- a/src/main/java/me/night/nullvalkyrie/ui/SideBarManager.java +++ b/src/main/java/me/night/nullvalkyrie/ui/SideBarManager.java @@ -1,6 +1,7 @@ package me.night.nullvalkyrie.ui; import me.night.nullvalkyrie.Main; +import me.night.nullvalkyrie.database.UserDataManager; import me.night.nullvalkyrie.util.Util; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -8,11 +9,9 @@ import org.bukkit.entity.Player; import org.bukkit.scoreboard.*; import org.bukkit.scoreboard.Scoreboard; -import java.util.HashMap; import java.util.UUID; public class SideBarManager { - private final HashMap deaths = new HashMap<>(); private int taskID; private final Main main; public AnimatedSideBar board = null; @@ -44,15 +43,14 @@ public class SideBarManager { space2.setScore(2); Score website = obj.getScore(ChatColor.YELLOW + "cath.js.org"); website.setScore(1); - Team playersOnline; - if (board.getTeam("deathsTotal") != null) playersOnline = board.getTeam("deathsTotal"); - else playersOnline = board.registerNewTeam("deathsTotal"); - playersOnline.addEntry(ChatColor.BOLD.toString()); - playersOnline.setPrefix(ChatColor.BLUE + "Deaths: "); - playersOnline.setSuffix(ChatColor.YELLOW + "0"); + Team bankTeam; + if (board.getTeam("Bank") != null) bankTeam = board.getTeam("Bank"); + else bankTeam = board.registerNewTeam("Bank"); + bankTeam.addEntry(ChatColor.BOLD.toString()); + bankTeam.setPrefix(ChatColor.BLUE + "Bank: "); + bankTeam.setSuffix(ChatColor.YELLOW + UserDataManager.getUser(player.getUniqueId().toString()).get("Bank").toString()); obj.getScore(ChatColor.BOLD.toString()).setScore(3); player.setScoreboard(board); - deaths.put(player.getUniqueId(), 0); } public void start(Player player) { @@ -97,9 +95,9 @@ public class SideBarManager { }, 0, 10); } - public void changeOnDeath(Player player) { - int amount = deaths.get(player.getUniqueId()); - deaths.put(player.getUniqueId(), amount++); - player.getScoreboard().getTeam("deathsTotal").setSuffix(ChatColor.YELLOW.toString() + amount); + public void addBank(String uuid) { + UUID uid = UUID.fromString(uuid); + Bukkit.getPlayer(uid).getScoreboard().getTeam("Bank").setSuffix(ChatColor.YELLOW.toString() + UserDataManager.getUser(uuid).get("Bank")); + } }