SQL Update commit 1

This commit is contained in:
Jules 2023-04-16 00:56:17 +02:00
parent 547775a33e
commit baf664a608
25 changed files with 452 additions and 637 deletions

View File

@ -2,6 +2,7 @@ package net.Indyuce.mmocore;
import io.lumine.mythic.lib.MythicLib;
import io.lumine.mythic.lib.UtilityMethods;
import io.lumine.mythic.lib.data.sql.SQLDataSource;
import io.lumine.mythic.lib.metrics.bukkit.Metrics;
import io.lumine.mythic.lib.version.SpigotPlugin;
import net.Indyuce.mmocore.api.player.PlayerData;
@ -30,8 +31,11 @@ import net.Indyuce.mmocore.guild.provided.Guild;
import net.Indyuce.mmocore.guild.provided.MMOCoreGuildModule;
import net.Indyuce.mmocore.manager.*;
import net.Indyuce.mmocore.manager.data.DataProvider;
import net.Indyuce.mmocore.manager.data.mysql.MySQLDataProvider;
import net.Indyuce.mmocore.manager.data.yaml.YAMLDataProvider;
import net.Indyuce.mmocore.manager.data.GuildDataManager;
import net.Indyuce.mmocore.manager.data.LegacyDataProvider;
import net.Indyuce.mmocore.manager.data.PlayerDataManager;
import net.Indyuce.mmocore.manager.data.sql.SQLDataHandler;
import net.Indyuce.mmocore.guild.provided.YAMLGuildDataManager;
import net.Indyuce.mmocore.manager.profession.*;
import net.Indyuce.mmocore.manager.social.BoosterManager;
import net.Indyuce.mmocore.manager.social.PartyManager;
@ -48,6 +52,7 @@ import net.Indyuce.mmocore.skill.cast.SkillCastingMode;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.event.EventPriority;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;
@ -76,6 +81,10 @@ public class MMOCore extends JavaPlugin {
public final RestrictionManager restrictionManager = new RestrictionManager();
public final SkillTreeManager skillTreeManager = new SkillTreeManager();
public final StatManager statManager = new StatManager();
public final GuildDataManager nativeGuildManager = new YAMLGuildDataManager();
public final PlayerDataManager playerDataManager = new PlayerDataManager(this);
@Deprecated
public final DataProvider dataProvider = new LegacyDataProvider();
// Profession managers
public final CustomBlockManager mineManager = new CustomBlockManager();
@ -89,7 +98,6 @@ public class MMOCore extends JavaPlugin {
public VaultEconomy economy;
public RegionHandler regionHandler = new DefaultRegionHandler();
public PlaceholderParser placeholderParser = new DefaultParser();
public DataProvider dataProvider = new YAMLDataProvider();
// Modules
@NotNull
@ -140,8 +148,10 @@ public class MMOCore extends JavaPlugin {
getLogger().warning("(Your config version: '" + configVersion + "' | Expected config version: '" + defConfigVersion + "')");
}
if (getConfig().isConfigurationSection("mysql") && getConfig().getBoolean("mysql.enabled"))
dataProvider = new MySQLDataProvider(getConfig());
if (getConfig().isConfigurationSection("mysql") && getConfig().getBoolean("mysql.enabled")) {
final SQLDataSource dataSource = new SQLDataSource(this);
playerDataManager.setDataHandler(new SQLDataHandler(dataSource));
}
if (getConfig().isConfigurationSection("default-playerdata"))
dataProvider.getDataManager().loadDefaultData(getConfig().getConfigurationSection("default-playerdata"));
@ -237,6 +247,7 @@ public class MMOCore extends JavaPlugin {
try {
Class.forName("net.Indyuce.mmocore.MMOCoreBukkit").getConstructor(MMOCore.class).newInstance(this);
} catch (Throwable exception) {
exception.printStackTrace();
throw new RuntimeException("Cannot run an API build on Spigot!");
}
@ -245,7 +256,8 @@ public class MMOCore extends JavaPlugin {
* that after registering all the professses otherwise the player datas can't
* recognize what profess the player has and professes will be lost
*/
Bukkit.getOnlinePlayers().forEach(player -> dataProvider.getDataManager().setup(player.getUniqueId()));
playerDataManager.setupAll();
playerDataManager.registerEvents(EventPriority.NORMAL);
// load guild data after loading player data
dataProvider.getGuildManager().load();
@ -258,15 +270,17 @@ public class MMOCore extends JavaPlugin {
getCommand("mmocore").setExecutor(mmoCoreCommand);
getCommand("mmocore").setTabCompleter(mmoCoreCommand);
if (getConfig().getBoolean("auto-save.enabled")) {
if (getConfig().getBoolean("auto-save.enabled"))
{
int autosave = getConfig().getInt("auto-save.interval") * 20;
new BukkitRunnable() {
public void run() {
// Save player data
for (PlayerData data : PlayerData.getAll())
if (data.isFullyLoaded())
dataProvider.getDataManager().saveData(data, false);
if (data.isSynchronized())
dataProvider.getDataManager().getDataHandler().saveData(data, false);
// Save guild info
for (Guild guild : dataProvider.getGuildManager().getAll())
@ -288,11 +302,11 @@ public class MMOCore extends JavaPlugin {
// Save player data
for (PlayerData data : PlayerData.getAll())
if (data.isFullyLoaded()) {
if (data.isSynchronized()) {
data.close();
//Saves player health before saveData as the player will be considered offline into it if it is async.
// Saves player health before saveData as the player will be considered offline into it if it is async.
data.setHealth(data.getPlayer().getHealth());
dataProvider.getDataManager().saveData(data, true);
dataProvider.getDataManager().getDataHandler().saveData(data, true);
}
// Save guild info
@ -300,8 +314,7 @@ public class MMOCore extends JavaPlugin {
dataProvider.getGuildManager().save(guild);
// Close MySQL data provider (memory leaks)
if (dataProvider instanceof MySQLDataProvider)
((MySQLDataProvider) dataProvider).close();
playerDataManager.getDataHandler().close();
// Reset active blocks
mineManager.resetRemainingBlocks();

View File

@ -1,29 +0,0 @@
package net.Indyuce.mmocore.api.event;
import net.Indyuce.mmocore.api.player.PlayerData;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
/**
* In order to create bukkit async events we must call
* the right constructor:
* <p>
* {@link Event#Event(boolean)} and have the boolean set to true
*/
public abstract class AsyncPlayerDataEvent extends Event {
private final PlayerData playerData;
public AsyncPlayerDataEvent(PlayerData playerData) {
super(true);
this.playerData = playerData;
}
public PlayerData getData() {
return playerData;
}
public Player getPlayer() {
return playerData.getPlayer();
}
}

View File

@ -1,27 +0,0 @@
package net.Indyuce.mmocore.api.event;
import net.Indyuce.mmocore.api.player.PlayerData;
import org.bukkit.event.HandlerList;
public class AsyncPlayerDataLoadEvent extends AsyncPlayerDataEvent {
private static final HandlerList handlers = new HandlerList();
/**
* Called when a player data is being loaded into the game.
* This event is called async.
*
* @param playerData Player data being loaded
*/
public AsyncPlayerDataLoadEvent(PlayerData playerData) {
super(playerData);
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}

View File

@ -1,32 +0,0 @@
package net.Indyuce.mmocore.api.player;
import java.util.UUID;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.player.profess.PlayerClass;
public abstract class OfflinePlayerData {
private final UUID uuid;
public OfflinePlayerData(UUID uuid) {
this.uuid = uuid;
}
public UUID getUniqueId() {
return uuid;
}
public abstract void removeFriend(UUID uuid);
public abstract boolean hasFriend(UUID uuid);
public abstract PlayerClass getProfess();
public abstract int getLevel();
public abstract long getLastLogin();
public static OfflinePlayerData get(UUID uuid) {
return MMOCore.plugin.dataProvider.getDataManager().getOffline(uuid);
}
}

View File

@ -4,6 +4,7 @@ import io.lumine.mythic.lib.MythicLib;
import io.lumine.mythic.lib.api.player.MMOPlayerData;
import io.lumine.mythic.lib.api.stat.StatInstance;
import io.lumine.mythic.lib.api.stat.modifier.StatModifier;
import io.lumine.mythic.lib.data.SynchronizedDataHolder;
import io.lumine.mythic.lib.player.cooldown.CooldownMap;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.ConfigMessage;
@ -19,6 +20,7 @@ import net.Indyuce.mmocore.api.player.profess.PlayerClass;
import net.Indyuce.mmocore.api.player.profess.SavedClassInformation;
import net.Indyuce.mmocore.api.player.profess.Subclass;
import net.Indyuce.mmocore.api.player.profess.resource.PlayerResource;
import net.Indyuce.mmocore.manager.data.OfflinePlayerData;
import net.Indyuce.mmocore.skill.binding.BoundSkillInfo;
import net.Indyuce.mmocore.api.player.social.FriendRequest;
import net.Indyuce.mmocore.api.player.stats.PlayerStats;
@ -68,14 +70,7 @@ import java.util.*;
import java.util.logging.Level;
import java.util.stream.Collectors;
public class PlayerData extends OfflinePlayerData implements Closable, ExperienceTableClaimer, ClassDataContainer {
/**
* Corresponds to the MythicLib player data. It is used to keep
* track of the Player instance corresponding to that player data,
* as well as other things like the last time the player logged in/out
*/
private final MMOPlayerData mmoData;
public class PlayerData extends SynchronizedDataHolder implements OfflinePlayerData, Closable, ExperienceTableClaimer, ClassDataContainer {
/**
* Can be null, the {@link #getProfess()} method will return the
@ -140,16 +135,9 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc
// NON-FINAL player data stuff made public to facilitate field change
public boolean noCooldown;
/**
* Player data is stored in the data map before it's actually fully loaded
* so that external plugins don't necessarily have to listen to the PlayerDataLoadEvent.
*/
private boolean fullyLoaded = false;
public PlayerData(MMOPlayerData mmoData) {
super(mmoData.getUniqueId());
super(mmoData);
this.mmoData = mmoData;
questData = new PlayerQuests(this);
playerStats = new PlayerStats(this);
}
@ -255,7 +243,7 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc
}
public void resetTriggerStats() {
for (StatInstance instance : mmoData.getStatMap().getInstances()) {
for (StatInstance instance : getMMOPlayerData().getStatMap().getInstances()) {
Iterator<StatModifier> iter = instance.getModifiers().iterator();
while (iter.hasNext()) {
StatModifier modifier = iter.next();
@ -459,10 +447,6 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc
if (isCasting()) leaveSkillCasting();
}
public MMOPlayerData getMMOPlayerData() {
return mmoData;
}
public List<UUID> getFriends() {
return friends;
}
@ -475,10 +459,6 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc
return questData;
}
public Player getPlayer() {
return mmoData.getPlayer();
}
public long getLastActivity(PlayerActivity activity) {
return this.lastActivity.getOrDefault(activity, 0l);
}
@ -497,7 +477,7 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc
@Override
public long getLastLogin() {
return mmoData.getLastLogActivity();
return getMMOPlayerData().getLastLogActivity();
}
@Override
@ -592,7 +572,7 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc
}
public boolean isOnline() {
return mmoData.isOnline();
return getMMOPlayerData().isOnline();
}
public boolean inGuild() {
@ -1024,12 +1004,14 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc
stellium = Math.max(0, Math.min(amount, getStats().getStat("MAX_STELLIUM")));
}
@Deprecated
public boolean isFullyLoaded() {
return fullyLoaded;
return isSynchronized();
}
@Deprecated
public void setFullyLoaded() {
this.fullyLoaded = true;
markAsSynchronized();
}
public boolean isCasting() {
@ -1137,7 +1119,7 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc
}
public CooldownMap getCooldownMap() {
return mmoData.getCooldownMap();
return getMMOPlayerData().getCooldownMap();
}
public void setClass(@Nullable PlayerClass profess) {
@ -1230,12 +1212,12 @@ public class PlayerData extends OfflinePlayerData implements Closable, Experienc
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PlayerData that = (PlayerData) o;
return getUniqueId().equals(that.mmoData.getUniqueId());
return getUniqueId().equals(that.getUniqueId());
}
@Override
public int hashCode() {
return mmoData.hashCode();
return getMMOPlayerData().hashCode();
}
public static PlayerData get(OfflinePlayer player) {

View File

@ -2,9 +2,10 @@ package net.Indyuce.mmocore.command.rpg.admin;
import io.lumine.mythic.lib.api.player.MMOPlayerData;
import io.lumine.mythic.lib.command.api.CommandTreeNode;
import io.lumine.mythic.lib.data.sql.SQLDataSource;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.manager.data.mysql.MySQLDataProvider;
import net.Indyuce.mmocore.manager.data.sql.SQLDataHandler;
import org.bukkit.command.CommandSender;
import org.bukkit.scheduler.BukkitRunnable;
@ -51,9 +52,9 @@ public class ExportDataTreeNode extends CommandTreeNode {
.map(file -> UUID.fromString(file.getName().replace(".yml", ""))).collect(Collectors.toList());
// Initialize fake SQL data provider
final MySQLDataProvider sqlProvider;
final SQLDataHandler sqlHandler;
try {
sqlProvider = new MySQLDataProvider(MMOCore.plugin.getConfig());
sqlHandler = new SQLDataHandler(new SQLDataSource(MMOCore.plugin));
} catch (RuntimeException exception) {
sender.sendMessage("Could not initialize SQL provider (see console for stack trace): " + exception.getMessage());
return CommandResult.FAILURE;
@ -80,7 +81,7 @@ public class ExportDataTreeNode extends CommandTreeNode {
if (index >= playerIds.size()) {
cancel();
sqlProvider.close();
sqlHandler.close();
MMOCore.plugin.getLogger().log(Level.WARNING, "Exported " + playerIds.size() + " player datas to SQL database. Total errors: " + errorCount);
return;
}
@ -88,10 +89,10 @@ public class ExportDataTreeNode extends CommandTreeNode {
final UUID playerId = playerIds.get(index);
try {
final PlayerData offlinePlayerData = new PlayerData(new MMOPlayerData(playerId));
MMOCore.plugin.dataProvider.getDataManager().loadData(offlinePlayerData);
MMOCore.plugin.dataProvider.getDataManager().getDataHandler().loadData(offlinePlayerData);
// Player data is loaded, now it gets saved through SQL
sqlProvider.getDataManager().saveData(offlinePlayerData, true);
sqlHandler.saveData(offlinePlayerData, true);
} catch (RuntimeException exception) {
errorCount++;
exception.printStackTrace();

View File

@ -30,7 +30,7 @@ public class SaveDataTreeNode extends CommandTreeNode {
return CommandResult.FAILURE;
}
MMOCore.plugin.dataProvider.getDataManager().saveData(PlayerData.get(player), false);
MMOCore.plugin.dataProvider.getDataManager().getDataHandler().saveData(PlayerData.get(player), false);
return CommandResult.SUCCESS;
}

View File

@ -5,7 +5,7 @@ import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.gui.api.InventoryClickContext;
import net.Indyuce.mmocore.gui.api.GeneratedInventory;
import net.Indyuce.mmocore.gui.api.item.InventoryItem;
import net.Indyuce.mmocore.api.player.OfflinePlayerData;
import net.Indyuce.mmocore.manager.data.OfflinePlayerData;
import net.Indyuce.mmocore.gui.api.EditableInventory;
import net.Indyuce.mmocore.gui.api.item.Placeholders;
import org.bukkit.OfflinePlayer;

View File

@ -9,7 +9,7 @@ import net.Indyuce.mmocore.api.util.input.PlayerInput;
import net.Indyuce.mmocore.gui.api.GeneratedInventory;
import net.Indyuce.mmocore.gui.api.item.InventoryItem;
import net.Indyuce.mmocore.gui.api.item.SimplePlaceholderItem;
import net.Indyuce.mmocore.api.player.OfflinePlayerData;
import net.Indyuce.mmocore.manager.data.OfflinePlayerData;
import net.Indyuce.mmocore.gui.api.EditableInventory;
import net.Indyuce.mmocore.gui.api.item.Placeholders;
import org.apache.commons.lang.Validate;

View File

@ -1,4 +1,4 @@
package net.Indyuce.mmocore.manager.data.yaml;
package net.Indyuce.mmocore.guild.provided;
import java.io.File;
import java.util.ArrayList;

View File

@ -0,0 +1,19 @@
package net.Indyuce.mmocore.manager.data;
import net.Indyuce.mmocore.api.player.profess.PlayerClass;
import java.util.UUID;
public interface AbstractOfflinePlayerData {
public abstract void removeFriend(UUID uuid);
public abstract boolean hasFriend(UUID uuid);
public abstract PlayerClass getProfess();
public abstract int getLevel();
public abstract long getLastLogin();
}

View File

@ -1,15 +1,22 @@
package net.Indyuce.mmocore.manager.data;
import net.Indyuce.mmocore.MMOCore;
/**
* Used to separate MySQL data storage from YAML data storage.
* <p>
* There is one data provider per storage mecanism (one for YAML, one for MySQL).
* A data provider provides corresponding MMOManagers to correctly save and load
* data
*
* @deprecated Not being used anymore, see {@link MMOCore#nativeGuildManager} and {@link MMOCore#playerDataManager}
*/
@Deprecated
public interface DataProvider {
@Deprecated
PlayerDataManager getDataManager();
@Deprecated
GuildDataManager getGuildManager();
}

View File

@ -0,0 +1,18 @@
package net.Indyuce.mmocore.manager.data;
import net.Indyuce.mmocore.MMOCore;
@Deprecated
public class LegacyDataProvider implements DataProvider {
@Override
public PlayerDataManager getDataManager() {
return MMOCore.plugin.playerDataManager;
}
@Override
public GuildDataManager getGuildManager() {
return MMOCore.plugin.nativeGuildManager;
}
}

View File

@ -0,0 +1,24 @@
package net.Indyuce.mmocore.manager.data;
import java.util.UUID;
import io.lumine.mythic.lib.data.OfflineDataHolder;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.player.profess.PlayerClass;
public interface OfflinePlayerData extends OfflineDataHolder {
public abstract void removeFriend(UUID uuid);
public abstract boolean hasFriend(UUID uuid);
public abstract PlayerClass getProfess();
public abstract int getLevel();
public abstract long getLastLogin();
public static OfflinePlayerData get(UUID uuid) {
return MMOCore.plugin.dataProvider.getDataManager().getOffline(uuid);
}
}

View File

@ -1,102 +1,23 @@
package net.Indyuce.mmocore.manager.data;
import io.lumine.mythic.lib.api.player.MMOPlayerData;
import io.lumine.mythic.lib.data.SynchronizedDataManager;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.event.AsyncPlayerDataLoadEvent;
import net.Indyuce.mmocore.api.event.PlayerDataLoadEvent;
import net.Indyuce.mmocore.api.player.OfflinePlayerData;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.manager.data.yaml.YAMLPlayerDataHandler;
import net.Indyuce.mmocore.player.DefaultPlayerData;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.configuration.ConfigurationSection;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
public abstract class PlayerDataManager {
private final static Map<UUID, PlayerData> data = Collections.synchronizedMap(new HashMap<>());
public class PlayerDataManager extends SynchronizedDataManager<PlayerData, OfflinePlayerData> {
private DefaultPlayerData defaultData = DefaultPlayerData.DEFAULT;
public PlayerData get(OfflinePlayer player) {
return get(player.getUniqueId());
public PlayerDataManager(MMOCore plugin) {
super(plugin, new YAMLPlayerDataHandler(plugin));
}
/**
* Gets the player data, or throws an exception if not found.
* The player data should be loaded when the player logs in
* so it's really bad practice to setup the player data if it's not loaded.
*
* @param uuid Player UUID
* @return Player data, if it's loaded
*/
public PlayerData get(UUID uuid) {
return Objects.requireNonNull(data.get(uuid), "Player data is not loaded");
}
/**
* Safely unregisters the player data from the map.
* This saves the player data either through SQL or YAML,
* then closes the player data and clears it from the data map.
*
* @param playerData PLayer data to unregister
*/
public void unregisterSafe(PlayerData playerData) {
// Close and unregister data instantly if no error occured
playerData.close();
data.remove(playerData.getUniqueId());
// Save data async if required
if (playerData.isFullyLoaded())
Bukkit.getScheduler().runTaskAsynchronously(MMOCore.plugin, () -> saveData(playerData, true));
}
/**
* Offline player data is used to handle processes like friend removal
* which can still occur if one of the two players is offline.
* <p>
* Unlike {@link #get(UUID)} this method never returns a null instance
*
* @param uuid Player unique id
* @return Offline player data
*/
@NotNull
public abstract OfflinePlayerData getOffline(UUID uuid);
/**
* Called when a player logs in, loading the player data inside the map.
* <p>
* For YAML configs or SQL databases, data is loaded as not to overload
* the main thread with SQL requests. Therefore, the player data returned
* by that method, when the player joined for the first time, is not
* fully loaded YET.
*
* @param uniqueId Player UUID
* @return The loaded player data.
*/
public PlayerData setup(UUID uniqueId) {
// Load player data if it does not exist
final @Nullable PlayerData current = data.get(uniqueId);
if (current != null)
return current;
final PlayerData newData = new PlayerData(MMOPlayerData.get(uniqueId));
// Schedule async data loading
Bukkit.getScheduler().runTaskAsynchronously(MMOCore.plugin, () -> {
loadData(newData);
newData.getStats().updateStats();
Bukkit.getPluginManager().callEvent(new AsyncPlayerDataLoadEvent(newData));
Bukkit.getScheduler().runTask(MMOCore.plugin, () -> Bukkit.getPluginManager().callEvent(new PlayerDataLoadEvent(newData)));
});
// Update data map and return
data.put(uniqueId, newData);
return newData;
@Override
public PlayerData newPlayerData(MMOPlayerData playerData) {
return new PlayerData(playerData);
}
public DefaultPlayerData getDefaultData() {
@ -106,26 +27,4 @@ public abstract class PlayerDataManager {
public void loadDefaultData(ConfigurationSection config) {
defaultData = new DefaultPlayerData(config);
}
public boolean isLoaded(UUID uuid) {
return data.containsKey(uuid);
}
public Collection<PlayerData> getLoaded() {
return data.values();
}
public abstract void loadData(PlayerData data);
/**
* Called when player data must be saved in configs or database.
* This method should always be called sync because it DOES register
* an async task in case MySQL storage is used.
*
* @param data Player data to save
* @param logout When logging out, is_saved is switched back to 1. This parameter
* must be turned off when auto-saving because the player doesn't
* actually leave the server.
*/
public abstract void saveData(PlayerData data, boolean logout);
}

View File

@ -1,154 +0,0 @@
package net.Indyuce.mmocore.manager.data.mysql;
import com.google.gson.*;
import io.lumine.mythic.lib.UtilityMethods;
import io.lumine.mythic.lib.sql.DataSynchronizer;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.profess.PlayerClass;
import net.Indyuce.mmocore.api.player.profess.SavedClassInformation;
import net.Indyuce.mmocore.api.util.MMOCoreUtils;
import net.Indyuce.mmocore.guild.provided.Guild;
import net.Indyuce.mmocore.skilltree.SkillTreeNode;
import net.Indyuce.mmocore.skilltree.tree.SkillTree;
import org.apache.commons.lang.Validate;
import org.bukkit.attribute.Attribute;
import org.jetbrains.annotations.Nullable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.util.logging.Level;
public class MMOCoreDataSynchronizer extends DataSynchronizer {
private final PlayerData data;
private final MySQLPlayerDataManager manager;
public MMOCoreDataSynchronizer(MySQLPlayerDataManager manager, PlayerData data) {
super("mmocore_playerdata", "uuid", manager.getProvider(), data.getUniqueId());
this.manager = manager;
this.data = data;
}
@Override
public void loadData(ResultSet result) throws SQLException {
//Reset stats linked to triggers
data.resetTriggerStats();
data.setClassPoints(result.getInt("class_points"));
data.setSkillPoints(result.getInt("skill_points"));
data.setSkillReallocationPoints(result.getInt("skill_reallocation_points"));
data.setSkillTreeReallocationPoints(result.getInt("skill_tree_reallocation_points"));
data.setAttributePoints(result.getInt("attribute_points"));
data.setAttributeReallocationPoints(result.getInt("attribute_realloc_points"));
data.setLevel(result.getInt("level"));
data.setExperience(result.getInt("experience"));
if (!isEmpty(result.getString("class")))
data.setClass(MMOCore.plugin.classManager.get(result.getString("class")));
if (!isEmpty(result.getString("times_claimed"))) {
JsonObject json = new JsonParser().parse(result.getString("times_claimed")).getAsJsonObject();
json.entrySet().forEach(entry -> data.getItemClaims().put(entry.getKey(), entry.getValue().getAsInt()));
}
if (!isEmpty(result.getString("skill_tree_points"))) {
JsonObject json = new JsonParser().parse(result.getString("skill_tree_points")).getAsJsonObject();
for (SkillTree skillTree : MMOCore.plugin.skillTreeManager.getAll()) {
data.setSkillTreePoints(skillTree.getId(), json.has(skillTree.getId()) ? json.get(skillTree.getId()).getAsInt() : 0);
}
data.setSkillTreePoints("global", json.has("global") ? json.get("global").getAsInt() : 0);
}
if (!isEmpty(result.getString("skill_tree_levels"))) {
JsonObject json = new JsonParser().parse(result.getString("skill_tree_levels")).getAsJsonObject();
for (SkillTreeNode skillTreeNode : MMOCore.plugin.skillTreeManager.getAllNodes()) {
data.setNodeLevel(skillTreeNode, json.has(skillTreeNode.getFullId()) ? json.get(skillTreeNode.getFullId()).getAsInt() : 0);
}
}
data.setupSkillTree();
Set<String> unlockedItems = new HashSet<>();
if (!isEmpty(result.getString("unlocked_items"))) {
JsonArray unlockedItemsArray = new JsonParser().parse(result.getString("unlocked_items")).getAsJsonArray();
for (JsonElement item : unlockedItemsArray)
unlockedItems.add(item.getAsString());
}
data.setUnlockedItems(unlockedItems);
if (!isEmpty(result.getString("guild"))) {
final Guild guild = MMOCore.plugin.dataProvider.getGuildManager().getGuild(result.getString("guild"));
if (guild != null)
data.setGuild(guild.hasMember(data.getUniqueId()) ? guild : null);
}
if (!isEmpty(result.getString("attributes")))
data.getAttributes().load(result.getString("attributes"));
if (!isEmpty(result.getString("professions")))
data.getCollectionSkills().load(result.getString("professions"));
if (!isEmpty(result.getString("quests")))
data.getQuestData().load(result.getString("quests"));
data.getQuestData().updateBossBar();
if (!isEmpty(result.getString("waypoints")))
data.getWaypoints().addAll(MMOCoreUtils.jsonArrayToList(result.getString("waypoints")));
if (!isEmpty(result.getString("friends")))
MMOCoreUtils.jsonArrayToList(result.getString("friends")).forEach(str -> data.getFriends().add(UUID.fromString(str)));
if (!isEmpty(result.getString("skills"))) {
JsonObject object = new Gson().fromJson(result.getString("skills"), JsonObject.class);
for (Map.Entry<String, JsonElement> entry : object.entrySet())
data.setSkillLevel(entry.getKey(), entry.getValue().getAsInt());
}
if (!isEmpty(result.getString("bound_skills"))) {
JsonObject object = new Gson().fromJson(result.getString("bound_skills"), JsonObject.class);
for (Map.Entry<String, JsonElement> entry : object.entrySet())
data.bindSkill(Integer.parseInt(entry.getKey()), data.getProfess().getSkill(entry.getValue().getAsString()));
}
if (!isEmpty(result.getString("class_info"))) {
JsonObject object = new Gson().fromJson(result.getString("class_info"), JsonObject.class);
for (Map.Entry<String, JsonElement> entry : object.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());
}
}
}
/*
* These should be loaded after to make sure that the
* MAX_MANA, MAX_STAMINA & MAX_STELLIUM stats are already loaded.
*/
data.setHealth(result.getDouble("health"));
data.setMana(result.getDouble("mana"));
data.setStamina(result.getDouble("stamina"));
data.setStellium(result.getDouble("stellium"));
if (data.isOnline()) {
double health = data.getHealth();
health = health == 0 ? data.getPlayer().getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue() : health;
health = Math.max(Math.min(health, data.getPlayer().getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue()), 0);
data.getPlayer().setHealth(health);
}
UtilityMethods.debug(MMOCore.plugin, "SQL", String.format("{ class: %s, level: %d }", data.getProfess().getId(), data.getLevel()));
data.setFullyLoaded();
}
private boolean isEmpty(@Nullable String str) {
return str == null || str.equalsIgnoreCase("null") || str.equalsIgnoreCase("{}") || str.equalsIgnoreCase("[]") || str.equalsIgnoreCase("");
}
@Override
public void loadEmptyData() {
data.setLevel(manager.getDefaultData().getLevel());
data.setClassPoints(manager.getDefaultData().getClassPoints());
data.setSkillPoints(manager.getDefaultData().getSkillPoints());
data.setSkillReallocationPoints(manager.getDefaultData().getSkillReallocationPoints());
data.setAttributePoints(manager.getDefaultData().getAttributePoints());
data.setAttributeReallocationPoints(manager.getDefaultData().getAttributeReallocationPoints());
data.setExperience(0);
data.getQuestData().updateBossBar();
data.setFullyLoaded();
UtilityMethods.debug(MMOCore.plugin, "SQL", "Loaded DEFAULT data for: '" + data.getUniqueId() + "' as no saved data was found.");
}
}

View File

@ -1,94 +0,0 @@
package net.Indyuce.mmocore.manager.data.mysql;
import io.lumine.mythic.lib.sql.MMODataSource;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.manager.data.DataProvider;
import net.Indyuce.mmocore.manager.data.GuildDataManager;
import net.Indyuce.mmocore.manager.data.PlayerDataManager;
import net.Indyuce.mmocore.manager.data.yaml.YAMLGuildDataManager;
import org.bukkit.configuration.file.FileConfiguration;
import java.sql.SQLException;
public class MySQLDataProvider extends MMODataSource implements DataProvider {
private final MySQLPlayerDataManager playerManager = new MySQLPlayerDataManager(this);
private final YAMLGuildDataManager guildManager = new YAMLGuildDataManager();
private static final String[] NEW_COLUMNS = new String[]{
"times_claimed", "LONGTEXT",
"is_saved", "TINYINT",
"skill_reallocation_points", "INT(11)",
"skill_tree_reallocation_points", "INT(11)",
"skill_tree_points", "LONGTEXT",
"skill_tree_levels", "LONGTEXT",
"unlocked_items","LONGTEXT",
"health", "FLOAT",
"mana", "FLOAT",
"stamina", "FLOAT",
"stellium", "FLOAT"};
public MySQLDataProvider(FileConfiguration config) {
super(MMOCore.plugin);
this.setup(config);
}
@Override
public void load() {
// Fully create table
executeUpdateAsync("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) DEFAULT 0," +
"attribute_realloc_points INT(11) DEFAULT 0," +
"skill_reallocation_points INT(11) DEFAULT 0," +
"skill_tree_reallocation_points INT(11) DEFAULT 0," +
"skill_tree_points LONGTEXT," +
"skill_tree_levels LONGTEXT," +
"level INT(11) DEFAULT 1," +
"experience INT(11) DEFAULT 0," +
"class VARCHAR(20),guild VARCHAR(20)," +
"last_login LONG," +
"attributes LONGTEXT," +
"professions LONGTEXT," +
"times_claimed LONGTEXT," +
"quests LONGTEXT," +
"waypoints LONGTEXT," +
"friends LONGTEXT," +
"skills LONGTEXT," +
"bound_skills LONGTEXT," +
"health FLOAT," +
"mana FLOAT," +
"stamina FLOAT," +
"stellium FLOAT," +
"unlocked_items LONGTEXT," +
"class_info LONGTEXT," +
"is_saved TINYINT," +
"PRIMARY KEY (uuid));");
// Add columns that might not be here by default
for (int i = 0; i < NEW_COLUMNS.length; i += 2) {
final String columnName = NEW_COLUMNS[i];
final String dataType = NEW_COLUMNS[i + 1];
getResultAsync("SELECT * FROM information_schema.COLUMNS WHERE TABLE_NAME = 'mmocore_playerdata' AND COLUMN_NAME = '" + columnName + "'", result -> {
try {
if (!result.next())
executeUpdate("ALTER TABLE mmocore_playerdata ADD COLUMN " + columnName + " " + dataType);
} catch (SQLException exception) {
exception.printStackTrace();
}
});
}
}
@Override
public PlayerDataManager getDataManager() {
return playerManager;
}
@Override
public GuildDataManager getGuildManager() {
return guildManager;
}
}

View File

@ -0,0 +1,152 @@
package net.Indyuce.mmocore.manager.data.sql;
import com.google.gson.*;
import io.lumine.mythic.lib.MythicLib;
import io.lumine.mythic.lib.UtilityMethods;
import io.lumine.mythic.lib.data.sql.SQLDataSynchronizer;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.profess.PlayerClass;
import net.Indyuce.mmocore.api.player.profess.SavedClassInformation;
import net.Indyuce.mmocore.api.util.MMOCoreUtils;
import net.Indyuce.mmocore.guild.provided.Guild;
import net.Indyuce.mmocore.manager.data.PlayerDataManager;
import net.Indyuce.mmocore.skilltree.SkillTreeNode;
import net.Indyuce.mmocore.skilltree.tree.SkillTree;
import org.apache.commons.lang.Validate;
import org.bukkit.attribute.Attribute;
import org.jetbrains.annotations.Nullable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.logging.Level;
public class MMOCoreDataSynchronizer extends SQLDataSynchronizer<PlayerData> {
public MMOCoreDataSynchronizer(SQLDataHandler handler, PlayerData data) {
super("mmocore_playerdata", "uuid", handler.getDataSource(), data);
}
@Override
public void loadData(ResultSet result) throws SQLException {
//Reset stats linked to triggers
getData().resetTriggerStats();
getData().setClassPoints(result.getInt("class_points"));
getData().setSkillPoints(result.getInt("skill_points"));
getData().setSkillReallocationPoints(result.getInt("skill_reallocation_points"));
getData().setSkillTreeReallocationPoints(result.getInt("skill_tree_reallocation_points"));
getData().setAttributePoints(result.getInt("attribute_points"));
getData().setAttributeReallocationPoints(result.getInt("attribute_realloc_points"));
getData().setLevel(result.getInt("level"));
getData().setExperience(result.getInt("experience"));
if (!isEmpty(result.getString("class")))
getData().setClass(MMOCore.plugin.classManager.get(result.getString("class")));
if (!isEmpty(result.getString("times_claimed"))) {
JsonObject json = new JsonParser().parse(result.getString("times_claimed")).getAsJsonObject();
json.entrySet().forEach(entry -> getData().getItemClaims().put(entry.getKey(), entry.getValue().getAsInt()));
}
if (!isEmpty(result.getString("skill_tree_points"))) {
JsonObject json = new JsonParser().parse(result.getString("skill_tree_points")).getAsJsonObject();
for (SkillTree skillTree : MMOCore.plugin.skillTreeManager.getAll()) {
getData().setSkillTreePoints(skillTree.getId(), json.has(skillTree.getId()) ? json.get(skillTree.getId()).getAsInt() : 0);
}
getData().setSkillTreePoints("global", json.has("global") ? json.get("global").getAsInt() : 0);
}
if (!isEmpty(result.getString("skill_tree_levels"))) {
JsonObject json = new JsonParser().parse(result.getString("skill_tree_levels")).getAsJsonObject();
for (SkillTreeNode skillTreeNode : MMOCore.plugin.skillTreeManager.getAllNodes()) {
getData().setNodeLevel(skillTreeNode, json.has(skillTreeNode.getFullId()) ? json.get(skillTreeNode.getFullId()).getAsInt() : 0);
}
}
getData().setupSkillTree();
Set<String> unlockedItems = new HashSet<>();
if (!isEmpty(result.getString("unlocked_items"))) {
JsonArray unlockedItemsArray = new JsonParser().parse(result.getString("unlocked_items")).getAsJsonArray();
for (JsonElement item : unlockedItemsArray)
unlockedItems.add(item.getAsString());
}
getData().setUnlockedItems(unlockedItems);
if (!isEmpty(result.getString("guild"))) {
final Guild guild = MMOCore.plugin.dataProvider.getGuildManager().getGuild(result.getString("guild"));
if (guild != null)
getData().setGuild(guild.hasMember(getData().getUniqueId()) ? guild : null);
}
if (!isEmpty(result.getString("attributes")))
getData().getAttributes().load(result.getString("attributes"));
if (!isEmpty(result.getString("professions")))
getData().getCollectionSkills().load(result.getString("professions"));
if (!isEmpty(result.getString("quests")))
getData().getQuestData().load(result.getString("quests"));
getData().getQuestData().updateBossBar();
if (!isEmpty(result.getString("waypoints")))
getData().getWaypoints().addAll(MMOCoreUtils.jsonArrayToList(result.getString("waypoints")));
if (!isEmpty(result.getString("friends")))
MMOCoreUtils.jsonArrayToList(result.getString("friends")).forEach(str -> getData().getFriends().add(UUID.fromString(str)));
if (!isEmpty(result.getString("skills"))) {
JsonObject object = MythicLib.plugin.getGson().fromJson(result.getString("skills"), JsonObject.class);
for (Map.Entry<String, JsonElement> entry : object.entrySet())
getData().setSkillLevel(entry.getKey(), entry.getValue().getAsInt());
}
if (!isEmpty(result.getString("bound_skills"))) {
JsonObject object = MythicLib.plugin.getGson().fromJson(result.getString("bound_skills"), JsonObject.class);
for (Map.Entry<String, JsonElement> entry : object.entrySet())
getData().bindSkill(Integer.parseInt(entry.getKey()), getData().getProfess().getSkill(entry.getValue().getAsString()));
}
if (!isEmpty(result.getString("class_info"))) {
JsonObject object = MythicLib.plugin.getGson().fromJson(result.getString("class_info"), JsonObject.class);
for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
try {
PlayerClass profess = MMOCore.plugin.classManager.get(entry.getKey());
Validate.notNull(profess, "Could not find class '" + entry.getKey() + "'");
getData().applyClassInfo(profess, new SavedClassInformation(entry.getValue().getAsJsonObject()));
} catch (IllegalArgumentException exception) {
MMOCore.log(Level.WARNING, "Could not load class info '" + entry.getKey() + "': " + exception.getMessage());
}
}
}
/*
* These should be loaded after to make sure that the
* MAX_MANA, MAX_STAMINA & MAX_STELLIUM stats are already loaded.
*/
getData().setHealth(result.getDouble("health"));
getData().setMana(result.getDouble("mana"));
getData().setStamina(result.getDouble("stamina"));
getData().setStellium(result.getDouble("stellium"));
if (getData().isOnline()) {
double health = getData().getHealth();
health = health == 0 ? getData().getPlayer().getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue() : health;
health = Math.max(Math.min(health, getData().getPlayer().getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue()), 0);
getData().getPlayer().setHealth(health);
}
UtilityMethods.debug(MMOCore.plugin, "SQL", String.format("{ class: %s, level: %d }", getData().getProfess().getId(), getData().getLevel()));
}
private boolean isEmpty(@Nullable String str) {
return str == null || str.equalsIgnoreCase("null") || str.equalsIgnoreCase("{}") || str.equalsIgnoreCase("[]") || str.equalsIgnoreCase("");
}
@Override
public void loadEmptyData() {
final PlayerDataManager manager = MMOCore.plugin.playerDataManager;
getData().setLevel(manager.getDefaultData().getLevel());
getData().setClassPoints(manager.getDefaultData().getClassPoints());
getData().setSkillPoints(manager.getDefaultData().getSkillPoints());
getData().setSkillReallocationPoints(manager.getDefaultData().getSkillReallocationPoints());
getData().setAttributePoints(manager.getDefaultData().getAttributePoints());
getData().setAttributeReallocationPoints(manager.getDefaultData().getAttributeReallocationPoints());
getData().setExperience(0);
getData().getQuestData().updateBossBar();
UtilityMethods.debug(MMOCore.plugin, "SQL", "Loaded DEFAULT data for: '" + getData().getUniqueId() + "' as no saved data was found.");
}
}

View File

@ -1,10 +1,11 @@
package net.Indyuce.mmocore.manager.data.mysql;
package net.Indyuce.mmocore.manager.data.sql;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import io.lumine.mythic.lib.data.sql.SQLDataSource;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.manager.data.yaml.YAMLPlayerDataManager;
import net.Indyuce.mmocore.manager.data.yaml.YAMLPlayerDataHandler;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -19,15 +20,15 @@ import java.util.logging.Level;
public class PlayerDataTableUpdater {
private final PlayerData playerData;
private final MySQLDataProvider provider;
private final SQLDataSource provider;
private final Map<String, String> requestMap = new HashMap<>();
public PlayerDataTableUpdater(MySQLDataProvider provider, PlayerData playerData) {
public PlayerDataTableUpdater(SQLDataSource provider, PlayerData playerData) {
this.playerData = playerData;
this.provider = provider;
}
public void executeRequest(boolean logout) {
public void executeRequest(boolean autosave) {
final String request = "INSERT INTO mmocore_playerdata(uuid, " + formatCollection(requestMap.keySet(), false)
+ ") VALUES('" + playerData.getUniqueId() + "'," + formatCollection(requestMap.values(), true) + ")" +
" ON DUPLICATE KEY UPDATE " + formatMap() + ";";
@ -40,21 +41,21 @@ public class PlayerDataTableUpdater {
statement.executeUpdate();
} catch (SQLException exception) {
MMOCore.log(Level.WARNING, "Could not save player data of " + playerData.getUniqueId() + ", saving through YAML instead");
new YAMLPlayerDataManager(provider).saveData(playerData, logout);
new YAMLPlayerDataHandler(MMOCore.plugin).saveData(playerData, autosave);
exception.printStackTrace();
} finally {
statement.close();
}
} catch (SQLException exception) {
MMOCore.log(Level.WARNING, "Could not save player data of " + playerData.getUniqueId() + ", saving through YAML instead");
new YAMLPlayerDataManager(provider).saveData(playerData, logout);
new YAMLPlayerDataHandler(MMOCore.plugin).saveData(playerData, autosave);
exception.printStackTrace();
} finally {
connection.close();
}
} catch (SQLException exception) {
MMOCore.log(Level.WARNING, "Could not save player data of " + playerData.getUniqueId() + ", saving through YAML instead");
new YAMLPlayerDataManager(provider).saveData(playerData, logout);
new YAMLPlayerDataHandler(MMOCore.plugin).saveData(playerData, autosave);
exception.printStackTrace();
}
}
@ -95,7 +96,7 @@ public class PlayerDataTableUpdater {
addData(key, json.toString());
}
public <T,V> void addJSONObject(String key, Set<Map.Entry<T, V>> collection) {
public <T, V> void addJSONObject(String key, Set<Map.Entry<T, V>> collection) {
JsonObject json = new JsonObject();
for (Map.Entry<T, V> entry : collection)
json.addProperty(entry.getKey().toString(), entry.getValue().toString());

View File

@ -1,42 +1,100 @@
package net.Indyuce.mmocore.manager.data.mysql;
package net.Indyuce.mmocore.manager.data.sql;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import io.lumine.mythic.lib.UtilityMethods;
import io.lumine.mythic.lib.data.sql.SQLDataSource;
import io.lumine.mythic.lib.data.sql.SQLSynchronizedDataHandler;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.player.OfflinePlayerData;
import net.Indyuce.mmocore.manager.data.OfflinePlayerData;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.profess.PlayerClass;
import net.Indyuce.mmocore.api.player.profess.SavedClassInformation;
import net.Indyuce.mmocore.manager.data.PlayerDataManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.sql.SQLException;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
public class MySQLPlayerDataManager extends PlayerDataManager {
private final MySQLDataProvider provider;
public MySQLPlayerDataManager(MySQLDataProvider provider) {
this.provider = provider;
public class SQLDataHandler extends SQLSynchronizedDataHandler<PlayerData, OfflinePlayerData, MMOCoreDataSynchronizer> {
public SQLDataHandler(SQLDataSource dataSource) {
super(dataSource);
}
public MySQLDataProvider getProvider() {
return provider;
private static final String[] NEW_COLUMNS = new String[]{
"times_claimed", "LONGTEXT",
"is_saved", "TINYINT",
"skill_reallocation_points", "INT(11)",
"skill_tree_reallocation_points", "INT(11)",
"skill_tree_points", "LONGTEXT",
"skill_tree_levels", "LONGTEXT",
"unlocked_items", "LONGTEXT",
"health", "FLOAT",
"mana", "FLOAT",
"stamina", "FLOAT",
"stellium", "FLOAT"};
@Override
public void setup() {
// Fully create table
getDataSource().executeUpdateAsync("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) DEFAULT 0," +
"attribute_realloc_points INT(11) DEFAULT 0," +
"skill_reallocation_points INT(11) DEFAULT 0," +
"skill_tree_reallocation_points INT(11) DEFAULT 0," +
"skill_tree_points LONGTEXT," +
"skill_tree_levels LONGTEXT," +
"level INT(11) DEFAULT 1," +
"experience INT(11) DEFAULT 0," +
"class VARCHAR(20),guild VARCHAR(20)," +
"last_login LONG," +
"attributes LONGTEXT," +
"professions LONGTEXT," +
"times_claimed LONGTEXT," +
"quests LONGTEXT," +
"waypoints LONGTEXT," +
"friends LONGTEXT," +
"skills LONGTEXT," +
"bound_skills LONGTEXT," +
"health FLOAT," +
"mana FLOAT," +
"stamina FLOAT," +
"stellium FLOAT," +
"unlocked_items LONGTEXT," +
"class_info LONGTEXT," +
"is_saved TINYINT," +
"PRIMARY KEY (uuid));");
// Add columns that might not be here by default
for (int i = 0; i < NEW_COLUMNS.length; i += 2) {
final String columnName = NEW_COLUMNS[i];
final String dataType = NEW_COLUMNS[i + 1];
getDataSource().getResultAsync("SELECT * FROM information_schema.COLUMNS WHERE TABLE_NAME = 'mmocore_playerdata' AND COLUMN_NAME = '" + columnName + "'", result -> {
try {
if (!result.next())
getDataSource().executeUpdate("ALTER TABLE mmocore_playerdata ADD COLUMN " + columnName + " " + dataType);
} catch (SQLException exception) {
exception.printStackTrace();
}
});
}
}
@Override
public void loadData(PlayerData data) {
new MMOCoreDataSynchronizer(this, data).fetch();
public MMOCoreDataSynchronizer newDataSynchronizer(PlayerData playerData) {
return new MMOCoreDataSynchronizer(this, playerData);
}
@Override
public void saveData(PlayerData data, boolean logout) {
public void saveData(PlayerData data, boolean autosave) {
UtilityMethods.debug(MMOCore.plugin, "SQL", "Saving data for: '" + data.getUniqueId() + "'...");
final PlayerDataTableUpdater updater = new PlayerDataTableUpdater(provider, data);
final PlayerDataTableUpdater updater = new PlayerDataTableUpdater(getDataSource(), data);
updater.addData("class_points", data.getClassPoints());
updater.addData("skill_points", data.getSkillPoints());
updater.addData("skill_reallocation_points", data.getSkillReallocationPoints());
@ -44,7 +102,7 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
updater.addData("attribute_realloc_points", data.getAttributeReallocationPoints());
updater.addJSONArray("waypoints", data.getWaypoints());
updater.addData("skill_tree_reallocation_points", data.getSkillTreeReallocationPoints());
updater.addData("health",data.getHealth());
updater.addData("health", data.getHealth());
updater.addData("mana", data.getMana());
updater.addData("stellium", data.getStellium());
updater.addData("stamina", data.getStamina());
@ -65,10 +123,10 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
updater.addData("quests", data.getQuestData().toJsonString());
updater.addData("class_info", createClassInfoData(data).toString());
updater.addJSONArray("unlocked_items", data.getUnlockedItems());
if (logout)
if (!autosave)
updater.addData("is_saved", 1);
updater.executeRequest(logout);
updater.executeRequest(autosave);
UtilityMethods.debug(MMOCore.plugin, "SQL", "Saved data for: " + data.getUniqueId());
UtilityMethods.debug(MMOCore.plugin, "SQL", String.format("{ class: %s, level: %d }", data.getProfess().getId(), data.getLevel()));
@ -132,17 +190,20 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
@NotNull
@Override
public OfflinePlayerData getOffline(UUID uuid) {
return isLoaded(uuid) ? get(uuid) : new MySQLOfflinePlayerData(uuid);
return new MySQLOfflinePlayerData(uuid);
}
public class MySQLOfflinePlayerData extends OfflinePlayerData {
@Deprecated
public class MySQLOfflinePlayerData implements OfflinePlayerData {
private final UUID uuid;
private int level;
private long lastLogin;
private PlayerClass profess;
private List<UUID> friends;
@Deprecated
public MySQLOfflinePlayerData(UUID uuid) {
super(uuid);
this.uuid = uuid;
/*
provider.getResult("SELECT * FROM mmocore_playerdata WHERE uuid = '" + uuid + "';", (result) -> {
try {
@ -168,6 +229,12 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
}); */
}
@Override
@NotNull
public UUID getUniqueId() {
return uuid;
}
@Override
public void removeFriend(UUID uuid) {
// TODO recode

View File

@ -1,20 +0,0 @@
package net.Indyuce.mmocore.manager.data.yaml;
import net.Indyuce.mmocore.manager.data.DataProvider;
import net.Indyuce.mmocore.manager.data.GuildDataManager;
import net.Indyuce.mmocore.manager.data.PlayerDataManager;
public class YAMLDataProvider implements DataProvider {
private final YAMLPlayerDataManager playerManager = new YAMLPlayerDataManager(this);
private final YAMLGuildDataManager guildManager = new YAMLGuildDataManager();
@Override
public PlayerDataManager getDataManager() {
return playerManager;
}
@Override
public GuildDataManager getGuildManager() {
return guildManager;
}
}

View File

@ -6,46 +6,55 @@ import java.util.UUID;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.player.profess.PlayerClass;
import net.Indyuce.mmocore.api.ConfigFile;
import net.Indyuce.mmocore.api.player.OfflinePlayerData;
import net.Indyuce.mmocore.manager.data.OfflinePlayerData;
import org.jetbrains.annotations.NotNull;
public class YAMLOfflinePlayerData extends OfflinePlayerData {
private final ConfigFile config;
@Deprecated
public class YAMLOfflinePlayerData implements OfflinePlayerData {
private final UUID uuid;
private final ConfigFile config;
/**
* Supports offline player data operations like friend removals which can't
* be handled when their player data is not loaded in the data map.
*/
public YAMLOfflinePlayerData(UUID uuid) {
super(uuid);
/**
* Supports offline player data operations like friend removals which can't
* be handled when their player data is not loaded in the data map.
*/
@Deprecated
public YAMLOfflinePlayerData(UUID uuid) {
this.uuid = uuid;
config = new ConfigFile(uuid);
}
config = new ConfigFile(uuid);
}
@Override
@NotNull
public UUID getUniqueId() {
return uuid;
}
@Override
public void removeFriend(UUID uuid) {
List<String> friends = config.getConfig().getStringList("friends");
friends.remove(uuid.toString());
config.getConfig().set("friends", friends);
config.save();
}
@Override
public void removeFriend(UUID uuid) {
List<String> friends = config.getConfig().getStringList("friends");
friends.remove(uuid.toString());
config.getConfig().set("friends", friends);
config.save();
}
@Override
public boolean hasFriend(UUID uuid) {
return config.getConfig().getStringList("friends").contains(uuid.toString());
}
@Override
public boolean hasFriend(UUID uuid) {
return config.getConfig().getStringList("friends").contains(uuid.toString());
}
@Override
public PlayerClass getProfess() {
return config.getConfig().contains("class") ? MMOCore.plugin.classManager.get(config.getConfig().getString("class")) : MMOCore.plugin.classManager.getDefaultClass();
}
@Override
public PlayerClass getProfess() {
return config.getConfig().contains("class") ? MMOCore.plugin.classManager.get(config.getConfig().getString("class")) : MMOCore.plugin.classManager.getDefaultClass();
}
@Override
public int getLevel() {
return config.getConfig().getInt("level");
}
@Override
public int getLevel() {
return config.getConfig().getInt("level");
}
@Override
public long getLastLogin() {
return config.getConfig().getLong("last-login");
}
@Override
public long getLastLogin() {
return config.getConfig().getLong("last-login");
}
}

View File

@ -1,54 +1,61 @@
package net.Indyuce.mmocore.manager.data.yaml;
import io.lumine.mythic.lib.data.yaml.YAMLSynchronizedDataHandler;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.ConfigFile;
import net.Indyuce.mmocore.api.player.OfflinePlayerData;
import net.Indyuce.mmocore.manager.data.OfflinePlayerData;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.profess.PlayerClass;
import net.Indyuce.mmocore.api.player.profess.SavedClassInformation;
import net.Indyuce.mmocore.api.util.MMOCoreUtils;
import net.Indyuce.mmocore.guild.provided.Guild;
import net.Indyuce.mmocore.manager.data.DataProvider;
import net.Indyuce.mmocore.manager.data.PlayerDataManager;
import net.Indyuce.mmocore.player.DefaultPlayerData;
import net.Indyuce.mmocore.skill.ClassSkill;
import net.Indyuce.mmocore.skilltree.SkillTreeNode;
import org.apache.commons.lang.Validate;
import org.bukkit.attribute.Attribute;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import java.util.*;
import java.util.logging.Level;
import java.util.stream.Collectors;
public class YAMLPlayerDataManager extends PlayerDataManager {
private final DataProvider provider;
public YAMLPlayerDataManager(DataProvider provider) {
this.provider = provider;
public class YAMLPlayerDataHandler extends YAMLSynchronizedDataHandler<PlayerData, OfflinePlayerData> {
public YAMLPlayerDataHandler(Plugin owning) {
super(owning);
}
@Override
public void loadData(PlayerData data) {
FileConfiguration config = new ConfigFile(data.getUniqueId()).getConfig();
public void setup() {
}
@Override
public void close() {
}
@Override
public void loadFromSection(PlayerData data, ConfigurationSection config) {
// Reset stats linked to triggers.
data.resetTriggerStats();
data.setClassPoints(config.getInt("class-points", getDefaultData().getClassPoints()));
data.setSkillPoints(config.getInt("skill-points", getDefaultData().getSkillPoints()));
data.setSkillReallocationPoints(config.getInt("skill-reallocation-points", getDefaultData().getSkillReallocationPoints()));
data.setSkillTreeReallocationPoints(config.getInt("skill-tree-reallocation-points", getDefaultData().getSkillTreeReallocationPoints()));
data.setAttributePoints(config.getInt("attribute-points", getDefaultData().getAttributePoints()));
data.setAttributeReallocationPoints(config.getInt("attribute-realloc-points", getDefaultData().getAttributeReallocationPoints()));
data.setLevel(config.getInt("level", getDefaultData().getLevel()));
final DefaultPlayerData defaultData = MMOCore.plugin.playerDataManager.getDefaultData();
data.setClassPoints(config.getInt("class-points", defaultData.getClassPoints()));
data.setSkillPoints(config.getInt("skill-points", defaultData.getSkillPoints()));
data.setSkillReallocationPoints(config.getInt("skill-reallocation-points", defaultData.getSkillReallocationPoints()));
data.setSkillTreeReallocationPoints(config.getInt("skill-tree-reallocation-points", defaultData.getSkillTreeReallocationPoints()));
data.setAttributePoints(config.getInt("attribute-points", defaultData.getAttributePoints()));
data.setAttributeReallocationPoints(config.getInt("attribute-realloc-points", defaultData.getAttributeReallocationPoints()));
data.setLevel(config.getInt("level", defaultData.getLevel()));
data.setExperience(config.getInt("experience"));
if (config.contains("class"))
data.setClass(MMOCore.plugin.classManager.get(config.getString("class")));
if (config.contains("guild")) {
Guild guild = provider.getGuildManager().getGuild(config.getString("guild"));
Guild guild = MMOCore.plugin.nativeGuildManager.getGuild(config.getString("guild"));
data.setGuild(guild.hasMember(data.getUniqueId()) ? guild : null);
}
if (config.contains("attribute"))
@ -124,14 +131,10 @@ public class YAMLPlayerDataManager extends PlayerDataManager {
if (data.isOnline())
data.getPlayer().setHealth(MMOCoreUtils.fixResource(config.getDouble("health"), data.getPlayer().getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue()));
data.setFullyLoaded();
}
@Override
public void saveData(PlayerData data, boolean logout) {
ConfigFile file = new ConfigFile(data.getUniqueId());
FileConfiguration config = file.getConfig();
public void saveInSection(PlayerData data, ConfigurationSection config) {
config.set("class-points", data.getClassPoints());
config.set("skill-points", data.getSkillPoints());
@ -149,11 +152,11 @@ public class YAMLPlayerDataManager extends PlayerDataManager {
data.mapSkillTreePoints().forEach((key1, value) -> config.set("skill-tree-points." + key1, value));
config.set("skill-tree-reallocation-points", data.getSkillTreeReallocationPoints());
config.set("skill", null);
config.set("health",data.getHealth());
config.set("health", data.getHealth());
config.set("mana", data.getMana());
config.set("stellium", data.getStellium());
config.set("stamina", data.getStamina());
//Saves the nodes levels
// Saves the nodes levels
MMOCore.plugin.skillTreeManager.getAllNodes().forEach(node -> config.set("skill-tree-level." + node.getFullId(), data.getNodeLevel(node)));
data.mapSkillLevels().forEach((key1, value) -> config.set("skill." + key1, value));
data.getItemClaims().forEach((key, times) -> config.set("times-claimed." + key, times));
@ -194,14 +197,12 @@ public class YAMLPlayerDataManager extends PlayerDataManager {
info.mapBoundSkills().forEach((slot, skill) -> config.set("class-info." + key + ".bound-skills." + slot, skill));
config.set("class-info." + key + ".unlocked-items", new ArrayList<>(info.getUnlockedItems()));
}
file.save();
}
@NotNull
@Override
public OfflinePlayerData getOffline(UUID uuid) {
return isLoaded(uuid) ? get(uuid) : new YAMLOfflinePlayerData(uuid);
return new YAMLOfflinePlayerData(uuid);
}
}

View File

@ -18,22 +18,9 @@ import org.bukkit.event.entity.EntityRegainHealthEvent;
import org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
public class PlayerListener implements Listener {
/**
* Load player data. Event priority is set to LOW as most plugins
* do not change their priority which is NORMAL by default. Making
* it low is important because MMOCore is a core plugin so other plugins
* might rely on its data on startup.
*/
@EventHandler(priority = EventPriority.LOW)
public void loadPlayerData(PlayerJoinEvent event) {
MMOCore.plugin.dataProvider.getDataManager().setup(event.getPlayer().getUniqueId());
}
/**
* Register custom inventory clicks
*/
@ -70,16 +57,6 @@ public class PlayerListener implements Listener {
PlayerData.get(event.getEntity().getUniqueId()).getCombat().update();
}
@EventHandler
public void saveDataOnQuit(PlayerQuitEvent event) {
PlayerData playerData = PlayerData.get(event.getPlayer());
/**
* We save player health as it won't be accessible anymore when saving the player data (player will be offline).
*/
playerData.setHealth(event.getPlayer().getHealth());
MMOCore.plugin.dataProvider.getDataManager().unregisterSafe(playerData);
}
/**
* Using the Bukkit health update event is not a good way of
* interacting with MMOCore health regeneration. The

View File

@ -7,6 +7,7 @@ config-version: 9
auto-save:
enabled: true
interval: 1800 # In seconds (1800 = 30 minutes)
log: true
# MySQL Support
mysql: