diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 4f4ea7f..6ee4144 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,8 +4,16 @@
-
-
+
+
+
+
+
+
+
+
+
+
@@ -31,11 +39,15 @@
-
+
+
+
+
+
@@ -53,6 +65,7 @@
+
@@ -119,5 +132,6 @@
+
\ No newline at end of file
diff --git a/src/main/java/rip/tilly/bedwars/BedWars.java b/src/main/java/rip/tilly/bedwars/BedWars.java
index 58b50aa..0ea2ad5 100644
--- a/src/main/java/rip/tilly/bedwars/BedWars.java
+++ b/src/main/java/rip/tilly/bedwars/BedWars.java
@@ -2,9 +2,18 @@ package rip.tilly.bedwars;
import lombok.Getter;
import org.bukkit.Bukkit;
+import org.bukkit.Chunk;
+import org.bukkit.World;
+import org.bukkit.entity.Entity;
+import org.bukkit.entity.EntityType;
import org.bukkit.plugin.java.JavaPlugin;
+import rip.tilly.bedwars.mongo.MongoManager;
+import rip.tilly.bedwars.player.PlayerDataHandler;
+import rip.tilly.bedwars.player.PlayerDataManager;
import rip.tilly.bedwars.utils.CC;
+import java.util.Arrays;
+
/**
* Created by Lucanius & Trixkz
* Project: BedWars
@@ -14,6 +23,9 @@ public final class BedWars extends JavaPlugin {
@Getter private static BedWars instance;
+ private MongoManager mongoManager;
+ private PlayerDataManager playerDataManager;
+
@Override
public void onEnable() {
instance = this;
@@ -23,10 +35,48 @@ public final class BedWars extends JavaPlugin {
Bukkit.getConsoleSender().sendMessage(CC.translate("&dBedWars &8- &av" + getDescription().getVersion()));
Bukkit.getConsoleSender().sendMessage(CC.translate("&7Made on &dTilly RIP &7by &eTrixkz &7& &eLucanius"));
Bukkit.getConsoleSender().sendMessage("------------------------------------------------");
+
+ this.loadManagers();
+ this.loadListeners();
+
+ for (World world : Bukkit.getWorlds()) {
+ for (Entity entity : world.getEntities()) {
+ if (entity.getType() != EntityType.PLAYER && entity.getType() != EntityType.ITEM_FRAME) {
+ entity.remove();
+ }
+ }
+ world.setGameRuleValue("doDaylightCycle", "false");
+ world.setTime(0L);
+ world.setStorm(false);
+ }
}
@Override
public void onDisable() {
instance = null;
+
+ for (World world : Bukkit.getWorlds()) {
+ for (Entity entity : world.getEntities()) {
+ if (entity.getType() == EntityType.DROPPED_ITEM) {
+ entity.remove();
+ }
+ }
+ for (Chunk chunk : world.getLoadedChunks()) {
+ chunk.unload(true);
+ }
+ }
+
+ this.mongoManager.disconnect();
+ }
+
+ private void loadManagers() {
+ this.mongoManager = new MongoManager();
+ this.playerDataManager = new PlayerDataManager();
+ }
+
+ private void loadListeners() {
+ Arrays.asList(
+ new PlayerDataHandler()
+ ).forEach(listener -> this.getServer().getPluginManager().registerEvents(listener, this));
}
}
diff --git a/src/main/java/rip/tilly/bedwars/mongo/MongoManager.java b/src/main/java/rip/tilly/bedwars/mongo/MongoManager.java
new file mode 100644
index 0000000..aa741f8
--- /dev/null
+++ b/src/main/java/rip/tilly/bedwars/mongo/MongoManager.java
@@ -0,0 +1,72 @@
+package rip.tilly.bedwars.mongo;
+
+import com.mongodb.MongoClient;
+import com.mongodb.MongoCredential;
+import com.mongodb.ServerAddress;
+import com.mongodb.client.MongoCollection;
+import com.mongodb.client.MongoDatabase;
+import lombok.Getter;
+import org.bson.Document;
+import org.bukkit.Bukkit;
+import org.bukkit.configuration.ConfigurationSection;
+import rip.tilly.bedwars.BedWars;
+import rip.tilly.bedwars.utils.CC;
+
+import java.util.Collections;
+
+/**
+ * Created by Lucanius
+ * Project: BedWars
+ */
+@Getter
+public class MongoManager {
+
+ private final MongoManager instance;
+ private final BedWars plugin = BedWars.getInstance();
+ private final ConfigurationSection config = this.plugin.getConfig().getConfigurationSection("MONGO");
+
+ private MongoClient mongoClient;
+ private MongoDatabase mongoDatabase;
+
+ private final String host = config.getString("HOST");
+ private final int port = config.getInt("PORT");
+ private final String database = config.getString("DATABASE");
+ private final boolean auth = config.getBoolean("AUTH.ENABLED");
+ private final String user = config.getString("AUTH.USERNAME");
+ private final String password = config.getString("AUTH.PASSWORD");
+ private final String authDatabase = config.getString("AUTH.AUTH-DATABASE");
+
+ private boolean connected;
+
+ private MongoCollection players;
+
+ public MongoManager() {
+ instance = this;
+ try {
+ if (auth) {
+ final MongoCredential credential = MongoCredential.createCredential(user, authDatabase, password.toCharArray());
+ mongoClient = new MongoClient(new ServerAddress(host, port), Collections.singletonList(credential));
+ } else {
+ mongoClient = new MongoClient(host, port);
+ }
+ connected = true;
+ mongoDatabase = mongoClient.getDatabase(database);
+ Bukkit.getConsoleSender().sendMessage(CC.translate("&d[BedWars] &aSuccessfully connected to the database!"));
+ this.players = this.mongoDatabase.getCollection("players");
+ } catch (Exception exception) {
+ connected = false;
+ Bukkit.getConsoleSender().sendMessage(CC.translate("&d[BedWars] &cFailed to connect to the database!"));
+ exception.printStackTrace();
+ Bukkit.getPluginManager().disablePlugin(this.plugin);
+ Bukkit.getConsoleSender().sendMessage(CC.translate("&b[BedWars] &cDisabling BedWars..."));
+ }
+ }
+
+ public void disconnect() {
+ if (this.mongoClient != null) {
+ this.mongoClient.close();
+ this.connected = false;
+ Bukkit.getConsoleSender().sendMessage(CC.translate("&d[BedWars] &aSuccessfully disconnected from the database!"));
+ }
+ }
+}
diff --git a/src/main/java/rip/tilly/bedwars/player/PlayerData.java b/src/main/java/rip/tilly/bedwars/player/PlayerData.java
new file mode 100644
index 0000000..660b363
--- /dev/null
+++ b/src/main/java/rip/tilly/bedwars/player/PlayerData.java
@@ -0,0 +1,39 @@
+package rip.tilly.bedwars.player;
+
+import lombok.Getter;
+import lombok.Setter;
+import rip.tilly.bedwars.BedWars;
+
+import java.util.UUID;
+
+/**
+ * Created by Lucanius
+ * Project: BedWars
+ */
+@Getter
+@Setter
+public class PlayerData {
+
+ private final PlayerDataManager playerDataManager = BedWars.getInstance().getPlayerDataManager();
+ private PlayerState playerState = PlayerState.PLAYING;
+
+ private PlayerTeam playerTeam;
+
+ private final UUID uniqueId;
+ private boolean loaded;
+
+ private int kills;
+ private int deaths;
+ private int xp;
+ private int level;
+ private int wins;
+ private int losses;
+ private int gamesPlayed;
+
+ public PlayerData(UUID uniqueId) {
+ this.uniqueId = uniqueId;
+ this.loaded = false;
+
+ this.playerDataManager.loadPlayerData(this);
+ }
+}
diff --git a/src/main/java/rip/tilly/bedwars/player/PlayerDataHandler.java b/src/main/java/rip/tilly/bedwars/player/PlayerDataHandler.java
new file mode 100644
index 0000000..dada6b5
--- /dev/null
+++ b/src/main/java/rip/tilly/bedwars/player/PlayerDataHandler.java
@@ -0,0 +1,82 @@
+package rip.tilly.bedwars.player;
+
+import org.bukkit.Bukkit;
+import org.bukkit.entity.Player;
+import org.bukkit.event.EventHandler;
+import org.bukkit.event.EventPriority;
+import org.bukkit.event.Listener;
+import org.bukkit.event.player.*;
+import rip.tilly.bedwars.BedWars;
+
+/**
+ * Created by Lucanius
+ * Project: BedWars
+ */
+public class PlayerDataHandler implements Listener {
+
+ private final BedWars plugin = BedWars.getInstance();
+
+ @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
+ public void onAsyncPlayerPreLogin(AsyncPlayerPreLoginEvent event) {
+ Player player = Bukkit.getPlayer(event.getUniqueId());
+ if (player != null) {
+ if (player.isOnline()) {
+ event.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER);
+ event.setKickMessage("§cYou tried to login too quickly after disconnecting.\n§cTry again in a few seconds.");
+
+ this.plugin.getServer().getScheduler().runTask(this.plugin, () -> player.kickPlayer("§cDuplicate Login"));
+ return;
+ }
+
+ PlayerData playerData = this.plugin.getPlayerDataManager().getPlayerData(player.getUniqueId());
+ this.plugin.getPlayerDataManager().savePlayerData(playerData);
+ }
+ }
+
+ @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
+ public void onPlayerLogin(PlayerLoginEvent event) {
+ PlayerData playerData = this.plugin.getPlayerDataManager().getOrCreate(event.getPlayer().getUniqueId());
+ if (playerData == null) {
+ event.setResult(PlayerLoginEvent.Result.KICK_OTHER);
+ event.setKickMessage("§cAn error has occurred while loading your profile. Please reconnect.");
+ return;
+ }
+
+ if (!playerData.isLoaded()) {
+ this.plugin.getPlayerDataManager().savePlayerData(playerData);
+ event.setResult(PlayerLoginEvent.Result.KICK_OTHER);
+ event.setKickMessage("§cAn error has occurred while loading your profile. Please reconnect.");
+ }
+ }
+
+ @EventHandler
+ public void onPlayerQuit(PlayerQuitEvent event) {
+ event.setQuitMessage(null);
+
+ Player player = event.getPlayer();
+ PlayerData playerData = this.plugin.getPlayerDataManager().getPlayerData(player.getUniqueId());
+
+ handleDataSave(playerData);
+ }
+
+ @EventHandler
+ public void onPlayerKick(PlayerKickEvent event) {
+ event.setLeaveMessage(null);
+
+ Player player = event.getPlayer();
+ PlayerData playerData = this.plugin.getPlayerDataManager().getPlayerData(player.getUniqueId());
+
+ handleDataSave(playerData);
+ }
+
+ @EventHandler
+ public void onPlayerJoin(PlayerJoinEvent event) {
+ event.setJoinMessage(null);
+ }
+
+ private void handleDataSave(PlayerData playerData) {
+ if (playerData != null) {
+ this.plugin.getPlayerDataManager().deletePlayer(playerData.getUniqueId());
+ }
+ }
+}
diff --git a/src/main/java/rip/tilly/bedwars/player/PlayerDataManager.java b/src/main/java/rip/tilly/bedwars/player/PlayerDataManager.java
new file mode 100644
index 0000000..05009a9
--- /dev/null
+++ b/src/main/java/rip/tilly/bedwars/player/PlayerDataManager.java
@@ -0,0 +1,80 @@
+package rip.tilly.bedwars.player;
+
+import com.mongodb.client.MongoCursor;
+import com.mongodb.client.model.Filters;
+import com.mongodb.client.model.UpdateOptions;
+import lombok.Getter;
+import org.bson.Document;
+import rip.tilly.bedwars.BedWars;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+
+/**
+ * Created by Lucanius
+ * Project: BedWars
+ */
+public class PlayerDataManager {
+
+ @Getter private final Map players = new HashMap<>();
+
+ private final BedWars plugin = BedWars.getInstance();
+
+ public PlayerData getOrCreate(UUID uniqueId) {
+ return this.players.computeIfAbsent(uniqueId, PlayerData::new);
+ }
+
+ public PlayerData getPlayerData(UUID uniqueId) {
+ return this.players.getOrDefault(uniqueId, new PlayerData(uniqueId));
+ }
+
+ public Collection getAllPlayers() {
+ return this.players.values();
+ }
+
+ public void loadPlayerData(PlayerData playerData) {
+ Document document = this.plugin.getMongoManager().getPlayers().find(Filters.eq("uniqueId", playerData.getUniqueId().toString())).first();
+
+ if (document != null) {
+ playerData.setKills(document.getInteger("kills"));
+ playerData.setDeaths(document.getInteger("deaths"));
+ playerData.setXp(document.getInteger("xp"));
+ playerData.setLevel(document.getInteger("level"));
+ playerData.setWins(document.getInteger("wins"));
+ playerData.setLosses(document.getInteger("losses"));
+ playerData.setGamesPlayed(document.getInteger("gamesPlayed"));
+ }
+
+ playerData.setLoaded(true);
+ }
+
+ public void savePlayerData(PlayerData playerData) {
+ Document document = new Document();
+
+ document.put("uniqueId", playerData.getUniqueId().toString());
+
+ document.put("kills", playerData.getKills());
+ document.put("deaths", playerData.getDeaths());
+ document.put("xp", playerData.getXp());
+ document.put("level", playerData.getLevel());
+ document.put("wins", playerData.getWins());
+ document.put("losses", playerData.getLosses());
+ document.put("gamesPlayed", playerData.getGamesPlayed());
+
+ this.plugin.getMongoManager().getPlayers().replaceOne(Filters.eq("uniqueId", playerData.getUniqueId().toString()), document, new UpdateOptions().upsert(true));
+ }
+
+ public void deletePlayer(UUID uniqueId) {
+ this.savePlayerData(getPlayerData(uniqueId));
+ this.getPlayers().remove(uniqueId);
+ }
+
+ public MongoCursor getPlayersSorted(String stat, int limit) {
+ final Document document = new Document();
+ document.put(stat, -1);
+
+ return this.plugin.getMongoManager().getPlayers().find().sort(document).limit(limit).iterator();
+ }
+}
diff --git a/src/main/java/rip/tilly/bedwars/player/PlayerState.java b/src/main/java/rip/tilly/bedwars/player/PlayerState.java
new file mode 100644
index 0000000..a26f815
--- /dev/null
+++ b/src/main/java/rip/tilly/bedwars/player/PlayerState.java
@@ -0,0 +1,10 @@
+package rip.tilly.bedwars.player;
+
+/**
+ * Created by Lucanius
+ * Project: BedWars
+ */
+public enum PlayerState {
+ PLAYING,
+ SPECTATING
+}
diff --git a/src/main/java/rip/tilly/bedwars/player/PlayerTeam.java b/src/main/java/rip/tilly/bedwars/player/PlayerTeam.java
new file mode 100644
index 0000000..da5e943
--- /dev/null
+++ b/src/main/java/rip/tilly/bedwars/player/PlayerTeam.java
@@ -0,0 +1,46 @@
+package rip.tilly.bedwars.player;
+
+import lombok.Getter;
+import org.bukkit.ChatColor;
+import org.bukkit.Color;
+
+import java.util.Arrays;
+
+/**
+ * Created by Lucanius
+ * Project: BedWars
+ */
+@Getter
+public enum PlayerTeam {
+
+ WHITE("White", Color.WHITE, ChatColor.WHITE),
+ SILVER("Silver", Color.SILVER, ChatColor.GRAY),
+ GRAY("Gray", Color.GRAY, ChatColor.DARK_GRAY),
+ BLACK("Black", Color.BLACK, ChatColor.BLACK),
+ BLUE("Blue", Color.BLUE, ChatColor.BLUE),
+ NAVY("Navy", Color.NAVY, ChatColor.DARK_BLUE),
+ CYAN("Cyan", Color.TEAL, ChatColor.DARK_AQUA),
+ AQUA("Aqua", Color.AQUA, ChatColor.AQUA),
+ LIME("Lime", Color.LIME, ChatColor.GREEN),
+ GREEN("Green", Color.GREEN, ChatColor.DARK_GREEN),
+ YELLOW("Yellow", Color.YELLOW, ChatColor.YELLOW),
+ ORANGE("Orange", Color.ORANGE, ChatColor.GOLD),
+ RED("Red", Color.RED, ChatColor.RED),
+ MAROON("Maroon", Color.MAROON, ChatColor.DARK_RED),
+ PURPLE("Purple", Color.PURPLE, ChatColor.DARK_PURPLE),
+ PINK("Pink", Color.FUCHSIA, ChatColor.LIGHT_PURPLE);
+
+ private final String name;
+ private final Color color;
+ private final ChatColor chatColor;
+
+ PlayerTeam(String name, Color color, ChatColor chatColor) {
+ this.name = name;
+ this.color = color;
+ this.chatColor = chatColor;
+ }
+
+ public static PlayerTeam getFromName(String name) {
+ return Arrays.stream(values()).filter((team) -> team.name.equalsIgnoreCase(name) || team.getName().equalsIgnoreCase(name)).findFirst().orElse(null);
+ }
+}
diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml
index e69de29..439b71b 100644
--- a/src/main/resources/config.yml
+++ b/src/main/resources/config.yml
@@ -0,0 +1,9 @@
+MONGO:
+ HOST: "127.0.0.1"
+ PORT: 27017
+ DATABASE: "bedwars"
+ AUTH:
+ ENABLED: false
+ USERNAME: "user"
+ PASSWORD: "pass"
+ AUTH-DATABASE: "admin"
\ No newline at end of file