How to Create Custom Right-Click Actions for Any Item in Minecraft Bedrock (Script API Tutorial)

Introduction


Due to the recent removal of the “Holiday Creator Features,” it is no longer possible to control the behavior of original items using json files. Therefore, if you want to create usable items, you will need to use the Script API.
ScriptAPI is a feature that allows you to customize the game system using a programming language called JavaScript. For more information about this update, please see this article.

【マイクラ統合版】ver1.21.20のアップデートで「ホリデークリエイターの特徴」が削除されました – 影響と今後のアドオンについて解説
はじめに タイトルにある通り、今回の1.21.20のアップデートは衝撃的です。 なんと、実験的な機能の「ホリデークリエイターの特徴」が削除され、アドオンの制作が困難になったのです。本記事では、具体的にどのように困難になったのか、今後のアドオン開発はどうなるのかについて解説します。 本記事の内容は、こちらのYoutube動画でもまとめてありますので、こちらも併せてご覧ください。 https://youtu.be/darWNvpmynE 解説 まず、今回のアップデートであるver1.21.20の概要についてお話しします。 アップデートの内容 アップデートの内容については公式が言及しています。英語の記事ですが、こちらをご覧いただくと大枠は分かるかと思います。 https://www.minecraft.net/ja-jp/article/minecraft-1-21-20-bedrock-changelog ぱっと見、バンドルが追加されただとか、バグがなくなっただとか良い内容が目につきます。実際、巷のマイクラYoutuberのほとんどがこの良い部分のみに注目しています。おそらく多くのマインクラフトプレイヤーは、本バージョンのこうした良い部分のみに注目することでしょう。 しかし、以下の部分については聞捨てなりません: https://www.minecraft.net/ja-jp/article/minecraft-1-21-20-bedrock-changelogより引用いたします。 これによると、「ホリデークリエイターの特徴を削除した」ことが分かります。それにより、この実験的な機能に依存していた旧来のアドオンは悉く機能しなくなります。 影響 「どうせ、実験的な機能なんでしょ?」 そう侮ってはいけません。この変更でオリジナルアイテム制作に大きな影響が与えられます。というのも、旧来のオリジナルアイテムはこの「ホリデークリエイターの特徴」に依存していました。この機能がなくなることで、今後はオリジナルアイテムの制作が大変困難になります。オリジナルアイテムについてはこちらも併せてご覧ください: https://lemon-slime.com/?p=455 逆に無事なのは、「APIスクリプト」です。この記事の読者ならご存じでしょうが、APIスクリプトとはマインクラフトの統合版でプログラミング(Javascript)を用いてゲームシステムを改変することができる機能のことです。 https://lemon-slime.com/?p=935 また、意外ですが「何もしないアイテム」は依然制作可能です。これはホリデークリエイターの特徴に依存せず、format_versionを1.20.0に変更することでこれまでと同じように機能します。 一方で、使うことができるアイテムなど、何かしらの機能をもつアイテムは制作することが非常に困難になります。このアップデート以降でそのようなアイテムを作る方法については次章で解説します。 今後のアドオン制作について 武器や食べ物といった、特殊なアイテムについては制作方法が若干変更になるものの、依然制作が可能です。詳細についてはこちらの記事が詳しいです。 https://lemon-slime.com/?p=1109 一方で、eventsなどに依存していた、「使うことができるアイテム」については少々事情が異なります。というのも、前述のように、「APIスクリプト」は動作するので、必然的に「APIスクリプト」をベースとしたアドオン開発に移行することになります。 例えば、先ほど「使うことができるアイテム」は制作が困難だと申し上げました。「制作できない」ではなく「困難」と言ったのは、厳密には「APIスクリプト」を用いてアイテムの挙動の制御ができるからです。 例えば、アイテムを使用したときに発火する関数: function kansu(item,command){ server.world.beforeEvents.itemUse.subscribe(ev => { if (ev.itemStack.typeId == item){ ev.source.runCommand(command); } }); } コピー を定義するといった方法が考えられますね。 また、公式のアナウンスにもありますが、「カスタムコンポーネント(Custom component)」を用いるのも一つの方法です。こちらもAPIスクリプトに依存し、アップデート後でも動作可能です。カスタムコンポーネントについての詳細は、以下の記事で解説しています: https://lemon-slime.com/?p=2138 おしまい このように、今回のマインクラフト1.21.20のアップデートは、我々アドオンクラフターに激震を与えました。「郷に入れば郷に従え」という言葉の通り、我々はただ開発元に従うほかないのですが、今回のようなやり方には些か不満を感じるのは私だけでしょうか?

However, using ScriptAPI requires some prior preparation and basic knowledge. If you are not familiar with ScriptAPI, we recommend that you read this article before reading this one.
【マイクラ統合版】ScriptAPIのテンプレートを配布 – 使い方と基礎知識を解説
【マイクラ統合版】ScriptAPIのテンプレートを配布 – 使い方と基礎知識を解説
ダウンロードはこちら https://lemon-slime.com/resources/scriptAPI_template.zip 概要 突然ですが、皆さんは「スクリプトAPI」をご存じでしょうか? 「スクリプトAPI」とは、マインクラフトの統合版においてプログラミング言語(Javascript)を用いてゲームシステムをカスタマイズできる機能のこと。Javascriptの知識さえあれば、マインクラフトのシステムをある程度いじることができるんです。普通のゲームはそんなことできません。マインクラフトのこういうところが私は好きです。以下は「スクリプトAPI」の一例です。 ちなみに、ScriptAPIはアドオンの一部です しかし、このスクリプトAPIの使用にはある程度の事前準備が必要です。この点で、初心者には一定のハードルがあり、この事前準備の段階で躓いてしまう人も存在するものとみられます。これは非常に勿体ない。 本記事では、「スクリプトAPI」のテンプレートを配布することで、この事前準備の段階を飛ばし、すぐにコードを書き始められるようにすることを目的としています。ダウンロードは上のリンクをクリックすることで可能です。ご自由にお使いください。 一応、環境構築についてはこちらの記事で詳しく説明しています 使い方 まず、上記のzipファイルをダウンロードします、。その後、com.mojangフォルダ内のdevelopment_behavior_packs内に適当なフォルダー作成し、その中にテンプレートのファイルをすべてコピーアンドペーストしてください。フォルダcom.mojangは C:\Users\(ユーザー名)\AppData\Local\Packages\Microsoft.MinecraftUWP_8wekyb3d8bbwe\LocalState\games\com.mojang に存在します。エクスプローラー上で、「AppData」というファイルは隠しファイルなので、これを選択するためには、エクスプローラーの「表示」設定から隠しファイルのチェックをオンにしてください。または、「ファイル名を指定して実行」というアプリを起動し、「%LocalAppData%\Packages\Microsoft.MinecraftUWP_8wekyb3d8bbwe\LocalState\games\com.mojang」と入力することでも開けます。 展開が完了したら、マインクラフトを起動してみてください。ワールド起動時に、ビヘイビアパックの一覧を確認してみてください。その一つに「ScriptAPIのテンプレート」があれば成功です。 Youtube動画もあります 本記事の内容について詳細に解説したYoutube動画を作成しました。こちらも併せてご覧いただくと、APIスクリプトに関してより理解が深まります。 https://youtu.be/331LmvE2Cdk 動画内でいくつかスクリプトが登場しますが、以下にその内容を記載しておりますので、コピー&ペーストしてご利用ください。 スクリプトを記述 さて、基本的な準備は整いました。あとは、オリジナルのコードを書いていくだけです。 先ほどダウンロードしたScriptAPI_templateフォルダ内のscriptsフォルダ内にある、main.jsを開きましょう。メモ帳などのテキストエディタであれば大丈夫です(VSCodeなどの専門のテキストエディタがあるならそれを使うことをお勧めします)。 すると、おそらくこのように表示されることでしょう。 import * as server from '@minecraft/server'; //ここにスクリプトを記述 コピー Copied! 最初の1行目は、「minecraft/serverモジュールを使用する」ことを表しています。これによってマインクラフトの世界に干渉できるようになるので、ScriptAPIの記述においてこれは必須です。 「ここにスクリプトを記述」とあるところにスクリプトを実際に書いていきます(これはコメントなので消しても残してもどちらでも良いです)。実際に、スクリプトを書いてみましょう。試しに以下をコピー&ペーストしてみてください。 server.system.runInterval(ev => { server.world.sendMessage("Hello!"); }) コピー Copied! スクリプト全体は以下のようになります。 これを保存し、マインクラフトを起動しましょう。そして、「ScriptAPIのテンプレート」アドオンを有効化し、ワールドを作成します。すると、毎ティック「Hello!」と表示されるようになったのが分かるでしょうか?これが「スクリプトAPI」の効果です。 このようにして「スクリプトAPI」は動作します。 同様にして、「アイテムを使ったらコマンドが発動するスクリプト」を作ってみましょう。テンプレートはこんな感じです。 server.world.beforeEvents.itemUse.subscribe(ev => { if (ev.itemStack.typeId ==…

Below, we will assume you are familiar with APIScript and explain how to make any item available.

Character
This may be the most basic of basics when working with ScriptAPI.

overview


If you are using the SciptAPI template, open main.js in the script folder. First, write the import statement as usual (this step is not necessary if this statement already exists).

import * as server from '@minecraft/server';


This completes the declaration of the variable server that defines the Minecraft module. We will use this server below.

Write a script


Now it’s time to write the script. To make any item available, follow the steps below to implement it.

  • Detect when an item is used
  • If a specific item is used, it will execute commands under the user.


For example, “Use a wooden stick to obtain diamonds,” would look like this:

server.world.beforeEvents.itemUse.subscribe(ev => {
    if (ev.itemStack.typeId == "minecraft:stick"){
        ev.source.runCommandAsync("give @a diamond 1");
    }
})
Character
Instead of “beforeEvents”, “afterEvents” can be also used.


Copy and paste this text directly below the import statement above.
This script itself has appeared in a previous article, but in this article we will provide a detailed explanation of the script.

Commentary


The Minecraft module has a beforeEvents (or afterEvents) class in the world class. This triggers various events in the world and invokes functions when an event occurs. This is very commonly used in the Script API, so it’s important to remember it. In this example, we’re using the itemUse class within it to detect when an item has been used and fire a function. The subscribe() function promises to execute a certain function. The content of the function is written within these parentheses.
As shown above, the name of the function is ev (the initial letter of “event”).
The ev.itemStack.typeId in the if statement is the id of the used item (String type). In this case, if this is equal to minecraft:stick, it will return true and proceed to the next step.
ev.source is the entity that caused the event. In this case, it’s the player who used the stick. Under this entity, execute the command with the runCommandAsync() function. In this case, we used the give command. This will give diamonds when the stick is used.
You can customize it to your liking by changing the “minecraft:stick” and “give @a diamond 1” parts above. This allows you to make any item available for use.

Implemented cooldown


This article doesn’t end here, because after reading the comments on the YouTube video, I felt there was a demand for cooldowns, so in this article I’ll explain how to implement cooldowns on more usable items.
To begin with, a “cooldown” is a mechanism that means “once you’ve used an item, you can’t use it again for a certain period of time.” To implement this, you can take the following approaches:

  • When using an item, use the scoreboard command to give the user a score of (cooldown time [seconds]) x 20.
  • Use runInterval or tick.json to decrease the score by 1 every tick (1/20th of a second).
  • When using an item, read the player’s score, and if the score is 0, execute the command, otherwise do not execute it.


This time, let’s consider a method using the runInterval() function. We will explain tick.json in a separate video and article. The script should look something like this:

let cooldown_time = 3
let item_name = "minecraft:stick"
let command = "say hello!"
server.world.beforeEvents.itemUse.subscribe(ev => {
    if (ev.itemStack.typeId == item_name){
        ev.source.runCommandAsync("execute as @s[scores={test_score=0}] run " + command);
        ev.source.runCommandAsync("execute as @s[scores={test_score=0}] run scoreboard players set @s test_score " + cooldown_time*20);
    }
});
server.system.runInterval(ev => {
    for (const player of server.world.getAllPlayers()){
         player.runCommandAsync("scoreboard objectives add test_score dummy");
         player.runCommandAsync("scoreboard players remove @s[scores={test_score=0..}] test_score 1");
         player.runCommandAsync("scoreboard players set @s[scores={test_score=..-1}] test_score 0");
    }
}
);


This is an example of executing the command “say hello!” when using a wooden stick. The cooldown time is 3 seconds, so the command will not be executed until 3 seconds have passed since the last time it was used.
Feel free to change the cooldown_time, item_name, and command above to create your own arrangement.

Commentary


This script is divided into two main parts. The first half defines the function that fires when an item is used, and is not much different from the previous script. The only difference is that when an item is used, the scoreboard command is used to add to the score.
The second half defines a function that is always executed. Here, it mainly uses the scoreboard command to decrease the score by 1 every tick.

Restrict Use


Finally, I’ll explain how to restrict the use of items. Up until now, right-clicking on an item always invoked a command. However, in some cases, you may want to execute a command only under special conditions. Or, you may want a single item to have two functions (in fact, I often find myself doing this). Assuming such a situation, here is an example of activating a command when the player is in a certain condition.

server.world.beforeEvents.itemUse.subscribe(ev => {
    if (ev.itemStack.typeId == "minecraft:stick"){
        if (ev.source.isSneaking == true){
            ev.source.runCommandAsync("say Hello!");
        }
    }
});


In this example, if you sneak while using a stick, the command “say Hello!” will be activated. In this way, you can also set conditions for using items.
You can customize it to your liking by changing the IDs and commands of the above items.
Similarly, by setting the process for when you are not sneaking, you can equip one item with two functions. Below is an example.

server.world.beforeEvents.itemUse.subscribe(ev => {
    if (ev.itemStack.typeId == "minecraft:stick"){
        if (ev.source.isSneaking == true){
            ev.source.runCommandAsync("say Hello!");
        }else{
            ev.source.runCommandAsync("say I'm standing!");
        }
    }
});


This is an example where the item says “Hello!” when sneaking, and “I’m standing!” when not. In this way, by using conditional branching with if~else~ statements, you can give an item multiple functions.
Condition Examples
Here, we checked whether the object is sneaking or not. This is because the isSneaking property returns true if the object is sneaking, and false if it is not. The main properties that have this property are as follows:

  • isInWater
  • isOnGround
  • isSleeping
  • isSneaking
  • isSwimming
  • isFlying
  • isJumping


You can combine these to create unique items.

Apply the script


As mentioned above, the Script API is based on Javascript, so understanding Javascript will make writing scripts easier. Naturally, knowledge of Javascript will also be essential when arranging the script mentioned above. Here we will introduce a good book to help you master Javascript.

created by Rinker
¥3,278 (2025/10/18 11:17:17時点 楽天市場調べ-詳細)

The official documentation is a textbook on the scripting API. It is very helpful, so it’s definitely worth taking a look at (note that it’s only available in English).
https://learn.microsoft.com/ja-jp/minecraft/creator/scriptapi/minecraft/server/minecraft-server?view=minecraft-bedrock-experimental

The end


This article explains how to use ScriptAPI to make items usable. Please also refer to the following article for more information on ScriptAPI.

【Minecraft統合版】よく使うAPI Scriptのフレーズ 5選
はじめに みなさん、こんにちは!突然ですが、「API Script」をご存じでしょうか? 「API Script」というのは、マインクラフトの統合版において、プログラミング言語(Javascript)を用いてゲームシステムをカスタマイズできる機能のこと。Javascriptの知識さえあれば、マインクラフトのシステムをある程度いじることができるんです。普通のゲームはそんなことできません。マインクラフトのこういうところが私は好きです。 これは、APIスクリプト(ScriptAPI)の一例で、「ジャンプしたらkillコマンドが発動する」ことを意味しています。このようにしてゲームシステムに直接干渉するわけですね。 さて、本稿では、この「API Script」を使ったアドオンの制作において、ある程度よく用いられるフレーズを備忘録もかねて5つまとめてみたいと思います。あくまで「経験上」よく使われるか否かが論点であり、主観が多分に含まれていることに予めご了承ください。 個人的によく使うものをまとめてみました APIスクリプトの基本 本記事ではAPIスクリプトの基本事項については触れません。APIスクリプトの概要や基本事項、事前準備などについてはこちらの記事で詳細にまとめております。APIスクリプトの基本的な情報を知りたい方はまずこちらの記事を参照し、後で本記事に戻ることをお勧めします。 https://lemon-slime.com/?p=1210 以下では、APIスクリプトの基本を理解なさっていて、事前準備が完了していることを前提にお話しします。 本題 それでは、APIスクリプトの記述においてよく使われるフレーズをまとめます。 import文 API Scriptはimport文から始まります。これがなければただのjsファイルです。具体的には: import * as server from '@minecraft/server'; コピー のように記述し、これによって、マインクラフトの世界に干渉できるようになります。今のは一例で、人によっては、 from '@minecraft/server' import {world,system}; コピー のように書くこともあるらしいですね。好みが別れるところです。 言うまでもないですが、ここで同時に変数の宣言を行っています。以下で「server」という変数が突拍子もなく出てきますが、ここで宣言されたものです。 毎ティック実行 コマンドブロックに「常時実行」というモードがありますよね。これと同じように、常時プログラムを動かしたいときに重宝する関数があります。 server.system.runInterval(() => { //ここに内容を記述 }); コピー それがsystemクラスのrunInterval()です。これは、関数と間隔(int)を引数とすることで、名前の通り、一定周期で関数を実行することができます。上のように間隔を明記しない場合は常に実行します。常にプレイヤーの動きを監視するのにも使えます。ジャンプしたらkillされるAPI Script: にもこの関数が使われていますね。ちなみに、「常時実行」と言えば、functionsファイル内のtick.jsonを想起させます。ご存じのない方に向けて一応説明しますと、tick.jsonはワールド内で常時実行される関数スクリプト(一連のコマンド群で、mcfunction形式で記述されたもの)を定義します。このtick.jsonを用いることでも簡単に常時実行を達成することはできますが、こちらはあらかじめ定義されたコマンドしか使えないので一長一短です。必要に応じてrunInterval()とtick.jsonを使い分ける感じですね。 各プレイヤーについて実行 マインクラフトの主人公は、プレイヤーです。このプレイヤーを主体として何かを実行したいときもありますよね。そんな時に重宝するフレーズがあります。 for (const player of…

Also, this article provides more detailed information on the “Reverse ScriptAPI”:
逆引きScriptAPI – よく使う関数&スクリプト集【マイクラ統合版】
逆引きScriptAPI – よく使う関数&スクリプト集【マイクラ統合版】
はじめに 本ページは、ScriptAPIにおいてよく使う関数やスクリプトをまとめることで、コピーしてすぐに使えるようにすることが目的です。もし、「○○がしたいんだけど、どうすればいいの?」、「このスクリプト、よく使うんだけど載ってない」といったご意見・疑問点がありましたら、コメント欄より教えてください。随時追加していきます。 「そもそもScriptAPIって何ぞや」という方は、まずはこちらをご覧ください: https://lemon-slime.com/?p=1210 結構主観が入っています 手に持っているアイテムを取得 let item = player.getComponent("inventory").container.getItem(player.selectedSlotIndex); コピー Copied! この関数を使う機会は結構な頻度であると思います。特に、あるアイテムを所持している時に関数を発火させたり、特定の武器で敵を攻撃したときにエフェクトを付与したりするなど、ゲームシステムを改変するにあたってはこの関数の存在は見逃せません。 この関数の使用にあたっては、下の例で示す通り、playerクラスの変数が必要です。 使用例 server.world.afterEvents.entityHitEntity.subscribe(ev => { let player = ev.damagingEntity; if (player.typeId == "minecraft:player"){ let weapon = player.getComponent("inventory").container.getItem(player.selectedSlotIndex); if(weapon == undefined){return;} if(weapon.typeId == "minecraft:diamond_sword"){ player.sendMessage("ダイアモンドの剣を使いました"); } } }) コピー Copied! この例では、プレイヤーが敵に攻撃を当てたときに発火する関数を定義しています。関数内で手に持っている武器を検知し、それがダイアモンドの剣だった場合に「ダイアモンドの剣を使いました」というメッセージを表示しています。 例を見れば明らかですが、weaponがundefinedの場合を明確に区別し、その場合は関数から離脱するようにしています。特に素手であった場合にはweaponがundefinedとなるため、この設定がないと往々にしてエラーを吐く点には注意しましょう。 エラーが出てるとプレイ画面が見づらくなるからね... 現在の体力を検知 let health = player.getComponent("minecraft:health").currentValue;…
最新情報をチェックしよう!