#Simple Login Protection Grace Period

23 messages · Page 1 of 1 (latest)

blazing meteor
#

Made my own flavor of login protection for my modpack, because logging in to a death screen is hella frustrating especially when it's from a network error or other random disconnect mid-adventuring, but it was also made to hopefully be as minimally exploitable as possible.

function loginProtecc(event) {

    console.info(`Running loginProtecc for Player ${event.player.name.contents}`);

    let playerInput = false;

    let startingYaw = event.player.yaw;
    let startingPos = getPos(event);

    let playerMoved = false;
    let cameraMoved = false;

    let delayFlag = true;

    event.player.setInvulnerable(true);

    function checkForInput(event, previousCamera) {
        event.server.scheduleInTicks(8, callback => {
            //Utils.server.tell('playerInput = ' + playerInput);

            cameraMoved = startingYaw != event.player.yaw;
            playerMoved = !startingPos.every((val, index) => val === getPos(event)[index]);

            if (playerMoved) {

                // When player moves, immediately ends grace period.
                playerInput = true;
            }

            if (cameraMoved && delayFlag) {
                
                // When player moves camera, starts a timer that lets them get their bearings and look around a little
                // before their grace period is ended.
                delayFlag = false;
                event.server.scheduleInTicks(48, callback2 => {
                    playerInput = true;
                });
            }

            if (playerInput) {
                event.player.setInvulnerable(false);
                console.info(`Detected input from Player ${event.player.name.contents}. Grace period ended.`);
                return;
            } else {
                checkForInput(event);
            }
        });
    }

    return checkForInput(event);
    
}

function getPos(event) {
    let pos = [];

    pos.push(event.player.x);
    pos.push(event.player.y);
    pos.push(event.player.z);

    return pos;    
}


/* For testing
onEvent('item.entity_interact', event => {

    if (event.target.type == "minecraft:fox" && event.item.id == "minecraft:diamond") {

        loginProtecc(event);
        
    }    
}) */

onEvent('player.logged_in', event => {

    loginProtecc(event);
    
})
lament perchBOT
#

Paste version of login_protection.js from @blazing meteor

minor pelican
#

how about this?

let startingPos = getPos(event);
``````js
const { player, player: { pos } } = event;
...
const startingPos = [pos.x, pos.y ,pos.z];
blazing meteor
#

Honestly after reading about JS destructuring I'm still not sure what's going on there

#

Is that trying to read a pos property from the event.player object?

drifting acorn
#

or even better just ```js
const { player } = event;
const startingpos = [player.x, player.y, player.z];

blazing meteor
#

The player object does not have a pos property tho

#

Not on 1.18.2 at least

drifting acorn
#

oh weird

blazing meteor
#

yeah you have to manually get them individually

drifting acorn
#

well you can just do that ^ instead of making a method for it

blazing meteor
#

there we go

#

i mean i do fetch it twice, once the first time than once every check

drifting acorn
#

also instead of having three extra boolean variables you can put the checks inside the if statement

#
if (startingYaw != event.player.yaw || !startingPos.every((val, index) => val === getPos(event)[index])) {...}
blazing meteor
#

True, could do that, tho if there is no performance impact i do prefer readability

lament perchBOT
#

Paste version of login_protectionv2.js from @blazing meteor

blazing meteor
#

How do i upload a new file for the first post?

#

I made a small tweak. I made it so if the player ONLY looks around, it starts a small timer to let them get their bearings before setting them to vulnerable, but still immediately sets them to vulnerable if they physically move at all

#

w/e i just put the code as text kek

unique cedar