changing money amount in ssscoreboard

This commit is contained in:
NK 2022-12-03 14:02:36 +00:00
parent 95db3dc0e3
commit d4d4ef75e0
5 changed files with 37 additions and 37 deletions

View file

@ -1,5 +1,6 @@
package me.night.nullvalkyrie.commands; package me.night.nullvalkyrie.commands;
import me.night.nullvalkyrie.database.UserDataManager;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -14,6 +15,7 @@ public class BetaCommand extends Command {
@Override @Override
public void onCommand(CommandSender sender, String[] args) { public void onCommand(CommandSender sender, String[] args) {
if (sender instanceof Player player) { if (sender instanceof Player player) {
UserDataManager.updateUserBank(player.getUniqueId().toString(), 10);
} }
} }

View file

@ -1,38 +1,45 @@
package me.night.nullvalkyrie.database; package me.night.nullvalkyrie.database;
import com.mongodb.client.MongoCursor; import me.night.nullvalkyrie.ui.ScoreboardListener;
import com.mongodb.client.model.Filters;
import org.bson.Document; import org.bson.Document;
import org.bson.conversions.Bson; import org.bson.conversions.Bson;
import java.util.HashMap; import java.util.HashMap;
public class UserDataManager { public class UserDataManager {
public static void createUserSchema(String uuid) { public static void createUserBank(String uuid) {
Document document = new Document(); Document document = new Document();
document.put("UUID", uuid); document.put("UUID", uuid);
document.put("Bank", 0); document.put("Bank", 0);
DatabaseManager.getUsersDB().insertOne(document); 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(); Document document = DatabaseManager.getUsersDB().find(new Document("UUID", uuid)).first();
if (document != null) { 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); Bson update = new Document("$set", updated);
DatabaseManager.getUsersDB().updateOne(document, update); 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<String, Object> getUser(String uuid) { public static HashMap<String, Object> getUser(String uuid) {
try (MongoCursor<Document> cursor = DatabaseManager.getUsersDB().find(Filters.eq("UUID", uuid)).cursor()) { Document document = DatabaseManager.getUsersDB().find(new Document("UUID", uuid)).first();
while (cursor.hasNext()) { if (document != null) {
Document doc = cursor.next(); HashMap<String, Object> map = new HashMap<>();
HashMap<String, Object> map = new HashMap<>(); for (String key : document.keySet()) map.put(key, document.get(key));
for (String key : doc.keySet()) map.put(key, doc.get(key)); map.remove("_id");
map.remove("_id"); return map;
return map;
}
} }
return null; return null;
} }

View file

@ -15,7 +15,7 @@ public class NPCEvents implements Listener {
@EventHandler @EventHandler
public void onClick(RightClickNPC e) { public void onClick(RightClickNPC e) {
Player player = e.getPlayer(); 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")); player.sendMessage(Util.color("Hi"));
} }
} }

View file

@ -8,17 +8,16 @@ import org.bukkit.ChatColor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent; 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 class ScoreboardListener implements Listener {
public static NameTagManager nameTagManager; public static NameTagManager nameTagManager;
private final SideBarManager sideBarManager; public static SideBarManager sideBarManager;
private final BelowNameManager belowNameManager; private final BelowNameManager belowNameManager;
public ScoreboardListener(Main main) { public ScoreboardListener(Main main) {
@ -33,7 +32,7 @@ public class ScoreboardListener implements Listener {
if (!player.hasPlayedBefore()) { if (!player.hasPlayedBefore()) {
e.getPlayer().sendTitle(ChatColor.RED + "Welcome to Vanadium!", ChatColor.GREEN + "LOL", 20, 100, 20); e.getPlayer().sendTitle(ChatColor.RED + "Welcome to Vanadium!", ChatColor.GREEN + "LOL", 20, 100, 20);
RankDataManager.setRank(player.getUniqueId(), Rank.ROOKIE); 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"); 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); nameTagManager.setNametags(player);
@ -63,10 +62,4 @@ public class ScoreboardListener implements Listener {
Player player = e.getPlayer(); Player player = e.getPlayer();
Bukkit.broadcastMessage(RankDataManager.getRank(player.getUniqueId()).getDisplay() + " " + player.getName() + ChatColor.WHITE + ": " + e.getMessage()); 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());
}
} }

View file

@ -1,6 +1,7 @@
package me.night.nullvalkyrie.ui; package me.night.nullvalkyrie.ui;
import me.night.nullvalkyrie.Main; import me.night.nullvalkyrie.Main;
import me.night.nullvalkyrie.database.UserDataManager;
import me.night.nullvalkyrie.util.Util; import me.night.nullvalkyrie.util.Util;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
@ -8,11 +9,9 @@ import org.bukkit.entity.Player;
import org.bukkit.scoreboard.*; import org.bukkit.scoreboard.*;
import org.bukkit.scoreboard.Scoreboard; import org.bukkit.scoreboard.Scoreboard;
import java.util.HashMap;
import java.util.UUID; import java.util.UUID;
public class SideBarManager { public class SideBarManager {
private final HashMap<UUID, Integer> deaths = new HashMap<>();
private int taskID; private int taskID;
private final Main main; private final Main main;
public AnimatedSideBar board = null; public AnimatedSideBar board = null;
@ -44,15 +43,14 @@ public class SideBarManager {
space2.setScore(2); space2.setScore(2);
Score website = obj.getScore(ChatColor.YELLOW + "cath.js.org"); Score website = obj.getScore(ChatColor.YELLOW + "cath.js.org");
website.setScore(1); website.setScore(1);
Team playersOnline; Team bankTeam;
if (board.getTeam("deathsTotal") != null) playersOnline = board.getTeam("deathsTotal"); if (board.getTeam("Bank") != null) bankTeam = board.getTeam("Bank");
else playersOnline = board.registerNewTeam("deathsTotal"); else bankTeam = board.registerNewTeam("Bank");
playersOnline.addEntry(ChatColor.BOLD.toString()); bankTeam.addEntry(ChatColor.BOLD.toString());
playersOnline.setPrefix(ChatColor.BLUE + "Deaths: "); bankTeam.setPrefix(ChatColor.BLUE + "Bank: ");
playersOnline.setSuffix(ChatColor.YELLOW + "0"); bankTeam.setSuffix(ChatColor.YELLOW + UserDataManager.getUser(player.getUniqueId().toString()).get("Bank").toString());
obj.getScore(ChatColor.BOLD.toString()).setScore(3); obj.getScore(ChatColor.BOLD.toString()).setScore(3);
player.setScoreboard(board); player.setScoreboard(board);
deaths.put(player.getUniqueId(), 0);
} }
public void start(Player player) { public void start(Player player) {
@ -97,9 +95,9 @@ public class SideBarManager {
}, 0, 10); }, 0, 10);
} }
public void changeOnDeath(Player player) { public void addBank(String uuid) {
int amount = deaths.get(player.getUniqueId()); UUID uid = UUID.fromString(uuid);
deaths.put(player.getUniqueId(), amount++); Bukkit.getPlayer(uid).getScoreboard().getTeam("Bank").setSuffix(ChatColor.YELLOW.toString() + UserDataManager.getUser(uuid).get("Bank"));
player.getScoreboard().getTeam("deathsTotal").setSuffix(ChatColor.YELLOW.toString() + amount);
} }
} }