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.
This commit is contained in:
samo_lego 2020-01-14 22:18:06 +01:00
parent b01a3fe60e
commit eb2084ce96
7 changed files with 22 additions and 41 deletions

View File

@ -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.ChatCallback;
import org.samo_lego.simpleauth.event.entity.player.PlayerMoveCallback; 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.PlayerJoinServerCallback;
import org.samo_lego.simpleauth.event.entity.player.PlayerLeaveServerCallback;
import org.samo_lego.simpleauth.event.item.DropItemCallback; import org.samo_lego.simpleauth.event.item.DropItemCallback;
import org.samo_lego.simpleauth.utils.AuthConfig; import org.samo_lego.simpleauth.utils.AuthConfig;
@ -24,8 +23,12 @@ import java.util.HashSet;
public class SimpleAuth implements DedicatedServerModInitializer { public class SimpleAuth implements DedicatedServerModInitializer {
private static final Logger LOGGER = LogManager.getLogger(); private static final Logger LOGGER = LogManager.getLogger();
public static SimpleAuthDatabase db = new SimpleAuthDatabase(); public static SimpleAuthDatabase db = new SimpleAuthDatabase();
public static HashSet<PlayerEntity> authenticatedUsers = new HashSet<>(); // HashSet of players that are not authenticated
public static boolean isAuthenticated(ServerPlayerEntity player) { return authenticatedUsers.contains(player); } // Rather than storing all the authenticated players, we just store ones that are not authenticated
public static HashSet<PlayerEntity> 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; public static AuthConfig config;
@ -58,7 +61,6 @@ public class SimpleAuth implements DedicatedServerModInitializer {
// Registering the events // Registering the events
PlayerJoinServerCallback.EVENT.register(AuthEventHandler::onPlayerJoin); PlayerJoinServerCallback.EVENT.register(AuthEventHandler::onPlayerJoin);
PlayerLeaveServerCallback.EVENT.register(AuthEventHandler::onPlayerLeave);
DropItemCallback.EVENT.register(AuthEventHandler::onDropItem); DropItemCallback.EVENT.register(AuthEventHandler::onDropItem);
ChatCallback.EVENT.register(AuthEventHandler::onPlayerChat); ChatCallback.EVENT.register(AuthEventHandler::onPlayerChat);
PlayerMoveCallback.EVENT.register(AuthEventHandler::onPlayerMove); PlayerMoveCallback.EVENT.register(AuthEventHandler::onPlayerMove);

View File

@ -94,7 +94,7 @@ public class AuthCommand {
if(sender != null) if(sender != null)
sender.sendMessage(userdataUpdated); sender.sendMessage(userdataUpdated);
else else
LOGGER.info(userdataUpdated); LOGGER.info(SimpleAuth.config.lang.userdataUpdated);
// TODO -> Kick player whose name was changed? // TODO -> Kick player whose name was changed?
return 1; return 1;
} }
@ -107,7 +107,7 @@ public class AuthCommand {
if(sender != null) if(sender != null)
sender.sendMessage(userdataDeleted); sender.sendMessage(userdataDeleted);
else else
LOGGER.info(userdataDeleted); LOGGER.info(SimpleAuth.config.lang.userdataDeleted);
return 1; // Success return 1; // Success
} }
private static int reloadConfig(ServerCommandSource source) { private static int reloadConfig(ServerCommandSource source) {

View File

@ -42,7 +42,7 @@ public class LoginCommand {
return 0; return 0;
} }
else if (AuthHelper.checkPass(player.getUuidAsString(), pass.toCharArray())) { 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 no longer needs to be invisible and invulnerable
player.setInvulnerable(false); player.setInvulnerable(false);
player.setInvisible(false); player.setInvisible(false);

View File

@ -46,7 +46,7 @@ public class RegisterCommand {
else if(pass1.equals(pass2)) { else if(pass1.equals(pass2)) {
String hash = AuthHelper.hashPass(pass1.toCharArray()); String hash = AuthHelper.hashPass(pass1.toCharArray());
if (SimpleAuth.db.registerUser(player.getUuidAsString(), source.getName(), hash)) { 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 no longer needs to be invisible and invulnerable
player.setInvulnerable(false); player.setInvulnerable(false);
player.setInvisible(false); player.setInvisible(false);

View File

@ -23,7 +23,9 @@ public class AuthEventHandler {
// Player joining the server // Player joining the server
public static void onPlayerJoin(ServerPlayerEntity player) { public static void onPlayerJoin(ServerPlayerEntity player) {
SimpleAuth.deauthenticatedUsers.add(player);
// Player not authenticated // Player not authenticated
// If clause actually not needed, since we add player to deauthenticated hashset above
if (!SimpleAuth.isAuthenticated(player)) { if (!SimpleAuth.isAuthenticated(player)) {
player.sendMessage(notAuthenticated); player.sendMessage(notAuthenticated);
// Setting the player to be invisible to mobs and also invulnerable // Setting the player to be invisible to mobs and also invulnerable
@ -39,15 +41,13 @@ public class AuthEventHandler {
}, delay * 1000); }, 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) { public static ActionResult onPlayerChat(PlayerEntity player, ChatMessageC2SPacket chatMessageC2SPacket) {
// Getting the message to then be able to check it
String msg = chatMessageC2SPacket.getChatMessage(); String msg = chatMessageC2SPacket.getChatMessage();
if( if(
!SimpleAuth.authenticatedUsers.contains(player) && !SimpleAuth.isAuthenticated((ServerPlayerEntity) player) &&
!msg.startsWith("/login") && !msg.startsWith("/login") &&
!msg.startsWith("/register") && !msg.startsWith("/register") &&
(!SimpleAuth.config.main.allowChat || msg.startsWith("/")) (!SimpleAuth.config.main.allowChat || msg.startsWith("/"))
@ -59,7 +59,7 @@ public class AuthEventHandler {
} }
// Player movement // Player movement
public static ActionResult onPlayerMove(PlayerEntity player) { 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.FAIL;
} }
return ActionResult.PASS; return ActionResult.PASS;
@ -67,7 +67,7 @@ public class AuthEventHandler {
// Using a block (right-click function) // Using a block (right-click function)
public static ActionResult onUseBlock(PlayerEntity player) { 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); player.sendMessage(notAuthenticated);
return ActionResult.FAIL; return ActionResult.FAIL;
} }
@ -76,7 +76,7 @@ public class AuthEventHandler {
// Punching a block // Punching a block
public static ActionResult onAttackBlock(PlayerEntity player) { 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); player.sendMessage(notAuthenticated);
return ActionResult.FAIL; return ActionResult.FAIL;
} }
@ -85,7 +85,7 @@ public class AuthEventHandler {
// Using an item // Using an item
public static TypedActionResult<ItemStack> onUseItem(PlayerEntity player) { public static TypedActionResult<ItemStack> onUseItem(PlayerEntity player) {
if(!SimpleAuth.authenticatedUsers.contains(player) && !SimpleAuth.config.main.allowItemUse) { if(!SimpleAuth.isAuthenticated((ServerPlayerEntity) player) && !SimpleAuth.config.main.allowItemUse) {
player.sendMessage(notAuthenticated); player.sendMessage(notAuthenticated);
return TypedActionResult.fail(ItemStack.EMPTY); return TypedActionResult.fail(ItemStack.EMPTY);
} }
@ -94,7 +94,7 @@ public class AuthEventHandler {
} }
// Dropping an item // Dropping an item
public static ActionResult onDropItem(PlayerEntity player) { 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); player.sendMessage(notAuthenticated);
return ActionResult.FAIL; return ActionResult.FAIL;
} }
@ -102,7 +102,7 @@ public class AuthEventHandler {
} }
// Attacking an entity // Attacking an entity
public static ActionResult onAttackEntity(PlayerEntity player) { 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); player.sendMessage(notAuthenticated);
return ActionResult.FAIL; return ActionResult.FAIL;
} }
@ -111,7 +111,7 @@ public class AuthEventHandler {
} }
// Interacting with entity // Interacting with entity
public static ActionResult onUseEntity(PlayerEntity player) { 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); player.sendMessage(notAuthenticated);
return ActionResult.FAIL; return ActionResult.FAIL;
} }

View File

@ -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<PlayerLeaveServerCallback> EVENT = EventFactory.createArrayBacked(PlayerLeaveServerCallback.class, listeners -> (player) -> {
for (PlayerLeaveServerCallback callback : listeners) {
callback.onPlayerLeave(player);
}
});
void onPlayerLeave(ServerPlayerEntity player);
}

View File

@ -4,7 +4,6 @@ import net.minecraft.network.ClientConnection;
import net.minecraft.server.PlayerManager; import net.minecraft.server.PlayerManager;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
import org.samo_lego.simpleauth.event.entity.player.PlayerJoinServerCallback; 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.Mixin;
import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject; 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) { private void onPlayerConnect(ClientConnection clientConnection_1, ServerPlayerEntity serverPlayerEntity_1, CallbackInfo ci) {
PlayerJoinServerCallback.EVENT.invoker().onPlayerJoin(serverPlayerEntity_1); 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);
}
} }