diff --git a/docs/blocks/block-format-history.md b/docs/blocks/block-format-history.md index a98f87ca14..ab78c7eb97 100644 --- a/docs/blocks/block-format-history.md +++ b/docs/blocks/block-format-history.md @@ -90,6 +90,16 @@ An experiment must be enabled in your world for experimental changes to take eff - Released `minecraft:entity_fall_on` from experimental - Released `minecraft:tick` from experimental +## 1.21.0 + +### Components + + + +- Updated `minecraft:transformation` + - Added `scale_pivot` parameter + - Added `rotation_pivot` parameter + ## 1.20.80 diff --git a/docs/entities/spawning-tamed-entities.md b/docs/entities/spawning-tamed-entities.md index 96d4abc6c8..3991723cb1 100644 --- a/docs/entities/spawning-tamed-entities.md +++ b/docs/entities/spawning-tamed-entities.md @@ -1,7 +1,9 @@ --- title: Spawning Tamed Entities +description: Learn about different methods that can be used to spawn a pre-tamed entity. category: Tutorials tags: + - scripting - intermediate mentions: - Axelpvz2030 @@ -10,123 +12,132 @@ mentions: - MedicalJewel105 - SmokeyStack - ThomasOrs -description: In this tutorial, you will learn how to spawn a pre-tamed entity by running an event on the player you want the entity tamed to, and by throwing an item that transforms into a tamed entity upon impact. + - QuazChick --- -In this tutorial, you will learn how to spawn a pre-tamed entity by running an event on the player you want the entity tamed to, and by throwing an item that transforms into a tamed entity upon impact. +In this tutorial, you will learn how to spawn a pre-tamed entity via scripting, by running an event on the player you want the entity tamed to, or by throwing an item that transforms into a tamed entity upon impact. -## Overview +## Scripting Method -Traditionally, if you wanted entity tamed by a player, you had to force the player to interact with that entity through `minecraft:tameable`. However, you can also take advantage of the fact that vanilla projectiles keep track of the entity responsible for spawning\* them to spawn an entity pre-tamed. +This example will spawn a tamed wolf for each player whenever they spawn (or respawn) by using the [`EntityTameableComponent.tame`](https://learn.microsoft.com/minecraft/creator/scriptapi/minecraft/server/entitytameablecomponent#tame) method. -To do this, we'll summon a dummy middle-man projectile entity through `minecraft:spawn_entity`, which will instantly transform into the entity we want to spawn pre-tamed in this tutorial, a vanilla wolf through `minecraft:transformation` with `keep_owner` set to `true`. +```js +import { world } from "@minecraft/server"; -\*: _Spawning_ should not be confused with _summoning_. The projectile will keep track of the player if it was spawned by a spawn egg or `minecraft:spawn_entity` component, but not by a `/summon` command. +world.afterEvents.playerSpawn.subscribe(({ player }) => { + const wolf = player.dimension.spawnEntity("minecraft:wolf", player.location); -## player.json + // Tame the wolf to the player + const tameable = wolf.getComponent("minecraft:tameable"); + tameable.tame(player); +}); +``` -Here, we'll need a copy of the player's behavior file, which we will modify slightly. we'll add a simple event that adds a component group which will spawn our custom middle-man entity. +## Events Method -You can find the BP player entity file in the vanilla behavior pack provided by Mojang [here](https://aka.ms/behaviorpacktemplate). +:::danger UNRECOMMENDED +This method makes use of runtime identifiers and modification of the player entity, which can lead to incompatibility. +Using scripting is preferred as it does not have these downsides. +::: -BP/entities/player.json +### Tamed Entity Spawner + +We'll need to create a simple custom entity that will have the `minecraft:arrow` runtime identifier (other projectile runtime identifiers work as well), an empty projectile component, and a transformation component to turn into a tamed wolf. + +BP/entities/pretamed_wolf.json ```json { - "format_version":"1.16.0", - "minecraft:entity":{ - "description":{ - "identifier":"minecraft:player", - "is_spawnable":false, - "is_summonable":false, - "is_experimental":false + "format_version": "1.21.0", + "minecraft:entity": { + "description": { + "identifier": "wiki:pretamed_wolf", + "runtime_identifier": "minecraft:arrow", + "is_spawnable": false, + "is_summonable": true, + "is_experimental": false }, - "component_groups":{ - "wiki:spawn_tamed_wolf":{ - "minecraft:spawn_entity":{ - "entities":{ - "min_wait_time":0, - "max_wait_time":0, - "spawn_entity":"wiki:pretamed_wolf", - "single_use":true, - "num_to_spawn":1 - } - } - } - }, - ... - "events":{ - "wiki:spawn_tamed_wolf":{ - "add":{ - "component_groups":[ - "wiki:spawn_tamed_wolf" - ] - } + "components": { + "minecraft:projectile": {}, + "minecraft:transformation": { + "into": "minecraft:wolf", + "keep_owner": true } } } } ``` -## pretamed_wolf.json +You can also spawn it with a `wiki:pretamed_wolf` spawn egg by setting `is_spawnable` to `true`! -Afterwards, we'll need to create a simple custom entity that will have the `minecraft:arrow` runtime identifier (other projectile runtime identifiers work as well), an empty projectile component, and a transformation component to turn into a tamed wolf. +:::warning CUSTOM PETS +If you want to spawn a custom entity instead of a wolf using this method, you will need to make sure that the entity has the `minecraft:is_tamed` component for it to work properly. Otherwise, some behaviors will not function as expected for a tamed entity. +::: -BP/entities/pretamed_wolf.json +### Player Event + +Here, we'll need a copy of the player's behavior file, which we will modify slightly. we'll add a simple event that adds a component group which will spawn our custom middle-man entity. + +You can find the BP player entity file in the vanilla behavior pack provided by Mojang [here](https://aka.ms/behaviorpacktemplate). + +BP/entities/player.json ```json { - "format_version": "1.16.0", - "minecraft:entity": { - "description": { - "identifier": "wiki:pretamed_wolf", - "runtime_identifier": "minecraft:arrow", - "is_spawnable": false, - "is_summonable": true, - "is_experimental": false - }, - "components": { - "minecraft:projectile": {}, - "minecraft:transformation": { - "into": "minecraft:wolf", - "keep_owner": true - } - } - } + "format_version": "1.21.0", + "minecraft:entity": { + "description": { + "identifier": "minecraft:player", + "is_spawnable": false, + "is_summonable": false, + "is_experimental": false + }, + "component_groups": { + ... + "wiki:spawn_tamed_wolf": { + "minecraft:spawn_entity": { + "entities": { + "min_wait_time": 0, + "max_wait_time": 0, + "spawn_entity": "wiki:pretamed_wolf", + "single_use": true, + "num_to_spawn": 1 + } + } + } + }, + ... + "events": { + "wiki:spawn_tamed_wolf": { + "add": { + "component_groups": ["wiki:spawn_tamed_wolf"] + } + } + } + } } ``` -And now, you can spawn a tamed wolf next to the player with `/event entity @p wiki:spawn_tamed_wolf`. You can also spawn it with a `wiki:pretamed_wolf` spawn egg by setting `is_spawnable` to `true`! - -:::warning -If you want to spawn a custom entity instead of a wolf using this method, you will need to make sure that the entity has the minecraft:is_tamed component for it to work properly. Otherwise, some behaviors will not function as expected for a tamed entity. -::: - -## Integrating Item Projectiles (Alternate Method) +### Spawning on Projectile Hit -Introduced as one of [1.16's experimental item features](/items/item-components), the `shoot` event property can be used to make projectiles that transform into tamed entities upon impact. +To make the tamed wolf spawn only when the projectile hits something, we first need to create a throwable item: BP/items/throwable_pretamed_wolf.json ```json { - "format_version":"1.16.100", - "minecraft:item":{ - "description":{ - "identifier":"wiki:throwable_pretamed_wolf" + "format_version": "1.21.50", + "minecraft:item": { + "description": { + "identifier": "wiki:throwable_pretamed_wolf" }, - "components":{ - "minecraft:on_use":{ - "on_use":{ - "event":"wiki:on_use" - } - } - }, - "events":{ - "wiki:on_use":{ - "shoot":{ - "projectile":"wiki:pretamed_wolf" - } + "components": { + "minecraft:icon": "my_icon", + "minecraft:throwable": { + "do_swing_animation": true + }, + "minecraft:projectile": { + "projectile_entity": "wiki:pretamed_wolf" } } } @@ -139,43 +150,42 @@ We'll also need to make some adjustment to our custom projectile entity so that ```json { - "minecraft:entity":{ - "description":{ - "identifier":"wiki:pretamed_wolf", - "runtime_identifier":"minecraft:arrow", - "is_spawnable":false, - "is_summonable":true, - "is_experimental":false + "format_version": "1.21.0", + "minecraft:entity": { + "description": { + "identifier": "wiki:pretamed_wolf", + "runtime_identifier": "minecraft:arrow", + "is_spawnable": false, + "is_summonable": true, + "is_experimental": false }, - "component_groups":{ - "wiki:transform_to_entity":{ - "minecraft:transformation":{ - "into":"minecraft:wolf", - "keep_owner":true + "component_groups": { + "wiki:transform_to_entity": { + "minecraft:transformation": { + "into": "minecraft:wolf", + "keep_owner": true } } }, - "components":{ - "minecraft:projectile":{ - "on_hit":{ - "impact_damage":{ - "damage":0 + "components": { + "minecraft:projectile": { + "on_hit": { + "impact_damage": { + "damage": 0 }, - "stick_in_ground":{}, - "definition_event":{ - "event_trigger":{ - "event":"wiki:on_hit" + "stick_in_ground": {}, + "definition_event": { + "event_trigger": { + "event": "wiki:on_hit" } } } } }, - "events":{ - "wiki:on_hit":{ - "add":{ - "component_groups":[ - "wiki:transform_to_entity" - ] + "events": { + "wiki:on_hit": { + "add": { + "component_groups": ["wiki:transform_to_entity"] } } }