mirror of
https://github.com/HibiscusMC/HMCCosmetics.git
synced 2026-01-04 15:41:45 +00:00
initial work
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
package com.hibiscusmc.hmccosmetics.database;
|
||||
|
||||
import com.hibiscusmc.hmccosmetics.HMCCosmeticsPlugin;
|
||||
import com.hibiscusmc.hmccosmetics.config.DatabaseSettings;
|
||||
import com.hibiscusmc.hmccosmetics.database.types.Data;
|
||||
import com.hibiscusmc.hmccosmetics.database.types.InternalData;
|
||||
import com.hibiscusmc.hmccosmetics.database.types.MySQLData;
|
||||
import com.hibiscusmc.hmccosmetics.user.CosmeticUser;
|
||||
import com.hibiscusmc.hmccosmetics.user.CosmeticUsers;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class Database {
|
||||
|
||||
private static Data data;
|
||||
|
||||
private static InternalData INTERNAL_DATA = new InternalData();
|
||||
private static MySQLData MYSQL_DATA = new MySQLData();
|
||||
|
||||
public Database() {
|
||||
String databaseType = DatabaseSettings.getDatabaseType();
|
||||
data = INTERNAL_DATA; // default
|
||||
if (databaseType.equalsIgnoreCase("INTERNAL")) {
|
||||
data = INTERNAL_DATA;
|
||||
HMCCosmeticsPlugin.getInstance().getLogger().severe("Datatype set to internal data");
|
||||
}
|
||||
if (databaseType.equalsIgnoreCase("MySQL")) {
|
||||
data = MYSQL_DATA;
|
||||
HMCCosmeticsPlugin.getInstance().getLogger().severe("Datatype set to MySQL data");
|
||||
}
|
||||
HMCCosmeticsPlugin.getInstance().getLogger().severe("Database is " + data);
|
||||
|
||||
setup();
|
||||
}
|
||||
|
||||
public static void setup() {
|
||||
data.setup();
|
||||
}
|
||||
|
||||
public static void save(CosmeticUser user) {
|
||||
data.save(user);
|
||||
}
|
||||
|
||||
public static void save(Player player) {
|
||||
data.save(CosmeticUsers.getUser(player));
|
||||
}
|
||||
|
||||
public static CosmeticUser get(UUID uniqueId) {
|
||||
return data.get(uniqueId);
|
||||
}
|
||||
|
||||
public static Data getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public static void clearData(UUID uniqueId) {
|
||||
data.clear(uniqueId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.hibiscusmc.hmccosmetics.database.types;
|
||||
|
||||
import com.hibiscusmc.hmccosmetics.cosmetic.Cosmetic;
|
||||
import com.hibiscusmc.hmccosmetics.cosmetic.CosmeticSlot;
|
||||
import com.hibiscusmc.hmccosmetics.cosmetic.Cosmetics;
|
||||
import com.hibiscusmc.hmccosmetics.user.CosmeticUser;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public class Data {
|
||||
|
||||
public void setup() {
|
||||
// Override
|
||||
}
|
||||
|
||||
public void save(CosmeticUser user) {
|
||||
// Override
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public CosmeticUser get(UUID uniqueId) {
|
||||
// Override
|
||||
return null;
|
||||
}
|
||||
|
||||
public void clear(UUID uniqueId) {
|
||||
// Override
|
||||
}
|
||||
|
||||
public String steralizeData(CosmeticUser user) {
|
||||
String data = "";
|
||||
for (Cosmetic cosmetic : user.getCosmetic()) {
|
||||
String input = cosmetic.getSlot() + "=" + cosmetic.getId();
|
||||
data = data + "," + input;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
public Map<CosmeticSlot, Cosmetic> desteralizedata(String raw) {
|
||||
Map<CosmeticSlot, Cosmetic> cosmetics = new HashMap<>();
|
||||
|
||||
String[] rawData = raw.split(",");
|
||||
for (String a : rawData) {
|
||||
if (a == null || a.isEmpty()) continue;
|
||||
String[] splitData = a.split("=");
|
||||
CosmeticSlot slot = null;
|
||||
Cosmetic cosmetic = null;
|
||||
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;
|
||||
cosmetics.put(slot, cosmetic);
|
||||
}
|
||||
return cosmetics;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
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;
|
||||
import com.hibiscusmc.hmccosmetics.user.CosmeticUser;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public class InternalData extends Data {
|
||||
|
||||
NamespacedKey key = new NamespacedKey(HMCCosmeticsPlugin.getInstance(), "cosmetics");
|
||||
|
||||
@Override
|
||||
public void setup() {
|
||||
// Nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(CosmeticUser user) {
|
||||
Player player = Bukkit.getPlayer(user.getUniqueId());
|
||||
|
||||
player.getPersistentDataContainer().set(key, PersistentDataType.STRING, steralizeData(user));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CosmeticUser get(UUID uniqueId) {
|
||||
Player player = Bukkit.getPlayer(uniqueId);
|
||||
CosmeticUser user = new CosmeticUser(uniqueId);
|
||||
|
||||
if (!player.getPersistentDataContainer().has(key, PersistentDataType.STRING)) return user;
|
||||
String rawData = player.getPersistentDataContainer().get(key, PersistentDataType.STRING);
|
||||
|
||||
Map<CosmeticSlot, Cosmetic> a = desteralizedata(rawData);
|
||||
for (CosmeticSlot slot : a.keySet()) {
|
||||
user.addPlayerCosmetic(a.get(slot));
|
||||
//HMCCosmeticsPlugin.getInstance().getLogger().info("Retrieved " + player.getName() + " | slot " + slot + " | cosmetic " + Cosmetics.getCosmetic(player.getPersistentDataContainer().get(key, PersistentDataType.STRING)));
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear(UUID uniqueId) {
|
||||
Player player = Bukkit.getPlayer(uniqueId);
|
||||
|
||||
if (player.getPersistentDataContainer().has(key, PersistentDataType.STRING)) {
|
||||
player.getPersistentDataContainer().remove(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
package com.hibiscusmc.hmccosmetics.database.types;
|
||||
|
||||
import com.hibiscusmc.hmccosmetics.HMCCosmeticsPlugin;
|
||||
import com.hibiscusmc.hmccosmetics.config.DatabaseSettings;
|
||||
import com.hibiscusmc.hmccosmetics.cosmetic.Cosmetic;
|
||||
import com.hibiscusmc.hmccosmetics.cosmetic.CosmeticSlot;
|
||||
import com.hibiscusmc.hmccosmetics.user.CosmeticUser;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.UUID;
|
||||
|
||||
public class MySQLData extends Data {
|
||||
|
||||
// Connection Information
|
||||
private String host;
|
||||
private String user;
|
||||
private String database;
|
||||
private String password;
|
||||
private int port;
|
||||
|
||||
private Connection connection;
|
||||
|
||||
@Override
|
||||
public void setup() {
|
||||
host = DatabaseSettings.getHost();
|
||||
user = DatabaseSettings.getUsername();
|
||||
database = DatabaseSettings.getDatabase();
|
||||
password = DatabaseSettings.getPassword();
|
||||
port = DatabaseSettings.getPort();
|
||||
|
||||
HMCCosmeticsPlugin plugin = HMCCosmeticsPlugin.getInstance();
|
||||
try {
|
||||
openConnection();
|
||||
connection.prepareStatement("CREATE TABLE IF NOT EXISTS `COSMETICDATABASE` " +
|
||||
"(UUID varchar(200) PRIMARY KEY, " +
|
||||
"COSMETICS MEDIUMTEXT " +
|
||||
");");
|
||||
} catch (SQLException e) {
|
||||
plugin.getLogger().severe("");
|
||||
plugin.getLogger().severe("");
|
||||
plugin.getLogger().severe("MySQL DATABASE CAN NOT BE REACHED.");
|
||||
plugin.getLogger().severe("CHECK CONFIG FOR ERRORS");
|
||||
plugin.getLogger().severe("");
|
||||
plugin.getLogger().severe("SAFETY SHUTTING DOWN SERVER");
|
||||
plugin.getLogger().severe("");
|
||||
plugin.getLogger().severe("");
|
||||
Bukkit.shutdown();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void save(CosmeticUser user) {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(HMCCosmeticsPlugin.getInstance(), () -> {
|
||||
try {
|
||||
PreparedStatement preparedSt = preparedStatement("REPLACE INTO COSMETICDATABASE(UUID,COSMETICS) VALUES(?,?);");
|
||||
preparedSt.setString(1, user.getUniqueId().toString());
|
||||
preparedSt.setString(2, steralizeData(user));
|
||||
preparedSt.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public CosmeticUser get(UUID uniqueId) {
|
||||
CosmeticUser user = new CosmeticUser(uniqueId);
|
||||
Bukkit.getScheduler().runTaskAsynchronously(HMCCosmeticsPlugin.getInstance(), () -> {
|
||||
try {
|
||||
PreparedStatement preparedStatement = preparedStatement("SELECT COUNT(UUID) FROM COSMETICDATABASE WHERE UUID = ?;");
|
||||
preparedStatement.setString(1, uniqueId.toString());
|
||||
ResultSet rs = preparedStatement.executeQuery();
|
||||
if (rs.next()) {
|
||||
if (rs.getInt(1) == 0) { // Not in the system
|
||||
// nothing
|
||||
} else {
|
||||
PreparedStatement preState2 = preparedStatement("SELECT * FROM COSMETICDATABASE WHERE UUID = ?;");
|
||||
preState2.setString(1, uniqueId.toString());
|
||||
ResultSet rs2 = preState2.executeQuery();
|
||||
if (rs2.next()) {
|
||||
String rawData = rs.getString("COSMETICS");
|
||||
Map<CosmeticSlot, Cosmetic> cosmetics = desteralizedata(rawData);
|
||||
for (Cosmetic cosmetic : cosmetics.values()) {
|
||||
user.addPlayerCosmetic(cosmetic);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
return user;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear(UUID unqiueId) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
private void openConnection() throws SQLException {
|
||||
if (connection != null && !connection.isClosed()) {
|
||||
return;
|
||||
}
|
||||
Bukkit.getScheduler().runTaskAsynchronously(HMCCosmeticsPlugin.getInstance(), () -> {
|
||||
|
||||
//close Connection if still active
|
||||
if (connection != null) {
|
||||
close();
|
||||
}
|
||||
|
||||
//connect to database host
|
||||
try {
|
||||
Class.forName("com.mysql.jdbc.Driver");
|
||||
|
||||
connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, setupProperties());
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.out.println(e.getMessage());
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
});
|
||||
connection = DriverManager.getConnection("jdbc:mysql://" + DatabaseSettings.getHost() + ":" + DatabaseSettings.getPort() + "/" + DatabaseSettings.getDatabase(), setupProperties());
|
||||
}
|
||||
|
||||
public void close() {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(HMCCosmeticsPlugin.getInstance(), () -> {
|
||||
try {
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private Properties setupProperties() {
|
||||
Properties props = new Properties();
|
||||
props.put("user", user);
|
||||
props.put("password", password);
|
||||
props.put("autoReconnect", "true");
|
||||
return props;
|
||||
}
|
||||
|
||||
private boolean isConnectionOpen() throws SQLException{
|
||||
if (connection == null || connection.isClosed()) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public PreparedStatement preparedStatement(String query) {
|
||||
PreparedStatement ps = null;
|
||||
try {
|
||||
ps = connection.prepareStatement(query);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return ps;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user