Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added item event migration page #858

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
171 changes: 171 additions & 0 deletions docs/items/item-event-migration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
---
title: Item Event Migration
description: View Script API implementations of the deprecated JSON item event responses.
category: General
nav_order: 4
tags:
- help
mentions:
- QuazChick
---

:::tip BEFORE YOU START
This page requires you to be comfortable with basic JavaScript and requires knowledge of how modern [item events](/items/item-events) work.
:::

Struggling to upgrade your items' JSON events to [custom components](/items/item-events#registering-custom-components)? Don't worry! This page will help you to understand what the deprecated JSON event responses look like implemented using the Script API.

## Add Mob Effect

<CodeHeader>Custom Component</CodeHeader>

```js
onHitEntity({ hitEntity }) {
hitEntity.addEffect("regeneration", 30, {
amplifier: 10,
showParticles: false
});
}
```

## Damage (Holder)

```js
import { EntityDamageCause } from "@minecraft/server";
```

<CodeHeader>Custom Component</CodeHeader>

```js
onUse({ source }) {
source.applyDamage(2, {
cause: EntityDamageCause.drowning
});
}
```

## Damage (Item)

```js
import { EquipmentSlot, GameMode, Player } from "@minecraft/server";
```

<CodeHeader>Custom Component</CodeHeader>

```js
onMineBlock({ source }) {
// Get main hand slot
if (!(source instanceof Player)) return;

const equippable = source.getComponent("minecraft:equippable");
if (!equippable) return;

const mainhand = equippable.getEquipmentSlot(EquipmentSlot.Mainhand);
if (!mainhand.hasItem()) return;

// Apply durability damage when not in creative mode
if (source.getGameMode() === GameMode.creative) return;

const itemStack = mainhand.getItem(); // Allows us to get item components

const durability = itemStack.getComponent("minecraft:durability");
if (!durability) return;

// Factor in unbreaking enchantment
const enchantable = itemStack.getComponent("minecraft:enchantable");
const unbreakingLevel = enchantable?.getEnchantment("unbreaking")?.level;

const damageChance = durability.getDamageChance(unbreakingLevel) / 100;

if (Math.random() > damageChance) return; // Randomly skip damage based on unbreaking level

// Damage the item
const shouldBreak = durability.damage === durability.maxDurability;

if (shouldBreak) {
mainhand.setItem(undefined); // Remove the item
source.playSound("random.break"); // Play break sound
} else {
durability.damage++; // Increase durability damage
mainhand.setItem(itemStack); // Update item in main hand
}
}
```

## Decrement Stack

```js
import { EquipmentSlot, GameMode } from "@minecraft/server";
```

<CodeHeader>Custom Component</CodeHeader>

```js
onUse({ source }) {
if (!source) return;

const equippable = source.getComponent("minecraft:equippable");
if (!equippable) return;

const mainhand = equippable.getEquipmentSlot(EquipmentSlot.Mainhand);
if (!mainhand.hasItem()) return;

if (source.getGameMode() !== GameMode.creative) {
if (mainhand.amount > 1) {
mainhand.amount--; // Remove one item from the stack
} else {
mainhand.setItem(undefined); // Remove the item stack
}
}
}
```

## Remove Mob Effect

<CodeHeader>Custom Component</CodeHeader>

```js
onHitEntity({ hitEntity }) {
hitEntity.removeEffect("regeneration");
}
```

## Run Command

<CodeHeader>Custom Component</CodeHeader>

```js
onUse({ source }) {
source.runCommand("say Hello there!")
source.runCommand("say Welcome to my world!")
}
```

## Teleport

<CodeHeader>Custom Component</CodeHeader>

```js
onConsume({ source }) {
source.teleport({ x: 100, y: 20, z: 786 });
}
```

## Transform Item

```js
import { EquipmentSlot, ItemStack } from "@minecraft/server";
```

<CodeHeader>Custom Component</CodeHeader>

```js
onUse({ source }) {
const equippable = source?.getComponent("minecraft:equippable");
if (!equippable) return;

const mainhand = equippable.getEquipmentSlot(EquipmentSlot.Mainhand);

mainhand.setItem(new ItemStack("minecraft:suspicious_stew"));
}
```
1 change: 0 additions & 1 deletion docs/items/troubleshooting-items.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
title: Troubleshooting Items
category: General
nav_order: 4
tags:
- help
mentions:
Expand Down