Skip to content

Commit

Permalink
Merge pull request #973 from Bedrock-OSS/spawning-tamed-entities
Browse files Browse the repository at this point in the history
Add scripting method to spawn tamed entities
  • Loading branch information
QuazChick authored Jan 19, 2025
2 parents 6838334 + 27da294 commit 6f01fc0
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 109 deletions.
10 changes: 10 additions & 0 deletions docs/blocks/block-format-history.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

<Label color="green">Versioned</Label>

- Updated `minecraft:transformation`
- Added `scale_pivot` parameter
- Added `rotation_pivot` parameter

## 1.20.80

<Tag name="experimental" />
Expand Down
228 changes: 119 additions & 109 deletions docs/entities/spawning-tamed-entities.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
:::

<CodeHeader>BP/entities/player.json</CodeHeader>
### 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.

<CodeHeader>BP/entities/pretamed_wolf.json</CodeHeader>

```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<minecraft:on_tame>",
"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.
:::

<CodeHeader>BP/entities/pretamed_wolf.json</CodeHeader>
### 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).

<CodeHeader>BP/entities/player.json</CodeHeader>

```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<minecraft:on_tame>",
"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:

<CodeHeader>BP/items/throwable_pretamed_wolf.json</CodeHeader>

```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"
}
}
}
Expand All @@ -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<minecraft:on_tame>",
"keep_owner":true
"component_groups": {
"wiki:transform_to_entity": {
"minecraft:transformation": {
"into": "minecraft:wolf<minecraft:on_tame>",
"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"]
}
}
}
Expand Down

0 comments on commit 6f01fc0

Please sign in to comment.