修复玩家名称中包含字符 ' 时执行SQL出错的异常

This commit is contained in:
2021-09-10 23:54:06 +08:00
parent dd2ecca1a8
commit ca220775b6
2 changed files with 3 additions and 4 deletions

View File

@@ -3,7 +3,7 @@ plugins {
} }
group 'cn.hamster3' group 'cn.hamster3'
version '2.0.6-SNAPSHOT' version '2.0.7-SNAPSHOT'
repositories { repositories {
maven { maven {

View File

@@ -45,14 +45,13 @@ public class PlayerData {
} }
} }
@SuppressWarnings("ConstantConditions")
public PlayerData(ConfigurationSection config) { public PlayerData(ConfigurationSection config) {
uuid = UUID.fromString(config.getString("uuid")); uuid = UUID.fromString(config.getString("uuid"));
OfflinePlayer player = Bukkit.getOfflinePlayer(uuid); OfflinePlayer player = Bukkit.getOfflinePlayer(uuid);
if (player.getName() != null) { if (player.getName() != null) {
playerName = player.getName(); playerName = player.getName();
} else { } else {
playerName = config.getString("playerName"); playerName = config.getString("playerName").replace("\\'", "'");
} }
playerCurrencies = new HashMap<>(); playerCurrencies = new HashMap<>();
ConfigurationSection playerCurrenciesConfig = config.getConfigurationSection("playerCurrencies"); ConfigurationSection playerCurrenciesConfig = config.getConfigurationSection("playerCurrencies");
@@ -64,7 +63,7 @@ public class PlayerData {
public JsonObject saveToJson() { public JsonObject saveToJson() {
JsonObject object = new JsonObject(); JsonObject object = new JsonObject();
object.addProperty("uuid", uuid.toString()); object.addProperty("uuid", uuid.toString());
object.addProperty("playerName", playerName); object.addProperty("playerName", playerName.replace("'", "\\'"));
JsonObject playerCurrenciesJson = new JsonObject(); JsonObject playerCurrenciesJson = new JsonObject();
for (Map.Entry<String, Double> entry : playerCurrencies.entrySet()) { for (Map.Entry<String, Double> entry : playerCurrencies.entrySet()) {
playerCurrenciesJson.addProperty(entry.getKey(), entry.getValue()); playerCurrenciesJson.addProperty(entry.getKey(), entry.getValue());