![](https://stat.ameba.jp/user_images/20220412/22/sasamiyu58/d5/f1/p/o0231016915102021173.png?caw=800)
以前、マインクラフトの雪玉に炎魔法や氷魔法、雷魔法、風魔法などの魔法が使えるようになるコマンドを紹介しましたが、魔法の属性を変えたいときは毎回、コマンドブロックを有効にしたり、無効にしないと切り替えることができませんでした
![悲しい](https://stat100.ameba.jp/blog/ucs/img/char/char4/596.png)
今回は新たに追加された
hasitemを使ってスロットごとに雪玉の魔法属性を簡単に切り替えれる方法を紹介します
![ニコニコ](https://stat100.ameba.jp/blog/ucs/img/char/char4/590.png)
コマンドで雪玉に色んな魔法をかける記事
hasitemって?
hasitemはマインクラフト バージョン1.18.20から新たに追加されました。セレクターにhasitemを使うと、より細かくプレイヤーが特定のアイテムを持っているか条件を絞り込むことができます![ひらめき](https://stat100.ameba.jp/blog/ucs/img/char/char4/591.png)
hasitem使用例
その1、プレイヤーがダイヤモンドのツルハシを持っていたらエンチャントされた金のリンゴをプレゼント
/give @p[hasitem={item=diamond_pickaxe,location=slot.weapon.mainhand}] appleenchanted
![](https://stat.ameba.jp/user_images/20220417/23/sasamiyu58/01/cf/p/o1080052915104468009.png?caw=800)
その2、プレイヤーがダイヤモンドのヘルメットを装備していたら水中呼吸のエフェクトがつく
/effect @p[hasitem={item=diamond_helmet,location=slot.armor.head}] water_breathing 10000 100 true
![](https://stat.ameba.jp/user_images/20220417/23/sasamiyu58/30/32/p/o1080053715104468012.png?caw=800)
その3、プレイヤーの持ち物のスロット4に金の剣を持っていたら物理攻撃力をアップする
/effect @p[hasitem={item=golden_sword,location=slot.hotbar,slot=4}] strength 10000 100 true
![](https://stat.ameba.jp/user_images/20220417/23/sasamiyu58/02/a1/p/o1080053715104468018.png?caw=800)
その4、プレイヤーがエメラルドを5個以上持っていたら、ケーキをゲットする
/give @p[hasitem={item=emerald,quantity=5..}] cake
![](https://stat.ameba.jp/user_images/20220417/23/sasamiyu58/de/3f/p/o1080013015104468021.png?caw=800)
など、hasitemでプレイヤーが特定のアイテムを持っていた時のみコマンドを実行できるようになります。
コマンドブロックを用意
持ち物のスロット番号ごとに魔法属性のコマンドブロックを用意しました。
雪玉が持ち物のスロット番号の場所によって、炎魔法や氷魔法、雷魔法など魔法の属性を切り替えれるようになっています。
コマンドブロックは各魔法属性につき1個ずつ用意しています。
※今回は魔法の視覚的効果(パーティクル)は省略しています。
![](https://stat.ameba.jp/user_images/20220412/22/sasamiyu58/8e/11/p/o0860060815102025358.png?caw=800)
コマンドブロックの設定
コマンドブロックは全て
反復、無条件、常にアクティブになっています。
hasitemでスロット指定
スロット番号0番目に雪玉を持っていたら、炎魔法を実行すると書いてあります。
持ち物のスロット番号は0から始まります。
@e[hasitem={item=snowball,location=slot.hotbar,slot=0}]
スロット番目0 炎魔法
雪玉が当たると炎に包まれます
![](https://stat.ameba.jp/user_images/20220413/23/sasamiyu58/35/49/p/o1080088215102543426.png?caw=800)
execute @e[hasitem={item=snowball,location=slot.hotbar,slot=0}] ~ ~ ~ execute @e[type=snowball] ~ ~ ~ execute @e[type=!player,type=!snowball,r=5] ~ ~ ~ fill ~-1 ~ ~-1 ~1 ~1 ~1 fire
スロット番号1 氷魔法
execute @e[hasitem={item=snowball,location=slot.hotbar,slot=1}] ~ ~ ~ execute @e[type=snowball] ~ ~ ~ execute @e[type=!player,type=!snowball,r=5] ~ ~ ~ fill ~-1 ~ ~-1 ~1 ~1 ~1 ice
スロット番号2 雷魔法
execute @e[hasitem={item=snowball,location=slot.hotbar,slot=2}] ~ ~ ~ execute @e[type=snowball] ~ ~ ~ summon lightning_bolt ~ ~ ~
スロット番号3 風魔法
execute @e[hasitem={item=snowball,location=slot.hotbar,slot=3}] ~ ~ ~ execute @e[type=snowball] ~ ~ ~ effect @e[type=!player,type=!snowball,r=5] levitation 8 5 true
スロット番号4 土魔法
execute @e[hasitem={item=snowball,location=slot.hotbar,slot=4}] ~ ~ ~ execute @e[type=snowball] ~ ~ ~ execute @e[type=!player,type=!snowball,r=5] ~ ~ ~ fill ~-1 ~ ~-1 ~1 ~1 ~1 dirt
スロット番号5 毒魔法
execute @e[hasitem={item=snowball,location=slot.hotbar,slot=5}] ~ ~ ~ execute @e[type=snowball] ~ ~ ~ effect @e[type=!player,type=!snowball,r=5] fatal_poison 10000 60 true
スロット番号6 闇魔法
![](https://stat.ameba.jp/user_images/20220412/22/sasamiyu58/46/1e/p/o0570024415102029141.png?caw=800)
雪玉が当たると敵は徐々に衰弱し死にます
![](https://stat.ameba.jp/user_images/20220413/23/sasamiyu58/71/ee/j/o1080081315102544505.jpg?caw=800)
※イメージ図
execute @e[hasitem={item=snowball,location=slot.hotbar,slot=6}] ~ ~ ~ execute @e[type=snowball] ~ ~ ~ effect @e[type=!player,type=!snowball,r=5] wither 10000 100 true
スロット番号7 回復魔法
![](https://stat.ameba.jp/user_images/20220412/22/sasamiyu58/68/48/p/o0570024415102029143.png?caw=800)
雪玉が味方に当たると回復します
![](https://stat.ameba.jp/user_images/20220413/23/sasamiyu58/26/1a/j/o1080080915102544509.jpg?caw=800)
※イメージ図
execute @e[hasitem={item=snowball,location=slot.hotbar,slot=7}] ~ ~ ~ execute @e[type=snowball] ~ ~ ~ effect @e[type=!snowball,r=5] instant_health 10000 100 true
になります。
hasitemを使うことで持ち物のスロットの番号に雪玉を装備するだけで、簡単に魔法属性を切り替えれるようになりました
![ニコニコ](https://stat100.ameba.jp/blog/ucs/img/char/char4/590.png)
hasitemの使い方次第で色んなことができそうですね
![お願い](https://stat100.ameba.jp/blog/ucs/img/char/char3/025.png)
まだマルチで複数プレイヤーがいたときの検証などしていません
![驚き](https://stat100.ameba.jp/blog/ucs/img/char/char4/619.png)
のイラストを使用しています。いつもありがとうございますm(_ _)m