Database stuff
Mostly updating the database manager class. Added translations which don't work yet.
This commit is contained in:
parent
b6c1ff6c3b
commit
bac5c879fa
|
@ -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<ServerPlayerEntity> authenticatedUsers = new HashSet<>();
|
||||
|
||||
|
|
|
@ -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 <password> 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<ServerCommandSource> dispatcher) {
|
||||
// Registering the "/login" command
|
||||
|
|
|
@ -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 <password> <password> to login.");
|
||||
private static LiteralText EnterPassword = new LiteralText("§6You need to enter your password twice.");
|
||||
private static TranslatableText PleaseRegister = new TranslatableText("§4Type /register <password> <password> to login.");
|
||||
private static TranslatableText EnterPassword = new TranslatableText("command.simpleauth.passwordTwice");
|
||||
|
||||
public static void registerCommand(CommandDispatcher<ServerCommandSource> 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
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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`)
|
||||
);
|
|
@ -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."
|
||||
}
|
|
@ -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`)
|
||||
);
|
|
@ -1,4 +0,0 @@
|
|||
{
|
||||
"text.simpleauth.passwordmatch": "Passwords must match!",
|
||||
"text.simpleauth.wrongpassword": "Wrong password!"
|
||||
}
|
Loading…
Reference in New Issue