Skip to content

Commit

Permalink
really need to stop having gpt write my sql statements
Browse files Browse the repository at this point in the history
  • Loading branch information
Jsinco committed Aug 24, 2024
1 parent 8016e4d commit d5b5e76
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 62 deletions.
10 changes: 7 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
val langVersion: Int = 17

group = "com.dre.brewery"
version = "3.2.5"
version = "3.2.6"

repositories {
mavenCentral()
Expand All @@ -29,7 +29,9 @@ repositories {

// TODO: Figure out exclusions because this is a mess.
dependencies {
compileOnly("org.spigotmc:spigot-api:1.20.2-R0.1-SNAPSHOT")
compileOnly("org.spigotmc:spigot-api:1.20.2-R0.1-SNAPSHOT") {
exclude("com.google.code.gson", "gson")
}

compileOnly("net.milkbowl.vault:VaultAPI:1.6")
compileOnly("com.sk89q:worldguard:6.1") // https://dev.bukkit.org/projects/worldedit/files
Expand All @@ -49,6 +51,7 @@ dependencies {
compileOnly("nl.rutgerkok:blocklocker:1.10.4") // https://www.spigotmc.org/resources/blocklocker.3268/history
compileOnly("me.clip:placeholderapi:2.11.5") // https://www.spigotmc.org/resources/placeholderapi.6245/history

implementation("com.google.code.gson:gson:2.11.0")
implementation("org.jetbrains:annotations:16.0.2") // https://www.jetbrains.com/help/idea/annotating-source-code.html
implementation("com.github.Anon8281:UniversalScheduler:0.1.3") // https://github.com/Anon8281/UniversalScheduler
//implementation("org.bstats:bstats-bukkit:3.0.2") // https://bstats.org/getting-started/include-metrics
Expand Down Expand Up @@ -85,8 +88,9 @@ tasks {
}

shadowJar {
//relocate("org.bstats", "com.dre.brewery.integration.bstats")
relocate("com.google", "com.dre.brewery.integration.google")
relocate("com.github.Anon8281.universalScheduler", "com.dre.brewery.integration.universalScheduler")
//relocate("org.bstats", "com.dre.brewery.integration.bstats")

archiveClassifier.set("")
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/dre/brewery/recipe/BRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public static List<RecipeItem> loadIngredients(ConfigurationSection cfg, String
// Check if this is a Plugin Item
String[] pluginItem = matParts[0].split(":");
if (pluginItem.length > 1) {
RecipeItem custom = PluginItem.fromConfig(pluginItem[0], pluginItem[1]);
RecipeItem custom = com.dre.brewery.recipe.PluginItem.fromConfig(pluginItem[0], pluginItem[1]);
if (custom != null) {
custom.setAmount(amount);
custom.makeImmutable();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/dre/brewery/recipe/PluginItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* <p>See /integration/item for examples on how to extend this class.
* <p>This class stores items as name of the plugin and item id
*/
public abstract class PluginItem extends RecipeItem implements Ingredient {
public abstract class PluginItem extends com.dre.brewery.recipe.RecipeItem implements com.dre.brewery.recipe.Ingredient {

private static Map<String, Supplier<PluginItem>> constructors = new HashMap<>();

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/dre/brewery/storage/DataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,14 @@ public static DataManager createDataManager(ConfiguredDataManager record) throws
if (BData.checkForLegacyData()) {
long start = System.currentTimeMillis();
plugin.log("&5Brewery is loading data from a legacy format!");
plugin.log("&6Brewery can only load legacy data from worlds that exist. If you're trying to migrate old cauldrons, barrels, etc. And the worlds they're in don't exist, you'll need to migrate manually.");

BData.readData();
BData.finalizeLegacyDataMigration();

dataManager.saveAll(false);

plugin.log("&5Finished migrating legacy data! Took&7: &6" + (System.currentTimeMillis() - start) + "&5ms &cJoin our discord if you need assistance&7: &chttps://discord.gg/3FkNaNDnta");
plugin.log("&5Finished migrating legacy data! Took&7: &a" + (System.currentTimeMillis() - start) + "ms! &5Join our discord if you need assistance&7: &5https://discord.gg/3FkNaNDnta");
plugin.warningLog("Brewery can only load legacy data from worlds that exist. If you're trying to migrate old cauldrons, barrels, etc. And the worlds they're in don't exist, you'll need to migrate manually.");
}


Expand Down
110 changes: 55 additions & 55 deletions src/main/java/com/dre/brewery/storage/impls/MySQLStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private <T> T getGeneric(UUID id, String table, Class<T> type) {
return null;
}

private <T> List<T> getAllGeneric(String table, Class<T> type) {
private <T extends SerializableThing> List<T> getAllGeneric(String table, Class<T> type) {
String sql = "SELECT id, data FROM " + tablePrefix + table;
List<T> objects = new ArrayList<>();

Expand All @@ -108,60 +108,60 @@ private <T> List<T> getAllGeneric(String table, Class<T> type) {
}


private void saveAllGeneric(List<? extends SerializableThing> serializableThings, String table, boolean overwrite) {
if (!overwrite) {
String insertSql = "INSERT INTO " + tablePrefix + table + " (id, data) VALUES (?, ?) ON DUPLICATE KEY UPDATE data = VALUES(data)";
try (PreparedStatement insertStatement = connection.prepareStatement(insertSql)) {
for (SerializableThing serializableThing : serializableThings) {
insertStatement.setString(1, serializableThing.getId());
insertStatement.setString(2, serializer.serialize(serializableThing));
insertStatement.addBatch();
}
insertStatement.executeBatch();
} catch (SQLException e) {
plugin.errorLog("Failed to save barrels to MySQL!", e);
}
return;
}

String createTempTableSql = "CREATE TEMPORARY TABLE temp_" + table + " (id VARCHAR(36), data LONGTEXT, PRIMARY KEY (id))";
String insertTempTableSql = "INSERT INTO temp_" + table + " (id, data) VALUES (?, ?)";
String replaceTableSql = "REPLACE INTO " + tablePrefix + table + " SELECT * FROM temp_" + table;
String dropTempTableSql = "DROP TEMPORARY TABLE temp_" + table;

try {
connection.setAutoCommit(false);

try (PreparedStatement createTempTableStmt = connection.prepareStatement(createTempTableSql);
PreparedStatement insertTempTableStmt = connection.prepareStatement(insertTempTableSql)) {

createTempTableStmt.execute();

for (SerializableThing serializableThing : serializableThings) {
insertTempTableStmt.setString(1, serializableThing.getId());
insertTempTableStmt.setString(2, serializer.serialize(serializableThing));
insertTempTableStmt.addBatch();
}
insertTempTableStmt.executeBatch();

try (PreparedStatement replaceTableStmt = connection.prepareStatement(replaceTableSql);
PreparedStatement dropTempTableStmt = connection.prepareStatement(dropTempTableSql)) {

replaceTableStmt.execute();
dropTempTableStmt.execute();
}

connection.commit();
} catch (SQLException e) {
connection.rollback();
plugin.errorLog("Failed to save objects to: " + table + ", to: MySQL!", e);
} finally {
connection.setAutoCommit(true);
}
} catch (SQLException e) {
plugin.errorLog("Failed to manage transaction for saving objects to: " + table + ", to: MySQL!", e);
}
}
private void saveAllGeneric(List<? extends SerializableThing> serializableThings, String table, boolean overwrite) {
if (!overwrite) {
String insertSql = "INSERT INTO " + tablePrefix + table + " (id, data) VALUES (?, ?) ON DUPLICATE KEY UPDATE data = VALUES(data)";
try (PreparedStatement insertStatement = connection.prepareStatement(insertSql)) {
for (SerializableThing serializableThing : serializableThings) {
insertStatement.setString(1, serializableThing.getId());
insertStatement.setString(2, serializer.serialize(serializableThing));
insertStatement.addBatch();
}
insertStatement.executeBatch();
} catch (SQLException e) {
plugin.errorLog("Failed to save to MySQL!", e);
}
return;
}

String createTempTableSql = "CREATE TEMPORARY TABLE temp_" + table + " (id VARCHAR(36), data LONGTEXT, PRIMARY KEY (id))";
String insertTempTableSql = "INSERT INTO temp_" + table + " (id, data) VALUES (?, ?) ON DUPLICATE KEY UPDATE data = VALUES(data)";
String replaceTableSql = "REPLACE INTO " + tablePrefix + table + " SELECT * FROM temp_" + table;
String dropTempTableSql = "DROP TEMPORARY TABLE temp_" + table;

try {
connection.setAutoCommit(false);

try (PreparedStatement createTempTableStmt = connection.prepareStatement(createTempTableSql);
PreparedStatement insertTempTableStmt = connection.prepareStatement(insertTempTableSql)) {

createTempTableStmt.execute();

for (SerializableThing serializableThing : serializableThings) {
insertTempTableStmt.setString(1, serializableThing.getId());
insertTempTableStmt.setString(2, serializer.serialize(serializableThing));
insertTempTableStmt.addBatch();
}
insertTempTableStmt.executeBatch();

try (PreparedStatement replaceTableStmt = connection.prepareStatement(replaceTableSql);
PreparedStatement dropTempTableStmt = connection.prepareStatement(dropTempTableSql)) {

replaceTableStmt.execute();
dropTempTableStmt.execute();
}

connection.commit();
} catch (SQLException e) {
connection.rollback();
plugin.errorLog("Failed to save objects to: " + table + " due to SQL exception!", e);
} finally {
connection.setAutoCommit(true);
}
} catch (SQLException e) {
plugin.errorLog("Failed to manage transaction for saving objects to: " + table + " due to SQL exception!", e);
}
}

private <T extends SerializableThing> void saveGeneric(T serializableThing, String table) {
String sql = "INSERT INTO " + tablePrefix + table + " (id, data) VALUES (?, ?) ON DUPLICATE KEY UPDATE data = VALUES(data)";
Expand Down

0 comments on commit d5b5e76

Please sign in to comment.