ServerEvents.tick(event => {
    if (event.server.tickCount % 50 != 0) return;

    event.server.players.forEach(player => {
        if (!player.persistentData.firstj) { // Checks for the firstj tag, the player shouldnt have this if they just joined the world. Also works for each player independently because tags are individual
            player.persistentData.firstj = true; // Adds the firstj tag so that if they rejoin their world, the screen won't pop up.
            fadeWelcomeUI(player, 0, 5); 
        }
    });
});

function fadeWelcomeUI(player, alpha, step) { // This makes a function that plays the actual welcome UI. For ease of use, you can just edit the nextAlpha and step (or actually, just alpha and step) for how fast you want it to fade in or out.
    // Text Alpha (00-FF)
    let textHexAlpha = Math.floor(alpha).toString(16).padStart(2, '0'); //alpha or transparency for the text itself
    
    // Background Alpha (Scales 0-255 input to 0-230 for 90% max darkness) OR TRANSPARENCY
    let bgAlphaDecimal = Math.floor(alpha * (180 / 255)); 
    let bgHexAlpha = bgAlphaDecimal.toString(16).padStart(2, '0');

    player.paint({
        // 1. The Overlay: Changed type to 'rectangle' for better 1.20.1 compatibility. actually this should work on alot more versions, i just heard it'd look better this way (it does)
        background_overlay: {
            type: 'rectangle',
            x: 0, y: 0,
            w: '$screenW', // Stretch to full screen width
            h: '$screenH', // Stretch to full screen height
            color: `#${bgHexAlpha}000000`, // Pure black with dynamic transparency
            draw: 'always' //This is just making the background as pure black, and the first 2 numbers of the HEX are for transparency.
        },
        // 2. The Text Elements
        welcome_top: {
            type: 'text', text: 'Welcome to N.E.E.P. Trifecta',
            scale: 2.5, color: `#${textHexAlpha}ff0000`,
            alignX: 'center', alignY: 'center', y: -40, draw: 'always' // Top text. Should be the biggest. ff0000 is the last hex code for the type of red i wanted.
        },
        welcome_bottom: {
            type: 'text', text: "Thank you for playing, " + player.username,
            scale: 1.5, color: `#${textHexAlpha}ffffff`,
            alignX: 'center', alignY: 'center', y: 0, draw: 'always' // Middle text. It's a little smaller and shifted down more. ffffff is for pure white text
        },
        welcome_info: {
            type: 'text', text: "Please report any issues in the discord server or github repo :p",
            scale: 0.8, color: `#${textHexAlpha}ffffff4b`,
            alignX: 'center', alignY: 'center', y: 40, draw: 'always' // Moved down and made text smaller for the third line. Text SHOULD be a little more gray
        }
    });

    let nextAlpha = alpha + step; // this part just repeats, mainly

    if (step > 0 && nextAlpha >= 255) {
        player.server.scheduleInTicks(100, () => fadeWelcomeUI(player, 255, -5));
    } 
    else if (step < 0 && nextAlpha <= 0) {
        player.paint({ 
            welcome_top: { remove: true }, 
            welcome_bottom: { remove: true }, 
            welcome_info: { remove: true },
            background_overlay: { remove: true } // This just removes all the text if their transparency is at 100%-
        });
    } 
    else {
        player.server.scheduleInTicks(1, () => fadeWelcomeUI(player, nextAlpha, step)); // After joining, it'll play the animation with the three parameters. Recommended to not change most directly except for step value.
    }
} // Dont mind these errors, VScode is RIGHT
