Some updates to commands

This commit is contained in:
samo_lego 2019-10-27 11:19:13 +01:00
parent 9adff8c50d
commit 91ddfa1fe8
6 changed files with 104 additions and 67 deletions

View File

@ -21,7 +21,7 @@ dependencies {
// Fabric API. This is technically optional, but you probably want it anyway. // Fabric API. This is technically optional, but you probably want it anyway.
modCompile "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" 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. // PSA: Some older mods, compiled on Loom 0.2.1, might have outdated Maven POMs.
// You may need to force-disable transitiveness on them. // You may need to force-disable transitiveness on them.
@ -77,5 +77,6 @@ publishing {
repositories { repositories {
// uncomment to publish to the local maven // uncomment to publish to the local maven
// mavenLocal() // mavenLocal()
mavenCentral()
} }
} }

View File

@ -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<ServerCommandSource> 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
}
}

View File

@ -2,15 +2,23 @@ package org.samo_lego.simpleauth;
import net.fabricmc.api.DedicatedServerModInitializer; import net.fabricmc.api.DedicatedServerModInitializer;
import net.fabricmc.fabric.api.registry.CommandRegistry; 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 { public class SimpleAuth implements DedicatedServerModInitializer {
private static final Logger LOGGER = LogManager.getLogger();
@Override @Override
public void onInitializeServer() { public void onInitializeServer() {
// This code runs as soon as Minecraft is in a mod-load-ready state. // Info I guess :D
LOGGER.info("SimpleAuth mod by samo_lego.");
System.out.println("SimpleAuth mod by samo_lego."); // Info I guess :D
// Registering the commands // Registering the commands
CommandRegistry.INSTANCE.register(true, AuthCommands::registerCommands); CommandRegistry.INSTANCE.register(false, dispatcher -> {
RegisterCommand.register(dispatcher);
LoginCommand.register(dispatcher);
});
} }
} }

View File

@ -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<ServerCommandSource> 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
}
}

View File

@ -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<ServerCommandSource> 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;
}
}

View File

@ -0,0 +1,4 @@
{
"text.simpleauth.passwordmatch": "Passwords must match!",
"text.simpleauth.wrongpassword": "Wrong password!"
}