Added logout command.

This commit is contained in:
samo_lego 2020-03-28 20:35:16 +01:00
parent 091be6b588
commit aba4100297
6 changed files with 66 additions and 24 deletions

View File

@ -10,6 +10,6 @@ loader_version=0.7.8+build.189
fabric_version=0.5.6+build.313-1.16 fabric_version=0.5.6+build.313-1.16
# Mod Properties # Mod Properties
mod_version = 1.3.0 mod_version = 1.3.1
maven_group = org.samo_lego maven_group = org.samo_lego
archives_base_name = simpleauth archives_base_name = simpleauth

View File

@ -9,6 +9,7 @@ import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory; import net.minecraft.inventory.Inventory;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -25,6 +26,8 @@ import org.samo_lego.simpleauth.utils.AuthConfig;
import java.io.File; import java.io.File;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Timer;
import java.util.TimerTask;
public class SimpleAuth implements DedicatedServerModInitializer { public class SimpleAuth implements DedicatedServerModInitializer {
private static final Logger LOGGER = LogManager.getLogger(); private static final Logger LOGGER = LogManager.getLogger();
@ -66,6 +69,7 @@ public class SimpleAuth implements DedicatedServerModInitializer {
CommandRegistry.INSTANCE.register(false, dispatcher -> { CommandRegistry.INSTANCE.register(false, dispatcher -> {
RegisterCommand.registerCommand(dispatcher); RegisterCommand.registerCommand(dispatcher);
LoginCommand.registerCommand(dispatcher); LoginCommand.registerCommand(dispatcher);
LogoutCommand.registerCommand(dispatcher);
ChangepwCommand.registerCommand(dispatcher); ChangepwCommand.registerCommand(dispatcher);
UnregisterCommand.registerCommand(dispatcher); UnregisterCommand.registerCommand(dispatcher);
AuthCommand.registerCommand(dispatcher); AuthCommand.registerCommand(dispatcher);
@ -96,4 +100,34 @@ public class SimpleAuth implements DedicatedServerModInitializer {
player.setInvisible(false); player.setInvisible(false);
player.sendMessage(msg); player.sendMessage(msg);
} }
// Getting some config options
private static Text notAuthenticated() {
if(SimpleAuth.config.main.enableGlobalPassword) {
return new LiteralText(SimpleAuth.config.lang.loginRequired);
}
return new LiteralText(SimpleAuth.config.lang.notAuthenticated);
}
private static Text timeExpired = new LiteralText(SimpleAuth.config.lang.timeExpired);
private static int delay = SimpleAuth.config.main.delay;
public static void deauthenticatePlayer(ServerPlayerEntity player) {
// Marking player as not authenticated, (re)setting login tries to zero
SimpleAuth.deauthenticatedUsers.put(player, 0);
// Player is now not authenticated
player.sendMessage(notAuthenticated());
// Setting the player to be invisible to mobs and also invulnerable
player.setInvulnerable(SimpleAuth.config.main.playerInvulnerable);
player.setInvisible(SimpleAuth.config.main.playerInvisible);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if(!SimpleAuth.isAuthenticated(player)) // Kicking player if not authenticated
player.networkHandler.disconnect(timeExpired);
}
}, delay * 1000);
}
} }

View File

@ -20,7 +20,7 @@ public class LoginCommand {
private static Text alreadyAuthenticated = new LiteralText(SimpleAuth.config.lang.alreadyAuthenticated); private static Text alreadyAuthenticated = new LiteralText(SimpleAuth.config.lang.alreadyAuthenticated);
private static Text loginTriesExceeded = new LiteralText(SimpleAuth.config.lang.loginTriesExceeded); private static Text loginTriesExceeded = new LiteralText(SimpleAuth.config.lang.loginTriesExceeded);
private static Text successfullyAuthenticated = new LiteralText(SimpleAuth.config.lang.successfullyAuthenticated); private static Text successfullyAuthenticated = new LiteralText(SimpleAuth.config.lang.successfullyAuthenticated);
private static int maxLoginTries = 3; private static int maxLoginTries = SimpleAuth.config.main.maxLoginTries;
public static void registerCommand(CommandDispatcher<ServerCommandSource> dispatcher) { public static void registerCommand(CommandDispatcher<ServerCommandSource> dispatcher) {
// Registering the "/login" command // Registering the "/login" command

View File

@ -0,0 +1,28 @@
package org.samo_lego.simpleauth.commands;
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 net.minecraft.text.Text;
import org.samo_lego.simpleauth.SimpleAuth;
import static net.minecraft.server.command.CommandManager.literal;
public class LogoutCommand {
private static Text successfulLogout = new LiteralText(SimpleAuth.config.lang.successfulLogout);
public static void registerCommand(CommandDispatcher<ServerCommandSource> dispatcher) {
// Registering the "/login" command
dispatcher.register(literal("login")
.executes(ctx -> logout(ctx.getSource())) // Tries to authenticate user
);
}
private static int logout(ServerCommandSource serverCommandSource) throws CommandSyntaxException {
ServerPlayerEntity player = serverCommandSource.getPlayer();
SimpleAuth.deauthenticatePlayer(player);
return 1;
}
}

View File

@ -24,30 +24,10 @@ public class AuthEventHandler {
} }
return new LiteralText(SimpleAuth.config.lang.notAuthenticated); return new LiteralText(SimpleAuth.config.lang.notAuthenticated);
} }
private static Text timeExpired = new LiteralText(SimpleAuth.config.lang.timeExpired);
private static int delay = SimpleAuth.config.main.delay;
// Player joining the server // Player joining the server
public static void onPlayerJoin(ServerPlayerEntity player) { public static void onPlayerJoin(ServerPlayerEntity player) {
// Marking player as not authenticated, (re)setting login tries to zero SimpleAuth.deauthenticatePlayer(player);
SimpleAuth.deauthenticatedUsers.put(player, 0);
// Player not authenticated
// If clause actually not needed, since we add player to deauthenticated hashset above
if (!SimpleAuth.isAuthenticated(player)) {
player.sendMessage(notAuthenticated());
// Setting the player to be invisible to mobs and also invulnerable
player.setInvulnerable(SimpleAuth.config.main.playerInvulnerable);
player.setInvisible(SimpleAuth.config.main.playerInvisible);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
if(!SimpleAuth.isAuthenticated(player)) // Kicking player if not authenticated
player.networkHandler.disconnect(timeExpired);
}
}, delay * 1000);
}
} }
// Player chatting // Player chatting

View File

@ -86,7 +86,7 @@ public class AuthConfig {
public String userdataUpdated = "§aUserdata updated."; public String userdataUpdated = "§aUserdata updated.";
public String accountDeleted = "§4Your account was successfully deleted!"; public String accountDeleted = "§4Your account was successfully deleted!";
public String configurationReloaded = "§aConfiguration file was reloaded successfully."; public String configurationReloaded = "§aConfiguration file was reloaded successfully.";
public String successfulLogout;
} }
private static final Logger LOGGER = LogManager.getLogger(); private static final Logger LOGGER = LogManager.getLogger();
private static final Gson gson = new GsonBuilder() private static final Gson gson = new GsonBuilder()