Untitled

mail@pastecode.io avatar
unknown
java
a month ago
2.5 kB
2
Indexable
Never
@Getter
@RequiredArgsConstructor
public class SessionListener implements Listener
{

    private final Sylph plugin;

    @EventHandler
    public void on(final PlayerChatEvent event)
    {
        final var session = this.getPlugin()
            .getSessions()
            .get(event.getPlayer().getName());

        final var password = Password.hash(event.getMessage())
            .withArgon2()
            .getResult();

        if (
            Objects.isNull(session.getPassword())
        )
        {
            if (!(
                this.getPlugin()
                    .getConfirmations()
                    .contains(event.getPlayer().getName())
            ))
            {
                event.setCancelled();

                this.getPlugin()
                    .getConfirmations()
                    .add(event.getPlayer().getName());

                session.setPassword(password);

                event.getPlayer().sendMessage(
                    "Please confirm your password by typing it again."
                );

                return;
            }

            if (
                this.getPlugin()
                    .getConfirmations()
                    .contains(event.getPlayer().getName())
            )
            {
                event.setCancelled();

                if (
                    Password.check(event.getMessage().trim(), session.getPassword())
                        .withArgon2()
                )
                {
                    this.getPlugin()
                        .getConfirmations()
                        .remove(event.getPlayer().getName());

                    event.getPlayer().sendMessage(
                        "Password confirmed."
                    );

                    return;
                }

                event.getPlayer().sendMessage(
                    "Passwords do not match. Please try again."
                );

                return;
            }
        }

        if (
            Password.check(event.getMessage().trim(), session.getPassword())
                .withArgon2()
        )
        {
            event.setCancelled();

            event.getPlayer().sendMessage(
                "Password confirmed."
            );

            return;
        }

        event.setCancelled();

        event.getPlayer().sendMessage(
            "Incorrect password. Please try again."
        );
    }

}
Leave a Comment