From bac5c879fa0143e404b0863ed5aee1ce939746dc Mon Sep 17 00:00:00 2001 From: samo_lego <34912839+samolego@users.noreply.github.com> Date: Thu, 7 Nov 2019 22:18:14 +0100 Subject: [PATCH] Database stuff Mostly updating the database manager class. Added translations which don't work yet. --- .../org/samo_lego/simpleauth/SimpleAuth.java | 3 +- .../simpleauth/commands/LoginCommand.java | 3 +- .../simpleauth/commands/RegisterCommand.java | 11 ++- .../database/SimpleAuthDatabase.java | 79 +++++++++++++++---- .../simpleauth/event/AuthEventHandler.java | 3 +- src/main/resources/CreateDatabase.sql | 7 -- .../assets/simpleauth/lang/en_us.lang | 8 ++ src/main/resources/create.sql | 7 ++ src/main/resources/lang/en_us.json | 4 - 9 files changed, 94 insertions(+), 31 deletions(-) delete mode 100644 src/main/resources/CreateDatabase.sql create mode 100644 src/main/resources/assets/simpleauth/lang/en_us.lang create mode 100644 src/main/resources/create.sql delete mode 100644 src/main/resources/lang/en_us.json diff --git a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java index 9261b14..b76b608 100644 --- a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java +++ b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java @@ -24,6 +24,7 @@ public class SimpleAuth implements DedicatedServerModInitializer { public void onInitializeServer() { // Info I guess :D LOGGER.info("SimpleAuth mod by samo_lego."); + // The support on discord was great! I really appreciate your help. LOGGER.info("This mod wouldn't exist without the awesome Fabric Community. TYSM guys!"); // Creating data directory (database is stored there) @@ -47,7 +48,7 @@ public class SimpleAuth implements DedicatedServerModInitializer { BreakBlockCallback.EVENT.register((world, pos, state, player) -> AuthEventHandler.onBlockBroken(player)); // Connection to database - SimpleAuthDatabase.connect(); + SimpleAuthDatabase.main(); } public static HashSet authenticatedUsers = new HashSet<>(); diff --git a/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java b/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java index 1e8830a..044d677 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java @@ -6,6 +6,7 @@ import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.LiteralText; import net.minecraft.text.Text; +import net.minecraft.text.TranslatableText; import org.samo_lego.simpleauth.SimpleAuth; import static com.mojang.brigadier.arguments.StringArgumentType.getString; @@ -15,7 +16,7 @@ import static net.minecraft.server.command.CommandManager.literal; public class LoginCommand { private static LiteralText PleaseLogin = new LiteralText("§4Type /login to login."); - private static LiteralText EnterPassword = new LiteralText("§6You need to enter your password."); + private static TranslatableText EnterPassword = new TranslatableText("command.simpleauth.password"); public static void registerCommand(CommandDispatcher dispatcher) { // Registering the "/login" command diff --git a/src/main/java/org/samo_lego/simpleauth/commands/RegisterCommand.java b/src/main/java/org/samo_lego/simpleauth/commands/RegisterCommand.java index 0acac81..651c199 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/RegisterCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/RegisterCommand.java @@ -4,6 +4,10 @@ import com.google.common.io.Files; import com.mojang.brigadier.CommandDispatcher; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.text.LiteralText; +import net.minecraft.text.TranslatableText; +import org.samo_lego.simpleauth.database.SimpleAuthDatabase; + +import java.util.Objects; import static com.mojang.brigadier.arguments.StringArgumentType.getString; import static com.mojang.brigadier.arguments.StringArgumentType.word; @@ -12,8 +16,8 @@ import static net.minecraft.server.command.CommandManager.literal; public class RegisterCommand { - private static LiteralText PleaseRegister = new LiteralText("§4Type /register to login."); - private static LiteralText EnterPassword = new LiteralText("§6You need to enter your password twice."); + private static TranslatableText PleaseRegister = new TranslatableText("§4Type /register to login."); + private static TranslatableText EnterPassword = new TranslatableText("command.simpleauth.passwordTwice"); public static void registerCommand(CommandDispatcher dispatcher) { @@ -29,10 +33,11 @@ public class RegisterCommand { })); } - // Registering our "register" command + // Registering our "register" command (ik, sounds a bit confusing) private static int register(ServerCommandSource source, String pass1, String pass2) { if(pass1.equals(pass2)){ // Hashing the password + SimpleAuthDatabase.insert(Objects.requireNonNull(source.getEntity()).getUuidAsString(), source.getName(), pass1); source.getMinecraftServer().getPlayerManager().broadcastChatMessage( new LiteralText(source.getName() + ", you have registered successfully!"), false diff --git a/src/main/java/org/samo_lego/simpleauth/database/SimpleAuthDatabase.java b/src/main/java/org/samo_lego/simpleauth/database/SimpleAuthDatabase.java index 53311b9..a243247 100644 --- a/src/main/java/org/samo_lego/simpleauth/database/SimpleAuthDatabase.java +++ b/src/main/java/org/samo_lego/simpleauth/database/SimpleAuthDatabase.java @@ -1,17 +1,18 @@ package org.samo_lego.simpleauth.database; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; -import java.sql.Statement; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.sql.*; +import java.util.UUID; /** - * * @author sqlitetutorial.net */ public class SimpleAuthDatabase { - public static void connect() { + private static final Logger LOGGER = LogManager.getLogger(); + /*public static void connect() { Connection conn = null; try { // db parameters @@ -19,28 +20,78 @@ public class SimpleAuthDatabase { // create a connection to the database conn = DriverManager.getConnection(url); + // Creating database table + String sql = "CREATE TABLE IF NOT EXISTS users (\n" + + " `UserID` INTEGER PRIMARY KEY AUTOINCREMENT,\n" + + " `UUID` BINARY(16) NOT NULL,\n" + + " `Username` VARCHAR(16) NOT NULL,\n" + + " `Password` VARCHAR(64) NOT NULL,\n" + + " UNIQUE (`UUID`)\n" + + ");"; + Statement stmt = conn.createStatement(); + stmt.execute(sql); } catch (SQLException e) { System.out.println(e.getMessage()); } finally { + //Main? try { if (conn != null) { conn.close(); - // Main stuff here? - main(conn); } } catch (SQLException ex) { System.out.println(ex.getMessage()); } } - } - private static void main(Connection conn) { - String sql = ""; - try (Statement stmt = conn.createStatement()) { - // create a new table + }*/ + private static Connection connect() { + // SQLite connection string + String url = "jdbc:sqlite:mods/SimpleAuth/players.db"; + Connection conn = null; + try { + conn = DriverManager.getConnection(url); + // Creating database table if it doesn't exist yet + String sql = "CREATE TABLE IF NOT EXISTS users (\n" + + " `UserID` INTEGER PRIMARY KEY AUTOINCREMENT,\n" + + " `UUID` BINARY(16) NOT NULL,\n" + + " `Username` VARCHAR(16) NOT NULL,\n" + + " `Password` VARCHAR(64) NOT NULL,\n" + + " UNIQUE (`UUID`)\n" + + ");"; + Statement stmt = conn.createStatement(); stmt.execute(sql); + } catch (SQLException e) { - System.out.println(e.getMessage()); + LOGGER.error(e.getMessage()); + } + return conn; + } + + public static void insert(String uuid, String username, String password) { + String sql = "INSERT INTO users(uuid, username, password) VALUES(?,?,?)"; + + try (Connection conn = connect(); + PreparedStatement pstmt = conn.prepareStatement(sql)) { + pstmt.setString(1, uuid); + pstmt.setString(2, username); + pstmt.setString(3, password); + pstmt.executeUpdate(); + } catch (SQLException e) { + LOGGER.error(e.getMessage()); } } + private static void disconnect(Connection conn) { + try { + if (conn != null) { + conn.close(); + } + } catch (SQLException ex) { + LOGGER.error(ex.getMessage()); + } + } + + public static void main() { + Connection conn = connect(); + } + } diff --git a/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java b/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java index 2f6a5dc..94e5036 100644 --- a/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java +++ b/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java @@ -3,6 +3,7 @@ package org.samo_lego.simpleauth.event; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.LiteralText; +import net.minecraft.text.TranslatableText; import net.minecraft.util.ActionResult; import org.samo_lego.simpleauth.SimpleAuth; @@ -11,7 +12,7 @@ import org.samo_lego.simpleauth.SimpleAuth; * and cancels them if they aren't authenticated */ public class AuthEventHandler { - private static LiteralText notAuthenticated = new LiteralText("§4You aren't authenticated."); + private static TranslatableText notAuthenticated = new TranslatableText("command.simpleauth.notAuthenticated"); // Player joining the server public static void onPlayerJoin(ServerPlayerEntity player) { diff --git a/src/main/resources/CreateDatabase.sql b/src/main/resources/CreateDatabase.sql deleted file mode 100644 index 0cfdc2d..0000000 --- a/src/main/resources/CreateDatabase.sql +++ /dev/null @@ -1,7 +0,0 @@ -CREATE TABLE IF NOT EXISTS `{TABLE_NAME}` ( - `UserID` INTEGER PRIMARY KEY AUTO_INCREMENT, - `UUID` BINARY(16) NOT NULL, - `Username` VARCHAR(16), NOT NULL, - `Password` VARCHAR(64) NOT NULL, - UNIQUE (`UUID`) -); \ No newline at end of file diff --git a/src/main/resources/assets/simpleauth/lang/en_us.lang b/src/main/resources/assets/simpleauth/lang/en_us.lang new file mode 100644 index 0000000..967c9f1 --- /dev/null +++ b/src/main/resources/assets/simpleauth/lang/en_us.lang @@ -0,0 +1,8 @@ +{ + "command.simpleauth.password": "§6You need to enter your password!", + "command.simpleauth.passwordTwice": "§6You need to enter your password twice!", + "command.simpleauth.passwordMatch": "§6Passwords must match!", + "command.simpleauth.passwordWrong": "§4Wrong password!", + "command.simpleauth.notAuthenticated": "§4You are not authenticated!", + "command.simpleauth.authenticated": "§aYou are now authenticated." +} \ No newline at end of file diff --git a/src/main/resources/create.sql b/src/main/resources/create.sql new file mode 100644 index 0000000..611f878 --- /dev/null +++ b/src/main/resources/create.sql @@ -0,0 +1,7 @@ +CREATE TABLE IF NOT EXISTS users ( + `UserID` INTEGER PRIMARY KEY AUTOINCREMENT, + `UUID` BINARY(16) NOT NULL, + `Username` VARCHAR(16) NOT NULL, + `Password` VARCHAR(64) NOT NULL, + UNIQUE (`UUID`) +); \ No newline at end of file diff --git a/src/main/resources/lang/en_us.json b/src/main/resources/lang/en_us.json deleted file mode 100644 index 3acd795..0000000 --- a/src/main/resources/lang/en_us.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "text.simpleauth.passwordmatch": "Passwords must match!", - "text.simpleauth.wrongpassword": "Wrong password!" -} \ No newline at end of file