sirokuma-blog

sirokuma-blog

最近はskyblockのクソみたいなまとめ記事しか書いていません

「Minecraft」ver:1.13〜 コマンドでのmobのHPゲージ(体力表示)の作り方

f:id:skyblue_000:20190620191219p:plain


前置きはいらないのでさっさと本編書いてきます。

適当すぎたのでいつか直します

 

作成

仕組み

ダメージを受けたMobいる?

⬇︎

いた場合、そのMobの最大HPと現在のHPから割合を計算。

⬇︎

割合に応じて、Mobの名前を変更する。 (よって、mobに名前を付けても上書きされます)

 

HPに減るにつれ、ゲージが緑から黄色、赤へと

変化してきます。

 

最初にやること

/scoreboard objectives add hp dummy

/scoreboard objectives add max_hp dummy

/scoreboard objectives add mob_hp_keep dummy

/scoreboard players set 10 hp 10

Functionに書く

functionにコマンドを書いていきます。

コマンドブロックに書いたら負荷がものすごくなってしまったので

書きません。

作り方は割愛。

フォルダー📂はmobhpという名前です。

そのfunctionsの中にcommon.mcfunctionとhp.mcfunctionを作ります

(名前は何でもいいです)

common

execute if entity @e[nbt={HurtTime:10s}] run function mobhp:hp

hp

 

execute as @e[nbt={HurtTime:10s}] at @s store result score @s max_hp run data get entity @s Attributes[0].Base
execute as @e[nbt={HurtTime:10s}] at @s store result score @s hp run data get entity @s Health
execute as @e[nbt={HurtTime:10s}] at @s run scoreboard players operation @s mob_hp_keep = @s hp
execute as @e[nbt={HurtTime:10s}] at @s run scoreboard players operation @s mob_hp_keep *= 10 hp
execute as @e[nbt={HurtTime:10s}] at @s run scoreboard players operation @s mob_hp_keep /= @s max_hp

execute as @e[nbt={HurtTime:10s}] run execute if score @s mob_hp_keep matches 1 run data merge entity @s {CustomName:"{"text":"■□□□□□□□□□","color":"red"}"}
execute as @e[nbt={HurtTime:10s}] run execute if score @s mob_hp_keep matches 2 run data merge entity @s {CustomName:"{"text":"■■□□□□□□□□","color":"red"}"}
execute as @e[nbt={HurtTime:10s}] run execute if score @s mob_hp_keep matches 3 run data merge entity @s {CustomName:"{"text":"■■■□□□□□□□","color":"red"}"}
execute as @e[nbt={HurtTime:10s}] run execute if score @s mob_hp_keep matches 4 run data merge entity @s {CustomName:"{"text":"■■■■□□□□□□","color":"yellow"}"}
execute as @e[nbt={HurtTime:10s}] run execute if score @s mob_hp_keep matches 5 run data merge entity @s {CustomName:"{"text":"■■■■■□□□□□","color":"yellow"}"}
execute as @e[nbt={HurtTime:10s}] run execute if score @s mob_hp_keep matches 6 run data merge entity @s {CustomName:"{"text":"■■■■■■□□□□","color":"yellow"}"}
execute as @e[nbt={HurtTime:10s}] run execute if score @s mob_hp_keep matches 7 run data merge entity @s {CustomName:"{"text":"■■■■■■■□□□","color":"green"}"}
execute as @e[nbt={HurtTime:10s}] run execute if score @s mob_hp_keep matches 8 run data merge entity @s {CustomName:"{"text":"■■■■■■■■□□","color":"green"}"}
execute as @e[nbt={HurtTime:10s}] run execute if score @s mob_hp_keep matches 9 run data merge entity @s {CustomName:"{"text":"■■■■■■■■■□","color":"green"}"}
execute as @e[nbt={HurtTime:10s}] run execute if score @s mob_hp_keep matches 10 run data merge entity @s {CustomName:"{"text":"■■■■■■■■■■","color":"green"}"}

 

 

https://ux.getuploader.com/skyblue_/download/1

上記のサイトからダウンロードしてdatapacksフォルダーにいれると

動きます。

「Minecraft」シロクマのコマンドメモ

自分用です。

指定した名前,idのアイテムを持っているのを検知

/execute  if entity @p[nbt={SelectedItem:{id:"minecraft:アイテムID",tag:{"display":{"Name":"{\"text\":\"名前\"}"}}}}]

指定したLoreのアイテムを持っているか

/execute if entity @p[nbt={SelectedItem:{tag:{display:{Lore:["{\"text\":\"LORE\"}"]}}}}]

どの行に書いてあっても検知される。

Lore追加

/data modify entity @e[type=item,limit=1] Item.tag.display.Lore prepend value "{\"text\":\"LORE\"}"

Loreを行で分ける

/give @p stone{display:{Lore:["{\"text\":\"1\"}","{\"text\":\"2\"}","{\"text\":\"3\"}","{\"text\":\"4\"}"]}}

f:id:skyblue_000:20181124141713p:plain

Mobの方を向いているか検知

/execute as @e[tag=TEST] at @s rotated as @p positioned ^ ^ ^10 facing entity @p eyes positioned ^ ^ ^10 positioned ~ ~ ~ run execute at @e[tag=TEST,distance=..0.4] run ~~~

Mobがダメージを受けたか検知

/execute if entity @e[tag=TEST,nbt={HurtTime:10s}]

プレイヤーがダメージを受けたか検知

execute if entity @p[nbt={HurtTime:9s}] 

nbtを看板に代入(例:Lore)

data modify block ~ ~ ~ Text1 set from entity @s SelectedItem.tag.display.Lore[0]

看板の文字をnbtに代入(例:CustomName)

data modify entity @s CustomName set from block ~ ~ ~ Text1

entityの名前をtellrawで表示

tellraw @a [{"selector":"@s"}]

スコアを看板に代入(@sのtestの値を代入)

data modify block ~ ~ ~ Text1 set value "{\"score\":{\"name\":\"@s\",\"objective\":\"test\"}}"

矢のparticleを消す

{Color:-1}

手持ちのアイテムのdamege値にスコアを代入

/execute store result entity @s SelectedItem.tag.Damage byte 1 run scoreboard players get Damage number

 手持ちのアイテムのdamage値をセット

 /data modify entity @s SelectedItem.tag.Damage set value 10

 

 

 

 

 

 

Minecraft(ver:1.13) HPの割合表示

※追記※

軽量化のためにプレイヤーがダメージを受けたことを検知→割合計算→アイテムセット とかの方がいいかもしれないです。

作成

最初にやること

/scoreboard objectives add number dummy

number

/scoreboard players set #20 number 20

 

割合を算出する

今のHP*20/最大HP をして割合を求めます。

  1. scoreboard players operation max_hp_keep number = max_hp number
  2. scoreboard players operation now_hp_keep number = now_hp number
  3. scoreboard players operation now_hp_keep number *= #20 number
  4. scoreboard players operation now_hp_keep number /= max_hp_keep number
  5. scoreboard players operation hp_ratio number = now_hp_keep number

全て常時実行です。

 

HPの増減をする

次は、計算してでた割合によってオフハンドにアイテムをセットしてHPを増やしたり減らしたりします。

  1. execute if score hp_ratio number matches 1 run replaceitem entity @p weapon.offhand minecraft:carrot_on_a_stick{AttributeModifiers:[{AttributeName:"generic.maxHealth",Name:"generic.maxHealth",Amount:-19,Operation:0,UUIDLeast:531231,UUIDMost:460930}]} 1
  2. execute if score hp_ratio number matches 2 run replaceitem entity @p weapon.offhand minecraft:carrot_on_a_stick{AttributeModifiers:[{AttributeName:"generic.maxHealth",Name:"generic.maxHealth",Amount:-18,Operation:0,UUIDLeast:531232,UUIDMost:460930}]} 1
  3. execute if score hp_ratio number matches 3 run replaceitem entity @p weapon.offhand minecraft:carrot_on_a_stick{AttributeModifiers:[{AttributeName:"generic.maxHealth",Name:"generic.maxHealth",Amount:-17,Operation:0,UUIDLeast:531233,UUIDMost:460930}]} 1
  4. execute if score hp_ratio number matches 4 run replaceitem entity @p weapon.offhand minecraft:carrot_on_a_stick{AttributeModifiers:[{AttributeName:"generic.maxHealth",Name:"generic.maxHealth",Amount:-16,Operation:0,UUIDLeast:531234,UUIDMost:460930}]} 1
  5. execute if score hp_ratio number matches 5 run replaceitem entity @p weapon.offhand minecraft:carrot_on_a_stick{AttributeModifiers:[{AttributeName:"generic.maxHealth",Name:"generic.maxHealth",Amount:-15,Operation:0,UUIDLeast:531235,UUIDMost:460930}]} 1
  6. execute if score hp_ratio number matches 6 run replaceitem entity @p weapon.offhand minecraft:carrot_on_a_stick{AttributeModifiers:[{AttributeName:"generic.maxHealth",Name:"generic.maxHealth",Amount:-14,Operation:0,UUIDLeast:531236,UUIDMost:460930}]} 1
  7. execute if score hp_ratio number matches 7 run replaceitem entity @p weapon.offhand minecraft:carrot_on_a_stick{AttributeModifiers:[{AttributeName:"generic.maxHealth",Name:"generic.maxHealth",Amount:-13,Operation:0,UUIDLeast:531237,UUIDMost:460930}]} 1
  8. execute if score hp_ratio number matches 8 run replaceitem entity @p weapon.offhand minecraft:carrot_on_a_stick{AttributeModifiers:[{AttributeName:"generic.maxHealth",Name:"generic.maxHealth",Amount:-12,Operation:0,UUIDLeast:531238,UUIDMost:460930}]} 1
  9. execute if score hp_ratio number matches 9 run replaceitem entity @p weapon.offhand minecraft:carrot_on_a_stick{AttributeModifiers:[{AttributeName:"generic.maxHealth",Name:"generic.maxHealth",Amount:-11,Operation:0,UUIDLeast:531239,UUIDMost:460930}]} 1
  10. execute if score hp_ratio number matches 10  run replaceitem entity @p weapon.offhand minecraft:carrot_on_a_stick{AttributeModifiers:[{AttributeName:"generic.maxHealth",Name:"generic.maxHealth",Amount:-10,Operation:0,UUIDLeast:531230,UUIDMost:460930}]} 1
  11. execute if score hp_ratio number matches 11  run replaceitem entity @p weapon.offhand minecraft:carrot_on_a_stick{AttributeModifiers:[{AttributeName:"generic.maxHealth",Name:"generic.maxHealth",Amount:-9,Operation:0,UUIDLeast:531231,UUIDMost:160930}]} 1
  12. execute if score hp_ratio number matches 12  run replaceitem entity @p weapon.offhand minecraft:carrot_on_a_stick{AttributeModifiers:[{AttributeName:"generic.maxHealth",Name:"generic.maxHealth",Amount:-8,Operation:0,UUIDLeast:531231,UUIDMost:260930}]} 1
  13. execute if score hp_ratio number matches 13  run replaceitem entity @p weapon.offhand minecraft:carrot_on_a_stick{AttributeModifiers:[{AttributeName:"generic.maxHealth",Name:"generic.maxHealth",Amount:-7,Operation:0,UUIDLeast:531231,UUIDMost:360930}]} 1
  14. execute if score hp_ratio number matches 14  run replaceitem entity @p weapon.offhand minecraft:carrot_on_a_stick{AttributeModifiers:[{AttributeName:"generic.maxHealth",Name:"generic.maxHealth",Amount:-6,Operation:0,UUIDLeast:531231,UUIDMost:560930}]} 1
  15. execute if score hp_ratio number matches 15  run replaceitem entity @p weapon.offhand minecraft:carrot_on_a_stick{AttributeModifiers:[{AttributeName:"generic.maxHealth",Name:"generic.maxHealth",Amount:-5,Operation:0,UUIDLeast:531231,UUIDMost:660930}]} 1
  16. execute if score hp_ratio number matches 16  run replaceitem entity @p weapon.offhand minecraft:carrot_on_a_stick{AttributeModifiers:[{AttributeName:"generic.maxHealth",Name:"generic.maxHealth",Amount:-4,Operation:0,UUIDLeast:531231,UUIDMost:760930}]} 1
  17. execute if score hp_ratio number matches 17  run replaceitem entity @p weapon.offhand minecraft:carrot_on_a_stick{AttributeModifiers:[{AttributeName:"generic.maxHealth",Name:"generic.maxHealth",Amount:-3,Operation:0,UUIDLeast:531231,UUIDMost:860930}]} 1
  18. execute if score hp_ratio number matches 18  run replaceitem entity @p weapon.offhand minecraft:carrot_on_a_stick{AttributeModifiers:[{AttributeName:"generic.maxHealth",Name:"generic.maxHealth",Amount:-2,Operation:0,UUIDLeast:531231,UUIDMost:960930}]} 1
  19. execute if score hp_ratio number matches 19  run replaceitem entity @p weapon.offhand minecraft:carrot_on_a_stick{AttributeModifiers:[{AttributeName:"generic.maxHealth",Name:"generic.maxHealth",Amount:-1,Operation:0,UUIDLeast:531231,UUIDMost:120930}]} 1
  20. execute if score hp_ratio number matches 20 run replaceitem entity @p weapon.offhand minecraft:carrot_on_a_stick{AttributeModifiers:[{AttributeName:"generic.maxHealth",Name:"generic.maxHealth",Amount:0,Operation:0,UUIDLeast:531231,UUIDMost:230930}]} 1

これもまた全て常時実行です。

actionbarに表示させる

/title @p actionbar ["",{"text":"HP","bold":true,"color":"green"},{"text":":","bold":true},{"score":{"name":"now_hp","objective":"number"},"bold":true,"color":"green"},{"text":"/","bold":true},{"score":{"name":"max_hp","objective":"number"},"bold":true,"color":"green"}]

最後に..

HPを割合表示することで、様々な事が出来るようになりついでにMobのHPをスコア化してしまえばより楽しくなるでしょう。