forked from Upstream/mmocore
Fixed /mmocore admin exportdata
This commit is contained in:
parent
235b7bbe27
commit
dea5ce3afd
@ -1,6 +1,5 @@
|
|||||||
package net.Indyuce.mmocore.api.player;
|
package net.Indyuce.mmocore.api.player;
|
||||||
|
|
||||||
import com.google.gson.JsonObject;
|
|
||||||
import io.lumine.mythic.lib.MythicLib;
|
import io.lumine.mythic.lib.MythicLib;
|
||||||
import io.lumine.mythic.lib.api.player.MMOPlayerData;
|
import io.lumine.mythic.lib.api.player.MMOPlayerData;
|
||||||
import io.lumine.mythic.lib.player.TemporaryPlayerData;
|
import io.lumine.mythic.lib.player.TemporaryPlayerData;
|
||||||
@ -30,7 +29,6 @@ import net.Indyuce.mmocore.experience.droptable.ExperienceItem;
|
|||||||
import net.Indyuce.mmocore.experience.droptable.ExperienceTable;
|
import net.Indyuce.mmocore.experience.droptable.ExperienceTable;
|
||||||
import net.Indyuce.mmocore.guild.provided.Guild;
|
import net.Indyuce.mmocore.guild.provided.Guild;
|
||||||
import net.Indyuce.mmocore.loot.chest.particle.SmallParticleEffect;
|
import net.Indyuce.mmocore.loot.chest.particle.SmallParticleEffect;
|
||||||
import net.Indyuce.mmocore.manager.data.mysql.MySQLTableEditor;
|
|
||||||
import net.Indyuce.mmocore.party.AbstractParty;
|
import net.Indyuce.mmocore.party.AbstractParty;
|
||||||
import net.Indyuce.mmocore.party.provided.Party;
|
import net.Indyuce.mmocore.party.provided.Party;
|
||||||
import net.Indyuce.mmocore.player.Unlockable;
|
import net.Indyuce.mmocore.player.Unlockable;
|
||||||
@ -53,8 +51,6 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
|
|
||||||
public class PlayerData extends OfflinePlayerData implements Closable, ExperienceTableClaimer {
|
public class PlayerData extends OfflinePlayerData implements Closable, ExperienceTableClaimer {
|
||||||
|
|
||||||
@ -105,7 +101,6 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc
|
|||||||
private final Map<String, Integer> tableItemClaims = new HashMap<>();
|
private final Map<String, Integer> tableItemClaims = new HashMap<>();
|
||||||
|
|
||||||
// NON-FINAL player data stuff made public to facilitate field change
|
// NON-FINAL player data stuff made public to facilitate field change
|
||||||
public int skillGuiDisplayOffset;
|
|
||||||
public boolean noCooldown;
|
public boolean noCooldown;
|
||||||
public CombatRunnable combat;
|
public CombatRunnable combat;
|
||||||
|
|
||||||
@ -311,9 +306,11 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc
|
|||||||
public void setLevel(int level) {
|
public void setLevel(int level) {
|
||||||
this.level = Math.max(1, level);
|
this.level = Math.max(1, level);
|
||||||
|
|
||||||
|
if (isOnline()) {
|
||||||
getStats().updateStats();
|
getStats().updateStats();
|
||||||
refreshVanillaExp();
|
refreshVanillaExp();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void takeLevels(int value) {
|
public void takeLevels(int value) {
|
||||||
setLevel(level - value);
|
setLevel(level - value);
|
||||||
@ -328,6 +325,8 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc
|
|||||||
|
|
||||||
public void setExperience(double value) {
|
public void setExperience(double value) {
|
||||||
experience = Math.max(0, value);
|
experience = Math.max(0, value);
|
||||||
|
|
||||||
|
if (isOnline())
|
||||||
refreshVanillaExp();
|
refreshVanillaExp();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -336,8 +335,9 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc
|
|||||||
* This updates the exp bar to display the player class level and exp.
|
* This updates the exp bar to display the player class level and exp.
|
||||||
*/
|
*/
|
||||||
public void refreshVanillaExp() {
|
public void refreshVanillaExp() {
|
||||||
if (!isOnline() || !MMOCore.plugin.configManager.overrideVanillaExp)
|
if (!MMOCore.plugin.configManager.overrideVanillaExp)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
getPlayer().sendExperienceChange(0.01f);
|
getPlayer().sendExperienceChange(0.01f);
|
||||||
getPlayer().setLevel(getLevel());
|
getPlayer().setLevel(getLevel());
|
||||||
getPlayer().setExp(Math.max(0, Math.min(1, (float) experience / (float) getLevelUpExperience())));
|
getPlayer().setExp(Math.max(0, Math.min(1, (float) experience / (float) getLevelUpExperience())));
|
||||||
@ -512,63 +512,6 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc
|
|||||||
}.runTaskTimer(MMOCore.plugin, 0, 1);
|
}.runTaskTimer(MMOCore.plugin, 0, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toJson() {
|
|
||||||
|
|
||||||
//We create the JSON corresponding to player Data
|
|
||||||
JsonObject jsonObject = new JsonObject();
|
|
||||||
MMOCore.sqlDebug("Saving data for: '" + getUniqueId() + "'...");
|
|
||||||
|
|
||||||
jsonObject.addProperty("class_points", getClassPoints());
|
|
||||||
jsonObject.addProperty("skill_points", getSkillPoints());
|
|
||||||
jsonObject.addProperty("attribute_points", getAttributePoints());
|
|
||||||
jsonObject.addProperty("attribute_realloc_points", getAttributeReallocationPoints());
|
|
||||||
jsonObject.addProperty("level", getLevel());
|
|
||||||
jsonObject.addProperty("experience", getExperience());
|
|
||||||
jsonObject.addProperty("class", getProfess().getId());
|
|
||||||
jsonObject.addProperty("last_login", getLastLogin());
|
|
||||||
jsonObject.addProperty("guild", hasGuild() ? getGuild().getId() : null);
|
|
||||||
|
|
||||||
jsonObject.addProperty("waypoints", MMOCoreUtils.arrayToJsonString(getWaypoints()));
|
|
||||||
jsonObject.addProperty("friends", MMOCoreUtils.arrayToJsonString(getFriends().stream().map(UUID::toString).collect(Collectors.toList())));
|
|
||||||
jsonObject.addProperty("bound_skills", MMOCoreUtils.arrayToJsonString(getBoundSkills().stream().map(skill -> skill.getSkill().getHandler().getId()).collect(Collectors.toList())));
|
|
||||||
|
|
||||||
jsonObject.addProperty("skills", MMOCoreUtils.entrySetToJsonString(mapSkillLevels().entrySet()));
|
|
||||||
jsonObject.addProperty("times_claimed", MMOCoreUtils.entrySetToJsonString(getItemClaims().entrySet()));
|
|
||||||
|
|
||||||
jsonObject.addProperty("attributes", getAttributes().toJsonString());
|
|
||||||
jsonObject.addProperty("professions", getCollectionSkills().toJsonString());
|
|
||||||
jsonObject.addProperty("quests", getQuestData().toJsonString());
|
|
||||||
jsonObject.addProperty("class_info", createClassInfoData(this).toString());
|
|
||||||
|
|
||||||
return jsonObject.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static JsonObject createClassInfoData(PlayerData data) {
|
|
||||||
JsonObject json = new JsonObject();
|
|
||||||
for (String c : data.getSavedClasses()) {
|
|
||||||
SavedClassInformation info = data.getClassInfo(c);
|
|
||||||
JsonObject classinfo = new JsonObject();
|
|
||||||
classinfo.addProperty("level", info.getLevel());
|
|
||||||
classinfo.addProperty("experience", info.getExperience());
|
|
||||||
classinfo.addProperty("skill-points", info.getSkillPoints());
|
|
||||||
classinfo.addProperty("attribute-points", info.getAttributePoints());
|
|
||||||
classinfo.addProperty("attribute-realloc-points", info.getAttributeReallocationPoints());
|
|
||||||
JsonObject skillinfo = new JsonObject();
|
|
||||||
for (String skill : info.getSkillKeys())
|
|
||||||
skillinfo.addProperty(skill, info.getSkillLevel(skill));
|
|
||||||
classinfo.add("skill", skillinfo);
|
|
||||||
JsonObject attributeinfo = new JsonObject();
|
|
||||||
for (String attribute : info.getAttributeKeys())
|
|
||||||
attributeinfo.addProperty(attribute, info.getAttributeLevel(attribute));
|
|
||||||
classinfo.add("attribute", attributeinfo);
|
|
||||||
|
|
||||||
json.add(c, classinfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
return json;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean hasReachedMaxLevel() {
|
public boolean hasReachedMaxLevel() {
|
||||||
return getProfess().getMaxLevel() > 0 && getLevel() >= getProfess().getMaxLevel();
|
return getProfess().getMaxLevel() > 0 && getLevel() >= getProfess().getMaxLevel();
|
||||||
}
|
}
|
||||||
@ -656,8 +599,6 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc
|
|||||||
refreshVanillaExp();
|
refreshVanillaExp();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public double getExperience() {
|
public double getExperience() {
|
||||||
return experience;
|
return experience;
|
||||||
}
|
}
|
||||||
@ -899,6 +840,7 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc
|
|||||||
iterator.remove();
|
iterator.remove();
|
||||||
|
|
||||||
// Update stats
|
// Update stats
|
||||||
|
if (isOnline())
|
||||||
getStats().updateStats();
|
getStats().updateStats();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -955,34 +897,6 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc
|
|||||||
combat = new CombatRunnable(this);
|
combat = new CombatRunnable(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return The savingPlayerData object corresponding to the playerData
|
|
||||||
*/
|
|
||||||
public SavingPlayerData getSavingPlayerData() {
|
|
||||||
return new SavingPlayerData(
|
|
||||||
getUniqueId(),
|
|
||||||
getClassPoints(),
|
|
||||||
getSkillPoints(),
|
|
||||||
getAttributePoints(),
|
|
||||||
getAttributeReallocationPoints(),
|
|
||||||
getLevel(),
|
|
||||||
getExperience(),
|
|
||||||
getProfess().getId(),
|
|
||||||
getLastLogin(),
|
|
||||||
hasGuild() ? getGuild().getId() : null,
|
|
||||||
getWaypoints(),
|
|
||||||
getFriends(),
|
|
||||||
getBoundSkills().stream().map(skill -> skill.getSkill().getHandler().getId()).toList(),
|
|
||||||
mapSkillLevels(),
|
|
||||||
getItemClaims(),
|
|
||||||
getAttributes().toJsonString(),
|
|
||||||
getCollectionSkills().toJsonString(),
|
|
||||||
getQuestData().toJsonString(),
|
|
||||||
createClassInfoData(this).toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return mmoData.hashCode();
|
return mmoData.hashCode();
|
||||||
|
@ -1,42 +0,0 @@
|
|||||||
package net.Indyuce.mmocore.api.player;
|
|
||||||
|
|
||||||
import net.Indyuce.mmocore.api.player.attribute.PlayerAttributes;
|
|
||||||
import net.Indyuce.mmocore.api.quest.PlayerQuests;
|
|
||||||
import net.Indyuce.mmocore.experience.PlayerProfessions;
|
|
||||||
import net.Indyuce.mmocore.skill.ClassSkill;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Just a container holding the basic playerData information that are needed to save it in a database.
|
|
||||||
*/
|
|
||||||
public record SavingPlayerData(
|
|
||||||
UUID uuid,
|
|
||||||
int classPoints,
|
|
||||||
int skillPoints,
|
|
||||||
int attributePoints,
|
|
||||||
int attributeReallocationPoints,
|
|
||||||
int level,
|
|
||||||
double experience,
|
|
||||||
String classId,
|
|
||||||
long lastLogin,
|
|
||||||
String guildId,
|
|
||||||
Set<String> waypoints,
|
|
||||||
List<UUID> friends,
|
|
||||||
List<String> boundSkills,
|
|
||||||
Map<String,Integer> skills,
|
|
||||||
Map<String,Integer> itemClaims,
|
|
||||||
String attributes,
|
|
||||||
String collectionsSkills,
|
|
||||||
String questData,
|
|
||||||
String classInfoData)
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -11,6 +11,7 @@ import net.Indyuce.mmocore.MMOCore;
|
|||||||
import net.Indyuce.mmocore.api.player.PlayerData;
|
import net.Indyuce.mmocore.api.player.PlayerData;
|
||||||
import org.apache.commons.lang.Validate;
|
import org.apache.commons.lang.Validate;
|
||||||
import org.bukkit.configuration.ConfigurationSection;
|
import org.bukkit.configuration.ConfigurationSection;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -89,15 +90,12 @@ public class PlayerAttributes {
|
|||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public AttributeInstance getInstance(String attribute) {
|
public AttributeInstance getInstance(String attribute) {
|
||||||
if (instances.containsKey(attribute))
|
return instances.computeIfAbsent(attribute, AttributeInstance::new);
|
||||||
return instances.get(attribute);
|
|
||||||
|
|
||||||
AttributeInstance ins = new AttributeInstance(attribute);
|
|
||||||
instances.put(attribute, ins);
|
|
||||||
return ins;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public AttributeInstance getInstance(PlayerAttribute attribute) {
|
public AttributeInstance getInstance(PlayerAttribute attribute) {
|
||||||
return getInstance(attribute.getId());
|
return getInstance(attribute.getId());
|
||||||
}
|
}
|
||||||
@ -126,6 +124,7 @@ public class PlayerAttributes {
|
|||||||
public void setBase(int value) {
|
public void setBase(int value) {
|
||||||
spent = Math.max(0, value);
|
spent = Math.max(0, value);
|
||||||
|
|
||||||
|
if (data.isOnline())
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,6 +11,8 @@ import net.Indyuce.mmocore.api.player.PlayerData;
|
|||||||
import net.Indyuce.mmocore.experience.Profession;
|
import net.Indyuce.mmocore.experience.Profession;
|
||||||
import net.Indyuce.mmocore.player.stats.StatInfo;
|
import net.Indyuce.mmocore.player.stats.StatInfo;
|
||||||
import net.Indyuce.mmocore.skill.ClassSkill;
|
import net.Indyuce.mmocore.skill.ClassSkill;
|
||||||
|
import org.apache.commons.lang.Validate;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
public class PlayerStats {
|
public class PlayerStats {
|
||||||
private final PlayerData data;
|
private final PlayerData data;
|
||||||
|
@ -34,10 +34,9 @@ public class PlayerQuests implements Closable {
|
|||||||
public PlayerQuests(PlayerData playerData) {
|
public PlayerQuests(PlayerData playerData) {
|
||||||
this.playerData = playerData;
|
this.playerData = playerData;
|
||||||
|
|
||||||
if (MMOCore.plugin.configManager.questBossBar) {
|
if (MMOCore.plugin.configManager.questBossBar && playerData.isOnline()) {
|
||||||
bossbarNamespacedKey = new NamespacedKey(MMOCore.plugin, "mmocore_quest_progress_" + playerData.getUniqueId().toString());
|
bossbarNamespacedKey = new NamespacedKey(MMOCore.plugin, "mmocore_quest_progress_" + playerData.getUniqueId().toString());
|
||||||
bossbar = Bukkit.createBossBar(bossbarNamespacedKey, "", BarColor.PURPLE, BarStyle.SEGMENTED_20);
|
bossbar = Bukkit.createBossBar(bossbarNamespacedKey, "", BarColor.PURPLE, BarStyle.SEGMENTED_20);
|
||||||
if (playerData.isOnline())
|
|
||||||
bossbar.addPlayer(playerData.getPlayer());
|
bossbar.addPlayer(playerData.getPlayer());
|
||||||
|
|
||||||
// Bossbar is disabled
|
// Bossbar is disabled
|
||||||
|
@ -15,7 +15,7 @@ public class AdminCommandTreeNode extends CommandTreeNode {
|
|||||||
addChild(new InfoCommandTreeNode(this));
|
addChild(new InfoCommandTreeNode(this));
|
||||||
addChild(new ClassCommandTreeNode(this));
|
addChild(new ClassCommandTreeNode(this));
|
||||||
addChild(new ForceClassCommandTreeNode(this));
|
addChild(new ForceClassCommandTreeNode(this));
|
||||||
addChild(new TransferDataTreeNode(this));
|
addChild(new ExportDataTreeNode(this));
|
||||||
|
|
||||||
addChild(new ExperienceCommandTreeNode(this));
|
addChild(new ExperienceCommandTreeNode(this));
|
||||||
addChild(new LevelCommandTreeNode(this));
|
addChild(new LevelCommandTreeNode(this));
|
||||||
|
@ -0,0 +1,71 @@
|
|||||||
|
package net.Indyuce.mmocore.command.rpg.admin;
|
||||||
|
|
||||||
|
import io.lumine.mythic.lib.api.player.MMOPlayerData;
|
||||||
|
import io.lumine.mythic.lib.command.api.CommandTreeNode;
|
||||||
|
import net.Indyuce.mmocore.MMOCore;
|
||||||
|
import net.Indyuce.mmocore.api.player.PlayerData;
|
||||||
|
import net.Indyuce.mmocore.manager.data.mysql.MySQLDataProvider;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This command allows to transfer data from your actual storage type
|
||||||
|
* to the other one which lets the user switch between storage types.
|
||||||
|
*/
|
||||||
|
public class ExportDataTreeNode extends CommandTreeNode {
|
||||||
|
public ExportDataTreeNode(CommandTreeNode parent) {
|
||||||
|
super(parent, "exportdata");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CommandResult execute(CommandSender sender, String[] strings) {
|
||||||
|
|
||||||
|
final List<UUID> playerIds = Arrays.stream(new File(MMOCore.plugin.getDataFolder() + "/userdata").listFiles())
|
||||||
|
.map(file -> UUID.fromString(file.getName().replace(".yml", ""))).collect(Collectors.toList());
|
||||||
|
|
||||||
|
// Initialize fake SQL data provider
|
||||||
|
final MySQLDataProvider sqlProvider;
|
||||||
|
try {
|
||||||
|
sqlProvider = new MySQLDataProvider(MMOCore.plugin.getConfig());
|
||||||
|
} catch (RuntimeException exception) {
|
||||||
|
sender.sendMessage("Could not initialize SQL provider (see console for stack trace): " + exception.getMessage());
|
||||||
|
return CommandResult.FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
sender.sendMessage("Exporting " + playerIds.size() + " player data(s).. See console for details");
|
||||||
|
|
||||||
|
// Save player data
|
||||||
|
Bukkit.getScheduler().runTaskAsynchronously(MMOCore.plugin, () -> {
|
||||||
|
|
||||||
|
// Loop first for garbage collection
|
||||||
|
int errorCount = 0;
|
||||||
|
for (UUID playerId : playerIds)
|
||||||
|
try {
|
||||||
|
final PlayerData offlinePlayerData = new PlayerData(new MMOPlayerData(playerId));
|
||||||
|
MMOCore.plugin.dataProvider.getDataManager().loadData(offlinePlayerData);
|
||||||
|
|
||||||
|
// Player data is loaded, now it gets saved through SQL
|
||||||
|
sqlProvider.getDataManager().saveData(offlinePlayerData);
|
||||||
|
} catch (RuntimeException exception) {
|
||||||
|
errorCount++;
|
||||||
|
exception.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
MMOCore.plugin.getLogger().log(Level.WARNING, "Exported player data to SQL database. Total errors: " + errorCount);
|
||||||
|
|
||||||
|
// Close connection to avoid memory leaks
|
||||||
|
sqlProvider.close();
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
return CommandResult.SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
@ -1,108 +0,0 @@
|
|||||||
package net.Indyuce.mmocore.command.rpg.admin;
|
|
||||||
|
|
||||||
import io.lumine.mythic.lib.command.api.CommandTreeNode;
|
|
||||||
import net.Indyuce.mmocore.MMOCore;
|
|
||||||
import net.Indyuce.mmocore.api.player.PlayerData;
|
|
||||||
import net.Indyuce.mmocore.api.player.SavingPlayerData;
|
|
||||||
import net.Indyuce.mmocore.guild.provided.Guild;
|
|
||||||
import net.Indyuce.mmocore.manager.data.DataProvider;
|
|
||||||
import net.Indyuce.mmocore.manager.data.mysql.MySQLDataProvider;
|
|
||||||
import net.Indyuce.mmocore.manager.data.yaml.YAMLDataProvider;
|
|
||||||
import org.bukkit.command.CommandSender;
|
|
||||||
import org.bukkit.scheduler.BukkitRunnable;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.UUID;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This command allows to transfer data from your actual storage type
|
|
||||||
* to the other one which lets the user switch between storage types.
|
|
||||||
*/
|
|
||||||
public class TransferDataTreeNode extends CommandTreeNode {
|
|
||||||
public TransferDataTreeNode(CommandTreeNode parent) {
|
|
||||||
super(parent, "transferdata");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public CommandResult execute(CommandSender commandSender, String[] strings) {
|
|
||||||
|
|
||||||
final List<UUID> playerUUIDs = new ArrayList<>();
|
|
||||||
if (MMOCore.plugin.dataProvider instanceof YAMLDataProvider) {
|
|
||||||
File folder = new File(MMOCore.plugin.getDataFolder() + "/userdata");
|
|
||||||
playerUUIDs.addAll(Arrays.stream(folder.listFiles())
|
|
||||||
.map(file -> UUID.fromString(file.getName().replace(".yml", "")))
|
|
||||||
.collect(Collectors.toList()));
|
|
||||||
} else {
|
|
||||||
((MySQLDataProvider) MMOCore.plugin.dataProvider).getResult(
|
|
||||||
"SELECT uuid from mmocore_playerdata", (result) -> {
|
|
||||||
try {
|
|
||||||
|
|
||||||
while (result.next()) {
|
|
||||||
playerUUIDs.add(UUID.fromString(result.getString("uuid")));
|
|
||||||
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
List<SavingPlayerData> savingPlayerDataList = new ArrayList<>();
|
|
||||||
for (UUID uuid : playerUUIDs) {
|
|
||||||
MMOCore.plugin.dataProvider.getDataManager().loadSavingPlayerData(uuid, savingPlayerDataList);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
final DataProvider provider;
|
|
||||||
if (MMOCore.plugin.dataProvider instanceof YAMLDataProvider) {
|
|
||||||
provider = new MySQLDataProvider(MMOCore.plugin.getConfig());
|
|
||||||
((MySQLDataProvider) provider).load();
|
|
||||||
} else {
|
|
||||||
provider = new YAMLDataProvider();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//5 seconds later we put all this data into the other data storage.
|
|
||||||
new BukkitRunnable() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
// Save player data
|
|
||||||
for (SavingPlayerData data : savingPlayerDataList)
|
|
||||||
provider.getDataManager().saveData(data);
|
|
||||||
|
|
||||||
// Save guild info
|
|
||||||
for (Guild guild : provider.getGuildManager().getAll())
|
|
||||||
provider.getGuildManager().save(guild);
|
|
||||||
} catch (Exception e) {
|
|
||||||
commandSender.sendMessage("Couldn't transfer properly the data.");
|
|
||||||
e.printStackTrace();
|
|
||||||
if (provider != null && provider instanceof MySQLDataProvider) {
|
|
||||||
((MySQLDataProvider) provider).close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}.runTaskLater(MMOCore.plugin, 100L);
|
|
||||||
|
|
||||||
|
|
||||||
//We close the connection 10 s later to avoid memory leaks.
|
|
||||||
new BukkitRunnable() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
if (provider != null && provider instanceof MySQLDataProvider) {
|
|
||||||
((MySQLDataProvider) provider).close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}.runTaskLater(MMOCore.plugin, 200);
|
|
||||||
|
|
||||||
return CommandResult.SUCCESS;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,28 +1,18 @@
|
|||||||
package net.Indyuce.mmocore.manager.data;
|
package net.Indyuce.mmocore.manager.data;
|
||||||
|
|
||||||
import com.google.gson.JsonElement;
|
|
||||||
import com.google.gson.JsonObject;
|
|
||||||
import io.lumine.mythic.lib.MythicLib;
|
|
||||||
import io.lumine.mythic.lib.api.player.MMOPlayerData;
|
import io.lumine.mythic.lib.api.player.MMOPlayerData;
|
||||||
import io.lumine.mythic.lib.player.TemporaryPlayerData;
|
import io.lumine.mythic.lib.player.TemporaryPlayerData;
|
||||||
import net.Indyuce.mmocore.MMOCore;
|
import net.Indyuce.mmocore.MMOCore;
|
||||||
import net.Indyuce.mmocore.api.event.AsyncPlayerDataLoadEvent;
|
import net.Indyuce.mmocore.api.event.AsyncPlayerDataLoadEvent;
|
||||||
import net.Indyuce.mmocore.api.event.PlayerDataLoadEvent;
|
import net.Indyuce.mmocore.api.event.PlayerDataLoadEvent;
|
||||||
import net.Indyuce.mmocore.api.player.SavingPlayerData;
|
|
||||||
import net.Indyuce.mmocore.api.player.profess.PlayerClass;
|
|
||||||
import net.Indyuce.mmocore.api.player.OfflinePlayerData;
|
import net.Indyuce.mmocore.api.player.OfflinePlayerData;
|
||||||
import net.Indyuce.mmocore.api.player.PlayerData;
|
import net.Indyuce.mmocore.api.player.PlayerData;
|
||||||
import net.Indyuce.mmocore.api.player.profess.SavedClassInformation;
|
|
||||||
import net.Indyuce.mmocore.api.util.MMOCoreUtils;
|
|
||||||
import net.Indyuce.mmocore.guild.provided.Guild;
|
|
||||||
import org.apache.commons.lang.Validate;
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.OfflinePlayer;
|
import org.bukkit.OfflinePlayer;
|
||||||
import org.bukkit.configuration.ConfigurationSection;
|
import org.bukkit.configuration.ConfigurationSection;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.logging.Level;
|
|
||||||
|
|
||||||
public abstract class PlayerDataManager {
|
public abstract class PlayerDataManager {
|
||||||
private final static Map<UUID, PlayerData> data = Collections.synchronizedMap(new HashMap<>());
|
private final static Map<UUID, PlayerData> data = Collections.synchronizedMap(new HashMap<>());
|
||||||
@ -45,63 +35,6 @@ public abstract class PlayerDataManager {
|
|||||||
return Objects.requireNonNull(data.get(uuid), "Player data is not loaded");
|
return Objects.requireNonNull(data.get(uuid), "Player data is not loaded");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void loadDataFromJson(@NotNull PlayerData data, String json) {
|
|
||||||
JsonObject object = MythicLib.plugin.getJson().parse(json, JsonObject.class);
|
|
||||||
|
|
||||||
data.setClassPoints(object.get("class_points").getAsInt());
|
|
||||||
data.setSkillPoints(object.get("skill_points").getAsInt());
|
|
||||||
data.setAttributePoints(object.get("attribute_points").getAsInt());
|
|
||||||
data.setAttributeReallocationPoints(object.get("attribute_realloc_points").getAsInt());
|
|
||||||
data.setLevel(object.get("level").getAsInt());
|
|
||||||
data.setExperience(object.get("experience").getAsInt());
|
|
||||||
if (object.has("class"))
|
|
||||||
data.setClass(MMOCore.plugin.classManager.get(object.get(("class")).getAsString()));
|
|
||||||
|
|
||||||
if (object.has("times_claimed")) {
|
|
||||||
JsonObject timesClaimed =object.get(("times_claimed")).getAsJsonObject();
|
|
||||||
timesClaimed.entrySet().forEach(entry -> data.getItemClaims().put(entry.getKey(), entry.getValue().getAsInt()));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (object.has(("guild"))) {
|
|
||||||
Guild guild = MMOCore.plugin.dataProvider.getGuildManager().getGuild(object.get("guild").getAsString());
|
|
||||||
data.setGuild(guild.hasMember(data.getUniqueId()) ? guild : null);
|
|
||||||
}
|
|
||||||
if (object.has(("attributes"))) data.getAttributes().load(object.get("attributes").getAsString());
|
|
||||||
if (object.has(("professions")))
|
|
||||||
data.getCollectionSkills().load(object.get("professions").getAsString());
|
|
||||||
if (object.has(("quests"))) data.getQuestData().load(object.get("quests").getAsString());
|
|
||||||
data.getQuestData().updateBossBar();
|
|
||||||
if (object.has(("waypoints")))
|
|
||||||
data.getWaypoints().addAll(MMOCoreUtils.jsonArrayToList(object.get("waypoints").getAsString()));
|
|
||||||
if (object.has(("friends")))
|
|
||||||
MMOCoreUtils.jsonArrayToList(object.get("friends").getAsString()).forEach(str -> data.getFriends().add(UUID.fromString(str)))
|
|
||||||
;
|
|
||||||
if (object.has(("skills"))) {
|
|
||||||
JsonObject skillsObject = object.get("skills").getAsJsonObject();
|
|
||||||
for (Map.Entry<String, JsonElement> entry : skillsObject.entrySet())
|
|
||||||
data.setSkillLevel(entry.getKey(), entry.getValue().getAsInt());
|
|
||||||
}
|
|
||||||
if (object.has(("bound_skills")))
|
|
||||||
for (String skill : MMOCoreUtils.jsonArrayToList(object.get("bound_skills").getAsString()))
|
|
||||||
if (data.getProfess().hasSkill(skill))
|
|
||||||
data.getBoundSkills().add(data.getProfess().getSkill(skill));
|
|
||||||
if (object.has(("class_info"))) {
|
|
||||||
JsonObject classObject = object.get("class_info").getAsJsonObject();
|
|
||||||
for (Map.Entry<String, JsonElement> entry : classObject.entrySet()) {
|
|
||||||
try {
|
|
||||||
PlayerClass profess = MMOCore.plugin.classManager.get(entry.getKey());
|
|
||||||
Validate.notNull(profess, "Could not find class '" + entry.getKey() + "'");
|
|
||||||
data.applyClassInfo(profess, new SavedClassInformation(entry.getValue().getAsJsonObject()));
|
|
||||||
} catch (IllegalArgumentException exception) {
|
|
||||||
MMOCore.log(Level.WARNING, "Could not load class info '" + entry.getKey() + "': " + exception.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Safely unregisters the player data from the map.
|
* Safely unregisters the player data from the map.
|
||||||
* This saves the player data either through SQL or YAML,
|
* This saves the player data either through SQL or YAML,
|
||||||
@ -152,6 +85,7 @@ public abstract class PlayerDataManager {
|
|||||||
* @return The loaded player data.
|
* @return The loaded player data.
|
||||||
*/
|
*/
|
||||||
public PlayerData setup(UUID uniqueId) {
|
public PlayerData setup(UUID uniqueId) {
|
||||||
|
|
||||||
// Load player data if it does not exist
|
// Load player data if it does not exist
|
||||||
if (!data.containsKey(uniqueId)) {
|
if (!data.containsKey(uniqueId)) {
|
||||||
PlayerData newData = TemporaryPlayerData.has(uniqueId) ? new PlayerData(MMOPlayerData.get(uniqueId), TemporaryPlayerData.get(uniqueId)) : new PlayerData(MMOPlayerData.get(uniqueId));
|
PlayerData newData = TemporaryPlayerData.has(uniqueId) ? new PlayerData(MMOPlayerData.get(uniqueId), TemporaryPlayerData.get(uniqueId)) : new PlayerData(MMOPlayerData.get(uniqueId));
|
||||||
@ -189,15 +123,6 @@ public abstract class PlayerDataManager {
|
|||||||
return data.values();
|
return data.values();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Called when player data must be loaded from database or config.
|
|
||||||
*
|
|
||||||
* @param uuid The uuid to load
|
|
||||||
* @param savingPlayerDataList the list where the data will be added.
|
|
||||||
*/
|
|
||||||
public abstract void loadSavingPlayerData(UUID uuid,List<SavingPlayerData> savingPlayerDataList);
|
|
||||||
|
|
||||||
|
|
||||||
public abstract void loadData(PlayerData data);
|
public abstract void loadData(PlayerData data);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -207,11 +132,7 @@ public abstract class PlayerDataManager {
|
|||||||
*
|
*
|
||||||
* @param data Player data to save
|
* @param data Player data to save
|
||||||
*/
|
*/
|
||||||
public void saveData(PlayerData data) {
|
public abstract void saveData(PlayerData data);
|
||||||
saveData(data.getSavingPlayerData());
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract void saveData(SavingPlayerData data);
|
|
||||||
|
|
||||||
public class DefaultPlayerData {
|
public class DefaultPlayerData {
|
||||||
private final int level, classPoints, skillPoints, attributePoints, attrReallocPoints;
|
private final int level, classPoints, skillPoints, attributePoints, attrReallocPoints;
|
||||||
|
@ -20,23 +20,7 @@ public class MySQLDataProvider extends MMODataSource implements DataProvider {
|
|||||||
@Override
|
@Override
|
||||||
public void load() {
|
public void load() {
|
||||||
|
|
||||||
|
// Fully create table
|
||||||
/*TODO
|
|
||||||
also move Debug function to mysql data provider
|
|
||||||
|
|
||||||
String tableName = getPoolName();
|
|
||||||
getResultAsync("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '" +getPoolName()+ "' AND TABLE_NAME = 'mmocore_playerdata' AND COLUMN_NAME = 'times_claimed'",
|
|
||||||
result -> {
|
|
||||||
if (!result.next())
|
|
||||||
executeUpdateAsync()
|
|
||||||
}):
|
|
||||||
executeUpdateAsync();
|
|
||||||
|
|
||||||
|
|
||||||
if (!provider.prepareStatement("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '" + provider.getDatabase()
|
|
||||||
+ "' AND TABLE_NAME = '" + provider.getBountyDataTable() + "' AND COLUMN_NAME = 'last_updated'").executeQuery().next())
|
|
||||||
provider.prepareStatement("ALTER TABLE " + provider.getBountyDataTable() + " ADD COLUMN last_updated BIGINT").execute();*/
|
|
||||||
|
|
||||||
executeUpdateAsync(
|
executeUpdateAsync(
|
||||||
"CREATE TABLE IF NOT EXISTS mmocore_playerdata(uuid VARCHAR(36),class_points "
|
"CREATE TABLE IF NOT EXISTS mmocore_playerdata(uuid VARCHAR(36),class_points "
|
||||||
+ "INT(11) DEFAULT 0,skill_points INT(11) DEFAULT 0,attribute_points INT(11) "
|
+ "INT(11) DEFAULT 0,skill_points INT(11) DEFAULT 0,attribute_points INT(11) "
|
||||||
@ -46,16 +30,24 @@ getResultAsync("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '"
|
|||||||
+ "waypoints LONGTEXT,friends LONGTEXT,skills LONGTEXT,bound_skills LONGTEXT,"
|
+ "waypoints LONGTEXT,friends LONGTEXT,skills LONGTEXT,bound_skills LONGTEXT,"
|
||||||
+ "class_info LONGTEXT, is_saved TINYINT, PRIMARY KEY (uuid));");
|
+ "class_info LONGTEXT, is_saved TINYINT, PRIMARY KEY (uuid));");
|
||||||
|
|
||||||
|
// Add 'times_claimed' if it doesn't exist
|
||||||
|
getResultAsync("SELECT * FROM information_schema.COLUMNS WHERE TABLE_NAME = 'mmocore_playerdata' AND COLUMN_NAME = 'times_claimed'", result -> {
|
||||||
|
try {
|
||||||
|
if (!result.next())
|
||||||
|
executeUpdateAsync("ALTER TABLE mmocore_playerdata ADD COLUMN times_claimed LONGTEXT");
|
||||||
|
} catch (SQLException exception) {
|
||||||
|
exception.printStackTrace();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
getResult("SELECT * FROM information_schema.COLUMNS WHERE TABLE_NAME = 'mmocore_playerdata'" +
|
// Add 'is_saved' if it doesn't exist
|
||||||
" AND COLUMN_NAME = 'is_saved'",(result)-> {
|
getResultAsync("SELECT * FROM information_schema.COLUMNS WHERE TABLE_NAME = 'mmocore_playerdata' AND COLUMN_NAME = 'is_saved'", result -> {
|
||||||
try {
|
try {
|
||||||
if (!result.next())
|
if (!result.next())
|
||||||
executeUpdate("ALTER TABLE mmocore_playerdata ADD COLUMN is_saved TINYINT");
|
executeUpdate("ALTER TABLE mmocore_playerdata ADD COLUMN is_saved TINYINT");
|
||||||
} catch (SQLException e) {
|
} catch (SQLException exception) {
|
||||||
throw new RuntimeException(e);
|
exception.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package net.Indyuce.mmocore.manager.data.mysql;
|
package net.Indyuce.mmocore.manager.data.mysql;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.google.gson.JsonParser;
|
import com.google.gson.JsonParser;
|
||||||
@ -8,21 +7,18 @@ import io.lumine.mythic.lib.MythicLib;
|
|||||||
import net.Indyuce.mmocore.MMOCore;
|
import net.Indyuce.mmocore.MMOCore;
|
||||||
import net.Indyuce.mmocore.api.player.OfflinePlayerData;
|
import net.Indyuce.mmocore.api.player.OfflinePlayerData;
|
||||||
import net.Indyuce.mmocore.api.player.PlayerData;
|
import net.Indyuce.mmocore.api.player.PlayerData;
|
||||||
import net.Indyuce.mmocore.api.player.SavingPlayerData;
|
|
||||||
import net.Indyuce.mmocore.api.player.profess.PlayerClass;
|
import net.Indyuce.mmocore.api.player.profess.PlayerClass;
|
||||||
import net.Indyuce.mmocore.api.player.profess.SavedClassInformation;
|
import net.Indyuce.mmocore.api.player.profess.SavedClassInformation;
|
||||||
import net.Indyuce.mmocore.api.util.MMOCoreUtils;
|
import net.Indyuce.mmocore.api.util.MMOCoreUtils;
|
||||||
import net.Indyuce.mmocore.guild.provided.Guild;
|
import net.Indyuce.mmocore.guild.provided.Guild;
|
||||||
import net.Indyuce.mmocore.manager.data.PlayerDataManager;
|
import net.Indyuce.mmocore.manager.data.PlayerDataManager;
|
||||||
import org.apache.commons.lang.Validate;
|
import org.apache.commons.lang.Validate;
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.scheduler.BukkitRunnable;
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@ -143,115 +139,77 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
|
|||||||
};
|
};
|
||||||
|
|
||||||
runnable.runTaskTimerAsynchronously(MMOCore.plugin, 0L, 10L);
|
runnable.runTaskTimerAsynchronously(MMOCore.plugin, 0L, 10L);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void saveData(PlayerData data) {
|
||||||
|
|
||||||
|
MySQLTableEditor sql = new MySQLTableEditor(MySQLTableEditor.Table.PLAYERDATA, data.getUniqueId(),provider);
|
||||||
|
MMOCore.sqlDebug("Saving data for: '" + data.getUniqueId() + "'...");
|
||||||
|
|
||||||
|
sql.updateData("class_points", data.getClassPoints());
|
||||||
|
sql.updateData("skill_points", data.getSkillPoints());
|
||||||
|
sql.updateData("attribute_points", data.getAttributePoints());
|
||||||
|
sql.updateData("attribute_realloc_points", data.getAttributeReallocationPoints());
|
||||||
|
sql.updateData("level", data.getLevel());
|
||||||
|
sql.updateData("experience", data.getExperience());
|
||||||
|
sql.updateData("class", data.getProfess().getId());
|
||||||
|
sql.updateData("last_login", data.getLastLogin());
|
||||||
|
sql.updateData("guild", data.hasGuild() ? data.getGuild().getId() : null);
|
||||||
|
|
||||||
|
sql.updateJSONArray("waypoints", data.getWaypoints());
|
||||||
|
sql.updateJSONArray("friends", data.getFriends().stream().map(UUID::toString).collect(Collectors.toList()));
|
||||||
|
sql.updateJSONArray("bound_skills", data.getBoundSkills().stream().map(skill -> skill.getSkill().getHandler().getId()).collect(Collectors.toList()));
|
||||||
|
|
||||||
|
sql.updateJSONObject("skills", data.mapSkillLevels().entrySet());
|
||||||
|
sql.updateJSONObject("times_claimed", data.getItemClaims().entrySet());
|
||||||
|
|
||||||
|
sql.updateData("attributes", data.getAttributes().toJsonString());
|
||||||
|
sql.updateData("professions", data.getCollectionSkills().toJsonString());
|
||||||
|
sql.updateData("quests", data.getQuestData().toJsonString());
|
||||||
|
|
||||||
|
sql.updateData("class_info", createClassInfoData(data).toString());
|
||||||
|
sql.updateData("is_saved", 1);
|
||||||
|
|
||||||
|
MMOCore.sqlDebug("Saved data for: " + data.getUniqueId());
|
||||||
|
MMOCore.sqlDebug(String.format("{ class: %s, level: %d }", data.getProfess().getId(), data.getLevel()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private JsonObject createClassInfoData(PlayerData playerData) {
|
||||||
|
final JsonObject json = new JsonObject();
|
||||||
|
for (String c : playerData.getSavedClasses()) {
|
||||||
|
final SavedClassInformation info = playerData.getClassInfo(c);
|
||||||
|
JsonObject classinfo = new JsonObject();
|
||||||
|
classinfo.addProperty("level", info.getLevel());
|
||||||
|
classinfo.addProperty("experience", info.getExperience());
|
||||||
|
classinfo.addProperty("skill-points", info.getSkillPoints());
|
||||||
|
classinfo.addProperty("attribute-points", info.getAttributePoints());
|
||||||
|
classinfo.addProperty("attribute-realloc-points", info.getAttributeReallocationPoints());
|
||||||
|
JsonObject skillinfo = new JsonObject();
|
||||||
|
for (String skill : info.getSkillKeys())
|
||||||
|
skillinfo.addProperty(skill, info.getSkillLevel(skill));
|
||||||
|
classinfo.add("skill", skillinfo);
|
||||||
|
JsonObject attributeinfo = new JsonObject();
|
||||||
|
for (String attribute : info.getAttributeKeys())
|
||||||
|
attributeinfo.addProperty(attribute, info.getAttributeLevel(attribute));
|
||||||
|
classinfo.add("attribute", attributeinfo);
|
||||||
|
|
||||||
|
json.add(c, classinfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
private boolean isEmpty(String s) {
|
private boolean isEmpty(String s) {
|
||||||
return s == null || s.equalsIgnoreCase("null") || s.equalsIgnoreCase("{}") || s.equalsIgnoreCase("[]") || s.equalsIgnoreCase("");
|
return s == null || s.equalsIgnoreCase("null") || s.equalsIgnoreCase("{}") || s.equalsIgnoreCase("[]") || s.equalsIgnoreCase("");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void saveData(SavingPlayerData data) {
|
|
||||||
|
|
||||||
MySQLTableEditor sql = new MySQLTableEditor(MySQLTableEditor.Table.PLAYERDATA, data.uuid(), provider);
|
|
||||||
MMOCore.sqlDebug("Saving data for: '" + data.uuid() + "'...");
|
|
||||||
|
|
||||||
sql.updateData("class_points", data.classPoints());
|
|
||||||
sql.updateData("skill_points", data.skillPoints());
|
|
||||||
sql.updateData("attribute_points", data.attributePoints());
|
|
||||||
sql.updateData("attribute_realloc_points", data.attributeReallocationPoints());
|
|
||||||
sql.updateData("level", data.level());
|
|
||||||
sql.updateData("experience", data.experience());
|
|
||||||
sql.updateData("class", data.classId());
|
|
||||||
sql.updateData("last_login", data.lastLogin());
|
|
||||||
sql.updateData("guild", data.guildId());
|
|
||||||
|
|
||||||
sql.updateJSONArray("waypoints", data.waypoints());
|
|
||||||
sql.updateJSONArray("friends", data.friends().stream().map(UUID::toString).collect(Collectors.toList()));
|
|
||||||
sql.updateJSONArray("bound_skills", data.boundSkills());
|
|
||||||
|
|
||||||
sql.updateJSONObject("skills", data.skills().entrySet());
|
|
||||||
sql.updateJSONObject("times_claimed", data.itemClaims().entrySet());
|
|
||||||
|
|
||||||
sql.updateData("attributes", data.attributes());
|
|
||||||
sql.updateData("professions", data.collectionsSkills());
|
|
||||||
sql.updateData("quests", data.questData());
|
|
||||||
|
|
||||||
sql.updateData("class_info", data.classInfoData());
|
|
||||||
sql.updateData("is_saved", 1);
|
|
||||||
|
|
||||||
MMOCore.sqlDebug("Saved data for: " + data.uuid());
|
|
||||||
MMOCore.sqlDebug(String.format("{ class: %s, level: %d }", data.classId(), data.level()));
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public OfflinePlayerData getOffline(UUID uuid) {
|
public OfflinePlayerData getOffline(UUID uuid) {
|
||||||
return isLoaded(uuid) ? get(uuid) : new MySQLOfflinePlayerData(uuid);
|
return isLoaded(uuid) ? get(uuid) : new MySQLOfflinePlayerData(uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void loadSavingPlayerData(UUID uuid, List<SavingPlayerData> savingPlayerDataList) {
|
|
||||||
|
|
||||||
provider.getResult("SELECT * FROM mmocore_playerdata WHERE uuid = '" + uuid + "';", (result) -> {
|
|
||||||
try {
|
|
||||||
|
|
||||||
|
|
||||||
if (result.next()) {
|
|
||||||
Map<String, Integer> skills = new HashMap<>();
|
|
||||||
Map<String, Integer> itemClaims = new HashMap<>();
|
|
||||||
|
|
||||||
if (!isEmpty(result.getString("skills"))) {
|
|
||||||
JsonObject object = MythicLib.plugin.getJson().parse(result.getString("skills"), JsonObject.class);
|
|
||||||
for (Entry<String, JsonElement> entry : object.entrySet())
|
|
||||||
skills.put(entry.getKey(), entry.getValue().getAsInt());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isEmpty(result.getString("times_claimed"))) {
|
|
||||||
JsonObject json = new JsonParser().parse(result.getString("times_claimed")).getAsJsonObject();
|
|
||||||
json.entrySet().forEach(entry -> itemClaims.put(entry.getKey(), entry.getValue().getAsInt()));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
SavingPlayerData data = new SavingPlayerData(
|
|
||||||
uuid,
|
|
||||||
result.getInt("class_points"),
|
|
||||||
result.getInt("skill_points"),
|
|
||||||
result.getInt("attribute_points"),
|
|
||||||
result.getInt("attribute_realloc_points"),
|
|
||||||
result.getInt("level"),
|
|
||||||
result.getInt("experience"),
|
|
||||||
result.getString("class"),
|
|
||||||
result.getLong("last_login"),
|
|
||||||
result.getString("guild"),
|
|
||||||
MMOCoreUtils.jsonArrayToList(result.getString("waypoints")).stream().collect(Collectors.toSet()),
|
|
||||||
MMOCoreUtils.jsonArrayToList(result.getString("friends")).stream().map(str -> UUID.fromString(str)).toList(),
|
|
||||||
MMOCoreUtils.jsonArrayToList(result.getString("bound_skills")).stream().toList(),
|
|
||||||
skills,
|
|
||||||
itemClaims,
|
|
||||||
result.getString("attributes"),
|
|
||||||
result.getString("professions"),
|
|
||||||
result.getString("quests"),
|
|
||||||
result.getString("class_info"));
|
|
||||||
|
|
||||||
savingPlayerDataList.add(data);
|
|
||||||
Bukkit.broadcastMessage(new Gson().toJson(data));
|
|
||||||
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public class MySQLOfflinePlayerData extends OfflinePlayerData {
|
public class MySQLOfflinePlayerData extends OfflinePlayerData {
|
||||||
private int level;
|
private int level;
|
||||||
private long lastLogin;
|
private long lastLogin;
|
||||||
|
@ -1,26 +1,15 @@
|
|||||||
package net.Indyuce.mmocore.manager.data.yaml;
|
package net.Indyuce.mmocore.manager.data.yaml;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
|
||||||
import com.google.gson.JsonElement;
|
|
||||||
import com.google.gson.JsonObject;
|
|
||||||
import net.Indyuce.mmocore.MMOCore;
|
import net.Indyuce.mmocore.MMOCore;
|
||||||
import net.Indyuce.mmocore.api.player.SavingPlayerData;
|
|
||||||
import net.Indyuce.mmocore.api.player.attribute.PlayerAttribute;
|
|
||||||
import net.Indyuce.mmocore.api.player.attribute.PlayerAttributes;
|
|
||||||
import net.Indyuce.mmocore.api.player.profess.PlayerClass;
|
import net.Indyuce.mmocore.api.player.profess.PlayerClass;
|
||||||
import net.Indyuce.mmocore.api.ConfigFile;
|
import net.Indyuce.mmocore.api.ConfigFile;
|
||||||
import net.Indyuce.mmocore.api.player.OfflinePlayerData;
|
import net.Indyuce.mmocore.api.player.OfflinePlayerData;
|
||||||
import net.Indyuce.mmocore.api.player.PlayerData;
|
import net.Indyuce.mmocore.api.player.PlayerData;
|
||||||
import net.Indyuce.mmocore.api.player.profess.SavedClassInformation;
|
import net.Indyuce.mmocore.api.player.profess.SavedClassInformation;
|
||||||
import net.Indyuce.mmocore.api.quest.PlayerQuests;
|
|
||||||
import net.Indyuce.mmocore.experience.PlayerProfessions;
|
|
||||||
import net.Indyuce.mmocore.experience.Profession;
|
|
||||||
import net.Indyuce.mmocore.guild.provided.Guild;
|
import net.Indyuce.mmocore.guild.provided.Guild;
|
||||||
import net.Indyuce.mmocore.manager.data.DataProvider;
|
import net.Indyuce.mmocore.manager.data.DataProvider;
|
||||||
import net.Indyuce.mmocore.manager.data.PlayerDataManager;
|
import net.Indyuce.mmocore.manager.data.PlayerDataManager;
|
||||||
import org.apache.commons.lang.Validate;
|
import org.apache.commons.lang.Validate;
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.configuration.ConfigurationSection;
|
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
@ -48,7 +37,7 @@ public class YAMLPlayerDataManager extends PlayerDataManager {
|
|||||||
if (config.contains("class"))
|
if (config.contains("class"))
|
||||||
data.setClass(MMOCore.plugin.classManager.get(config.getString("class")));
|
data.setClass(MMOCore.plugin.classManager.get(config.getString("class")));
|
||||||
|
|
||||||
if (!data.hasUsedTemporaryData()) {
|
if (!data.hasUsedTemporaryData() && data.isOnline()) {
|
||||||
data.setMana(data.getStats().getStat("MAX_MANA"));
|
data.setMana(data.getStats().getStat("MAX_MANA"));
|
||||||
data.setStamina(data.getStats().getStat("MAX_STAMINA"));
|
data.setStamina(data.getStats().getStat("MAX_STAMINA"));
|
||||||
data.setStellium(data.getStats().getStat("MAX_STELLIUM"));
|
data.setStellium(data.getStats().getStat("MAX_STELLIUM"));
|
||||||
@ -148,228 +137,11 @@ public class YAMLPlayerDataManager extends PlayerDataManager {
|
|||||||
file.save();
|
file.save();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Used to save Data from a SavingPlayerDataInstance (Data of an offline player)
|
|
||||||
*
|
|
||||||
* @param data
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void saveData(SavingPlayerData data) {
|
|
||||||
ConfigFile file = new ConfigFile(data.uuid());
|
|
||||||
FileConfiguration config = file.getConfig();
|
|
||||||
|
|
||||||
config.set("class-points", data.classPoints());
|
|
||||||
config.set("skill-points", data.skillPoints());
|
|
||||||
config.set("attribute-points", data.attributePoints());
|
|
||||||
// config.set("skill-realloc-points", skillReallocationPoints);
|
|
||||||
config.set("attribute-realloc-points", data.attributeReallocationPoints());
|
|
||||||
config.set("level", data.level());
|
|
||||||
config.set("experience", data.experience());
|
|
||||||
config.set("class", data.classId());
|
|
||||||
config.set("waypoints", data.waypoints().stream().toList());
|
|
||||||
config.set("friends", data.friends());
|
|
||||||
config.set("last-login", data.lastLogin());
|
|
||||||
if (data.guildId() != null)
|
|
||||||
config.set("guild", data.guildId());
|
|
||||||
|
|
||||||
config.set("skill", null);
|
|
||||||
data.skills().forEach((key1, value) -> config.set("skill." + key1, value));
|
|
||||||
data.itemClaims().forEach((key, times) -> config.set("times-claimed." + key, times));
|
|
||||||
|
|
||||||
List<String> boundSkills = new ArrayList<>();
|
|
||||||
data.boundSkills().forEach(skill -> boundSkills.add(skill));
|
|
||||||
config.set("bound-skills", boundSkills);
|
|
||||||
|
|
||||||
config.set("attribute", null);
|
|
||||||
Gson parser = new Gson();
|
|
||||||
JsonObject jo = parser.fromJson(data.attributes(), JsonObject.class);
|
|
||||||
for (Map.Entry<String, JsonElement> entry : jo.entrySet()) {
|
|
||||||
try {
|
|
||||||
String id = entry.getKey().toLowerCase().replace("_", "-").replace(" ", "-");
|
|
||||||
config.set("attribute." + id, entry.getValue().getAsInt());
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
jo = parser.fromJson(data.collectionsSkills(), JsonObject.class);
|
|
||||||
config.createSection("profession");
|
|
||||||
ConfigurationSection section = config.getConfigurationSection("profession");
|
|
||||||
// Load profession exp and levels
|
|
||||||
for (Map.Entry<String, JsonElement> entry : jo.entrySet())
|
|
||||||
if (MMOCore.plugin.professionManager.has(entry.getKey())) {
|
|
||||||
JsonObject value = entry.getValue().getAsJsonObject();
|
|
||||||
section.set(entry.getKey() + ".exp", value.get("exp").getAsDouble());
|
|
||||||
section.set(entry.getKey() + ".level", value.get("level").getAsInt());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
config.set("quest", null);
|
|
||||||
config.createSection("quest");
|
|
||||||
section = config.getConfigurationSection("quest");
|
|
||||||
jo = parser.fromJson(data.questData(), JsonObject.class);
|
|
||||||
if (jo.has("current")) {
|
|
||||||
JsonObject cur = jo.getAsJsonObject("current");
|
|
||||||
try {
|
|
||||||
section.set("current.id", cur.get("id").getAsString());
|
|
||||||
section.set("current.objective", cur.get("objective").getAsInt());
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (jo.has("finished"))
|
|
||||||
for (Map.Entry<String, JsonElement> entry : jo.getAsJsonObject("finished").entrySet())
|
|
||||||
section.set("finished." + entry.getKey(), entry.getValue().getAsLong());
|
|
||||||
|
|
||||||
|
|
||||||
config.set("class-info", null);
|
|
||||||
jo = parser.fromJson(data.classInfoData(), JsonObject.class);
|
|
||||||
|
|
||||||
|
|
||||||
for (Map.Entry<String, JsonElement> entry : jo.entrySet()) {
|
|
||||||
try {
|
|
||||||
String key = entry.getKey();
|
|
||||||
JsonObject info = entry.getValue().getAsJsonObject();
|
|
||||||
config.set("class-info." + key + ".level", info.get("level").getAsInt());
|
|
||||||
config.set("class-info." + key + ".experience", info.get("experience").getAsDouble());
|
|
||||||
config.set("class-info." + key + ".skill-points", info.get("skill-points").getAsInt());
|
|
||||||
config.set("class-info." + key + ".attribute-points", info.get("attribute-points").getAsInt());
|
|
||||||
config.set("class-info." + key + ".attribute-realloc-points", info.get("attribute-realloc-points").getAsInt());
|
|
||||||
|
|
||||||
if (info.has("attribute"))
|
|
||||||
for (Map.Entry<String, JsonElement> attributesEntry : info.getAsJsonObject("attribute").entrySet())
|
|
||||||
config.set("class-info." + key + ".attribute." + attributesEntry.getKey(), attributesEntry.getValue().getAsInt());
|
|
||||||
|
|
||||||
if (info.has("skill"))
|
|
||||||
for (Map.Entry<String, JsonElement> skillsEntry : info.getAsJsonObject("skill").entrySet())
|
|
||||||
config.set("class-info." + key + ".skill." + skillsEntry.getKey(), skillsEntry.getValue().getAsInt());
|
|
||||||
|
|
||||||
} catch (IllegalArgumentException exception) {
|
|
||||||
MMOCore.log(Level.WARNING, "Could not load class info '" + entry.getKey() + "': " + exception.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Bukkit.broadcastMessage("SAVED");
|
|
||||||
file.save();
|
|
||||||
}
|
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Override
|
@Override
|
||||||
public OfflinePlayerData getOffline(UUID uuid) {
|
public OfflinePlayerData getOffline(UUID uuid) {
|
||||||
return isLoaded(uuid) ? get(uuid) : new YAMLOfflinePlayerData(uuid);
|
return isLoaded(uuid) ? get(uuid) : new YAMLOfflinePlayerData(uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void loadSavingPlayerData(UUID uuid, List<SavingPlayerData> savingPlayerDataList) {
|
|
||||||
FileConfiguration config = new ConfigFile(uuid).getConfig();
|
|
||||||
|
|
||||||
Map<String, Integer> skills = new HashMap<>();
|
|
||||||
if (config.contains("skill"))
|
|
||||||
config.getConfigurationSection("skill").getKeys(false).forEach(id -> skills.put(id, config.getInt("skill." + id)));
|
|
||||||
|
|
||||||
Map<String, Integer> itemClaims = new HashMap<>();
|
|
||||||
if (config.contains("times-claimed"))
|
|
||||||
for (String key : config.getConfigurationSection("times-claimed").getKeys(true))
|
|
||||||
itemClaims.put(key, config.getInt("times-claimed." + key));
|
|
||||||
|
|
||||||
|
|
||||||
//Creates the attributes json
|
|
||||||
|
|
||||||
|
|
||||||
ConfigurationSection section = config.getConfigurationSection("attributes");
|
|
||||||
JsonObject attributesJson = new JsonObject();
|
|
||||||
if (section != null)
|
|
||||||
for (String key : section.getKeys(false)) {
|
|
||||||
String id = key.toLowerCase().replace("-", "_").replace(" ", "_");
|
|
||||||
attributesJson.addProperty(id, section.getInt(key));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//Creates the profession json
|
|
||||||
|
|
||||||
|
|
||||||
section = config.getConfigurationSection("profession");
|
|
||||||
JsonObject collectionSkillsJson = new JsonObject();
|
|
||||||
if (section != null)
|
|
||||||
for (String key : section.getKeys(false)) {
|
|
||||||
if (MMOCore.plugin.professionManager.has(key)) {
|
|
||||||
JsonObject object = new JsonObject();
|
|
||||||
object.addProperty("exp", section.getDouble(key + ".exp"));
|
|
||||||
object.addProperty("level", section.getInt(key + ".level"));
|
|
||||||
|
|
||||||
collectionSkillsJson.add(key, object);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//Creates the questJson
|
|
||||||
|
|
||||||
section = config.getConfigurationSection("quest");
|
|
||||||
JsonObject questsJson = new JsonObject();
|
|
||||||
|
|
||||||
if (section.contains("current")) {
|
|
||||||
JsonObject cur = new JsonObject();
|
|
||||||
cur.addProperty("id", section.getString("current.id"));
|
|
||||||
cur.addProperty("objective", section.getInt("current.objective"));
|
|
||||||
questsJson.add("current", cur);
|
|
||||||
}
|
|
||||||
JsonObject fin = new JsonObject();
|
|
||||||
if (section.contains("finished"))
|
|
||||||
for (String key : section.getConfigurationSection("finished").getKeys(false))
|
|
||||||
fin.addProperty(key, section.getLong("finished." + key));
|
|
||||||
|
|
||||||
if (fin.size() != 0)
|
|
||||||
questsJson.add("finished", fin);
|
|
||||||
|
|
||||||
|
|
||||||
JsonObject classInfoJson = new JsonObject();
|
|
||||||
// Load class slots, use try so the player can log in.
|
|
||||||
if (config.contains("class-info"))
|
|
||||||
for (String key : config.getConfigurationSection("class-info").getKeys(false)) {
|
|
||||||
SavedClassInformation info = new SavedClassInformation(config.getConfigurationSection("class-info." + key));
|
|
||||||
JsonObject classinfo = new JsonObject();
|
|
||||||
classinfo.addProperty("level", info.getLevel());
|
|
||||||
classinfo.addProperty("experience", info.getExperience());
|
|
||||||
classinfo.addProperty("skill-points", info.getSkillPoints());
|
|
||||||
classinfo.addProperty("attribute-points", info.getAttributePoints());
|
|
||||||
classinfo.addProperty("attribute-realloc-points", info.getAttributeReallocationPoints());
|
|
||||||
JsonObject skillinfo = new JsonObject();
|
|
||||||
for (String skill : info.getSkillKeys())
|
|
||||||
skillinfo.addProperty(skill, info.getSkillLevel(skill));
|
|
||||||
classinfo.add("skill", skillinfo);
|
|
||||||
JsonObject attributeinfo = new JsonObject();
|
|
||||||
for (String attribute : info.getAttributeKeys())
|
|
||||||
attributeinfo.addProperty(attribute, info.getAttributeLevel(attribute));
|
|
||||||
classinfo.add("attribute", attributeinfo);
|
|
||||||
classInfoJson.add(key, classinfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
SavingPlayerData data = new SavingPlayerData(
|
|
||||||
uuid,
|
|
||||||
config.getInt("class-points"),
|
|
||||||
config.getInt("skill-points"),
|
|
||||||
config.getInt("attribute-points"),
|
|
||||||
config.getInt("attribute-realloc-points"),
|
|
||||||
config.getInt("level"),
|
|
||||||
config.getInt("experience"),
|
|
||||||
config.getString("class"),
|
|
||||||
config.getLong("last-login"),
|
|
||||||
config.getString("guild"),
|
|
||||||
config.getStringList("waypoints").stream().collect(Collectors.toSet()),
|
|
||||||
config.getStringList("friends").stream().map(UUID::fromString).toList(),
|
|
||||||
config.getStringList("bound-skills"),
|
|
||||||
skills,
|
|
||||||
itemClaims,
|
|
||||||
attributesJson.toString(),
|
|
||||||
collectionSkillsJson.toString(),
|
|
||||||
questsJson.toString(),
|
|
||||||
classInfoJson.toString());
|
|
||||||
|
|
||||||
savingPlayerDataList.add(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user