Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 0 additions & 42 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions src/lib/sidebar/tabs/latest/Guides.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import SidebarHeading from "$lib/sidebar/navigation/SidebarHeading.svelte";

// Icon imports
// https://tabler-icons.io
// https://tabler.io
// import Icon<whatever name> from "~icons/tabler/<icon ID>"
import IconHome from "~icons/tabler/home";
import IconPennant from "~icons/tabler/pennant";
Expand Down Expand Up @@ -44,6 +44,7 @@
import IconCat from "~icons/tabler/cat";
import IconBaguette from "~icons/tabler/baguette";
import SidebarPlaceholder from "../../navigation/SidebarPlaceholder.svelte";
import IconPick from '~icons/tabler/pick';
</script>

<!-- ADD PAGES AND CATEGORIES BELOW -->
Expand All @@ -66,10 +67,7 @@
<SidebarHeading label="Custom Items" />
<SidebarPage label="Introduction" icon={IconItem} page="/guide/adding-new-features/custom-items/intro" />
<SidebarPage label="Custom Item Recipes" icon={IconGrid} page="/guide/adding-new-features/custom-items/crafting" />
<SidebarPage
label="Custom Item Models and Textures"
icon={IconPhotoPlus}
page="/guide/adding-new-features/custom-items/models" />
<SidebarPage label="Custom Item Models and Textures" icon={IconPhotoPlus} page="/guide/adding-new-features/custom-items/models" />
</SidebarCategory>

<SidebarCategory name="Right Click Detection" icon={IconMouse}>
Expand All @@ -88,6 +86,7 @@
<SidebarPage label="Player ID System" icon={IconID} page="/guide/nbt-and-scores/player-id-system" />
<SidebarPage label="Floating Point Division" icon={IconDivide} page="/guide/nbt-and-scores/division" />
<SidebarPage label="Array Iteration" icon={IconList} page="/guide/nbt-and-scores/array-iteration" />
<SidebarPage label="Default Item Components" icon={IconPick} page="/guide/nbt-and-scores/get-default-item-components" />
</SidebarCategory>

<SidebarCategory name="Performance" icon={IconBrandSpeedtest}>
Expand Down
1 change: 1 addition & 0 deletions src/routes/credits/+page.svx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ These people have written or edited pages for the wiki or guides.
- Kraggle09
- Amandin
- Subzeroditto
- theblackswitch

:::note

Expand Down
101 changes: 101 additions & 0 deletions src/routes/guide/nbt-and-scores/get-default-item-components/+page.svx
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
---
title: Get default item components
description:
This section explains how you would collect the default data components from an item using a
Minecraft datapack.
version: 26.2
---

# Get Default Item Components
This method will explain how to get the default **data components** (commonly referred to as item components) from an item. These default components are usually hidden from a datapack but aquiring them can be really useful. We could, for example, use this to read the `max_stack_size` from any item.

The method relies on the `minecraft:copy_components` loot table function which copies item components from a source item, but also **includes** the (hidden) default components. We can use this functionality in a datapack to easily access the default components of any item.

## Method

To start, we make a loot table that drops a dummy item and applies the `copy_components` loot function. This function requires a source to copy the components from. This, unfortunatly, can only be one of `block_entity`, any entity from context (for example `this` or `attacker`) or a `tool`. The only source that can be an item is `tool` so we'll use that.

Now we need to "tell" the loot table what the `tool` is. This can be done through [loot context](https://minecraft.wiki/w/Loot_context). In this case we'll use fishing loot context.

```json:copy_default_components.json
{
"type": "minecraft:fishing",
"pools": [{
"rolls": 1,
"entries": [{
"type": "minecraft:item",
"name": "minecraft:poisonous_potato",
"functions": [{
"function": "minecraft:copy_components",
"source": "tool"
}]
}]
}]
}
```

The fishing loot context allows us to use the `mainhand` slot as a `tool`. This leads us to the following command:
```mcfunction
loot replace entity @s weapon.offhand fish namespace:copy_default_components ~ ~ ~ mainhand
```

This command will place a dummy item (from the loot table) into the player's offhand slot. (Since we don't want to replace the offhand slot or any other inventory slot, we'll have to summon a temporary entity to store the dummy item. More on that later.) Then, the loot table will also copy all components from the item in the `mainhand` to the dummy item in the offhand slot, even the default (hidden) components.

To check this worked, you can access the item components data manually using the command `/data get entity @s equipment.offhand.components` - you should see all the default components that are usually hidden.

:::info

The `/loot` command requires a position when you access a fishing-type loot table. In our case, this can be any position since we're not using said context.

:::

### Read any slot

Unfortunately, Minecraft only lets us use the `mainhand` slot as "tool" context. If we want to read from any slot, we need a small workaround.

To solve both this issue and the target slot issue (explained in the previous section), we can summon a temporary armor stand.

```mcfunction:get_default_components/init.mcfunction
# Tag the player here so we can find it without the execution context
tag @s add target_player
execute summon minecraft:armor_stand run function namespace:get_default_components/process
tag @s remove target_player
```

Next we copy the desired item to its mainhand. After that, we can use this mainhand slot to provide the "tool" context and copy the dummy item to it's offhand. Then the only thing we need to do is store the data from the armor stand and kill it.

```mcfunction:get_default_components/process.mcfunction
# Move the item from the desired player slot onto the armor stand
item replace entity @s weapon.mainhand from entity @a[tag=target_player,limit=1] armor.head

# Run the loot table
loot replace entity @s weapon.offhand fish namespace:get_default_components ~ ~ ~ mainhand

# Copy the components to a result storage
data remove storage namespace:temp default_components
data modify storage namespace:temp default_components set from entity @s equipment.offhand.components

# Kill the armor stand
kill @s
```

Now we can run the function `namespace:get_default_components/init` as a player to get the components from the chosen slot (in this case the head slot). The source slot can be changed by modifying `armor.head` on line 2. The item data components will be stored to the `namespace:default_components` storage, and from here you can access them in your datapack.

:::tip

In addition, you can also filter for or filter out any components using the `include` and `exclude` fields in your loot table function.
```json
{
"function": "minecraft:copy_components",
"source": "tool",
"include": [
"minecraft:max_stack_size"
]
}
```

:::

## Credits

This method was originally discovered by [baphomet42 on the Minecraft Commands discord server](https://discord.com/channels/154777837382008833/157097006500806656/1418032607215620096) and finalized by [Arkiitekk](https://discord.com/channels/935560260725379143/1435909929754955796/1538472517558083624).
2 changes: 1 addition & 1 deletion src/routes/search.json/meta.json

Large diffs are not rendered by default.