#Script to remove item just on first join?

6 messages · Page 1 of 1 (latest)

tidal terrace
#

Hello. I'm trying to remove or clean inventory on first join, to remove one item, which is added by mod but it's not possible to remove it through config or datapack. I have asked chatGPT, but obviously it doesn't work and I have no coding skills either. Can someone help me?

// kubejs/server_scripts/first_join_item_removal.js

// Replace 'modid:item_name' with the item you want to remove
const ITEM_TO_REMOVE = 'modid:item_name';

ServerEvents.playerLoggedIn(event => {
const player = event.player;

// Check if this is the player's first login
if (!player.persistentData.has('first_join_checked')) {
    // Mark that we've checked the player on first join
    player.persistentData.putBoolean('first_join_checked', true);

    // Check and remove the specific item from the player's inventory
    const inventory = player.inventory;
    const itemsToRemove = inventory.findAll(ITEM_TO_REMOVE);

    itemsToRemove.forEach(itemStack => {
        inventory.removeItem(itemStack);
    });

    // Optionally, log the action to the server console
    console.log(`Removed ${ITEM_TO_REMOVE} from player ${player.name.getString()} on first join.`);
}

});

winter vortexBOT
#

Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!

blazing rapidsBOT
#

You can write your code in a codeblock by typing it between the codeblock delimiters:
Note that these are backticks, not apostrophes

```js :arrow_left:

ServerEvents.recipes(event => {
event.smelting('minecraft:glass', '#forge:sand').xp(.1)
})

``` :arrow_left:

This example will look like this:

ServerEvents.recipes(event => {
  event.smelting('minecraft:glass', '#forge:sand').xp(.1)
})
blazing rapidsBOT
#

NEVER use any AI to generate KubeJS code. They currently know very little about it, and what they do know is often outdated or false. You can instead look at the KubeJS wiki or ask for help in #1047320998199955458.

haughty harness
#

regardless what your AI-generated script is, I believe I have just the solution!:

PlayerEvents.loggedIn(event => {
    const { player, server } = event

    if (!player.persistentData.getBoolean("first_join")) {
        var timer = 0

        player.persistentData.putBoolean("first_join", true)

        server.scheduleRepeatingInTicks(4, (c) => {
            if (player.inventory.find("item_ID_here") != -1 && timer < 100) { // Reschedule until item is gone and nonexistent
                timer = timer + 1
                server.runCommandSilent(`clear ${player.username} item_ID_here`)
                c.reschedule()
            } else {
                c.clear()
            }
        })
    }
})
#

replace item_ID_here with the item ID