Fixed SQL things

Requires MMOLib 1.3.7.1
This commit is contained in:
ASangarin 2020-10-06 18:59:05 +02:00
parent 54ba6b89b8
commit 239d70117d
7 changed files with 109 additions and 84 deletions

Binary file not shown.

View File

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.Indyuce</groupId>
<artifactId>MMOCore</artifactId>
<version>1.4.9</version>
<version>1.4.9.1</version>
<name>MMOCore</name>
<description>Offer your players a brand new RPG experience.</description>

View File

@ -363,7 +363,8 @@ public class MMOCore extends JavaPlugin {
for (Guild guild : dataProvider.getGuildManager().getAll())
dataProvider.getGuildManager().save(guild);
dataProvider.close();
mineManager.resetRemainingBlocks();
lootChests.getActive().forEach(chest -> chest.unregister(false));

View File

@ -11,4 +11,5 @@ public interface DataProvider {
PlayerDataManager getDataManager();
GuildDataManager getGuildManager();
void close();
}

View File

@ -11,7 +11,7 @@ import net.mmogroup.mmolib.sql.MMODataSource;
public class MySQLDataProvider extends MMODataSource implements DataProvider {
private final MySQLPlayerDataManager playerManager = new MySQLPlayerDataManager(this);
private final YAMLGuildDataManager guildManager = new YAMLGuildDataManager();
public MySQLDataProvider(FileConfiguration config) {
this.setup(config);
}

View File

@ -1,6 +1,5 @@
package net.Indyuce.mmocore.manager.data.mysql;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
@ -36,74 +35,87 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
@Override
public void loadData(PlayerData data) {
try {
ResultSet result = provider.getResultAsync("SELECT * FROM mmocore_playerdata WHERE uuid = '" + data.getUniqueId() + "';").get();
if (!result.first()) {
data.setLevel(getDefaultData().getLevel());
data.setClassPoints(getDefaultData().getClassPoints());
data.setSkillPoints(getDefaultData().getSkillPoints());
data.setAttributePoints(getDefaultData().getAttributePoints());
data.setAttributeReallocationPoints(getDefaultData().getAttributeReallocationPoints());
data.setExperience(0);
data.setMana(data.getStats().getStat(StatType.MAX_MANA));
data.setStamina(data.getStats().getStat(StatType.MAX_STAMINA));
data.setStellium(data.getStats().getStat(StatType.MAX_STELLIUM));
data.getQuestData().updateBossBar();
provider.getResultAsync("SELECT * FROM mmocore_playerdata WHERE uuid = '" + data.getUniqueId() + "';",
(result) -> {
try {
if (!result.first()) {
data.setLevel(getDefaultData().getLevel());
data.setClassPoints(getDefaultData().getClassPoints());
data.setSkillPoints(getDefaultData().getSkillPoints());
data.setAttributePoints(getDefaultData().getAttributePoints());
data.setAttributeReallocationPoints(getDefaultData().getAttributeReallocationPoints());
data.setExperience(0);
data.setMana(data.getStats().getStat(StatType.MAX_MANA));
data.setStamina(data.getStats().getStat(StatType.MAX_STAMINA));
data.setStellium(data.getStats().getStat(StatType.MAX_STELLIUM));
data.getQuestData().updateBossBar();
return;
}
return;
}
data.setClassPoints(result.getInt("class_points"));
data.setSkillPoints(result.getInt("skill_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")));
data.setMana(data.getStats().getStat(StatType.MAX_MANA));
data.setStamina(data.getStats().getStat(StatType.MAX_STAMINA));
data.setStellium(data.getStats().getStat(StatType.MAX_STELLIUM));
if (!isEmpty(result.getString("guild")))
data.setGuild(MMOCore.plugin.dataProvider.getGuildManager().stillInGuild(data.getUniqueId(), result.getString("guild")));
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(getJSONArray(result.getString("waypoints")));
if (!isEmpty(result.getString("friends")))
getJSONArray(result.getString("friends")).forEach(str -> data.getFriends().add(UUID.fromString(str)));
if (!isEmpty(result.getString("skills"))) {
JsonObject object = MMOLib.plugin.getJson().parse(result.getString("skills"), JsonObject.class);
for (Entry<String, JsonElement> entry : object.entrySet())
data.setSkillLevel(entry.getKey(), entry.getValue().getAsInt());
}
if (!isEmpty(result.getString("bound_skills")))
for (String skill : getJSONArray(result.getString("bound_skills")))
if (data.getProfess().hasSkill(skill))
data.getBoundSkills().add(data.getProfess().getSkill(skill));
if (!isEmpty(result.getString("class_info"))) {
JsonObject object = MMOLib.plugin.getJson().parse(result.getString("class_info"), JsonObject.class);
for (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());
}
}
}
} catch (SQLException | InterruptedException | ExecutionException e) {
data.setClassPoints(result.getInt("class_points"));
data.setSkillPoints(result.getInt("skill_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")));
data.setMana(data.getStats().getStat(StatType.MAX_MANA));
data.setStamina(data.getStats().getStat(StatType.MAX_STAMINA));
data.setStellium(data.getStats().getStat(StatType.MAX_STELLIUM));
if (!isEmpty(result.getString("guild")))
data.setGuild(MMOCore.plugin.dataProvider.getGuildManager()
.stillInGuild(data.getUniqueId(), result.getString("guild")));
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(getJSONArray(result.getString("waypoints")));
if (!isEmpty(result.getString("friends")))
getJSONArray(result.getString("friends"))
.forEach(str -> data.getFriends().add(UUID.fromString(str)));
if (!isEmpty(result.getString("skills"))) {
JsonObject object = MMOLib.plugin.getJson().parse(result.getString("skills"),
JsonObject.class);
for (Entry<String, JsonElement> entry : object.entrySet())
data.setSkillLevel(entry.getKey(), entry.getValue().getAsInt());
}
if (!isEmpty(result.getString("bound_skills")))
for (String skill : getJSONArray(result.getString("bound_skills")))
if (data.getProfess().hasSkill(skill))
data.getBoundSkills().add(data.getProfess().getSkill(skill));
if (!isEmpty(result.getString("class_info"))) {
JsonObject object = MMOLib.plugin.getJson().parse(result.getString("class_info"),
JsonObject.class);
for (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());
}
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}).get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
private boolean isEmpty(String s) {
return s.equalsIgnoreCase("null") || s.equalsIgnoreCase("{}") || s.equalsIgnoreCase("[]") || s.equalsIgnoreCase("");
return s == null || s.equalsIgnoreCase("null") || s.equalsIgnoreCase("{}") || s.equalsIgnoreCase("[]")
|| s.equalsIgnoreCase("");
}
@Override
@ -121,8 +133,10 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
sql.updateData("guild", data.hasGuild() ? data.getGuild().getId() : null);
sql.updateJSONArray("waypoints", data.getWaypoints());
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("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.updateJSONObject("skills", data.mapSkillLevels().entrySet());
@ -182,23 +196,29 @@ public class MySQLPlayerDataManager extends PlayerDataManager {
super(uuid);
try {
ResultSet result = provider.getResultAsync("SELECT * FROM mmocore_playerdata WHERE uuid = '" + uuid + "';").get();
if (!result.first()) {
level = 0;
lastLogin = 0;
profess = MMOCore.plugin.classManager.getDefaultClass();
friends = new ArrayList<UUID>();
} else {
level = result.getInt("level");
lastLogin = result.getLong("last_login");
profess = isEmpty(result.getString("class")) ? MMOCore.plugin.classManager.getDefaultClass()
: MMOCore.plugin.classManager.get(result.getString("class"));
if (!isEmpty(result.getString("friends")))
getJSONArray(result.getString("friends")).forEach(str -> friends.add(UUID.fromString(str)));
else
friends = new ArrayList<UUID>();
}
} catch (SQLException | InterruptedException | ExecutionException e) {
provider.getResultAsync("SELECT * FROM mmocore_playerdata WHERE uuid = '" + uuid + "';", (result) -> {
try {
if (!result.first()) {
level = 0;
lastLogin = 0;
profess = MMOCore.plugin.classManager.getDefaultClass();
friends = new ArrayList<UUID>();
} else {
level = result.getInt("level");
lastLogin = result.getLong("last_login");
profess = isEmpty(result.getString("class")) ? MMOCore.plugin.classManager.getDefaultClass()
: MMOCore.plugin.classManager.get(result.getString("class"));
if (!isEmpty(result.getString("friends")))
getJSONArray(result.getString("friends"))
.forEach(str -> friends.add(UUID.fromString(str)));
else
friends = new ArrayList<UUID>();
}
} catch (SQLException e) {
e.printStackTrace();
}
}).get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}

View File

@ -17,4 +17,7 @@ public class YAMLDataProvider implements DataProvider {
public GuildDataManager getGuildManager() {
return guildManager;
}
@Override
public void close() {}
}