!MySQL refactor

This commit is contained in:
Indyuce 2020-03-18 20:37:32 +01:00
parent b2b5693676
commit a3743f6fd3
15 changed files with 308 additions and 348 deletions

View File

@ -13,15 +13,10 @@ import org.bukkit.scheduler.BukkitRunnable;
import net.Indyuce.mmocore.api.ConfigFile; import net.Indyuce.mmocore.api.ConfigFile;
import net.Indyuce.mmocore.api.PlayerActionBar; import net.Indyuce.mmocore.api.PlayerActionBar;
import net.Indyuce.mmocore.api.data.DataProvider;
import net.Indyuce.mmocore.api.data.MySQLDataProvider;
import net.Indyuce.mmocore.api.data.YAMLDataProvider;
import net.Indyuce.mmocore.api.player.PlayerData; import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.profess.resource.PlayerResource; import net.Indyuce.mmocore.api.player.profess.resource.PlayerResource;
import net.Indyuce.mmocore.api.player.social.guilds.Guild; import net.Indyuce.mmocore.api.player.social.guilds.Guild;
import net.Indyuce.mmocore.api.player.stats.StatType; import net.Indyuce.mmocore.api.player.stats.StatType;
import net.Indyuce.mmocore.api.util.MMOSQL;
import net.Indyuce.mmocore.api.util.MMOSQL.MySQLConfig;
import net.Indyuce.mmocore.api.util.debug.DebugMode; import net.Indyuce.mmocore.api.util.debug.DebugMode;
import net.Indyuce.mmocore.command.AttributesCommand; import net.Indyuce.mmocore.command.AttributesCommand;
import net.Indyuce.mmocore.command.ClassCommand; import net.Indyuce.mmocore.command.ClassCommand;
@ -84,6 +79,9 @@ import net.Indyuce.mmocore.manager.QuestManager;
import net.Indyuce.mmocore.manager.RestrictionManager; import net.Indyuce.mmocore.manager.RestrictionManager;
import net.Indyuce.mmocore.manager.SkillManager; import net.Indyuce.mmocore.manager.SkillManager;
import net.Indyuce.mmocore.manager.WaypointManager; import net.Indyuce.mmocore.manager.WaypointManager;
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.profession.AlchemyManager; import net.Indyuce.mmocore.manager.profession.AlchemyManager;
import net.Indyuce.mmocore.manager.profession.EnchantManager; import net.Indyuce.mmocore.manager.profession.EnchantManager;
import net.Indyuce.mmocore.manager.profession.FishingManager; import net.Indyuce.mmocore.manager.profession.FishingManager;
@ -100,7 +98,7 @@ import net.mmogroup.mmolib.version.SpigotPlugin;
public class MMOCore extends JavaPlugin { public class MMOCore extends JavaPlugin {
public static MMOCore plugin; public static MMOCore plugin;
public ConfigManager configManager; public ConfigManager configManager;
public WaypointManager waypointManager; public WaypointManager waypointManager;
public RestrictionManager restrictionManager; public RestrictionManager restrictionManager;
@ -137,7 +135,7 @@ public class MMOCore extends JavaPlugin {
public RPGUtilHandler rpgUtilHandler = new DefaultRPGUtilHandler(); public RPGUtilHandler rpgUtilHandler = new DefaultRPGUtilHandler();
private boolean miLoaded, miChecked; private boolean miLoaded, miChecked;
public void onLoad() { public void onLoad() {
plugin = this; plugin = this;
@ -176,10 +174,9 @@ public class MMOCore extends JavaPlugin {
} }
}); });
if(getConfig().contains("mysql") && getConfig().getBoolean("mysql.enabled") && if (getConfig().contains("mysql") && getConfig().getBoolean("mysql.enabled"))
MMOSQL.testConnection(new MySQLConfig(getConfig().getConfigurationSection("mysql"))))
dataProvider = new MySQLDataProvider(); dataProvider = new MySQLDataProvider();
Bukkit.getPluginManager().registerEvents(new MythicMobsEnableListener(), this); Bukkit.getPluginManager().registerEvents(new MythicMobsEnableListener(), this);
if (Bukkit.getPluginManager().getPlugin("Vault") != null) if (Bukkit.getPluginManager().getPlugin("Vault") != null)
@ -353,7 +350,7 @@ public class MMOCore extends JavaPlugin {
} }
} }
public void onDisable() { public void onDisable() {
for (PlayerData data : PlayerData.getAll()) { for (PlayerData data : PlayerData.getAll()) {
data.getQuestData().resetBossBar(); data.getQuestData().resetBossBar();
dataProvider.getDataManager().saveData(data); dataProvider.getDataManager().saveData(data);
@ -361,7 +358,6 @@ public class MMOCore extends JavaPlugin {
for (Guild guild : dataProvider.getGuildManager().getAll()) for (Guild guild : dataProvider.getGuildManager().getAll())
dataProvider.getGuildManager().save(guild); dataProvider.getGuildManager().save(guild);
MMOSQL.stop();
mineManager.resetRemainingBlocks(); mineManager.resetRemainingBlocks();
} }

View File

@ -1,29 +0,0 @@
package net.Indyuce.mmocore.api.data;
import net.Indyuce.mmocore.api.util.MMOSQL;
import net.Indyuce.mmocore.manager.data.GuildDataManager;
import net.Indyuce.mmocore.manager.data.MySQLPlayerDataManager;
import net.Indyuce.mmocore.manager.data.PlayerDataManager;
import net.Indyuce.mmocore.manager.data.YAMLGuildDataManager;
public class MySQLDataProvider implements DataProvider {
private final MySQLPlayerDataManager playerManager;
private final YAMLGuildDataManager guildManager;
public MySQLDataProvider() {
playerManager = new MySQLPlayerDataManager();
guildManager = new YAMLGuildDataManager();
MMOSQL.createTables();
}
@Override
public PlayerDataManager getDataManager() {
return playerManager;
}
@Override
public GuildDataManager getGuildManager() {
return guildManager;
}
}

View File

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

View File

@ -1,150 +0,0 @@
package net.Indyuce.mmocore.api.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;
import org.bukkit.configuration.ConfigurationSection;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import net.Indyuce.mmocore.MMOCore;
public class MMOSQL {
private static Connection connection;
public static boolean testConnection(MySQLConfig config) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.err.println("jdbc driver unavailable!");
return false;
}
try {
connection = DriverManager.getConnection(config.getConnectionString(), config.getUser(), config.getPassword());
MMOCore.log("Successfully connected to MySQL Database!");
} catch (SQLException e) {
e.printStackTrace();
return false;
}
return true;
}
public static void stop() {
try {
if(connection != null && !connection.isClosed()) connection.close();
} catch(SQLException e) {
e.printStackTrace();
}
}
public static void createTables() {
executeUpdate("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,level INT(11) DEFAULT 0,experience INT(11) DEFAULT 0,class VARCHAR(20),guild VARCHAR(20),last_login LONG,attributes JSON,professions JSON,quests JSON,waypoints JSON,friends JSON,skills JSON,bound_skills JSON,class_info JSON,PRIMARY KEY (uuid));");
//executeUpdate("CREATE TABLE IF NOT EXISTS mmocore_guilddata(id varchar(10));");
}
public static ResultSet getResult(String sql) {
try {
PreparedStatement stmt = connection.prepareStatement(sql);
return stmt.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
public static Collection<String> getJSONArray(String json) {
Collection<String> collection = new ArrayList<String>();
Gson parser = new Gson();
for(String s : parser.fromJson(json, String[].class))
collection.add(s);
return collection;
}
private final Table table;
private final UUID uuid;
public MMOSQL(Table t, UUID player) {
table = t;
uuid = player;
}
public void updateData(String key, Object value) {
executeUpdate("INSERT INTO " + table + "(uuid, " + key + ") VALUES('" + uuid +
"', '" + value + "') ON DUPLICATE KEY UPDATE " + key + "='" + value + "';");
}
public void updateJSONArray(String key, Collection<String> collection) {
JsonArray json = new JsonArray();
for(String s : collection)
json.add(s);
updateData(key, json.toString());
}
public void updateJSONObject(String key, Set<Entry<String, Integer>> collection) {
JsonObject json = new JsonObject();
for(Entry<String, Integer> entry : collection)
json.addProperty(entry.getKey(), entry.getValue());
updateData(key, json.toString());
}
private static void executeUpdate(String sql) {
try {
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public enum Table {
PLAYERDATA("mmocore_playerdata"),
GUILDDATA("mmocore_guilddata");
final String tableName;
Table(String tN) {
tableName = tN;
}
@Override
public String toString() {
return tableName;
}
}
public static class MySQLConfig {
String database, hostname, userid, password, flags;
int port;
public MySQLConfig(ConfigurationSection config) {
database = config.getString("database", "minecraft");
hostname = config.getString("host", "localhost");
port = config.getInt("port", 3306);
userid = config.getString("user", "mmolover");
password = config.getString("pass", "ILoveAria");
flags = config.getString("flags", "?allowReconnect=true");
}
public String getConnectionString() {
return "jdbc:mysql://" + hostname + ":" + port + "/" + database + flags + "&useSSL=false";
}
public String getUser() {
return userid;
}
public String getPassword() {
return password;
}
}
}

View File

@ -1,7 +1,4 @@
package net.Indyuce.mmocore.api.data; package net.Indyuce.mmocore.manager.data;
import net.Indyuce.mmocore.manager.data.GuildDataManager;
import net.Indyuce.mmocore.manager.data.PlayerDataManager;
public interface DataProvider { public interface DataProvider {

View File

@ -1,73 +0,0 @@
package net.Indyuce.mmocore.manager.data;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.player.OfflinePlayerData;
import net.Indyuce.mmocore.api.player.profess.PlayerClass;
import net.Indyuce.mmocore.api.util.MMOSQL;
import net.Indyuce.mmocore.api.util.MMOSQL.Table;
public class MySQLOfflinePlayerData extends OfflinePlayerData {
private int level;
private long lastLogin;
private PlayerClass profess;
private List<UUID> friends;
public MySQLOfflinePlayerData(UUID uuid) {
super(uuid);
ResultSet result = MMOSQL.getResult("SELECT * FROM mmocore_playerdata WHERE uuid = '" + uuid + "';");
try {
if (!result.next()) {
level = 0;
lastLogin = 0;
profess = MMOCore.plugin.classManager.getDefaultClass();
friends = new ArrayList<UUID>();
} else
while (result.next()) {
level = result.getInt("level");
lastLogin = result.getLong("last_login");
profess = result.getString("class").equalsIgnoreCase("null") ? MMOCore.plugin.classManager.getDefaultClass() : MMOCore.plugin.classManager.get(result.getString("class"));
if (!result.getString("friends").equalsIgnoreCase("null"))
MMOSQL.getJSONArray(result.getString("friends")).forEach(str -> friends.add(UUID.fromString(str)));
else
friends = new ArrayList<UUID>();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void removeFriend(UUID uuid) {
friends.remove(uuid);
new MMOSQL(Table.PLAYERDATA, uuid).updateData("friends", friends.stream().map(friend -> friend.toString()).collect(Collectors.toList()));
}
@Override
public boolean hasFriend(UUID uuid) {
return friends.contains(uuid);
}
@Override
public PlayerClass getProfess() {
return profess;
}
@Override
public int getLevel() {
return level;
}
@Override
public long getLastLogin() {
return lastLogin;
}
}

View File

@ -0,0 +1,86 @@
package net.Indyuce.mmocore.manager.data.mysql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.bukkit.configuration.ConfigurationSection;
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;
public class MySQLDataProvider implements DataProvider {
private final MySQLPlayerDataManager playerManager = new MySQLPlayerDataManager(this);
private final YAMLGuildDataManager guildManager = new YAMLGuildDataManager();
private final Connection connection;
public MySQLDataProvider() {
MySQLConfig config = new MySQLConfig(MMOCore.plugin.getConfig().getConfigurationSection("mysql"));
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(config.getConnectionString(), config.getUser(), config.getPassword());
} catch (ClassNotFoundException | SQLException exception) {
throw new IllegalArgumentException("Could not initialize MySQL support: " + exception.getMessage());
}
executeUpdate("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,level INT(11) DEFAULT 0,experience INT(11) DEFAULT 0,class VARCHAR(20),guild VARCHAR(20),last_login LONG,attributes JSON,professions JSON,quests JSON,waypoints JSON,friends JSON,skills JSON,bound_skills JSON,class_info JSON,PRIMARY KEY (uuid));");
}
public ResultSet getResult(String sql) {
try {
return connection.prepareStatement(sql).executeQuery();
} catch (SQLException exception) {
exception.printStackTrace();
return null;
}
}
public void executeUpdate(String sql) {
try {
connection.prepareStatement(sql).executeUpdate();
} catch (SQLException exception) {
exception.printStackTrace();
}
}
@Override
public PlayerDataManager getDataManager() {
return playerManager;
}
@Override
public GuildDataManager getGuildManager() {
return guildManager;
}
public class MySQLConfig {
private final String database, hostname, userid, password, flags;
private final int port;
public MySQLConfig(ConfigurationSection config) {
database = config.getString("database", "minecraft");
hostname = config.getString("host", "localhost");
port = config.getInt("port", 3306);
userid = config.getString("user", "mmolover");
password = config.getString("pass", "ILoveAria");
flags = config.getString("flags", "?allowReconnect=true");
}
public String getConnectionString() {
return "jdbc:mysql://" + hostname + ":" + port + "/" + database + flags + "&useSSL=false";
}
public String getUser() {
return userid;
}
public String getPassword() {
return password;
}
}
}

View File

@ -1,4 +1,4 @@
package net.Indyuce.mmocore.manager.data; package net.Indyuce.mmocore.manager.data.mysql;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
@ -11,6 +11,7 @@ import org.bukkit.configuration.file.YamlConfiguration;
import net.Indyuce.mmocore.MMOCore; import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.ConfigFile; import net.Indyuce.mmocore.api.ConfigFile;
import net.Indyuce.mmocore.api.player.social.guilds.Guild; import net.Indyuce.mmocore.api.player.social.guilds.Guild;
import net.Indyuce.mmocore.manager.data.GuildDataManager;
public class MySQLGuildDataManager extends GuildDataManager { public class MySQLGuildDataManager extends GuildDataManager {
@Override @Override

View File

@ -1,7 +1,10 @@
package net.Indyuce.mmocore.manager.data; package net.Indyuce.mmocore.manager.data.mysql;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Level; import java.util.logging.Level;
@ -19,58 +22,64 @@ import net.Indyuce.mmocore.api.player.PlayerData;
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.player.stats.StatType; import net.Indyuce.mmocore.api.player.stats.StatType;
import net.Indyuce.mmocore.api.util.MMOSQL; import net.Indyuce.mmocore.manager.data.PlayerDataManager;
import net.Indyuce.mmocore.api.util.MMOSQL.Table; import net.Indyuce.mmocore.manager.data.mysql.MySQLTableEditor.Table;
public class MySQLPlayerDataManager extends PlayerDataManager { public class MySQLPlayerDataManager extends PlayerDataManager {
private final MySQLDataProvider provider;
public MySQLPlayerDataManager(MySQLDataProvider provider) {
this.provider = provider;
}
@Override @Override
public void loadData(PlayerData data) { public void loadData(PlayerData data) {
ResultSet result = MMOSQL.getResult("SELECT * FROM mmocore_playerdata WHERE uuid = '" + data.getUniqueId() + "';"); ResultSet result = provider.getResult("SELECT * FROM mmocore_playerdata WHERE uuid = '" + data.getUniqueId() + "';");
if(result == null) if (result == null)
MMOCore.log(Level.SEVERE, "Failed to load playerdata from MySQL!"); MMOCore.log(Level.SEVERE, "Failed to load playerdata from MySQL!");
try { try {
if(!result.next()) { if (!result.next()) {
return; return;
} }
Gson parser = new Gson(); Gson parser = new Gson();
data.setClassPoints(result.getInt("class_points")); data.setClassPoints(result.getInt("class_points"));
data.setSkillPoints(result.getInt("skill_points")); data.setSkillPoints(result.getInt("skill_points"));
data.setAttributePoints(result.getInt("attribute_points")); data.setAttributePoints(result.getInt("attribute_points"));
data.setAttributeReallocationPoints(result.getInt("attribute_realloc_points")); data.setAttributeReallocationPoints(result.getInt("attribute_realloc_points"));
data.setLevel(result.getInt("level")); data.setLevel(result.getInt("level"));
data.setExperience(result.getInt("experience")); data.setExperience(result.getInt("experience"));
if(!isEmpty(result.getString("class"))) if (!isEmpty(result.getString("class")))
data.setProfess(MMOCore.plugin.classManager.get(result.getString("class"))); data.setProfess(MMOCore.plugin.classManager.get(result.getString("class")));
data.setMana(data.getStats().getStat(StatType.MAX_MANA)); data.setMana(data.getStats().getStat(StatType.MAX_MANA));
data.setStamina(data.getStats().getStat(StatType.MAX_STAMINA)); data.setStamina(data.getStats().getStat(StatType.MAX_STAMINA));
data.setStellium(data.getStats().getStat(StatType.MAX_STELLIUM)); data.setStellium(data.getStats().getStat(StatType.MAX_STELLIUM));
if(!isEmpty(result.getString("guild"))) if (!isEmpty(result.getString("guild")))
data.setGuild(MMOCore.plugin.dataProvider.getGuildManager().stillInGuild(data.getUniqueId(), result.getString("guild"))); data.setGuild(MMOCore.plugin.dataProvider.getGuildManager().stillInGuild(data.getUniqueId(), result.getString("guild")));
if(!isEmpty(result.getString("attributes"))) if (!isEmpty(result.getString("attributes")))
data.getAttributes().load(result.getString("attributes")); data.getAttributes().load(result.getString("attributes"));
if(!isEmpty(result.getString("professions"))) if (!isEmpty(result.getString("professions")))
data.getCollectionSkills().load(result.getString("professions")); data.getCollectionSkills().load(result.getString("professions"));
String quests = result.getString("quests"); String quests = result.getString("quests");
if(!isEmpty(quests)) if (!isEmpty(quests))
data.getQuestData().load(quests); data.getQuestData().load(quests);
data.getQuestData().updateBossBar(); data.getQuestData().updateBossBar();
if(!isEmpty(result.getString("waypoints"))) if (!isEmpty(result.getString("waypoints")))
data.getWaypoints().addAll(MMOSQL.getJSONArray(result.getString("waypoints"))); data.getWaypoints().addAll(getJSONArray(result.getString("waypoints")));
if(!isEmpty(result.getString("friends"))) if (!isEmpty(result.getString("friends")))
MMOSQL.getJSONArray(result.getString("friends")).forEach(str -> data.getFriends().add(UUID.fromString(str))); getJSONArray(result.getString("friends")).forEach(str -> data.getFriends().add(UUID.fromString(str)));
if(!isEmpty(result.getString("skills"))) { if (!isEmpty(result.getString("skills"))) {
JsonObject object = parser.fromJson(result.getString("skills"), JsonObject.class); JsonObject object = parser.fromJson(result.getString("skills"), JsonObject.class);
for(Entry<String, JsonElement> entry : object.entrySet()) for (Entry<String, JsonElement> entry : object.entrySet())
data.setSkillLevel(entry.getKey(), entry.getValue().getAsInt()); data.setSkillLevel(entry.getKey(), entry.getValue().getAsInt());
} }
if(!isEmpty(result.getString("bound_skills"))) if (!isEmpty(result.getString("bound_skills")))
for(String skill : MMOSQL.getJSONArray(result.getString("bound_skills"))) for (String skill : getJSONArray(result.getString("bound_skills")))
if(MMOCore.plugin.skillManager.has(skill)) if (MMOCore.plugin.skillManager.has(skill))
data.getBoundSkills().add(data.getProfess().getSkill(skill)); data.getBoundSkills().add(data.getProfess().getSkill(skill));
if(!isEmpty(result.getString("class_info"))) { if (!isEmpty(result.getString("class_info"))) {
JsonObject object = parser.fromJson(result.getString("class_info"), JsonObject.class); JsonObject object = parser.fromJson(result.getString("class_info"), JsonObject.class);
for(Entry<String, JsonElement> entry : object.entrySet()) { for (Entry<String, JsonElement> entry : object.entrySet()) {
try { try {
PlayerClass profess = MMOCore.plugin.classManager.get(entry.getKey()); PlayerClass profess = MMOCore.plugin.classManager.get(entry.getKey());
Validate.notNull(profess, "Could not find class '" + entry.getKey() + "'"); Validate.notNull(profess, "Could not find class '" + entry.getKey() + "'");
@ -78,7 +87,7 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
} catch (IllegalArgumentException exception) { } catch (IllegalArgumentException exception) {
MMOCore.log(Level.WARNING, "Could not load class info '" + entry.getKey() + "': " + exception.getMessage()); MMOCore.log(Level.WARNING, "Could not load class info '" + entry.getKey() + "': " + exception.getMessage());
} }
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
MMOCore.log(Level.SEVERE, "Failed to load playerdata from MySQL!"); MMOCore.log(Level.SEVERE, "Failed to load playerdata from MySQL!");
@ -87,14 +96,13 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
} }
private boolean isEmpty(String s) { private boolean isEmpty(String s) {
return s.equalsIgnoreCase("null") || s.equalsIgnoreCase("{}") return s.equalsIgnoreCase("null") || s.equalsIgnoreCase("{}") || s.equalsIgnoreCase("[]") || s.equalsIgnoreCase("");
|| s.equalsIgnoreCase("[]") || s.equalsIgnoreCase("");
} }
@Override @Override
public void saveData(PlayerData data) { public void saveData(PlayerData data) {
MMOSQL sql = new MMOSQL(Table.PLAYERDATA, data.getUniqueId()); MySQLTableEditor sql = new MySQLTableEditor(Table.PLAYERDATA, data.getUniqueId());
sql.updateData("class_points", data.getClassPoints()); sql.updateData("class_points", data.getClassPoints());
sql.updateData("skill_points", data.getSkillPoints()); sql.updateData("skill_points", data.getSkillPoints());
sql.updateData("attribute_points", data.getAttributePoints()); sql.updateData("attribute_points", data.getAttributePoints());
@ -104,7 +112,7 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
sql.updateData("class", data.getProfess().getId()); sql.updateData("class", data.getProfess().getId());
sql.updateData("last_login", data.getLastLogin()); sql.updateData("last_login", data.getLastLogin());
sql.updateData("guild", data.hasGuild() ? data.getGuild().getId() : null); sql.updateData("guild", data.hasGuild() ? data.getGuild().getId() : null);
sql.updateJSONArray("waypoints", data.getWaypoints()); sql.updateJSONArray("waypoints", data.getWaypoints());
sql.updateJSONArray("friends", data.getFriends().stream().map(uuid -> uuid.toString()).collect(Collectors.toList())); sql.updateJSONArray("friends", data.getFriends().stream().map(uuid -> uuid.toString()).collect(Collectors.toList()));
sql.updateJSONArray("bound_skills", data.getBoundSkills().stream().map(skill -> skill.getSkill().getId()).collect(Collectors.toList())); sql.updateJSONArray("bound_skills", data.getBoundSkills().stream().map(skill -> skill.getSkill().getId()).collect(Collectors.toList()));
@ -117,10 +125,10 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
sql.updateData("class_info", createClassInfoData(data).toString()); sql.updateData("class_info", createClassInfoData(data).toString());
} }
private JsonObject createClassInfoData(PlayerData data) { private JsonObject createClassInfoData(PlayerData data) {
JsonObject json = new JsonObject(); JsonObject json = new JsonObject();
for(String c : data.getSavedClasses()) { for (String c : data.getSavedClasses()) {
SavedClassInformation info = data.getClassInfo(c); SavedClassInformation info = data.getClassInfo(c);
JsonObject classinfo = new JsonObject(); JsonObject classinfo = new JsonObject();
classinfo.addProperty("level", info.getLevel()); classinfo.addProperty("level", info.getLevel());
@ -129,17 +137,17 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
classinfo.addProperty("attribute-points", info.getAttributePoints()); classinfo.addProperty("attribute-points", info.getAttributePoints());
classinfo.addProperty("attribute-realloc-points", info.getAttributeReallocationPoints()); classinfo.addProperty("attribute-realloc-points", info.getAttributeReallocationPoints());
JsonObject skillinfo = new JsonObject(); JsonObject skillinfo = new JsonObject();
for(String skill : info.getSkillKeys()) for (String skill : info.getSkillKeys())
skillinfo.addProperty(skill, info.getSkillLevel(skill)); skillinfo.addProperty(skill, info.getSkillLevel(skill));
classinfo.add("skill", skillinfo); classinfo.add("skill", skillinfo);
JsonObject attributeinfo = new JsonObject(); JsonObject attributeinfo = new JsonObject();
for(String attribute : info.getAttributeKeys()) for (String attribute : info.getAttributeKeys())
attributeinfo.addProperty(attribute, info.getAttributeLevel(attribute)); attributeinfo.addProperty(attribute, info.getAttributeLevel(attribute));
classinfo.add("attribute", attributeinfo); classinfo.add("attribute", attributeinfo);
json.add(c, classinfo); json.add(c, classinfo);
} }
return json; return json;
} }
@ -147,4 +155,74 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
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);
} }
private Collection<String> getJSONArray(String json) {
Collection<String> collection = new ArrayList<String>();
Gson parser = new Gson();
for (String s : parser.fromJson(json, String[].class))
collection.add(s);
return collection;
}
public class MySQLOfflinePlayerData extends OfflinePlayerData {
private int level;
private long lastLogin;
private PlayerClass profess;
private List<UUID> friends;
public MySQLOfflinePlayerData(UUID uuid) {
super(uuid);
ResultSet result = provider.getResult("SELECT * FROM mmocore_playerdata WHERE uuid = '" + uuid + "';");
try {
if (!result.next()) {
level = 0;
lastLogin = 0;
profess = MMOCore.plugin.classManager.getDefaultClass();
friends = new ArrayList<UUID>();
} else
while (result.next()) {
level = result.getInt("level");
lastLogin = result.getLong("last_login");
profess = result.getString("class").equalsIgnoreCase("null") ? MMOCore.plugin.classManager.getDefaultClass() : MMOCore.plugin.classManager.get(result.getString("class"));
if (!result.getString("friends").equalsIgnoreCase("null"))
getJSONArray(result.getString("friends")).forEach(str -> friends.add(UUID.fromString(str)));
else
friends = new ArrayList<UUID>();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void removeFriend(UUID uuid) {
friends.remove(uuid);
new MySQLTableEditor(Table.PLAYERDATA, uuid).updateData("friends", friends.stream().map(friend -> friend.toString()).collect(Collectors.toList()));
}
@Override
public boolean hasFriend(UUID uuid) {
return friends.contains(uuid);
}
@Override
public PlayerClass getProfess() {
return profess;
}
@Override
public int getLevel() {
return level;
}
@Override
public long getLastLogin() {
return lastLogin;
}
}
} }

View File

@ -0,0 +1,57 @@
package net.Indyuce.mmocore.manager.data.mysql;
import java.util.Collection;
import java.util.Map.Entry;
import java.util.Set;
import java.util.UUID;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import net.Indyuce.mmocore.MMOCore;
public class MySQLTableEditor {
private final Table table;
private final UUID uuid;
private static final MySQLDataProvider dataProvider = (MySQLDataProvider) MMOCore.plugin.dataProvider;
public MySQLTableEditor(Table table, UUID uuid) {
this.table = table;
this.uuid = uuid;
}
public void updateData(String key, Object value) {
dataProvider.executeUpdate("INSERT INTO " + table + "(uuid, " + key + ") VALUES('" + uuid + "', '" + value + "') ON DUPLICATE KEY UPDATE " + key + "='" + value + "';");
}
public void updateJSONArray(String key, Collection<String> collection) {
JsonArray json = new JsonArray();
for (String s : collection)
json.add(s);
updateData(key, json.toString());
}
public void updateJSONObject(String key, Set<Entry<String, Integer>> collection) {
JsonObject json = new JsonObject();
for (Entry<String, Integer> entry : collection)
json.addProperty(entry.getKey(), entry.getValue());
updateData(key, json.toString());
}
public enum Table {
PLAYERDATA("mmocore_playerdata"),
GUILDDATA("mmocore_guilddata");
final String tableName;
Table(String tN) {
tableName = tN;
}
@Override
public String toString() {
return tableName;
}
}
}

View File

@ -0,0 +1,20 @@
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();
private final YAMLGuildDataManager guildManager = new YAMLGuildDataManager();
@Override
public PlayerDataManager getDataManager() {
return playerManager;
}
@Override
public GuildDataManager getGuildManager() {
return guildManager;
}
}

View File

@ -1,4 +1,4 @@
package net.Indyuce.mmocore.manager.data; package net.Indyuce.mmocore.manager.data.yaml;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
@ -11,6 +11,7 @@ import org.bukkit.configuration.file.YamlConfiguration;
import net.Indyuce.mmocore.MMOCore; import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.ConfigFile; import net.Indyuce.mmocore.api.ConfigFile;
import net.Indyuce.mmocore.api.player.social.guilds.Guild; import net.Indyuce.mmocore.api.player.social.guilds.Guild;
import net.Indyuce.mmocore.manager.data.GuildDataManager;
public class YAMLGuildDataManager extends GuildDataManager { public class YAMLGuildDataManager extends GuildDataManager {
@Override @Override
@ -19,26 +20,27 @@ public class YAMLGuildDataManager extends GuildDataManager {
config.getConfig().set("name", guild.getName()); config.getConfig().set("name", guild.getName());
config.getConfig().set("tag", guild.getTag()); config.getConfig().set("tag", guild.getTag());
config.getConfig().set("owner", guild.getOwner().toString()); config.getConfig().set("owner", guild.getOwner().toString());
List<String> memberList = new ArrayList<>(); List<String> memberList = new ArrayList<>();
guild.getMembers().forEach(uuid -> memberList.add(uuid.toString())); guild.getMembers().forEach(uuid -> memberList.add(uuid.toString()));
config.getConfig().set("members", memberList); config.getConfig().set("members", memberList);
config.save(); config.save();
} }
@Override @Override
public void load() { public void load() {
File guildsFolder = new File(MMOCore.plugin.getDataFolder(), "guilds"); File guildsFolder = new File(MMOCore.plugin.getDataFolder(), "guilds");
if(!guildsFolder.exists()) guildsFolder.mkdirs(); if (!guildsFolder.exists())
for (File file : guildsFolder.listFiles()) { guildsFolder.mkdirs();
if (!file.isDirectory() && file.getName().substring(file.getName().lastIndexOf('.')).equalsIgnoreCase(".yml")) { for (File file : guildsFolder.listFiles()) {
FileConfiguration c = YamlConfiguration.loadConfiguration(file); if (!file.isDirectory() && file.getName().substring(file.getName().lastIndexOf('.')).equalsIgnoreCase(".yml")) {
Guild guild = newRegisteredGuild(UUID.fromString(c.getString("owner")), c.getString("name"), c.getString("tag")); FileConfiguration c = YamlConfiguration.loadConfiguration(file);
for(String m : c.getStringList("members")) Guild guild = newRegisteredGuild(UUID.fromString(c.getString("owner")), c.getString("name"), c.getString("tag"));
guild.registerMember(UUID.fromString(m)); for (String m : c.getStringList("members"))
} guild.registerMember(UUID.fromString(m));
} }
}
} }
@Override @Override

View File

@ -1,4 +1,4 @@
package net.Indyuce.mmocore.manager.data; package net.Indyuce.mmocore.manager.data.yaml;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;

View File

@ -1,4 +1,4 @@
package net.Indyuce.mmocore.manager.data; package net.Indyuce.mmocore.manager.data.yaml;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -16,6 +16,7 @@ import net.Indyuce.mmocore.api.player.PlayerData;
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.player.stats.StatType; import net.Indyuce.mmocore.api.player.stats.StatType;
import net.Indyuce.mmocore.manager.data.PlayerDataManager;
public class YAMLPlayerDataManager extends PlayerDataManager { public class YAMLPlayerDataManager extends PlayerDataManager {

View File

@ -44,9 +44,6 @@ mana:
cast-particle: cast-particle:
particle: SPELL_INSTANT particle: SPELL_INSTANT
options:
off-combat-health-regen: true
# Special resource regeneration: (when out of combat), # Special resource regeneration: (when out of combat),
# players can regen a set % of their maximum mana/missing mana. # players can regen a set % of their maximum mana/missing mana.
# This % can scale with the player level. # This % can scale with the player level.
@ -66,6 +63,9 @@ resource:
# Only regen when out of combat. # Only regen when out of combat.
off-combat: true off-combat: true
options:
off-combat-health-regen: true
attributes: attributes:
max-health: max-health:
base: 18 base: 18