0.0.2: Adding support to YML
This commit is contained in:
parent
a8e7312f45
commit
8a8829fbf3
6 changed files with 177 additions and 28 deletions
2
pom.xml
2
pom.xml
|
@ -6,7 +6,7 @@
|
||||||
|
|
||||||
<groupId>gq.unurled</groupId>
|
<groupId>gq.unurled</groupId>
|
||||||
<artifactId>raxen</artifactId>
|
<artifactId>raxen</artifactId>
|
||||||
<version>0.0.1</version>
|
<version>0.0.2</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>Raxen</name>
|
<name>Raxen</name>
|
||||||
|
|
|
@ -24,13 +24,14 @@ public final class Raxen extends JavaPlugin {
|
||||||
private static final String version = "0.0.1";
|
private static final String version = "0.0.1";
|
||||||
private final PluginManager pm = getServer().getPluginManager();
|
private final PluginManager pm = getServer().getPluginManager();
|
||||||
|
|
||||||
|
@Getter
|
||||||
private static Raxen plugin;
|
private static Raxen plugin;
|
||||||
|
|
||||||
private static Logger logger;
|
private static Logger logger;
|
||||||
|
|
||||||
private static MongoDB mongoDB;
|
|
||||||
|
|
||||||
private static Config config;
|
private static Config config;
|
||||||
|
@Getter
|
||||||
|
private static StorageManager storageManager;
|
||||||
|
|
||||||
private static PlayerManager playerManager;
|
private static PlayerManager playerManager;
|
||||||
private static ProfileManager profileManager;
|
private static ProfileManager profileManager;
|
||||||
|
@ -50,7 +51,8 @@ public final class Raxen extends JavaPlugin {
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
plugin = this;
|
plugin = this;
|
||||||
logger = getLogger();
|
logger = getLogger();
|
||||||
mongoDB = new MongoDB();
|
config = new Config(this);
|
||||||
|
storageManager = new StorageManager(this);
|
||||||
//config = new Config(this);
|
//config = new Config(this);
|
||||||
|
|
||||||
profileManager = new ProfileManager(this);
|
profileManager = new ProfileManager(this);
|
||||||
|
@ -112,10 +114,6 @@ public final class Raxen extends JavaPlugin {
|
||||||
return playerManager;
|
return playerManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static MongoDB getMongoDB() {
|
|
||||||
return mongoDB;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static PlayerConfig getPlayerConfig() {
|
public static PlayerConfig getPlayerConfig() {
|
||||||
return playerConfig;
|
return playerConfig;
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@ public class Config {
|
||||||
public Config(Raxen main) {
|
public Config(Raxen main) {
|
||||||
this.main = main;
|
this.main = main;
|
||||||
this.config = main.getConfig();
|
this.config = main.getConfig();
|
||||||
|
this.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void init() {
|
public void init() {
|
||||||
|
|
|
@ -9,17 +9,20 @@ import gq.unurled.raxen.components.player.Skills;
|
||||||
import gq.unurled.raxen.components.player.Storage;
|
import gq.unurled.raxen.components.player.Storage;
|
||||||
import gq.unurled.raxen.components.player.Storages.EnderChest;
|
import gq.unurled.raxen.components.player.Storages.EnderChest;
|
||||||
import gq.unurled.raxen.manager.ProfileManager;
|
import gq.unurled.raxen.manager.ProfileManager;
|
||||||
|
import gq.unurled.raxen.manager.StorageManager;
|
||||||
import gq.unurled.raxen.utils.MongoDB;
|
import gq.unurled.raxen.utils.MongoDB;
|
||||||
import gq.unurled.raxen.utils.Utils;
|
import gq.unurled.raxen.utils.Utils;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.inventory.InventoryType;
|
import org.bukkit.event.inventory.InventoryType;
|
||||||
import org.bukkit.inventory.Inventory;
|
import org.bukkit.inventory.Inventory;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -27,31 +30,21 @@ import static gq.unurled.raxen.utils.Utils.log;
|
||||||
|
|
||||||
public class PlayerConfig {
|
public class PlayerConfig {
|
||||||
|
|
||||||
private Raxen main;
|
private static Raxen main;
|
||||||
private MongoDB mongoDB;
|
private MongoDB mongoDB;
|
||||||
private MongoCollection<Document> mongoCollection;
|
private MongoCollection<Document> mongoCollection;
|
||||||
private ProfileManager profileManager;
|
private static ProfileManager profileManager;
|
||||||
|
private static StorageManager sto;
|
||||||
|
|
||||||
public PlayerConfig(Raxen main) {
|
public PlayerConfig(Raxen main) {
|
||||||
this.main = main;
|
this.main = main;
|
||||||
this.mongoDB = main.getMongoDB();
|
this.sto = main.getStorageManager();
|
||||||
|
this.mongoDB = sto.getMongo();
|
||||||
this.mongoCollection = mongoDB.getMongoCollection();
|
this.mongoCollection = mongoDB.getMongoCollection();
|
||||||
this.profileManager = main.getProfileManager();
|
this.profileManager = main.getProfileManager();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void savePlayerConfig(Player player) {
|
public void saveUsingMongoDB(Player player, Skills skills, String invstr, Integer reverse) {
|
||||||
Skills skills = profileManager.getPlayerProfile(player.getUniqueId()).getSkils();
|
|
||||||
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 = Utils.itemStackSerilize(it, reverse);
|
|
||||||
listInv.add(s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
String invstr = Utils.listItemStackSerelize(listInv);
|
|
||||||
Document doc = new Document("uuid", player.getUniqueId().toString())
|
Document doc = new Document("uuid", player.getUniqueId().toString())
|
||||||
.append("name", player.getName())
|
.append("name", player.getName())
|
||||||
.append("health", skills.getHealth())
|
.append("health", skills.getHealth())
|
||||||
|
@ -82,10 +75,36 @@ public class PlayerConfig {
|
||||||
else {
|
else {
|
||||||
mongoCollection.replaceOne(Filters.eq("uuid", player.getUniqueId().toString()), doc);
|
mongoCollection.replaceOne(Filters.eq("uuid", player.getUniqueId().toString()), doc);
|
||||||
}
|
}
|
||||||
log("Player: " + player.getName() + " data successfully saved!");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadPlayerConfig(Player player) {
|
public void saveUsingYml(Player player, Skills skills, String invstr, Integer reverse) throws IOException {
|
||||||
|
FileConfiguration config = sto.createYml(player);
|
||||||
|
config.set("name", player.getName());
|
||||||
|
config.set("health", skills.getHealth());
|
||||||
|
config.set("strength", skills.getStrength());
|
||||||
|
config.set("speed", skills.getSpeed());
|
||||||
|
config.set("defense", skills.getDefense());
|
||||||
|
config.set("inv", invstr);
|
||||||
|
if (profileManager.getPlayerStorage(player.getUniqueId()) != null) {
|
||||||
|
if(profileManager.getPlayerStorage(player.getUniqueId()).getEc() != null) {
|
||||||
|
EnderChest ec = profileManager.getPlayerStorage(player.getUniqueId()).getEc();
|
||||||
|
List<String> list = new ArrayList<String>();
|
||||||
|
reverse = 0;
|
||||||
|
for(ItemStack it : ec.getEc()) {
|
||||||
|
reverse += 1;
|
||||||
|
if(it != null) {
|
||||||
|
String s = Utils.itemStackSerilize(it, reverse);
|
||||||
|
list.add(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String ecstr = Utils.listItemStackSerelize(list);
|
||||||
|
config.set("ec", ecstr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
config.save(main.getDataFolder() + "/" + player.getUniqueId() + "/" + "playerInfo.yml");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void loadUsingMongoDB(Player player) {
|
||||||
Document playerDoc = mongoCollection.find(Filters.eq("uuid", player.getUniqueId().toString())).first();
|
Document playerDoc = mongoCollection.find(Filters.eq("uuid", player.getUniqueId().toString())).first();
|
||||||
if(playerDoc == null) {
|
if(playerDoc == null) {
|
||||||
profileManager.createNewProfile(player);
|
profileManager.createNewProfile(player);
|
||||||
|
@ -95,6 +114,7 @@ public class PlayerConfig {
|
||||||
}
|
}
|
||||||
Skills skills = new Skills(playerDoc.getInteger("health"), playerDoc.getInteger("defense"), playerDoc.getInteger("speed"), playerDoc.getInteger("strength"));
|
Skills skills = new Skills(playerDoc.getInteger("health"), playerDoc.getInteger("defense"), playerDoc.getInteger("speed"), playerDoc.getInteger("strength"));
|
||||||
Profile profile = new Profile(skills);
|
Profile profile = new Profile(skills);
|
||||||
|
|
||||||
profileManager.setPlayerProfile(player.getUniqueId(), profile);
|
profileManager.setPlayerProfile(player.getUniqueId(), profile);
|
||||||
Inventory ec = Bukkit.createInventory(null, 54, Component.text("Ender Chest"));
|
Inventory ec = Bukkit.createInventory(null, 54, Component.text("Ender Chest"));
|
||||||
if(playerDoc.getString("ec") != null) {
|
if(playerDoc.getString("ec") != null) {
|
||||||
|
@ -111,8 +131,67 @@ public class PlayerConfig {
|
||||||
gq.unurled.raxen.components.player.Storages.Inventory invv = new gq.unurled.raxen.components.player.Storages.Inventory(inv);
|
gq.unurled.raxen.components.player.Storages.Inventory invv = new gq.unurled.raxen.components.player.Storages.Inventory(inv);
|
||||||
Inventories invvv = new Inventories(invv);
|
Inventories invvv = new Inventories(invv);
|
||||||
profileManager.setPlayerInventory(player.getUniqueId(), invvv);
|
profileManager.setPlayerInventory(player.getUniqueId(), invvv);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void loadUsingYml(Player player) {
|
||||||
|
FileConfiguration config = sto.createYml(player);
|
||||||
|
Skills skills = new Skills((Integer) config.get("health"), (Integer) config.get("defense"), (Integer) config.get("speed"), (Integer) config.get("strength"));
|
||||||
|
Profile profile = new Profile(skills);
|
||||||
|
profileManager.setPlayerProfile(player.getUniqueId(), profile);
|
||||||
|
|
||||||
|
Inventory ec = Bukkit.createInventory(null, 54, Component.text("Ender Chest"));
|
||||||
|
if((String) config.get("ec") != null) {
|
||||||
|
ItemStack[] itList = Utils.listItemStackDeserilize((String) config.get("ec"));
|
||||||
|
ec.addItem(itList);
|
||||||
|
}
|
||||||
|
EnderChest ecc = new EnderChest(ec);
|
||||||
|
Storage storage = new Storage(ecc);
|
||||||
|
profileManager.setPlayerStorage(player.getUniqueId(), storage);
|
||||||
|
Inventory inv = Bukkit.createInventory(player, InventoryType.PLAYER);
|
||||||
|
ItemStack[] itListInv = Utils.listItemStackDeserilize((String) config.getString("inv"));
|
||||||
|
inv.addItem(itListInv);
|
||||||
|
player.updateInventory();
|
||||||
|
gq.unurled.raxen.components.player.Storages.Inventory invv = new gq.unurled.raxen.components.player.Storages.Inventory(inv);
|
||||||
|
Inventories invvv = new Inventories(invv);
|
||||||
|
profileManager.setPlayerInventory(player.getUniqueId(), invvv);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void savePlayerConfig(Player player) {
|
||||||
|
Skills skills = profileManager.getPlayerProfile(player.getUniqueId()).getSkils();
|
||||||
|
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 = Utils.itemStackSerilize(it, reverse);
|
||||||
|
listInv.add(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String invstr = Utils.listItemStackSerelize(listInv);
|
||||||
|
|
||||||
|
|
||||||
|
switch(sto.getCurrently()) {
|
||||||
|
case "MONGODB":
|
||||||
|
saveUsingMongoDB(player, skills, invstr, reverse);
|
||||||
|
case "MYSQL":
|
||||||
|
case "YML":
|
||||||
|
saveUsingMongoDB(player, skills, invstr, reverse);
|
||||||
|
}
|
||||||
|
|
||||||
|
log("Player: " + player.getName() + " data successfully saved!");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void loadPlayerConfig(Player player) {
|
||||||
|
switch(sto.getCurrently()) {
|
||||||
|
case "MONGODB":
|
||||||
|
loadUsingMongoDB(player);
|
||||||
|
case "MYSQL":
|
||||||
|
case "YML":
|
||||||
|
loadUsingYml(player);
|
||||||
|
}
|
||||||
|
|
||||||
log("Player: " + player.getName() + " data successfully loaded!");
|
log("Player: " + player.getName() + " data successfully loaded!");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
69
src/main/java/gq/unurled/raxen/manager/StorageManager.java
Normal file
69
src/main/java/gq/unurled/raxen/manager/StorageManager.java
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
package gq.unurled.raxen.manager;
|
||||||
|
|
||||||
|
import gq.unurled.raxen.Raxen;
|
||||||
|
import gq.unurled.raxen.utils.MongoDB;
|
||||||
|
import lombok.Getter;
|
||||||
|
import org.bukkit.configuration.InvalidConfigurationException;
|
||||||
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
|
import static gq.unurled.raxen.utils.Utils.error;
|
||||||
|
import static gq.unurled.raxen.utils.Utils.log;
|
||||||
|
|
||||||
|
public class StorageManager {
|
||||||
|
|
||||||
|
private static Raxen main;
|
||||||
|
private static FileConfiguration config;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private static MongoDB mongo;
|
||||||
|
@Getter
|
||||||
|
private static String currently;
|
||||||
|
|
||||||
|
public StorageManager(Raxen main) {
|
||||||
|
this.main = main.getPlugin();
|
||||||
|
this.config = main.getConfig();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void connect() {
|
||||||
|
String s = (String) config.get("storage");
|
||||||
|
switch(s) {
|
||||||
|
case "MONGODB":
|
||||||
|
mongo.connect();
|
||||||
|
currently = s;
|
||||||
|
case "MYSQL":
|
||||||
|
error("Currently not implemented, switching to YML");
|
||||||
|
currently = "YML";
|
||||||
|
case "YML":
|
||||||
|
currently = s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static FileConfiguration createYml(Player player) {
|
||||||
|
String name = player.getName();
|
||||||
|
|
||||||
|
File customFile;
|
||||||
|
FileConfiguration customConfig;
|
||||||
|
log(main.getDataFolder() + "/" + player.getUniqueId() + "custom.yml");
|
||||||
|
customFile = new File(main.getDataFolder() + "/" + player.getUniqueId(), "playerInfo.yml");
|
||||||
|
|
||||||
|
if (!customFile.exists()) {
|
||||||
|
customFile.getParentFile().mkdirs();
|
||||||
|
main.saveResource("playerInfo.yml", false);
|
||||||
|
}
|
||||||
|
|
||||||
|
customConfig = new YamlConfiguration();
|
||||||
|
try {
|
||||||
|
customConfig.load(customFile);
|
||||||
|
} catch (IOException | InvalidConfigurationException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return customConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,3 +1,5 @@
|
||||||
version: "${project.version}"
|
version: "${project.version}"
|
||||||
motd: "YAY, Super server!"
|
motd: "YAY, Super server!"
|
||||||
motd-reload: "Server reloading!"
|
motd-reload: "Server reloading!"
|
||||||
|
|
||||||
|
storage: "MONGODB" #switch between "MONGODB", "MYSQL"(not implemented), "YML"(currzently making it)
|
Loading…
Reference in a new issue