PlayerData & MongoManager
This commit is contained in:
parent
b1a4f07a76
commit
6dcc92da42
@ -4,8 +4,16 @@
|
||||
<option name="autoReloadType" value="SELECTIVE" />
|
||||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="998d5e92-ac1f-4423-8aef-5f389ab213de" name="Changes" comment="">
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/rip/tilly/bedwars/utils/Utils.java" beforeDir="false" />
|
||||
<list default="true" id="5f649e9a-d936-4370-9e5c-062bcd7995b7" name="Changes" comment="">
|
||||
<change afterPath="$PROJECT_DIR$/src/main/java/rip/tilly/bedwars/mongo/MongoManager.java" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/src/main/java/rip/tilly/bedwars/player/PlayerData.java" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/src/main/java/rip/tilly/bedwars/player/PlayerDataHandler.java" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/src/main/java/rip/tilly/bedwars/player/PlayerDataManager.java" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/src/main/java/rip/tilly/bedwars/player/PlayerState.java" afterDir="false" />
|
||||
<change afterPath="$PROJECT_DIR$/src/main/java/rip/tilly/bedwars/player/PlayerTeam.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/rip/tilly/bedwars/BedWars.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/rip/tilly/bedwars/BedWars.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/resources/config.yml" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/resources/config.yml" afterDir="false" />
|
||||
</list>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
@ -31,11 +39,15 @@
|
||||
<component name="PropertiesComponent">
|
||||
<property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
|
||||
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
|
||||
<property name="last_opened_file_path" value="$PROJECT_DIR$/../Frozedcade" />
|
||||
<property name="last_opened_file_path" value="$PROJECT_DIR$/src/main/java/rip/tilly/bedwars" />
|
||||
<property name="settings.editor.selected.configurable" value="MTConfigurable" />
|
||||
</component>
|
||||
<component name="RecentsManager">
|
||||
<key name="CopyFile.RECENT_KEYS">
|
||||
<recent name="C:\Users\Luca\Documents\GitHub\BedWars\src\main\java\rip\tilly\bedwars" />
|
||||
</key>
|
||||
<key name="CopyClassDialog.RECENTS_KEY">
|
||||
<recent name="rip.tilly.bedwars.mongo" />
|
||||
<recent name="rip.tilly.bedwars.utils" />
|
||||
</key>
|
||||
</component>
|
||||
@ -53,6 +65,7 @@
|
||||
<option name="outputLevel" value="INFO" />
|
||||
<option name="pluginUpdatePolicy" value="DEFAULT" />
|
||||
<option name="printErrorStackTraces" value="false" />
|
||||
<option name="showDialogWithAdvancedSettings" value="false" />
|
||||
<option name="threads" />
|
||||
<option name="usePluginRegistry" value="false" />
|
||||
<option name="userSettingsFile" value="" />
|
||||
@ -119,5 +132,6 @@
|
||||
</entry>
|
||||
</map>
|
||||
</option>
|
||||
<option name="oldMeFiltersMigrated" value="true" />
|
||||
</component>
|
||||
</project>
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
72
src/main/java/rip/tilly/bedwars/mongo/MongoManager.java
Normal file
72
src/main/java/rip/tilly/bedwars/mongo/MongoManager.java
Normal file
@ -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<Document> 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!"));
|
||||
}
|
||||
}
|
||||
}
|
39
src/main/java/rip/tilly/bedwars/player/PlayerData.java
Normal file
39
src/main/java/rip/tilly/bedwars/player/PlayerData.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
@ -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<UUID, PlayerData> 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<PlayerData> 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<Document> 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();
|
||||
}
|
||||
}
|
10
src/main/java/rip/tilly/bedwars/player/PlayerState.java
Normal file
10
src/main/java/rip/tilly/bedwars/player/PlayerState.java
Normal file
@ -0,0 +1,10 @@
|
||||
package rip.tilly.bedwars.player;
|
||||
|
||||
/**
|
||||
* Created by Lucanius
|
||||
* Project: BedWars
|
||||
*/
|
||||
public enum PlayerState {
|
||||
PLAYING,
|
||||
SPECTATING
|
||||
}
|
46
src/main/java/rip/tilly/bedwars/player/PlayerTeam.java
Normal file
46
src/main/java/rip/tilly/bedwars/player/PlayerTeam.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
MONGO:
|
||||
HOST: "127.0.0.1"
|
||||
PORT: 27017
|
||||
DATABASE: "bedwars"
|
||||
AUTH:
|
||||
ENABLED: false
|
||||
USERNAME: "user"
|
||||
PASSWORD: "pass"
|
||||
AUTH-DATABASE: "admin"
|
Loading…
Reference in New Issue
Block a user