#How to give an enchanted book with an enchant list thingy

1 messages · Page 1 of 1 (latest)

karmic edge
#

Im trying to transfer all the enchantments from one item to an new sword which I give the player I currently can’t get the code but will when I wake up again what I already did is get the enchantments.enchantments from the item which is an enchantment list thingy. then I created an new ItemStack which then got the enchantments component from and tried to enchant but I failed somewhere

karmic edge
#

Please someone help

solemn crown
#

If you got the enchantment component from the item you just need to do something like js const itemStack = new ItemStack(‘item’,1) const enchants = item.getComponent(“enchantment”).enchantments for (const enchant of enchants) { const typeId = enchant.type const level = enchant.level itemStack.getComponent(“enchantment”).enchantments.addEnchantment(new Enchantment(type, level)) } I am writing this on mobile so I may have made a few mistakes

#

Also make sure to import Enchantment from Minecraft/server

karmic edge
#

alright

#

so now my problem is that the item isn't enchanted when giving to the Player

#
        for (const ench of enchantments) {
            itemEnchantComponent.addEnchantment(new Enchantment(ench.type, ench.level));
        }
        player.getComponent("inventory").container.addItem(item);
dull escarp
solemn crown
#

well you are just getting all the enchants and adding it to the component

#

but you have to first save them in a list

#

so you would do

#
const list = item.getComponent("enchantments").enchantments
list.addEnchantment(new Enchantment(typeId, level));
```and then you are gonna set the component to the list like so ```js
item.getComponent("enchantments").enchantments = list```
#

i have mine like this

#
const enchants = auctionItem.enchants
for (let i = 0; i < enchants.length; i++) {
    const typeId = enchants[i].type
    const level = enchants[i].level
    const list = newItem.getComponent("enchantments").enchantments
    list.addEnchantment(new Enchantment(typeId, level));
    newItem.getComponent("enchantments").enchantments = list
}```
karmic edge
#
            const enchants = itemStack.getComponent("enchantments").enchantments;
            const newItem = new ItemStack(MinecraftItemTypes.woodenSword, 1);
            for (let i = 0; i < enchants.length; i++) {
                const type = enchants[i].type;
                const level = enchants[i].level;
                const list = newItem.getComponent("enchantments").enchantments;
                list.addEnchantment(new Enchantment(type, level));
                newItem.getComponent("enchantments").enchantments = list;
            }
            player.getComponent("inventory").container.addItem(newItem);
#

this technically should work but Im maybe stupid

#

bcs it doesnt

verbal dirgeBOT
#
Debug Result

There are 4 errors in this [code](#1135355390922588210 message):

<repl>.js:1:30 - error TS2552: Cannot find name 'itemStack'. Did you mean 'ItemStack'?

1             const enchants = itemStack.getComponent("enchantments").enchantments;
                               ~~~~~~~~~

  @minecraft/server.d.ts:5805:15
    5805         class ItemStack {
                       ~~~~~~~~~
    'ItemStack' is declared here.

``````ansi
<repl>.js:6:30 - error TS2532: Object is possibly 'undefined'.

6                 const list = newItem.getComponent("enchantments").enchantments;
                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

``````ansi
<repl>.js:8:17 - error TS2532: Object is possibly 'undefined'.

8                 newItem.getComponent("enchantments").enchantments = list;
                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

``````ansi
<repl>.js:10:13 - error TS2552: Cannot find name 'player'. Did you mean 'Player'?

10             player.getComponent("inventory").container.addItem(newItem);
               ~~~~~~

  @minecraft/server.d.ts:8036:15
    8036         class Player extends Entity {
                       ~~~~~~
    'Player' is declared here.

chrome hedge
#
            const enchants = ItemStack.getComponent("enchantments").enchantments;
            const newItem = new ItemStack(MinecraftItemTypes.woodenSword, 1);
            for (let i = 0; i < enchants.length; i++) {
                const type = enchants[i].type;
                const level = enchants[i].level;
                const list = newItem.getComponent("enchantments").enchantments;
                list.addEnchantment(new Enchantment(type, level));
                newItem.getComponent("enchantments").enchantments = list;
            }
            player.getComponent("inventory").container.addItem(newItem);
karmic edge
#

what did you change?

#

btw its not bcs of the itemstack not being defined

#
world.beforeEvents.itemUseOn.subscribe(({ block, source: player, itemStack}) => {
chrome hedge
chrome hedge