Unifying some cmds to /account cmd
This commit is contained in:
parent
7edc9c6f64
commit
774042af9c
|
@ -89,9 +89,8 @@ public class SimpleAuth implements DedicatedServerModInitializer {
|
|||
RegisterCommand.registerCommand(dispatcher);
|
||||
LoginCommand.registerCommand(dispatcher);
|
||||
LogoutCommand.registerCommand(dispatcher);
|
||||
ChangepwCommand.registerCommand(dispatcher);
|
||||
UnregisterCommand.registerCommand(dispatcher);
|
||||
AuthCommand.registerCommand(dispatcher);
|
||||
AccountCommand.registerCommand(dispatcher);
|
||||
});
|
||||
|
||||
// Registering the events
|
||||
|
|
|
@ -0,0 +1,133 @@
|
|||
package org.samo_lego.simpleauth.commands;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.mojang.brigadier.CommandDispatcher;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import net.minecraft.server.command.ServerCommandSource;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
import net.minecraft.text.LiteralText;
|
||||
import org.samo_lego.simpleauth.SimpleAuth;
|
||||
import org.samo_lego.simpleauth.utils.AuthHelper;
|
||||
|
||||
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;
|
||||
import static org.samo_lego.simpleauth.SimpleAuth.config;
|
||||
import static org.samo_lego.simpleauth.utils.UuidConverter.convertUuid;
|
||||
|
||||
public class AccountCommand {
|
||||
|
||||
public static void registerCommand(CommandDispatcher<ServerCommandSource> dispatcher) {
|
||||
// Registering the "/account" command
|
||||
dispatcher.register(literal("account")
|
||||
.then(literal("unregister")
|
||||
.executes(ctx -> {
|
||||
ctx.getSource().getPlayer().sendMessage(
|
||||
new LiteralText(config.lang.enterPassword),
|
||||
false
|
||||
);
|
||||
return 1;
|
||||
})
|
||||
.then(argument("password", word())
|
||||
.executes( ctx -> unregister(
|
||||
ctx.getSource(),
|
||||
getString(ctx, "password")
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
.then(literal("changePassword")
|
||||
.then(argument("old password", word())
|
||||
.executes(ctx -> {
|
||||
ctx.getSource().getPlayer().sendMessage(
|
||||
new LiteralText(config.lang.enterNewPassword),
|
||||
false);
|
||||
return 1;
|
||||
})
|
||||
.then(argument("new password", word())
|
||||
.executes( ctx -> changePassword(
|
||||
ctx.getSource(),
|
||||
getString(ctx, "old password"),
|
||||
getString(ctx, "new password")
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// Method called for checking the password and then removing user's account from db
|
||||
private static int unregister(ServerCommandSource source, String pass) throws CommandSyntaxException {
|
||||
// Getting the player who send the command
|
||||
ServerPlayerEntity player = source.getPlayer();
|
||||
|
||||
if (config.main.enableGlobalPassword) {
|
||||
player.sendMessage(
|
||||
new LiteralText(config.lang.cannotUnregister),
|
||||
false
|
||||
);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (AuthHelper.checkPass(convertUuid(player), pass.toCharArray()) == 1) {
|
||||
SimpleAuth.db.deleteUserData(convertUuid(player));
|
||||
player.sendMessage(
|
||||
new LiteralText(config.lang.accountDeleted),
|
||||
false
|
||||
);
|
||||
SimpleAuth.deauthenticatePlayer(player);
|
||||
return 1;
|
||||
}
|
||||
player.sendMessage(
|
||||
new LiteralText(config.lang.wrongPassword),
|
||||
false
|
||||
);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Method called for checking the password and then changing it
|
||||
private static int changePassword(ServerCommandSource source, String oldPass, String newPass) throws CommandSyntaxException {
|
||||
// Getting the player who send the command
|
||||
ServerPlayerEntity player = source.getPlayer();
|
||||
|
||||
if (config.main.enableGlobalPassword) {
|
||||
player.sendMessage(
|
||||
new LiteralText(config.lang.cannotChangePassword),
|
||||
false
|
||||
);
|
||||
return 0;
|
||||
}
|
||||
else if (AuthHelper.checkPass(convertUuid(player), oldPass.toCharArray()) == 1) {
|
||||
if(newPass.length() < config.main.minPasswordChars) {
|
||||
player.sendMessage(new LiteralText(
|
||||
String.format(config.lang.minPasswordChars, config.main.minPasswordChars)
|
||||
), false);
|
||||
return 0;
|
||||
}
|
||||
else if(newPass.length() > config.main.maxPasswordChars && config.main.maxPasswordChars != -1) {
|
||||
player.sendMessage(new LiteralText(
|
||||
String.format(config.lang.maxPasswordChars, config.main.maxPasswordChars)
|
||||
), false);
|
||||
return 0;
|
||||
}
|
||||
// JSON object holding password (may hold some other info in the future)
|
||||
JsonObject playerdata = new JsonObject();
|
||||
String hash = AuthHelper.hashPass(newPass.toCharArray());
|
||||
playerdata.addProperty("password", hash);
|
||||
|
||||
SimpleAuth.db.updateUserData(convertUuid(player), playerdata.toString());
|
||||
player.sendMessage(
|
||||
new LiteralText(config.lang.passwordUpdated),
|
||||
false
|
||||
);
|
||||
return 1;
|
||||
}
|
||||
player.sendMessage(
|
||||
new LiteralText(config.lang.wrongPassword),
|
||||
false
|
||||
);
|
||||
return 0;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue