diff --git a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java index fe1fdb2..79a846f 100644 --- a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java +++ b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java @@ -108,9 +108,6 @@ public class SimpleAuth implements DedicatedServerModInitializer { } 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 @@ -126,8 +123,8 @@ public class SimpleAuth implements DedicatedServerModInitializer { @Override public void run() { if(!SimpleAuth.isAuthenticated(player)) // Kicking player if not authenticated - player.networkHandler.disconnect(timeExpired); + player.networkHandler.disconnect(new LiteralText(SimpleAuth.config.lang.timeExpired)); } - }, delay * 1000); + }, SimpleAuth.config.main.delay * 1000); } } \ No newline at end of file diff --git a/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java b/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java index 58d9a93..d858207 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java @@ -37,17 +37,20 @@ public class LoginCommand { // Method called for checking the password private static int login(ServerCommandSource source, String pass) throws CommandSyntaxException { // Getting the player who send the command + System.out.println(maxLoginTries); ServerPlayerEntity player = source.getPlayer(); + if(SimpleAuth.isAuthenticated(player)) { player.sendMessage(alreadyAuthenticated); return 0; } else if(SimpleAuth.deauthenticatedUsers.get(player) >= maxLoginTries && maxLoginTries != -1) { + SimpleAuth.deauthenticatePlayer(player); player.networkHandler.disconnect(loginTriesExceeded); return 0; } else if(SimpleAuth.config.main.enableGlobalPassword) { - if (AuthHelper.checkPass("globalPass", pass.toCharArray())) { + if (AuthHelper.checkPass(null, pass.toCharArray())) { SimpleAuth.authenticatePlayer(player, successfullyAuthenticated); return 1; } @@ -58,10 +61,10 @@ public class LoginCommand { } // Kicking the player out else if(maxLoginTries == 1) { + SimpleAuth.deauthenticatePlayer(player); player.networkHandler.disconnect(wrongPassword); return 0; } - // Sending wrong pass message player.sendMessage(wrongPassword); // ++ the login tries @@ -69,7 +72,6 @@ public class LoginCommand { player, SimpleAuth.deauthenticatedUsers.getOrDefault(player, 0) + 1 ); - return 0; } } diff --git a/src/main/java/org/samo_lego/simpleauth/commands/LogoutCommand.java b/src/main/java/org/samo_lego/simpleauth/commands/LogoutCommand.java index cdf2bb8..20a1972 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/LogoutCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/LogoutCommand.java @@ -14,15 +14,16 @@ public class LogoutCommand { private static Text successfulLogout = new LiteralText(SimpleAuth.config.lang.successfulLogout); public static void registerCommand(CommandDispatcher dispatcher) { - // Registering the "/login" command - dispatcher.register(literal("login") - .executes(ctx -> logout(ctx.getSource())) // Tries to authenticate user + // Registering the "/logout" command + dispatcher.register(literal("logout") + .executes(ctx -> logout(ctx.getSource())) // Tries to deauthenticate user ); } private static int logout(ServerCommandSource serverCommandSource) throws CommandSyntaxException { ServerPlayerEntity player = serverCommandSource.getPlayer(); SimpleAuth.deauthenticatePlayer(player); + player.sendMessage(successfulLogout); return 1; } } diff --git a/src/main/java/org/samo_lego/simpleauth/mixin/MixinServerPlayNetworkHandler.java b/src/main/java/org/samo_lego/simpleauth/mixin/MixinServerPlayNetworkHandler.java index 5fca44f..5c6f72a 100644 --- a/src/main/java/org/samo_lego/simpleauth/mixin/MixinServerPlayNetworkHandler.java +++ b/src/main/java/org/samo_lego/simpleauth/mixin/MixinServerPlayNetworkHandler.java @@ -6,12 +6,15 @@ import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.util.ActionResult; import org.samo_lego.simpleauth.event.entity.player.ChatCallback; import org.samo_lego.simpleauth.event.entity.player.PlayerMoveCallback; +import org.samo_lego.simpleauth.event.item.TakeItemCallback; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import static net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket.Action.SWAP_HELD_ITEMS; + @Mixin(ServerPlayNetworkHandler.class) public abstract class MixinServerPlayNetworkHandler { @Shadow @@ -27,13 +30,30 @@ public abstract class MixinServerPlayNetworkHandler { ), cancellable = true ) - private void onChatMessage(ChatMessageC2SPacket chatMessageC2SPacket_1, CallbackInfo ci) { - ActionResult result = ChatCallback.EVENT.invoker().onPlayerChat(player, chatMessageC2SPacket_1); + private void onChatMessage(ChatMessageC2SPacket chatMessageC2SPacket, CallbackInfo ci) { + ActionResult result = ChatCallback.EVENT.invoker().onPlayerChat(player, chatMessageC2SPacket); if (result == ActionResult.FAIL) { ci.cancel(); } } - // onClickWindow, onPickFromInventory todo + + @Inject( + method = "onPlayerAction(Lnet/minecraft/network/packet/c2s/play/PlayerActionC2SPacket;)V", + at = @At( + value = "INVOKE", + target = "net/minecraft/network/NetworkThreadUtils.forceMainThread(Lnet/minecraft/network/Packet;Lnet/minecraft/network/listener/PacketListener;Lnet/minecraft/server/world/ServerWorld;)V", + shift = At.Shift.AFTER + ), + cancellable = true + ) + private void onPlayerAction(PlayerActionC2SPacket packet, CallbackInfo ci) { + if(packet.getAction() == SWAP_HELD_ITEMS) { + ActionResult result = TakeItemCallback.EVENT.invoker().onTakeItem(player); + if (result == ActionResult.FAIL) { + ci.cancel(); + } + } + } @Inject( method="onPlayerMove(Lnet/minecraft/network/packet/c2s/play/PlayerMoveC2SPacket;)V", at = @At( @@ -44,11 +64,11 @@ public abstract class MixinServerPlayNetworkHandler { ), cancellable = true ) - private void onPlayerMove(PlayerMoveC2SPacket playerMoveC2SPacket_1, CallbackInfo ci) { + private void onPlayerMove(PlayerMoveC2SPacket playerMoveC2SPacket, CallbackInfo ci) { ActionResult result = PlayerMoveCallback.EVENT.invoker().onPlayerMove(player); if (result == ActionResult.FAIL) { // A bit ugly, I know. (we need to update player position) - player.teleport(player.getX(), player.getY(), player.getZ()); + player.requestTeleport(player.getX(), player.getY(), player.getZ()); ci.cancel(); } } diff --git a/src/main/java/org/samo_lego/simpleauth/utils/AuthConfig.java b/src/main/java/org/samo_lego/simpleauth/utils/AuthConfig.java index 43b3041..7215929 100644 --- a/src/main/java/org/samo_lego/simpleauth/utils/AuthConfig.java +++ b/src/main/java/org/samo_lego/simpleauth/utils/AuthConfig.java @@ -52,7 +52,7 @@ public class AuthConfig { public boolean playerInvulnerable = true; // If player should be invisible to mobs before authentication public boolean playerInvisible = true; - // Maximum login tries before kicking the player + // Maximum login tries before kicking the player from server // Set to -1 to allow unlimited, not recommended however public int maxLoginTries = 1; // Time after which player will be kicked if not authenticated - in seconds @@ -77,8 +77,9 @@ public class AuthConfig { public String cannotChangePassword = "§aYou cannot change password!"; public String cannotUnregister = "§aYou cannot unregister this account!"; public String notAuthenticated = "§cYou are not authenticated!\n§6Try with /login or /register."; - public String alreadyAuthenticated = "§4You are already authenticated."; + public String alreadyAuthenticated = "§6You are already authenticated."; public String successfullyAuthenticated = "§aYou are now authenticated."; + public String successfulLogout = "§aLogged out successfully."; public String timeExpired = "§cTime for authentication has expired."; public String alreadyRegistered = "§6This account name is already registered!"; public String registerSuccess = "§aYou are now authenticated."; @@ -86,7 +87,6 @@ public class AuthConfig { public String userdataUpdated = "§aUserdata updated."; public String accountDeleted = "§4Your account was successfully deleted!"; public String configurationReloaded = "§aConfiguration file was reloaded successfully."; - public String successfulLogout; } private static final Logger LOGGER = LogManager.getLogger(); private static final Gson gson = new GsonBuilder() diff --git a/src/main/java/org/samo_lego/simpleauth/utils/AuthHelper.java b/src/main/java/org/samo_lego/simpleauth/utils/AuthHelper.java index 2ddf3c9..bd8dcc1 100644 --- a/src/main/java/org/samo_lego/simpleauth/utils/AuthHelper.java +++ b/src/main/java/org/samo_lego/simpleauth/utils/AuthHelper.java @@ -13,7 +13,7 @@ public class AuthHelper { private static Argon2 argon2 = Argon2Factory.create(); public static boolean checkPass(String uuid, char[] pass) { - if(uuid.equals("globalPass") && SimpleAuth.config.main.enableGlobalPassword) { + if(SimpleAuth.config.main.enableGlobalPassword) { // We have global password enabled try { return argon2.verify(SimpleAuth.config.main.globalPassword, pass);