From 91ddfa1fe8ff93073fbf7afadd98119e6816f140 Mon Sep 17 00:00:00 2001 From: samo_lego <34912839+samolego@users.noreply.github.com> Date: Sun, 27 Oct 2019 11:19:13 +0100 Subject: [PATCH] Some updates to commands --- build.gradle | 3 +- .../samo_lego/simpleauth/AuthCommands.java | 62 ------------------- .../org/samo_lego/simpleauth/SimpleAuth.java | 16 +++-- .../simpleauth/commands/LoginCommand.java | 37 +++++++++++ .../simpleauth/commands/RegisterCommand.java | 49 +++++++++++++++ src/main/resources/lang/en_us.json | 4 ++ 6 files changed, 104 insertions(+), 67 deletions(-) delete mode 100644 src/main/java/org/samo_lego/simpleauth/AuthCommands.java create mode 100644 src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java create mode 100644 src/main/java/org/samo_lego/simpleauth/commands/RegisterCommand.java create mode 100644 src/main/resources/lang/en_us.json diff --git a/build.gradle b/build.gradle index 8d367ac..dce3048 100644 --- a/build.gradle +++ b/build.gradle @@ -21,7 +21,7 @@ dependencies { // Fabric API. This is technically optional, but you probably want it anyway. modCompile "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" - implementation 'org.jetbrains:annotations:15.0' + implementation "org.mindrot:jbcrypt:0.4" // PSA: Some older mods, compiled on Loom 0.2.1, might have outdated Maven POMs. // You may need to force-disable transitiveness on them. @@ -77,5 +77,6 @@ publishing { repositories { // uncomment to publish to the local maven // mavenLocal() + mavenCentral() } } diff --git a/src/main/java/org/samo_lego/simpleauth/AuthCommands.java b/src/main/java/org/samo_lego/simpleauth/AuthCommands.java deleted file mode 100644 index 839145c..0000000 --- a/src/main/java/org/samo_lego/simpleauth/AuthCommands.java +++ /dev/null @@ -1,62 +0,0 @@ -package org.samo_lego.simpleauth; - -import com.mojang.brigadier.CommandDispatcher; -import net.minecraft.server.command.CommandManager; -import net.minecraft.server.command.ServerCommandSource; -import net.minecraft.text.LiteralText; -import net.minecraft.text.Text; - -import static com.mojang.brigadier.arguments.StringArgumentType.*; -import static net.minecraft.server.command.CommandManager.literal; // literal("foo") -import static net.minecraft.server.command.CommandManager.argument; // argument("bar", word()) -import static net.minecraft.server.command.CommandManager.*; - - -class AuthCommands { - - static void registerCommands(CommandDispatcher dispatcher) { - dispatcher.register(literal("register") - .then(argument("password", greedyString()) - /*.executes(ctx -> { - System.out.println(getString(ctx, "password")); - return 1; - })*/ - .then(argument("passwordAgain", string()) - .executes( ctx -> { - System.out.println(getString(ctx, "passwordAgain")); - return 1;}/*register(ctx.getSource(), getString(ctx, "password"), getString(ctx, "password"))*/) - )) - .executes(ctx -> { - System.out.println("You need to enter your password twice!"); - return 1; - })); - // You can deal with the arguments out here and pipe them into the command. - dispatcher.register(literal("login") - .then(argument("password", greedyString()) - .executes(ctx -> login(ctx.getSource(), getString(ctx, "password")) - )) - .executes(ctx -> { - System.out.println("You need to enter your password!"); - return 1; - })); - } - - // Registering our "register" command - private static int register(ServerCommandSource source, String pass1, String pass2) { - System.out.println(pass1); - if(pass1.equals(pass1)){ - Text text = new LiteralText(source.getName() + ", you have entered register command"); - source.getMinecraftServer().getPlayerManager().broadcastChatMessage(text, false); - } - return 1; - } - - // Registering our "login" command - private static int login(ServerCommandSource source, String pass) { - if(pass.equals(pass)){ //From database - Text text = new LiteralText(source.getName() + ", you have entered login command"); - source.getMinecraftServer().getPlayerManager().broadcastChatMessage(text, false); - } - return 1; // Success - } -} diff --git a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java index 36cc31d..e5eabd0 100644 --- a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java +++ b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java @@ -2,15 +2,23 @@ package org.samo_lego.simpleauth; import net.fabricmc.api.DedicatedServerModInitializer; import net.fabricmc.fabric.api.registry.CommandRegistry; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.samo_lego.simpleauth.commands.LoginCommand; +import org.samo_lego.simpleauth.commands.RegisterCommand; public class SimpleAuth implements DedicatedServerModInitializer { + private static final Logger LOGGER = LogManager.getLogger(); + @Override public void onInitializeServer() { - // This code runs as soon as Minecraft is in a mod-load-ready state. - - System.out.println("SimpleAuth mod by samo_lego."); // Info I guess :D + // Info I guess :D + LOGGER.info("SimpleAuth mod by samo_lego."); // Registering the commands - CommandRegistry.INSTANCE.register(true, AuthCommands::registerCommands); + CommandRegistry.INSTANCE.register(false, dispatcher -> { + RegisterCommand.register(dispatcher); + LoginCommand.register(dispatcher); + }); } } diff --git a/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java b/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java new file mode 100644 index 0000000..ccbc74d --- /dev/null +++ b/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java @@ -0,0 +1,37 @@ +package org.samo_lego.simpleauth.commands; + +import com.mojang.brigadier.CommandDispatcher; +import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.text.LiteralText; +import net.minecraft.text.Text; +import org.mindrot.jbcrypt.BCrypt; + +import static com.mojang.brigadier.arguments.StringArgumentType.getString; +import static com.mojang.brigadier.arguments.StringArgumentType.word; +import static net.minecraft.server.command.CommandManager.argument; +import static net.minecraft.server.command.CommandManager.literal; + +public class LoginCommand { + public static void register(CommandDispatcher dispatcher) { + // Registering the "/login" command + dispatcher.register(literal("login") + .then(argument("password", word()) + .executes(ctx -> login(ctx.getSource(), getString(ctx, "password")) // Tries to authenticate user + )) + .executes(ctx -> { + System.out.println("You need to enter your password!"); + return 1; + })); + } + // Method called for checking the password + private static int login(ServerCommandSource source, String pass) { + String savedHashed = "judf"; // Hashed password provided upon registration + + // Comparing hashed password with one from the file + if(BCrypt.checkpw(pass, savedHashed)){ //From database + Text text = new LiteralText(source.getName() + ", you have entered login command"); + source.getMinecraftServer().getPlayerManager().broadcastChatMessage(text, false); + } + return 1; // Success + } +} diff --git a/src/main/java/org/samo_lego/simpleauth/commands/RegisterCommand.java b/src/main/java/org/samo_lego/simpleauth/commands/RegisterCommand.java new file mode 100644 index 0000000..3a88eb4 --- /dev/null +++ b/src/main/java/org/samo_lego/simpleauth/commands/RegisterCommand.java @@ -0,0 +1,49 @@ +package org.samo_lego.simpleauth.commands; + +import com.google.common.io.Files; +import com.mojang.brigadier.CommandDispatcher; +import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.text.LiteralText; +import org.mindrot.jbcrypt.BCrypt; + +import static com.mojang.brigadier.arguments.StringArgumentType.getString; +import static com.mojang.brigadier.arguments.StringArgumentType.word; +import static net.minecraft.server.command.CommandManager.argument; +import static net.minecraft.server.command.CommandManager.literal; + + +public class RegisterCommand { + + public static void register(CommandDispatcher dispatcher) { + + // Registering the "/register" command + dispatcher.register(literal("register") + .then(argument("password", word()) + .then(argument("passwordAgain", word()) + .executes( ctx -> register(ctx.getSource(), getString(ctx, "password"), getString(ctx, "passwordAgain"))) + )) + .executes(ctx -> { + System.out.println("You need to enter your password twice!"); + return 1; + })); + } + + // Registering our "register" command + private static int register(ServerCommandSource source, String pass1, String pass2) { + if(pass1.equals(pass2)){ + // Hashing the password with help of jBCrypt library + String hashed = BCrypt.hashpw(pass1, BCrypt.gensalt()); + + source.getMinecraftServer().getPlayerManager().broadcastChatMessage( + new LiteralText(source.getName() + ", you have registered successfully!"), + false + ); + } + else + source.getMinecraftServer().getPlayerManager().broadcastChatMessage( + new LiteralText(source.getName() + ", passwords must match!"), + false + ); + return 1; + } +} diff --git a/src/main/resources/lang/en_us.json b/src/main/resources/lang/en_us.json new file mode 100644 index 0000000..3acd795 --- /dev/null +++ b/src/main/resources/lang/en_us.json @@ -0,0 +1,4 @@ +{ + "text.simpleauth.passwordmatch": "Passwords must match!", + "text.simpleauth.wrongpassword": "Wrong password!" +} \ No newline at end of file