2

I'm trying to have my custom food item give the player a status effect when it is eaten. The item itself works fine (is edible, gives correct nutrition and saturation, uses eating animation, etc.) except for the status effect not being applied.

Following the official Microsoft documentation here and also this example from the Bedrock Wiki, I first tried using the food component's effects property, but this did nothing.

So, I did a search for alternatives and found others were using an event to apply status effects, such as in this video. But, when I tried calling the event from the on_consume component, it still did not apply the effect, and I saw this error in the console:

child add_mob_effect not valid here

Has the syntax changed significantly in the latest stable version (1.20.30 - I'm not using the experimental features in this pack)? Does anyone know the correct way of formatting the event for the current version?

Here is my complete amethyst_apple.item.json file from the behavior back:

{  
  "format_version": "1.20.30",  
  "minecraft:item": {  
        "description": {  
            "identifier": "addtns:amethyst_apple",  
            "menu_category": {  
                "group": "itemGroup.name.crop",
                "category": "nature"
            }  
        },  
        "components": {
            "minecraft:hand_equipped": false,           
            "minecraft:max_stack_size": 64,
            "minecraft:icon": {  
                "texture": "amethyst_apple"  
            },  
            "minecraft:display_name": {  
                "value": "Amethyst Apple"  
            },
            "minecraft:hover_text_color": "light_blue", // Not working: TODO: find correct color name
            "minecraft:use_duration": 2,
            "minecraft:use_animation": "eat",
            "minecraft:food": {
                "nutrition": 4, // Same as other apples
                "saturation_modifier": 9.6, // Same as Golden Apple
                "can_always_eat": true,

                // NOT WORKING: So, I have tried to accomplish the same thing with the "on_consume" event instead (for testing purposes, only with one effect instead of a sequence)
                "effects": [
                    {
                        "name": "slow_falling",
                        "chance": 1.0,
                        "duration": 120,
                        "amplifier": 1
                    },
                    {
                        "name": "levitation",
                        "chance": 1.0,
                        "duration": 10,
                        "amplifier": 1
                    },
                    {
                        "name": "regeneration",
                        "chance": 1.0,
                        "duration": 5,
                        "amplifier": 1
                    }
                ]
            },

            // This is ALSO not working. It complains that "child add_mob_effect not valid here"
            "minecraft:on_consume": {
                "event": "eat",
                "target": "holder" // Credit to IcyFlag from https://www.minecraftforum.net/forums/minecraft-pocket-edition/mcpe-mods-tools/mcpe-mod-tool-discussion/3062942-need-help-with-making-a-custom-item for the suggestion of making the target here the same as in the event (so, "holder" instead of "self")
            }
        },
        "events": {
            "eat": {
                "add_mob_effect": {
                    "effect": "levitation",
                    "duration": 10,
                    "amplifier": 1,
                    "target": "holder"
                }
            }
        }
    }
}

2 Answers 2

1

It's not just "on_consume" that no longer works in 1.20.30. All block and item events seem to be non-functional.

There is a workaround: the desired functionality can be accomplished using server-side scripting instead. A JavaScript file can be set up which will run commands to apply the status effects to the player upon use of an item specified by its id.

This means adding a scripts folder to the behavior pack at the same level as the items folder and the manifest.json file. The manifest.json file will also have to be updated to add the scripts as a dependency.

Here is the JavaScript file main.js, which, when placed in the behavior pack's new scripts folder, will add the desired functionality for the amethyst apple:

import { world, system } from "@minecraft/server"

world.afterEvents.itemCompleteUse.subscribe(event => { // NOT "itemUse", "itemCompleteUse"! This is important if you don't want the effect to happen until the item is consumed.
    if (event.itemStack.typeId === "addtns:amethyst_apple") {
        (async () => {
            await world.getDimension("overworld").runCommandAsync("effect @p slow_falling 120 1");
            await world.getDimension("overworld").runCommandAsync("effect @p levitation 10 1");
            await world.getDimension("overworld").runCommandAsync("effect @p regeneration 5 2");
        })();
    }
});

Now that the script is taking care of applying the effects, all effect-related components/events can be deleted from amethyst_apple.item.json. This leaves only the following:

{  
  "format_version": "1.20.30",  
  "minecraft:item": {  
        "description": {  
            "identifier": "addtns:amethyst_apple",  
            "menu_category": {  
                "group": "itemGroup.name.crop",
                "category": "nature"
            }  
        },  
        "components": {
            "minecraft:hand_equipped": false,           
            "minecraft:max_stack_size": 64,
            "minecraft:icon": {  
                "texture": "amethyst_apple"  
            },  
            "minecraft:display_name": {  
                "value": "Amethyst Apple"  
            },
            "minecraft:hover_text_color": "aqua", // Same as Golden Apple
            
            "minecraft:use_duration": 2,
            "minecraft:use_animation": "eat",
            "minecraft:food": {
                "nutrition": 4, // Same as other apples
                "saturation_modifier": 9.6, // Same as Golden Apple
                "can_always_eat": true
            }
        }
    }
}

Together, this plus the script above give the exact same functionality as the 1.10 item (see other answer), but don't require downgrading the format version or duplicating the .item.json file in the resource pack.

2
  • This solution has the advantage of keeping the custom items in the correct menu categories in the Creative Inventory. In order for the items to work in all dimensions, the script could be modified to get the player's current dimension and have that run the command, instead of hardcoding "overworld." Oct 8, 2023 at 5:42
  • Like this: const player = event.source; const d = player.dimension; (both inside the if statement but above the async block) then await d.runCommandAsync("effect @p slow_falling 120 1"); (inside the async block). Oct 8, 2023 at 21:08
0

It is actually possible to still use the 1.10 format (which is the one used for items in the vanilla behavior pack downloadable here) for food items which are meant to apply effects to the player when eaten. If the format is kept as 1.10, the "effects" property of "minecraft:food" works as intended. Note that it is not necessary for all items within a pack to have the same pack format! Others can be left as 1.20.30.


Previously, I had tried this and it hadn't worked, but that was because I had forgotten that in older formats, items are defined in two separate .item.json files: one in the behavior pack and one in the resource pack.

Newer formats eliminate the resource pack .item.json file entirely, and move all its components (such as the "icon" property) into the consolidated .item.json file in the behavior pack. However, "effects" for foods seems to be non-functional in newer formats.

The solution is to revert only those items which are foods applying effects when eaten to the older format, which can be done as follows for the amethyst apple:

  1. Copy and paste the vanilla .item.json file for the Golden Apple (which is 1.10 format) into the amethyst_apple.item.json file in the behavior pack, then make the relevant changes to the identifier, icon name, and the specific effects which are applied. The result should be this:
{  
    "format_version": "1.10",  
    "minecraft:item": {  
        "description": {  
            "identifier": "addtns:amethyst_apple" // Menu category has been relocated to resource pack file
        },  
        "components": {
            "minecraft:stacked_by_data": true,
            "minecraft:use_duration": 32, // Maybe this was in ticks? It certainly isn't seconds!
            "minecraft:foil": false, // This property is obsolete in newer versions (replaced with "glint")
            
            "minecraft:food": {
                "nutrition": 4,
                "saturation_modifier": "supernatural", // A string, not a number - equivalent to 9.6
                "can_always_eat": true,
                "effects": [
                    {
                        "name": "slow_falling",
                        "chance": 1.0,
                        "duration": 30,
                        "amplifier": 1
                    },
                    {
                        "name": "levitation",
                        "chance": 1.0,
                        "duration": 10,
                        "amplifier": 1
                    }
                ]
            }
        }
    }
}
  1. Create an items folder in the resource pack, at the same level as manifest.json and the textures folder. Inside it, put another file named amethyst_apple.item.json (the same name as the one in the BP) but with these contents:
{
    "format_version": "1.10",
    "minecraft:item": {
        "description": {
            "identifier": "addtns:amethyst_apple",
            "category": "Nature" // Just a "category" string, not the "menu_category" object
        },
        "components": {
            "minecraft:icon": "amethyst_apple",
            "minecraft:render_offsets": "apple", // Another obsolete property in newer formats
            "minecraft:hover_text_color": "aqua" // This is the correct name for that light blue color
        }
    }
}

A template for this file cannot be found in the vanilla resource pack, since vanilla items are hardcoded. Instead, you can find a reference on the Bedrock Wiki here.

The amethyst apple should now work as intended. Except for not showing up in the correct menu category (there's always something XD) -- it is only visible in the Creative Inventory under the Search/All tab, not under Nature.

2
  • Alternatively, the 1.16.100 file format can be used for the food item, along with the "on_consume" event; however, although the event will work and not give the error mentioned in the question's title, you will have to turn on the Experimental Features even in Bedrock 1.20.30. Using the 1.10 format avoids having to turn on Experimental Features. Oct 6, 2023 at 20:22
  • Additionally, "effects" no longer works as of 1.60.100 - it has been replaced by "on_consume", so it's either-or. Reference: wiki.bedrock.dev/items/items-16.html Oct 6, 2023 at 20:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .