Summary:
When adding or removing enchantments via the GameTest Framework -> mojang-minecraft API, nothing happens even though the API reports success and/or no errors.
Steps to Reproduce:
1. Create a simple js script (the example I used is screenshotted below) that creates an item, adds a valid enchantment to it, and then adds it to the player's inventory.
2. Load into any world seed.
3. Connect the Visual Studio Code debugger.
4. Put a breakpoint where the success of the adding enchantment API can be viewed.
5. In the game, do whatever is needed to cause the code to run (for my example code, simply right-click on any block).
6. Go back to VS Code and observe that the breakpoint has been hit.
7. Press F5 to leave the code and return to the game.
Observed Results:
The player has been given an unenchanted item, even though the API reported the enchantment was successfully added.
Expected Results:
The player has been given an enchanted item, because the API reported the enchantment was successfully added.
Screenshots/Videos:
Notes:
Similarly, if using the removeEnchantment API on an item with an enchantment, it throws no errors (does not return anything as it is a void function) and the enchantment is not removed.
Attachments
Comments 8
This may be a little confusing but the general flow with using enchant api is as follows:
1. Obtain the list of enchantments - enchantments property from enchantment component
2. Edit the list of enchantments - using addEnchantment() method
3. Assign the modified enchantment list as the value of enchantments property from enchantment component
Here's a modified version of the code from the attached screenshot:
// create the itemStack
let diamondSwordItem = new ItemStack(MinecraftItemTypes.diamondSword, 1, 0);
// obtain the enchantments component
let enchantmentsComponent = diamondSwordItem.getComponent(`minecraft:enchantments`);
// obtain the enchantments list for the itemStack
let enchantmentsList = enchantmentsComponent.enchantments;
// create the enchantment you want to add
let sharpness = new Enchantment(MinecraftEnchantmentTypes.sharpness, 5);
// modify the enchantments list;
enchantmentsList.addEnchantment(sharpness);
// replace the enchantment list on the item with the modified one
enchantmentsComponent.enchantments = enchantmentsList;
//lastly spawn the itemStack (or use it elsewere, e.g. add to a chest)
world.getDimension(`overworld`).spawnItem(diamondSwordItem, new BlockLocation(0, -59, 0));
Please update your file and let us know if the issue is fixed.
Thanks for the info! Everything makes sense, but having to modify the list and then reassign back isn't quite as intuitive as simply updating the list on the item. But I understand if that is the desired implementation.
I updated my file to do this and it is indeed working as the item now has the enchantment, so this issue can be closed.
Tested on 1.19.0 and this is still an issue, updated the Affected Versions to reflect this.