diff --git a/build.gradle b/build.gradle index 01a698c..5bb31a5 100644 --- a/build.gradle +++ b/build.gradle @@ -26,7 +26,7 @@ repositories { // includeGroup("cc.tweaked") // } // } - maven {url = "https://maven.kyrptonaught.dev"} + maven { url = "https://maven.kyrptonaught.dev" } maven { url = 'https://maven.minecraftforge.net/' } // for Terrablender } @@ -47,6 +47,9 @@ dependencies { mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2" modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" + // modImplementation "mysql:mysql-connector-java:9.1.0" + include(implementation("mysql:mysql-connector-java:8.0.27")) + // Fabric API. This is technically optional, but you probably want it anyway. modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" //modCompileOnly "cc.tweaked:cc-tweaked-1.20-fabric-api:1.105.0" diff --git a/mysql-connector-java-8.0.27.jar b/mysql-connector-java-8.0.27.jar new file mode 100644 index 0000000..683ac26 Binary files /dev/null and b/mysql-connector-java-8.0.27.jar differ diff --git a/src/main/java/jesse/keeblarcraft/BankMgr/IndividualBank.java b/src/main/java/jesse/keeblarcraft/BankMgr/IndividualBank.java index 298966b..0358f58 100644 --- a/src/main/java/jesse/keeblarcraft/BankMgr/IndividualBank.java +++ b/src/main/java/jesse/keeblarcraft/BankMgr/IndividualBank.java @@ -88,7 +88,7 @@ public class IndividualBank { // TODO: REPLACE WITH SQL SERVER. DIRTY ITERATE OVER ALL FILES IN DIRECTORY TO LOAD STRUCTURE File dir = new File(accountsListDir); File[] allFiles = dir.listFiles(); - if (allFiles != null) { + if (accounts != null && allFiles != null) { for (File file : allFiles ) { // First grab file identifier as KEY String accountIdentifier = file.getName(); @@ -123,22 +123,27 @@ public class IndividualBank { // 'accountsListFromName' structure. This makes it no worse than O(n) to fill these two structures in. // NOTE: This is an *EXPENSIVE* operation! Future us might need to update this. Also note a method is needed for everytime a player opens a new account // or gets put on one to update the map every time - for (Entry account : accounts.accountsList.entrySet()) { - // We must loop over the string of holders for each account as well to make the flattened accountsListFromName map - List accountHolders = account.getValue().GetAccountHolders(); + if (accounts != null) { + for (Entry account : accounts.accountsList.entrySet()) { + // We must loop over the string of holders for each account as well to make the flattened accountsListFromName map + List accountHolders = account.getValue().GetAccountHolders(); - // Match each user to the secondary map & add to list-value if not existing - for (Integer holderIndex = 0; holderIndex < accountHolders.size(); holderIndex++) { - if (accounts.accountsListFromName.containsKey(accountHolders.get(holderIndex))) { - // Case 1: User exists, update map entry - accounts.accountsListFromName.get(accountHolders.get(holderIndex)).add(account.getKey()); // Add a new account id to this person in the new flat map - } else { - // Case 2: User does not already exist; add a new map entry - accounts.accountsListFromName.put(accountHolders.get(holderIndex), List.of(account.getKey())); // Store name as key, and new List with the value of ACCOUNT # + // Match each user to the secondary map & add to list-value if not existing + for (Integer holderIndex = 0; holderIndex < accountHolders.size(); holderIndex++) { + if (accounts.accountsListFromName.containsKey(accountHolders.get(holderIndex))) { + // Case 1: User exists, update map entry + accounts.accountsListFromName.get(accountHolders.get(holderIndex)).add(account.getKey()); // Add a new account id to this person in the new flat map + } else { + // Case 2: User does not already exist; add a new map entry + accounts.accountsListFromName.put(accountHolders.get(holderIndex), List.of(account.getKey())); // Store name as key, and new List with the value of ACCOUNT # + } } } + numberOfAccounts = accounts.accountsList.size(); + } else { + accounts = new Accounts(); + numberOfAccounts = 0; } - numberOfAccounts = accounts.accountsList.size(); } ///////////////////////////////////////////////////////////////////////////// diff --git a/src/main/java/jesse/keeblarcraft/Commands/ShortcutCommands.java b/src/main/java/jesse/keeblarcraft/Commands/ShortcutCommands.java index 6b0aa2f..f835221 100644 --- a/src/main/java/jesse/keeblarcraft/Commands/ShortcutCommands.java +++ b/src/main/java/jesse/keeblarcraft/Commands/ShortcutCommands.java @@ -10,6 +10,8 @@ package jesse.keeblarcraft.Commands; +import java.util.function.Supplier; + import com.mojang.brigadier.arguments.IntegerArgumentType; import com.mojang.brigadier.context.CommandContext; diff --git a/src/main/java/jesse/keeblarcraft/ConfigMgr/ConfigManager.java b/src/main/java/jesse/keeblarcraft/ConfigMgr/ConfigManager.java index 729a754..576bc3d 100644 --- a/src/main/java/jesse/keeblarcraft/ConfigMgr/ConfigManager.java +++ b/src/main/java/jesse/keeblarcraft/ConfigMgr/ConfigManager.java @@ -31,7 +31,6 @@ import java.util.ArrayList; import java.util.HashMap; import jesse.keeblarcraft.Keeblarcraft; -import jesse.keeblarcraft.ChatStuff.ChatMsg; import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtIo; import net.minecraft.nbt.NbtList; @@ -45,6 +44,7 @@ public class ConfigManager { // Get a File reference to a file that is created on disk private File GetFile(String confFile) { File file = null; + System.out.println("Get file called for " + GLOBAL_CONFIG + confFile); try { file = new File(GLOBAL_CONFIG + confFile); } catch (Exception e) {} @@ -284,7 +284,6 @@ public class ConfigManager { public T GetJsonObjectFromFile(String fileName, Class classToConvertTo) throws JsonSyntaxException { Gson gson = new Gson(); String ret = ""; - fileName = "config/keeblarcraft/" + fileName; // hot fix: Not sure how to return "false" for invalid conversion when I'm forced to convert or just catch... Look into a better // return value in the future - but for now throw JsonSyntaxException no matter what exception is caught diff --git a/src/main/java/jesse/keeblarcraft/ConfigMgr/GeneralConfig.java b/src/main/java/jesse/keeblarcraft/ConfigMgr/GeneralConfig.java index 940dbd8..0ff4f65 100644 --- a/src/main/java/jesse/keeblarcraft/ConfigMgr/GeneralConfig.java +++ b/src/main/java/jesse/keeblarcraft/ConfigMgr/GeneralConfig.java @@ -45,7 +45,6 @@ public class GeneralConfig { try { config = cfgMgr.GetJsonObjectFromFile("general.json", ImplementedConfig.class); - System.out.println("Read in config. Random value: " + config.global_spawn.x); existingFile = true; } catch(Exception e) { System.out.println("FAILED TO READ IN GENERALCONFIG"); @@ -87,6 +86,15 @@ public class GeneralConfig { FlashConfig(); } + ///////////////////////////////////////////////////////////////////////////// + /// @fn GetSQLPassword + /// + /// @brief Returns the SQL password set in the text config + ///////////////////////////////////////////////////////////////////////////// + public String GetSQLPassword() { + return config.sqlPassword; + } + ///////////////////////////////////////////////////////////////////////////// /// @fn IsNewPlayer /// @@ -125,6 +133,7 @@ public class GeneralConfig { ///////////////////////////////////////////////////////////////////////////// private class ImplementedConfig { public String MOTD = "Welcome to the server! Have fun!"; + public String sqlPassword = "PUT SQL PASSWORD HERE"; // This is lazy, but this will fill with every unique UUID that has joined the server. This is how I am checking // to see if a player has just joined the server for a first time or not diff --git a/src/main/java/jesse/keeblarcraft/ConfigMgr/SQLConfig.java b/src/main/java/jesse/keeblarcraft/ConfigMgr/SQLConfig.java new file mode 100644 index 0000000..a0a0029 --- /dev/null +++ b/src/main/java/jesse/keeblarcraft/ConfigMgr/SQLConfig.java @@ -0,0 +1,47 @@ +package jesse.keeblarcraft.ConfigMgr; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +public class SQLConfig { + private static SQLConfig static_inst; + + public static SQLConfig GetInstance() { + if (static_inst == null) { + static_inst = new SQLConfig(); + } + + return static_inst; + } + + Connection conn = null; + + public SQLConfig() { + Boolean canConnect = false; + try { + // According to some random online tutorial; this loads the driver! + Class.forName("com.mysql.cj.jdbc.Driver"); + canConnect = true; + } catch (Exception e) { + System.out.println("Could not find the proper SQL JDBC Drivers. Cannot connect to SQL Config"); + e.printStackTrace(); + } + + if (canConnect) { + try { + conn = DriverManager.getConnection("jdbc:mysql://localhost/keeblarcraft", "keeblarcraft", GeneralConfig.GetInstance().GetSQLPassword()); + Statement stmnt = conn.createStatement(); + String testSql = "SELECT * FROM test_table"; + ResultSet rs = stmnt.executeQuery(testSql); + + System.out.println("Printing out result set from test query"); + while (rs.next()) { + System.out.println("[RS]: " + rs.getString("test_name")); + } + } catch (SQLException e) {} + } + } +} diff --git a/src/main/java/jesse/keeblarcraft/Keeblarcraft.java b/src/main/java/jesse/keeblarcraft/Keeblarcraft.java index 9a1c3fa..d6d25fe 100644 --- a/src/main/java/jesse/keeblarcraft/Keeblarcraft.java +++ b/src/main/java/jesse/keeblarcraft/Keeblarcraft.java @@ -40,6 +40,7 @@ import jesse.keeblarcraft.AttributeMgr.AttributeMgr; import jesse.keeblarcraft.AttributeMgr.AttributeTree; import jesse.keeblarcraft.BankMgr.BankManager; import jesse.keeblarcraft.Commands.CustomCommandManager; +import jesse.keeblarcraft.ConfigMgr.SQLConfig; import jesse.keeblarcraft.CustomBlocks.BlockList; import jesse.keeblarcraft.CustomBlocks.BlockEntities.BlockEntityRegistration; import jesse.keeblarcraft.CustomItems.ItemManager; @@ -118,6 +119,9 @@ public class Keeblarcraft implements ModInitializer { // Register the banking system BankManager.GetInstance().InitializeBanks(); + System.out.println("Attempting SQL Registration call"); + SQLConfig.GetInstance(); + /// THE BELOW ITEMS MUST BE DONE LAST IN THE STEPS // Register items