mirror of
https://github.com/HibiscusMC/HMCCosmetics.git
synced 2025-12-19 15:09:19 +00:00
MySQL Stability improvements
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.hibiscusmc.hmccosmetics.database.types;
|
||||
|
||||
import com.hibiscusmc.hmccosmetics.HMCCosmeticsPlugin;
|
||||
import com.hibiscusmc.hmccosmetics.cosmetic.Cosmetic;
|
||||
import com.hibiscusmc.hmccosmetics.cosmetic.CosmeticSlot;
|
||||
import com.hibiscusmc.hmccosmetics.cosmetic.Cosmetics;
|
||||
@@ -34,6 +35,10 @@ public class Data {
|
||||
String data = "";
|
||||
for (Cosmetic cosmetic : user.getCosmetic()) {
|
||||
String input = cosmetic.getSlot() + "=" + cosmetic.getId();
|
||||
if (data.length() == 0) {
|
||||
data = input;
|
||||
continue;
|
||||
}
|
||||
data = data + "," + input;
|
||||
}
|
||||
return data;
|
||||
@@ -48,6 +53,7 @@ public class Data {
|
||||
String[] splitData = a.split("=");
|
||||
CosmeticSlot slot = null;
|
||||
Cosmetic cosmetic = null;
|
||||
HMCCosmeticsPlugin.getInstance().getLogger().info("First split (suppose slot) " + splitData[0]);
|
||||
if (CosmeticSlot.valueOf(splitData[0]) != null) slot = CosmeticSlot.valueOf(splitData[0]);
|
||||
if (Cosmetics.hasCosmetic(splitData[1])) cosmetic = Cosmetics.getCosmetic(splitData[1]);
|
||||
if (slot == null || cosmetic == null) continue;
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.hibiscusmc.hmccosmetics.cosmetic.CosmeticSlot;
|
||||
import com.hibiscusmc.hmccosmetics.user.CosmeticUser;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
@@ -46,10 +47,14 @@ public class InternalData extends Data {
|
||||
|
||||
@Override
|
||||
public void clear(UUID uniqueId) {
|
||||
Player player = Bukkit.getPlayer(uniqueId);
|
||||
OfflinePlayer player = Bukkit.getOfflinePlayer(uniqueId);
|
||||
|
||||
if (player.getPersistentDataContainer().has(key, PersistentDataType.STRING)) {
|
||||
player.getPersistentDataContainer().remove(key);
|
||||
if (player.isOnline()) {
|
||||
Player onlinePlayer = player.getPlayer();
|
||||
if (onlinePlayer.getPersistentDataContainer().has(key, PersistentDataType.STRING)) {
|
||||
onlinePlayer.getPersistentDataContainer().remove(key);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import com.hibiscusmc.hmccosmetics.user.CosmeticUser;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.UUID;
|
||||
@@ -37,7 +38,7 @@ public class MySQLData extends Data {
|
||||
connection.prepareStatement("CREATE TABLE IF NOT EXISTS `COSMETICDATABASE` " +
|
||||
"(UUID varchar(200) PRIMARY KEY, " +
|
||||
"COSMETICS MEDIUMTEXT " +
|
||||
");");
|
||||
");").execute();
|
||||
} catch (SQLException e) {
|
||||
plugin.getLogger().severe("");
|
||||
plugin.getLogger().severe("");
|
||||
@@ -68,7 +69,9 @@ public class MySQLData extends Data {
|
||||
@Override
|
||||
public CosmeticUser get(UUID uniqueId) {
|
||||
CosmeticUser user = new CosmeticUser(uniqueId);
|
||||
Bukkit.getScheduler().runTaskAsynchronously(HMCCosmeticsPlugin.getInstance(), () -> {
|
||||
|
||||
// Need to figure out how to properly do this async.
|
||||
Bukkit.getScheduler().runTask(HMCCosmeticsPlugin.getInstance(), () -> {
|
||||
try {
|
||||
PreparedStatement preparedStatement = preparedStatement("SELECT COUNT(UUID) FROM COSMETICDATABASE WHERE UUID = ?;");
|
||||
preparedStatement.setString(1, uniqueId.toString());
|
||||
@@ -81,7 +84,7 @@ public class MySQLData extends Data {
|
||||
preState2.setString(1, uniqueId.toString());
|
||||
ResultSet rs2 = preState2.executeQuery();
|
||||
if (rs2.next()) {
|
||||
String rawData = rs.getString("COSMETICS");
|
||||
String rawData = rs2.getString("COSMETICS");
|
||||
Map<CosmeticSlot, Cosmetic> cosmetics = desteralizedata(rawData);
|
||||
for (Cosmetic cosmetic : cosmetics.values()) {
|
||||
user.addPlayerCosmetic(cosmetic);
|
||||
@@ -105,7 +108,7 @@ public class MySQLData extends Data {
|
||||
if (connection != null && !connection.isClosed()) {
|
||||
return;
|
||||
}
|
||||
Bukkit.getScheduler().runTaskAsynchronously(HMCCosmeticsPlugin.getInstance(), () -> {
|
||||
//Bukkit.getScheduler().runTaskAsynchronously(HMCCosmeticsPlugin.getInstance(), () -> {
|
||||
|
||||
//close Connection if still active
|
||||
if (connection != null) {
|
||||
@@ -124,8 +127,8 @@ public class MySQLData extends Data {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
});
|
||||
connection = DriverManager.getConnection("jdbc:mysql://" + DatabaseSettings.getHost() + ":" + DatabaseSettings.getPort() + "/" + DatabaseSettings.getDatabase(), setupProperties());
|
||||
//});
|
||||
//connection = DriverManager.getConnection("jdbc:mysql://" + DatabaseSettings.getHost() + ":" + DatabaseSettings.getPort() + "/" + DatabaseSettings.getDatabase(), setupProperties());
|
||||
}
|
||||
|
||||
public void close() {
|
||||
@@ -146,16 +149,23 @@ public class MySQLData extends Data {
|
||||
return props;
|
||||
}
|
||||
|
||||
private boolean isConnectionOpen() throws SQLException{
|
||||
if (connection == null || connection.isClosed()) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
private boolean isConnectionOpen() {
|
||||
try {
|
||||
if (connection == null || connection.isClosed()) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public PreparedStatement preparedStatement(String query) {
|
||||
PreparedStatement ps = null;
|
||||
if (!isConnectionOpen()) {
|
||||
HMCCosmeticsPlugin.getInstance().getLogger().info("Connection is not open");
|
||||
}
|
||||
try {
|
||||
ps = connection.prepareStatement(query);
|
||||
} catch (SQLException e) {
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package com.hibiscusmc.hmccosmetics.listener;
|
||||
|
||||
import com.hibiscusmc.hmccosmetics.HMCCosmeticsPlugin;
|
||||
import com.hibiscusmc.hmccosmetics.database.Database;
|
||||
import com.hibiscusmc.hmccosmetics.user.CosmeticUser;
|
||||
import com.hibiscusmc.hmccosmetics.user.CosmeticUsers;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.event.EventHandler;
|
||||
@@ -16,7 +18,9 @@ public class PlayerConnectionListener implements Listener {
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
CosmeticUser user = Database.get(event.getPlayer().getUniqueId());
|
||||
CosmeticUsers.addUser(user);
|
||||
user.updateCosmetic();
|
||||
Bukkit.getScheduler().runTaskLater(HMCCosmeticsPlugin.getInstance(), () -> {
|
||||
user.updateCosmetic();
|
||||
}, 2);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
|
||||
@@ -2,6 +2,8 @@ package com.hibiscusmc.hmccosmetics.util;
|
||||
|
||||
import com.comphenix.protocol.wrappers.WrappedGameProfile;
|
||||
import com.comphenix.protocol.wrappers.WrappedSignedProperty;
|
||||
import com.hibiscusmc.hmccosmetics.HMCCosmeticsPlugin;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
Reference in New Issue
Block a user