From eb2084ce96696a64f87196aa0ec9673e397f7cb4 Mon Sep 17 00:00:00 2001 From: samo_lego <34912839+samolego@users.noreply.github.com> Date: Tue, 14 Jan 2020 22:18:06 +0100 Subject: [PATCH] Fixing msgs, adding comments Changed HashSet of authenticated users to deauthenticated ones to speed up the process. Removed the player leave mixin callback since it's not needed anymore. --- .../org/samo_lego/simpleauth/SimpleAuth.java | 10 ++++---- .../simpleauth/commands/AuthCommand.java | 4 ++-- .../simpleauth/commands/LoginCommand.java | 2 +- .../simpleauth/commands/RegisterCommand.java | 2 +- .../simpleauth/event/AuthEventHandler.java | 24 +++++++++---------- .../player/PlayerLeaveServerCallback.java | 15 ------------ .../simpleauth/mixin/MixinPlayerManager.java | 6 ----- 7 files changed, 22 insertions(+), 41 deletions(-) delete mode 100644 src/main/java/org/samo_lego/simpleauth/event/entity/player/PlayerLeaveServerCallback.java diff --git a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java index 4043f1e..0fb396a 100644 --- a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java +++ b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java @@ -14,7 +14,6 @@ import org.samo_lego.simpleauth.event.AuthEventHandler; import org.samo_lego.simpleauth.event.entity.player.ChatCallback; import org.samo_lego.simpleauth.event.entity.player.PlayerMoveCallback; import org.samo_lego.simpleauth.event.entity.player.PlayerJoinServerCallback; -import org.samo_lego.simpleauth.event.entity.player.PlayerLeaveServerCallback; import org.samo_lego.simpleauth.event.item.DropItemCallback; import org.samo_lego.simpleauth.utils.AuthConfig; @@ -24,8 +23,12 @@ import java.util.HashSet; public class SimpleAuth implements DedicatedServerModInitializer { private static final Logger LOGGER = LogManager.getLogger(); public static SimpleAuthDatabase db = new SimpleAuthDatabase(); - public static HashSet authenticatedUsers = new HashSet<>(); - public static boolean isAuthenticated(ServerPlayerEntity player) { return authenticatedUsers.contains(player); } + // HashSet of players that are not authenticated + // Rather than storing all the authenticated players, we just store ones that are not authenticated + public static HashSet deauthenticatedUsers = new HashSet<>(); + // Boolean for easier checking if player is authenticated + public static boolean isAuthenticated(ServerPlayerEntity player) { return !deauthenticatedUsers.contains(player); } + // Mod config public static AuthConfig config; @@ -58,7 +61,6 @@ public class SimpleAuth implements DedicatedServerModInitializer { // Registering the events PlayerJoinServerCallback.EVENT.register(AuthEventHandler::onPlayerJoin); - PlayerLeaveServerCallback.EVENT.register(AuthEventHandler::onPlayerLeave); DropItemCallback.EVENT.register(AuthEventHandler::onDropItem); ChatCallback.EVENT.register(AuthEventHandler::onPlayerChat); PlayerMoveCallback.EVENT.register(AuthEventHandler::onPlayerMove); diff --git a/src/main/java/org/samo_lego/simpleauth/commands/AuthCommand.java b/src/main/java/org/samo_lego/simpleauth/commands/AuthCommand.java index 325251c..6bb8bd5 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/AuthCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/AuthCommand.java @@ -94,7 +94,7 @@ public class AuthCommand { if(sender != null) sender.sendMessage(userdataUpdated); else - LOGGER.info(userdataUpdated); + LOGGER.info(SimpleAuth.config.lang.userdataUpdated); // TODO -> Kick player whose name was changed? return 1; } @@ -107,7 +107,7 @@ public class AuthCommand { if(sender != null) sender.sendMessage(userdataDeleted); else - LOGGER.info(userdataDeleted); + LOGGER.info(SimpleAuth.config.lang.userdataDeleted); return 1; // Success } private static int reloadConfig(ServerCommandSource source) { 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 0d58fca..206cd6d 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/LoginCommand.java @@ -42,7 +42,7 @@ public class LoginCommand { return 0; } else if (AuthHelper.checkPass(player.getUuidAsString(), pass.toCharArray())) { - SimpleAuth.authenticatedUsers.add(player); + SimpleAuth.deauthenticatedUsers.remove(player); // Player no longer needs to be invisible and invulnerable player.setInvulnerable(false); player.setInvisible(false); diff --git a/src/main/java/org/samo_lego/simpleauth/commands/RegisterCommand.java b/src/main/java/org/samo_lego/simpleauth/commands/RegisterCommand.java index dcae593..4f1ea64 100644 --- a/src/main/java/org/samo_lego/simpleauth/commands/RegisterCommand.java +++ b/src/main/java/org/samo_lego/simpleauth/commands/RegisterCommand.java @@ -46,7 +46,7 @@ public class RegisterCommand { else if(pass1.equals(pass2)) { String hash = AuthHelper.hashPass(pass1.toCharArray()); if (SimpleAuth.db.registerUser(player.getUuidAsString(), source.getName(), hash)) { - SimpleAuth.authenticatedUsers.add(player); + SimpleAuth.deauthenticatedUsers.remove(player); // Player no longer needs to be invisible and invulnerable player.setInvulnerable(false); player.setInvisible(false); diff --git a/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java b/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java index ab67b1a..354d97c 100644 --- a/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java +++ b/src/main/java/org/samo_lego/simpleauth/event/AuthEventHandler.java @@ -23,7 +23,9 @@ public class AuthEventHandler { // Player joining the server public static void onPlayerJoin(ServerPlayerEntity player) { + SimpleAuth.deauthenticatedUsers.add(player); // 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 @@ -39,15 +41,13 @@ public class AuthEventHandler { }, delay * 1000); } } - // Player leaving the server - public static void onPlayerLeave(ServerPlayerEntity player) { - SimpleAuth.authenticatedUsers.remove(player); - } + // Player chatting public static ActionResult onPlayerChat(PlayerEntity player, ChatMessageC2SPacket chatMessageC2SPacket) { + // Getting the message to then be able to check it String msg = chatMessageC2SPacket.getChatMessage(); if( - !SimpleAuth.authenticatedUsers.contains(player) && + !SimpleAuth.isAuthenticated((ServerPlayerEntity) player) && !msg.startsWith("/login") && !msg.startsWith("/register") && (!SimpleAuth.config.main.allowChat || msg.startsWith("/")) @@ -59,7 +59,7 @@ public class AuthEventHandler { } // Player movement public static ActionResult onPlayerMove(PlayerEntity player) { - if(!SimpleAuth.authenticatedUsers.contains(player) && !SimpleAuth.config.main.allowMovement) { + if(!SimpleAuth.isAuthenticated((ServerPlayerEntity) player) && !SimpleAuth.config.main.allowMovement) { return ActionResult.FAIL; } return ActionResult.PASS; @@ -67,7 +67,7 @@ public class AuthEventHandler { // Using a block (right-click function) public static ActionResult onUseBlock(PlayerEntity player) { - if(!SimpleAuth.authenticatedUsers.contains(player) && !SimpleAuth.config.main.allowBlockUse) { + if(!SimpleAuth.isAuthenticated((ServerPlayerEntity) player) && !SimpleAuth.config.main.allowBlockUse) { player.sendMessage(notAuthenticated); return ActionResult.FAIL; } @@ -76,7 +76,7 @@ public class AuthEventHandler { // Punching a block public static ActionResult onAttackBlock(PlayerEntity player) { - if(!SimpleAuth.authenticatedUsers.contains(player) && !SimpleAuth.config.main.allowBlockPunch) { + if(!SimpleAuth.isAuthenticated((ServerPlayerEntity) player) && !SimpleAuth.config.main.allowBlockPunch) { player.sendMessage(notAuthenticated); return ActionResult.FAIL; } @@ -85,7 +85,7 @@ public class AuthEventHandler { // Using an item public static TypedActionResult onUseItem(PlayerEntity player) { - if(!SimpleAuth.authenticatedUsers.contains(player) && !SimpleAuth.config.main.allowItemUse) { + if(!SimpleAuth.isAuthenticated((ServerPlayerEntity) player) && !SimpleAuth.config.main.allowItemUse) { player.sendMessage(notAuthenticated); return TypedActionResult.fail(ItemStack.EMPTY); } @@ -94,7 +94,7 @@ public class AuthEventHandler { } // Dropping an item public static ActionResult onDropItem(PlayerEntity player) { - if(!SimpleAuth.authenticatedUsers.contains(player) && !SimpleAuth.config.main.allowItemDrop) { + if(!SimpleAuth.isAuthenticated((ServerPlayerEntity) player) && !SimpleAuth.config.main.allowItemDrop) { player.sendMessage(notAuthenticated); return ActionResult.FAIL; } @@ -102,7 +102,7 @@ public class AuthEventHandler { } // Attacking an entity public static ActionResult onAttackEntity(PlayerEntity player) { - if(!SimpleAuth.authenticatedUsers.contains(player) && !SimpleAuth.config.main.allowEntityPunch) { + if(!SimpleAuth.isAuthenticated((ServerPlayerEntity) player) && !SimpleAuth.config.main.allowEntityPunch) { player.sendMessage(notAuthenticated); return ActionResult.FAIL; } @@ -111,7 +111,7 @@ public class AuthEventHandler { } // Interacting with entity public static ActionResult onUseEntity(PlayerEntity player) { - if(!SimpleAuth.authenticatedUsers.contains(player) && !SimpleAuth.config.main.allowEntityInteract) { + if(!SimpleAuth.isAuthenticated((ServerPlayerEntity) player) && !SimpleAuth.config.main.allowEntityInteract) { player.sendMessage(notAuthenticated); return ActionResult.FAIL; } diff --git a/src/main/java/org/samo_lego/simpleauth/event/entity/player/PlayerLeaveServerCallback.java b/src/main/java/org/samo_lego/simpleauth/event/entity/player/PlayerLeaveServerCallback.java deleted file mode 100644 index 6d99cf5..0000000 --- a/src/main/java/org/samo_lego/simpleauth/event/entity/player/PlayerLeaveServerCallback.java +++ /dev/null @@ -1,15 +0,0 @@ -package org.samo_lego.simpleauth.event.entity.player; - -import net.fabricmc.fabric.api.event.Event; -import net.fabricmc.fabric.api.event.EventFactory; -import net.minecraft.server.network.ServerPlayerEntity; - -public interface PlayerLeaveServerCallback { - - Event EVENT = EventFactory.createArrayBacked(PlayerLeaveServerCallback.class, listeners -> (player) -> { - for (PlayerLeaveServerCallback callback : listeners) { - callback.onPlayerLeave(player); - } - }); - void onPlayerLeave(ServerPlayerEntity player); -} diff --git a/src/main/java/org/samo_lego/simpleauth/mixin/MixinPlayerManager.java b/src/main/java/org/samo_lego/simpleauth/mixin/MixinPlayerManager.java index 88035a9..9f336ad 100644 --- a/src/main/java/org/samo_lego/simpleauth/mixin/MixinPlayerManager.java +++ b/src/main/java/org/samo_lego/simpleauth/mixin/MixinPlayerManager.java @@ -4,7 +4,6 @@ import net.minecraft.network.ClientConnection; import net.minecraft.server.PlayerManager; import net.minecraft.server.network.ServerPlayerEntity; import org.samo_lego.simpleauth.event.entity.player.PlayerJoinServerCallback; -import org.samo_lego.simpleauth.event.entity.player.PlayerLeaveServerCallback; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; @@ -17,9 +16,4 @@ public abstract class MixinPlayerManager { private void onPlayerConnect(ClientConnection clientConnection_1, ServerPlayerEntity serverPlayerEntity_1, CallbackInfo ci) { PlayerJoinServerCallback.EVENT.invoker().onPlayerJoin(serverPlayerEntity_1); } - @Inject(method = "remove", at = @At("RETURN")) - private void remove(ServerPlayerEntity serverPlayerEntity_1, CallbackInfo ci) { - PlayerLeaveServerCallback.EVENT.invoker().onPlayerLeave(serverPlayerEntity_1); - } - }