存在しないNBTタグをテストするにはどうすればよいですか?

Aug 24 2020

私には3人の村人がいますが、カスタム名がある場合とない場合があります。

カスタム名はCustomNameNBTタグによって制御されます。

カスタム名の村人の場合、CustomNameタグは次のようになります。

{CustomName:'{"text":"Chad Ferguson"}'}

カスタム名のない村人の場合、CustomNameタグは存在しません。

村人が特定の名前を持っているかどうかをテストするためのコードを次に示しnbtます(引数を使用)。

/execute if entity @e[type=villager,nbt={CustomName:'{"text":"Chad Ferguson"}'}] run tellraw @a {"text":"Chad is here!"}

ここでCustomName、次のような、存在しないタグを持つエンティティをテストしたいと思います。

/execute if entity @e[type=villager,nbt={CustomName:''}] run tellraw @a {"text":"An unknown villager is near"}

現在、既存の空の文字列存在しないタグとは異なるため、このコマンドは機能しません。

この!ため、引数は機能しません。

execute
  if entity @e[type=villager,nbt=!{CustomName:''}]    # If there is an entity that does not have an empty custom name...
run tellraw @a {"text":"An unknown villager is near"} # then say a message.

このコマンドは、タグが存在しない場合、またはタグに空の文字列以外のものがある場合に実行されます''

NBTタグが存在しないエンティティをテストするにはどうすればよいですか?私はMCJE1.16.2にいます。

回答

6 ExpertCoder14 Aug 24 2020 at 08:12

次のコマンドを使用します。

execute
  as @e[type=villager]           # Tell all the villagers to run the following:
  store success score @s nbtTest # Store the success/failure of the final command to run here in a scoreboard.
run data get @s CustomName       # Get my custom name.

これにより、すべての村人が/dataコマンドを実行してカスタム名を取得します。

魔法が起こる場所とここにある:コマンドが渡されたタグが存在する場合、および失敗したことがない場合。そして、成功/失敗をスコアボードに保存しているので、見ることができます。スコアボード値が0のエンティティには、存在しないCustomNameタグがあります。