#Question: Storing of XP

1 messages · Page 1 of 1 (latest)

clear shale
#

How can I create a custom item (xp storage book) that stores one xp level when right clicked and removes one xp level when right clicked while sneaking? Additionally I want to limit the number of xp levels that can be stored in the book. (Also if possible without beta-api)

#

I already have the xp book I just need to know how to make a script since I am new to this

uncut vapor
mellow juniper
#

Actually a fun idea

runic jackal
#

After level 16 it starts to take more experience points to get each level. Not sure if it's necessary to account for since it always seemed arbitrary to me, but something to consider.

clear shale
#
import * as mc from "@minecraft/server";

const xpLevelProperty = "xpLevel";
const maxXpLimit = 10;

function transferXpToItem(player, itemStack, amount = 1) {
    const playerXp = player.getXpLevel();
    const storedXpLevel = itemStack.getDynamicProperty(xpLevelProperty) || 0;
    if (playerXp >= amount && storedXpLevel + amount <= maxXpLimit) {
        player.addExperienceLevels(-amount);
        itemStack.setDynamicProperty(xpLevelProperty, storedXpLevel + amount);
        updateItemLore(itemStack, storedXpLevel + amount);
    }
}

function transferXpToPlayer(player, itemStack, amount = 1) {
    const storedXpLevel = itemStack.getDynamicProperty(xpLevelProperty) || 0;
    if (storedXpLevel >= amount) {
        player.addExperienceLevels(amount);
        itemStack.setDynamicProperty(xpLevelProperty, storedXpLevel - amount);
        updateItemLore(itemStack, storedXpLevel - amount);
    }
}

function updateItemLore(itemStack, storedXpLevel) {
    const lore = [`XP: ${storedXpLevel}`];
    itemStack.setLore(lore);
}

mc.world.afterEvents.itemUse.subscribe(({ itemStack, source }) => {
    if (itemStack && itemStack.typeId === "wc:xp_tome") {
        if (source.isSneaking) {
            transferXpToPlayer(source, itemStack);
        } else {
            transferXpToItem(source, itemStack);
        }
    }
});

I don't know why this isn't working. Content logs ain't showing anything either.

kind cairn
clear shale
uncut vapor
#

JSON UI is harder imo lol. Ok so what's not working exactly

clear shale
woeful thunder
uncut vapor
clear shale
#

"minecraft:use_modifiers": true ?

uncut vapor
#

Yep, your item needs that in order to be right clickable

#
minecraft:use_modifiers:{
  use_duration: 1
}
```