#Question: Storing of XP
1 messages · Page 1 of 1 (latest)
I already have the xp book I just need to know how to make a script since I am new to this
Youll need to have the itemUse custom component (or the normal non custom component) -> grab the players xp level -> check if player is sneaking -> if so, give 1 level and remove from item, if not, remove 1 level and add to item.
You can use dynamic properties to store the xp level stored, make sure your item is a non stackable
Thanks a lot
Actually a fun idea
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.
good point
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.
if content log isnt showing, its either
- the functions are not triggering
- the script has a syntax error
Still nothing, scripting is so confusing I think I should stick to json ui 💀
JSON UI is harder imo lol. Ok so what's not working exactly
When right clicking it doesn't deduct xp from my xp bar and doesn't store it
If no content log are showing that means that your logic isn’t working so you have to create your content logs doing console.warn(``) to get some variables if they re correct or to know if an IF condition ran or got false
Does your item have the use_modifeirs component?
Nope, I think that might be it
"minecraft:use_modifiers": true ?