Some world management system with multiverse. Also beginning to use dungeons/gates. Need to build maps to use in dungeons.
255 lines
12 KiB
Java
255 lines
12 KiB
Java
package gq.unurled.raxen.config;
|
|
|
|
import com.mongodb.client.MongoCollection;
|
|
import com.mongodb.client.model.Filters;
|
|
import gq.unurled.raxen.Raxen;
|
|
import gq.unurled.raxen.components.entity.Attributes.Attribute;
|
|
import gq.unurled.raxen.components.entity.player.Inventories;
|
|
import gq.unurled.raxen.components.entity.player.PlayerUtils;
|
|
import gq.unurled.raxen.components.entity.player.Storage;
|
|
import gq.unurled.raxen.components.entity.player.attributes.Attributes;
|
|
import gq.unurled.raxen.components.entity.player.storages.EnderChest;
|
|
import gq.unurled.raxen.manager.entity.PlayerManager;
|
|
import gq.unurled.raxen.manager.entity.StorageManager;
|
|
import gq.unurled.raxen.utils.Items;
|
|
import gq.unurled.raxen.utils.libs.MongoDB;
|
|
import net.kyori.adventure.text.Component;
|
|
import org.bson.Document;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.Material;
|
|
import org.bukkit.configuration.file.FileConfiguration;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.event.inventory.InventoryType;
|
|
import org.bukkit.inventory.Inventory;
|
|
import org.bukkit.inventory.ItemStack;
|
|
import org.bukkit.inventory.PlayerInventory;
|
|
import org.bukkit.scheduler.BukkitRunnable;
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
|
|
import static gq.unurled.raxen.utils.Items.listItemStackDeserilize;
|
|
import static gq.unurled.raxen.utils.Items.setItemsToInventory;
|
|
import static gq.unurled.raxen.utils.Utils.debug;
|
|
|
|
public class PlayerConfig {
|
|
|
|
private static Raxen main;
|
|
private MongoDB mongoDB;
|
|
private MongoCollection<Document> mongoCollection;
|
|
private static PlayerManager playerManager;
|
|
private static StorageManager sto;
|
|
private Attributes attributes;
|
|
|
|
public PlayerConfig(Raxen main) {
|
|
this.main = main;
|
|
sto = main.getManager().getStorageManager();
|
|
this.mongoDB = main.getManager().getLibsManager().getMongoDB();
|
|
this.mongoCollection = mongoDB.getMongoCollection();
|
|
playerManager = main.getManager().getPlayerManager();
|
|
this.attributes = new Attributes(main);
|
|
}
|
|
|
|
public void close() {
|
|
mongoDB.close();
|
|
}
|
|
|
|
public void saveUsingMongoDB(@NotNull Player player, Attributes attributes, String invstr, Integer reverse) {
|
|
debug(main, "Saving " + player.getName() + "'s data!");
|
|
Document doc = new Document("uuid", player.getUniqueId().toString())
|
|
.append("name", player.getName())
|
|
.append("inv", invstr);
|
|
HashMap<String, Attribute> attribute = main.getManager().getPlayerManager().getAttribute();
|
|
for (String s : attribute.keySet()) {
|
|
debug("saving " + s);
|
|
doc.append(s, attribute.get(s).get(player));
|
|
}
|
|
if (playerManager.getRaxenPlayer(player).getStorage() != null) {
|
|
if(playerManager.getRaxenPlayer(player).getStorage().getEc() != null) {
|
|
debug(main, "Saving " + player.getName() + "'s storage!");
|
|
EnderChest ec = playerManager.getRaxenPlayer(player).getStorage().getEc();
|
|
List<String> list = new ArrayList<String>();
|
|
reverse = 0;
|
|
for(ItemStack it : ec.getEc()) {
|
|
if(it != null && it.getType() != Material.AIR) {
|
|
String s = Items.itemStackSerilize(it, reverse);
|
|
list.add(s);
|
|
}
|
|
reverse += 1;
|
|
}
|
|
String ecstr = Items.listItemStackSerelize(list);
|
|
doc.append("ec", ecstr);
|
|
}
|
|
}
|
|
Document playerDoc = mongoCollection.find(Filters.eq("uuid", player.getUniqueId().toString())).first();
|
|
if(playerDoc == null) {
|
|
mongoCollection.insertOne(doc);
|
|
debug(main, "insert one");
|
|
}
|
|
else {
|
|
mongoCollection.replaceOne(Filters.eq("uuid", player.getUniqueId().toString()), doc);
|
|
debug(main, "replace one");
|
|
}
|
|
}
|
|
|
|
public void saveUsingYml(Player player, Attributes attributes, String invstr, Integer reverse) {
|
|
FileConfiguration config = sto.createYml(player);
|
|
config.set("name", player.getName());
|
|
HashMap<String, Attribute> attribute = main.getManager().getPlayerManager().getAttribute();
|
|
for (String s : attribute.keySet()) {
|
|
config.set(s, attribute.get(s).get(player));
|
|
}
|
|
config.set("inv", invstr);
|
|
if (playerManager.getRaxenPlayer(player).getStorage() != null) {
|
|
if(playerManager.getRaxenPlayer(player).getStorage().getEc() != null) {
|
|
EnderChest ec = playerManager.getRaxenPlayer(player).getStorage().getEc();
|
|
List<String> list = new ArrayList<String>();
|
|
reverse = 0;
|
|
for(ItemStack it : ec.getEc()) {
|
|
reverse += 1;
|
|
if(it != null) {
|
|
String s = Items.itemStackSerilize(it, reverse);
|
|
list.add(s);
|
|
}
|
|
}
|
|
String ecstr = Items.listItemStackSerelize(list);
|
|
config.set("ec", ecstr);
|
|
}
|
|
}
|
|
try {
|
|
config.save(main.getDataFolder() + "/playerInfo/" + player.getUniqueId() + "/" + "playerInfo.yml");
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public void loadUsingMongoDB(@NotNull Player player) {
|
|
Document playerDoc = mongoCollection.find(Filters.eq("uuid", player.getUniqueId().toString())).first();
|
|
if(playerDoc == null) {
|
|
debug(main, "Intiatilising Data for Player: " + player.getName());
|
|
playerManager.getRaxenPlayer(player).createNewAttribute();
|
|
playerManager.getRaxenPlayer(player).createStorage();
|
|
playerManager.getRaxenPlayer(player).createInventory();
|
|
PlayerUtils.updateSkills(main, player);
|
|
return;
|
|
}
|
|
debug(main, "Loading " + player.getName() + "'s data!");
|
|
attributes.setAttributes(player, playerDoc.getInteger("MAX_HEALTH"), playerDoc.getInteger("HEALTH"), playerDoc.getInteger("ITEM_HEALTH"),
|
|
playerDoc.getInteger("DEFENSE"), playerDoc.getInteger("ITEM_DEFENSE"),
|
|
playerDoc.getInteger("SPEED"), playerDoc.getInteger("ITEM_SPEED"),
|
|
playerDoc.getInteger("STRENGTH"), playerDoc.getInteger("ITEM_STRENGTH"),
|
|
playerDoc.getInteger("MAX_MANA"), playerDoc.getInteger("MANA"), playerDoc.getInteger("ITEM_MANA"),
|
|
playerDoc.getInteger("LUCK"), playerDoc.getInteger("ITEM_LUCK"));
|
|
Inventory ec = Bukkit.createInventory(null, 54, Component.text("Ender Chest"));
|
|
if(playerDoc.getString("ec") != null) {
|
|
debug(main, "Loading " + player.getName() + "'s storage.");
|
|
ec = setItemsToInventory(listItemStackDeserilize(playerDoc.getString("ec")), ec);
|
|
}
|
|
playerManager.getRaxenPlayer(player).setStorage(ec);
|
|
PlayerInventory inv = player.getInventory();
|
|
inv = setItemsToInventory(listItemStackDeserilize(playerDoc.getString("inv")), inv);
|
|
player.getInventory().setContents(inv.getContents());
|
|
player.updateInventory();
|
|
gq.unurled.raxen.components.entity.player.storages.Inventory invv = new gq.unurled.raxen.components.entity.player.storages.Inventory(inv);
|
|
Inventories invvv = new Inventories(invv);
|
|
playerManager.getRaxenPlayer(player).setInventory(inv);
|
|
PlayerUtils.updateSkills(main, player);
|
|
}
|
|
|
|
public void loadUsingYml(Player player) throws IOException {
|
|
FileConfiguration config = sto.createYml(player);
|
|
attributes.setAttributes(player, config.getInt("maxHealth"), config.getInt("health"), config.getInt("itemHealth"),
|
|
config.getInt("defense"), config.getInt("itemDefense"),
|
|
config.getInt("speed"), config.getInt("itemSpeed"),
|
|
config.getInt("strength"), config.getInt("itemStrength"),
|
|
config.getInt("maxMana"), config.getInt("mana"), config.getInt("itemMana"),
|
|
config.getInt("luck"), config.getInt("itemLuck"));
|
|
Inventory ec = Bukkit.createInventory(null, 54, Component.text("Ender Chest"));
|
|
if((String) config.get("ec") != null) {
|
|
ec = setItemsToInventory(listItemStackDeserilize((String) config.get("ec")), ec);
|
|
}
|
|
EnderChest ecc = new EnderChest(ec);
|
|
Storage storage = new Storage(ecc);
|
|
playerManager.getRaxenPlayer(player).setStorage(ec);
|
|
Inventory inv = Bukkit.createInventory(player, InventoryType.PLAYER);
|
|
inv = setItemsToInventory(listItemStackDeserilize((String) config.getString("inv")), inv);
|
|
player.getInventory().setContents(inv.getContents());
|
|
player.updateInventory();
|
|
gq.unurled.raxen.components.entity.player.storages.Inventory invv = new gq.unurled.raxen.components.entity.player.storages.Inventory(inv);
|
|
Inventories invvv = new Inventories(invv);
|
|
playerManager.getRaxenPlayer(player).setInventory(inv);
|
|
}
|
|
|
|
public void savePlayerConfig(@NotNull Player player) {
|
|
Attributes attributes = new Attributes(main);
|
|
Inventory inv = player.getInventory();
|
|
List<String> listInv = new ArrayList<String>();
|
|
Integer reverse = 0;
|
|
for(ItemStack it : inv) {
|
|
reverse = reverse + 1;
|
|
if(it != null && it.getType() != Material.AIR) {
|
|
String s = Items.itemStackSerilize(it, reverse);
|
|
listInv.add(s);
|
|
}
|
|
}
|
|
String invstr= "";
|
|
if (listInv.size() > 0) {
|
|
invstr = Items.listItemStackSerelize(listInv);
|
|
}
|
|
debug(main, "saving...");
|
|
String storage = sto.getConfig().getString("storage");
|
|
Integer finalReverse = reverse;
|
|
String finalInvstr = invstr;
|
|
new BukkitRunnable() {
|
|
@Override
|
|
public void run() {
|
|
saveUsingMongoDB(player, attributes, finalInvstr, finalReverse);
|
|
}
|
|
}.runTaskAsynchronously(main);
|
|
debug(main, "using mongo");
|
|
if (storage == "MYSQL" || storage == "YML") {
|
|
String finalInvstr1 = invstr;
|
|
Integer finalReverse1 = reverse;
|
|
new BukkitRunnable() {
|
|
@Override
|
|
public void run() {
|
|
saveUsingYml(player, attributes, finalInvstr1, finalReverse1);
|
|
}
|
|
}.runTaskAsynchronously(main);
|
|
debug(main, "using yml");
|
|
}
|
|
|
|
debug(main, "Player: " + player.getName() + " data successfully saved!");
|
|
}
|
|
|
|
public void loadPlayerConfig(Player player) {
|
|
String st = StorageManager.getConfig().getString("storage");
|
|
String value = "";
|
|
if (st.equals("MONGODB")) {
|
|
new BukkitRunnable() {
|
|
@Override
|
|
public void run() {
|
|
loadUsingMongoDB(player);
|
|
}
|
|
}.runTaskAsynchronously(main);
|
|
value = "mongodb";
|
|
} else {
|
|
value = "yml";
|
|
new BukkitRunnable() {
|
|
@Override
|
|
public void run() {
|
|
try {
|
|
loadUsingYml(player);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}.runTaskAsynchronously(main);
|
|
}
|
|
debug(main, "Player: " + player.getName() + " data successfully loaded using " + value);
|
|
}
|
|
|
|
}
|