forked from sorceress/EasyAuth
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() {
|
public void onInitializeServer() {
|
||||||
// Info I guess :D
|
// Info I guess :D
|
||||||
LOGGER.info("SimpleAuth mod by samo_lego.");
|
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!");
|
LOGGER.info("This mod wouldn't exist without the awesome Fabric Community. TYSM guys!");
|
||||||
|
|
||||||
// Creating data directory (database is stored there)
|
// 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));
|
BreakBlockCallback.EVENT.register((world, pos, state, player) -> AuthEventHandler.onBlockBroken(player));
|
||||||
|
|
||||||
// Connection to database
|
// Connection to database
|
||||||
SimpleAuthDatabase.connect();
|
SimpleAuthDatabase.main();
|
||||||
}
|
}
|
||||||
public static HashSet<ServerPlayerEntity> authenticatedUsers = new HashSet<>();
|
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.server.network.ServerPlayerEntity;
|
||||||
import net.minecraft.text.LiteralText;
|
import net.minecraft.text.LiteralText;
|
||||||
import net.minecraft.text.Text;
|
import net.minecraft.text.Text;
|
||||||
|
import net.minecraft.text.TranslatableText;
|
||||||
import org.samo_lego.simpleauth.SimpleAuth;
|
import org.samo_lego.simpleauth.SimpleAuth;
|
||||||
|
|
||||||
import static com.mojang.brigadier.arguments.StringArgumentType.getString;
|
import static com.mojang.brigadier.arguments.StringArgumentType.getString;
|
||||||
|
@ -15,7 +16,7 @@ import static net.minecraft.server.command.CommandManager.literal;
|
||||||
|
|
||||||
public class LoginCommand {
|
public class LoginCommand {
|
||||||
private static LiteralText PleaseLogin = new LiteralText("§4Type /login <password> to login.");
|
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) {
|
public static void registerCommand(CommandDispatcher<ServerCommandSource> dispatcher) {
|
||||||
// Registering the "/login" command
|
// Registering the "/login" command
|
||||||
|
|
|
@ -4,6 +4,10 @@ import com.google.common.io.Files;
|
||||||
import com.mojang.brigadier.CommandDispatcher;
|
import com.mojang.brigadier.CommandDispatcher;
|
||||||
import net.minecraft.server.command.ServerCommandSource;
|
import net.minecraft.server.command.ServerCommandSource;
|
||||||
import net.minecraft.text.LiteralText;
|
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.getString;
|
||||||
import static com.mojang.brigadier.arguments.StringArgumentType.word;
|
import static com.mojang.brigadier.arguments.StringArgumentType.word;
|
||||||
|
@ -12,8 +16,8 @@ import static net.minecraft.server.command.CommandManager.literal;
|
||||||
|
|
||||||
|
|
||||||
public class RegisterCommand {
|
public class RegisterCommand {
|
||||||
private static LiteralText PleaseRegister = new LiteralText("§4Type /register <password> <password> to login.");
|
private static TranslatableText PleaseRegister = new TranslatableText("§4Type /register <password> <password> to login.");
|
||||||
private static LiteralText EnterPassword = new LiteralText("§6You need to enter your password twice.");
|
private static TranslatableText EnterPassword = new TranslatableText("command.simpleauth.passwordTwice");
|
||||||
|
|
||||||
public static void registerCommand(CommandDispatcher<ServerCommandSource> dispatcher) {
|
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) {
|
private static int register(ServerCommandSource source, String pass1, String pass2) {
|
||||||
if(pass1.equals(pass2)){
|
if(pass1.equals(pass2)){
|
||||||
// Hashing the password
|
// Hashing the password
|
||||||
|
SimpleAuthDatabase.insert(Objects.requireNonNull(source.getEntity()).getUuidAsString(), source.getName(), pass1);
|
||||||
source.getMinecraftServer().getPlayerManager().broadcastChatMessage(
|
source.getMinecraftServer().getPlayerManager().broadcastChatMessage(
|
||||||
new LiteralText(source.getName() + ", you have registered successfully!"),
|
new LiteralText(source.getName() + ", you have registered successfully!"),
|
||||||
false
|
false
|
||||||
|
|
|
@ -1,17 +1,18 @@
|
||||||
package org.samo_lego.simpleauth.database;
|
package org.samo_lego.simpleauth.database;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import java.sql.DriverManager;
|
import org.apache.logging.log4j.Logger;
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.sql.Statement;
|
import java.sql.*;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @author sqlitetutorial.net
|
* @author sqlitetutorial.net
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class SimpleAuthDatabase {
|
public class SimpleAuthDatabase {
|
||||||
public static void connect() {
|
private static final Logger LOGGER = LogManager.getLogger();
|
||||||
|
/*public static void connect() {
|
||||||
Connection conn = null;
|
Connection conn = null;
|
||||||
try {
|
try {
|
||||||
// db parameters
|
// db parameters
|
||||||
|
@ -19,28 +20,78 @@ public class SimpleAuthDatabase {
|
||||||
// create a connection to the database
|
// create a connection to the database
|
||||||
conn = DriverManager.getConnection(url);
|
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) {
|
} catch (SQLException e) {
|
||||||
System.out.println(e.getMessage());
|
System.out.println(e.getMessage());
|
||||||
} finally {
|
} finally {
|
||||||
|
//Main?
|
||||||
try {
|
try {
|
||||||
if (conn != null) {
|
if (conn != null) {
|
||||||
conn.close();
|
conn.close();
|
||||||
// Main stuff here?
|
|
||||||
main(conn);
|
|
||||||
}
|
}
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
System.out.println(ex.getMessage());
|
System.out.println(ex.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
private static void main(Connection conn) {
|
private static Connection connect() {
|
||||||
String sql = "";
|
// SQLite connection string
|
||||||
try (Statement stmt = conn.createStatement()) {
|
String url = "jdbc:sqlite:mods/SimpleAuth/players.db";
|
||||||
// create a new table
|
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);
|
stmt.execute(sql);
|
||||||
|
|
||||||
} catch (SQLException e) {
|
} 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.entity.player.PlayerEntity;
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
import net.minecraft.text.LiteralText;
|
import net.minecraft.text.LiteralText;
|
||||||
|
import net.minecraft.text.TranslatableText;
|
||||||
import net.minecraft.util.ActionResult;
|
import net.minecraft.util.ActionResult;
|
||||||
import org.samo_lego.simpleauth.SimpleAuth;
|
import org.samo_lego.simpleauth.SimpleAuth;
|
||||||
|
|
||||||
|
@ -11,7 +12,7 @@ import org.samo_lego.simpleauth.SimpleAuth;
|
||||||
* and cancels them if they aren't authenticated
|
* and cancels them if they aren't authenticated
|
||||||
*/
|
*/
|
||||||
public class AuthEventHandler {
|
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
|
// Player joining the server
|
||||||
public static void onPlayerJoin(ServerPlayerEntity player) {
|
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