0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【1.21.4】Spigot+NBT APIでItemStackにコンポーネント「can_break」を設定する

Posted at

1.したいこと

↓これがしたい
/give @p iron_pickaxe[can_break={predicates:[{blocks:"stone"}]}] 1

なんとSpigot APIはcan_break(旧can_destroy)に対応していません! ←なんで?

NMSはさっぱりなので外部ライブラリ「NBT API」を使用した。

2.結論

執筆現在、1.21.5でcan breakのアイテムフォーマットに再度の変更が予定されています。
※Minecraft wiki

サンプルコード(Java)

Java用のコードを用意するのが面倒だったのでChatGPTに変換してもらいました。
一通り目は通してますが動かなかったらごめんなさい

Java by ChatGPT
import java.util.List;
import java.util.ArrayList;
import de.tr7zw.nbtapi.NBT;
import de.tr7zw.nbtapi.ReadWriteNBT;
import de.tr7zw.nbtapi.ReadWriteNBTCompoundList;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;

public class ItemUtils {
    public static ItemStack setCanDestroy(ItemStack itemStack, List<Material> target, boolean showInToolTip) {
        NBT.modifyComponents(itemStack, nbt -> {
            ReadWriteNBTCompoundList predicates = nbt.getOrCreateCompound("can_break").getCompoundList("predicates");
            
            for (Material material : target) {
                predicates.addCompound().setString("blocks", String.valueOf(material.getKey()));
            }
            
            nbt.getOrCreateCompound("can_break").setBoolean("show_in_tooltip", showInToolTip);
        });
        return itemStack;
    }

    public static ItemStack setCanDestroy(ItemStack itemStack, List<Material> target) {
        return setCanDestroy(itemStack, target, false);
    }

    public static ItemStack setCanDestroy(ItemStack itemStack, Material target) {
        List<Material> list = new ArrayList<>();
        list.add(target);
        return setCanDestroy(itemStack, list, false);
    }
}
サンプルコード(Kotlin)
Kotlin
import de.tr7zw.nbtapi.NBT
import de.tr7zw.nbtapi.iface.ReadWriteNBT
import de.tr7zw.nbtapi.iface.ReadWriteNBTCompoundList
import org.bukkit.Material
import org.bukkit.inventory.ItemStack

object ItemUtil {
    fun setCanDestroy(itemStack: ItemStack, target: MutableList<Material>, showInToolTip: Boolean): ItemStack {
        NBT.modifyComponents(itemStack) {nbt: ReadWriteNBT ->
            val predicates: ReadWriteNBTCompoundList =nbt.getOrCreateCompound("can_break").getCompoundList("predicates")

            target.forEach { material: Material ->
                predicates.addCompound().setString("blocks", material.keyOrNull.toString())
            }

            nbt.getOrCreateCompound("can_break").setBoolean("show_in_tooltip", showInToolTip)
        }
        return itemStack
    }

    fun setCanDestroy(itemStack: ItemStack, target: MutableList<Material>): ItemStack {
        return setCanDestroy(itemStack, target, false)
    }

    fun setCanDestroy(itemStack: ItemStack, target: Material): ItemStack {
        return setCanDestroy(itemStack, mutableListOf(target), false)
    }
}

3.参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?