change to mongodb
This commit is contained in:
parent
a7b7db3db2
commit
605a82dc4b
7 changed files with 75 additions and 72 deletions
|
@ -1,6 +1,7 @@
|
||||||
package me.night.nullvalkyrie.commands;
|
package me.night.nullvalkyrie.commands;
|
||||||
|
|
||||||
import me.night.nullvalkyrie.Main;
|
import me.night.nullvalkyrie.Main;
|
||||||
|
import me.night.nullvalkyrie.database.DatabaseManager;
|
||||||
import me.night.nullvalkyrie.npc.NPCManager;
|
import me.night.nullvalkyrie.npc.NPCManager;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
@ -18,6 +19,7 @@ public class BetaCommand extends Command {
|
||||||
if (sender instanceof Player) {
|
if (sender instanceof Player) {
|
||||||
Player player = (Player) sender;
|
Player player = (Player) sender;
|
||||||
NPCManager.createNPC(player, "&a&lNK");
|
NPCManager.createNPC(player, "&a&lNK");
|
||||||
|
DatabaseManager.getUser("Kaly15");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -2,6 +2,7 @@ package me.night.nullvalkyrie.commands;
|
||||||
|
|
||||||
import com.google.common.cache.Cache;
|
import com.google.common.cache.Cache;
|
||||||
import com.google.common.cache.CacheBuilder;
|
import com.google.common.cache.CacheBuilder;
|
||||||
|
import me.night.nullvalkyrie.database.ranks.RankManager;
|
||||||
import me.night.nullvalkyrie.ui.Rank;
|
import me.night.nullvalkyrie.ui.Rank;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
|
@ -15,8 +16,6 @@ import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import static me.night.nullvalkyrie.ui.ScoreboardListener.rankManager;
|
|
||||||
|
|
||||||
public class RankCommand extends Command {
|
public class RankCommand extends Command {
|
||||||
|
|
||||||
public RankCommand() {
|
public RankCommand() {
|
||||||
|
@ -45,7 +44,7 @@ public class RankCommand extends Command {
|
||||||
OfflinePlayer target = Bukkit.getOfflinePlayer(args[0]);
|
OfflinePlayer target = Bukkit.getOfflinePlayer(args[0]);
|
||||||
for (Rank rank : Rank.values()) {
|
for (Rank rank : Rank.values()) {
|
||||||
if (rank.name().equalsIgnoreCase(args[1])) {
|
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());
|
player.sendMessage(ChatColor.GREEN + "You changed " + target.getName() + "'s rank to " + rank.getDisplay());
|
||||||
if (target.isOnline()) {
|
if (target.isOnline()) {
|
||||||
target.getPlayer().sendMessage(ChatColor.GREEN + player.getName() + " set your rank to " + rank.getDisplay());
|
target.getPlayer().sendMessage(ChatColor.GREEN + player.getName() + " set your rank to " + rank.getDisplay());
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
package me.night.nullvalkyrie.database;
|
package me.night.nullvalkyrie.database;
|
||||||
|
|
||||||
import com.mongodb.MongoException;
|
|
||||||
import com.mongodb.client.*;
|
import com.mongodb.client.*;
|
||||||
import com.mongodb.client.model.Filters;
|
import com.mongodb.client.model.Filters;
|
||||||
import me.night.nullvalkyrie.Main;
|
import me.night.nullvalkyrie.Main;
|
||||||
|
import me.night.nullvalkyrie.database.ranks.RankManager;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
import org.bson.conversions.Bson;
|
import org.bson.conversions.Bson;
|
||||||
|
|
||||||
|
@ -11,20 +11,18 @@ import java.util.HashMap;
|
||||||
|
|
||||||
public class DatabaseManager {
|
public class DatabaseManager {
|
||||||
private static MongoCollection<Document> users;
|
private static MongoCollection<Document> users;
|
||||||
|
private static MongoCollection<Document> custom_weapons;
|
||||||
|
public static MongoCollection<Document> ranks;
|
||||||
|
public MongoClient client;
|
||||||
|
public static MongoDatabase database;
|
||||||
private Main main;
|
private Main main;
|
||||||
public DatabaseManager(Main main) {
|
public DatabaseManager(Main main) {
|
||||||
this.main = main;
|
this.main = main;
|
||||||
connect();
|
this.client = MongoClients.create(Main.env.get("MONGODB_URI"));
|
||||||
}
|
database = client.getDatabase("NullValkyrie");
|
||||||
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");
|
|
||||||
users = database.getCollection("users");
|
users = database.getCollection("users");
|
||||||
|
custom_weapons = database.getCollection("custom_weapons");
|
||||||
|
ranks = database.getCollection("ranks");
|
||||||
}
|
}
|
||||||
public static void createUserSchema(String username) {
|
public static void createUserSchema(String username) {
|
||||||
Document document = new Document();
|
Document document = new Document();
|
||||||
|
@ -39,7 +37,6 @@ public class DatabaseManager {
|
||||||
Bson update = new Document("$set", updated);
|
Bson update = new Document("$set", updated);
|
||||||
users.updateOne(document, update);
|
users.updateOne(document, update);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
public static HashMap<String, Object> getUser(String username) {
|
public static HashMap<String, Object> getUser(String username) {
|
||||||
try (MongoCursor<Document> cursor = users.find(Filters.eq("Username", username)).cursor()) {
|
try (MongoCursor<Document> cursor = users.find(Filters.eq("Username", username)).cursor()) {
|
||||||
|
|
|
@ -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<Document> 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
package me.night.nullvalkyrie.ui;
|
package me.night.nullvalkyrie.ui;
|
||||||
|
|
||||||
|
import me.night.nullvalkyrie.database.ranks.RankManager;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.scoreboard.*;
|
import org.bukkit.scoreboard.*;
|
||||||
|
@ -7,7 +8,7 @@ import org.bukkit.scoreboard.*;
|
||||||
public class NameTagManager {
|
public class NameTagManager {
|
||||||
public void setNametags(Player player) {
|
public void setNametags(Player player) {
|
||||||
Scoreboard newScoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
|
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);
|
obj.setDisplaySlot(DisplaySlot.PLAYER_LIST);
|
||||||
player.setScoreboard(newScoreboard);
|
player.setScoreboard(newScoreboard);
|
||||||
for (Rank rank : Rank.values()) {
|
for (Rank rank : Rank.values()) {
|
||||||
|
@ -16,15 +17,16 @@ public class NameTagManager {
|
||||||
}
|
}
|
||||||
for (Player target : Bukkit.getOnlinePlayers()) {
|
for (Player target : Bukkit.getOnlinePlayers()) {
|
||||||
if (player.getUniqueId() != target.getUniqueId()) {
|
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());
|
player.getScoreboard().getTeam(rank.name()).addEntry(target.getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void newTag(Player player) {
|
public void newTag(Player player) {
|
||||||
Rank rank = ScoreboardListener.rankManager.getRank(player.getUniqueId());
|
Rank rank = RankManager.getRank(player.getUniqueId());
|
||||||
for(Player target : Bukkit.getOnlinePlayers()) {
|
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) {
|
public void removeTag(Player player) {
|
||||||
|
|
|
@ -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()));
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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.ranks.RankManager;
|
||||||
import me.night.nullvalkyrie.npc.NPCManager;
|
import me.night.nullvalkyrie.npc.NPCManager;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
|
@ -16,13 +17,11 @@ import static me.night.nullvalkyrie.database.DatabaseManager.createUserSchema;
|
||||||
|
|
||||||
public class ScoreboardListener implements Listener {
|
public class ScoreboardListener implements Listener {
|
||||||
|
|
||||||
public static RankManager rankManager;
|
|
||||||
public static NameTagManager nameTagManager;
|
public static NameTagManager nameTagManager;
|
||||||
private final SideBarManager sideBarManager;
|
private final SideBarManager sideBarManager;
|
||||||
private final BelowNameManager belowNameManager;
|
private final BelowNameManager belowNameManager;
|
||||||
public ScoreboardListener(Main main) {
|
public ScoreboardListener(Main main) {
|
||||||
nameTagManager = new NameTagManager();
|
nameTagManager = new NameTagManager();
|
||||||
rankManager = new RankManager(main);
|
|
||||||
sideBarManager = new SideBarManager(main);
|
sideBarManager = new SideBarManager(main);
|
||||||
belowNameManager = new BelowNameManager();
|
belowNameManager = new BelowNameManager();
|
||||||
}
|
}
|
||||||
|
@ -32,7 +31,7 @@ public class ScoreboardListener implements Listener {
|
||||||
Player player = e.getPlayer();
|
Player player = e.getPlayer();
|
||||||
if(!player.hasPlayedBefore()) {
|
if(!player.hasPlayedBefore()) {
|
||||||
e.getPlayer().sendTitle(ChatColor.RED + "Welcome to Matrix!", ChatColor.GREEN + "LOL", 20, 100, 20);
|
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());
|
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");
|
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.setSideBar(player);
|
||||||
sideBarManager.start(player);
|
sideBarManager.start(player);
|
||||||
belowNameManager.setBelowName(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() == null) return;
|
||||||
if(NPCManager.getNPCs().isEmpty()) return;
|
if(NPCManager.getNPCs().isEmpty()) return;
|
||||||
NPCManager.addJoinPacket(e.getPlayer());
|
NPCManager.addJoinPacket(e.getPlayer());
|
||||||
|
@ -62,7 +61,7 @@ public class ScoreboardListener implements Listener {
|
||||||
public void onChat(AsyncPlayerChatEvent e) {
|
public void onChat(AsyncPlayerChatEvent e) {
|
||||||
e.setCancelled(true);
|
e.setCancelled(true);
|
||||||
Player player = e.getPlayer();
|
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
|
//Death changing in sidebar
|
||||||
|
|
Loading…
Reference in a new issue