【統合版解説】カスタムアイテム #5 範囲破壊のツルハシ

スポンサーリンク

#4では銅のツルハシとそのレシピを作成しました。
今回は3×3範囲破壊ができる特殊なツルハシを作成します。

ビヘイビアパック

Group-49.png

custom_item_BP/manifest.jsonは 前々回解説(#2) を参照

custom_item_BP/items/saba_3x3_pickaxe.json

{
    "format_version": "1.21.10",
    "minecraft:item": {
        "description": {
            "identifier": "saba:3x3_pickaxe", // アイテムの識別子
            "menu_category": {
                "category": "Equipment", // アイテムのカテゴリ
                "group": "itemGroup.name.pickaxe" // アイテムのグループ
            }
        },
        "components": {
            "minecraft:display_name": {
                "value": "3x3範囲破壊ツルハシ" // アイテム名
            },
            "minecraft:icon": {
                "textures": {
                    "default": "saba:3x3_pickaxe" // テクスチャ
                }
            },
            "minecraft:damage": {
                "value": 4 // ダメージ
            },
            "minecraft:durability": {
                "max_durability": 150 // 最大耐久値
            },
            "minecraft:repairable":{ // 修理の設定
                "repair_items": [
                    {
                        "items":[
                            "minecraft:netherite_ingot" // ネザライトインゴット
                        ],
                        "repair_amount": 10 // 耐久回復値
                    }
                ]
            },
            "minecraft:max_stack_size": {
                "value": 1 // スタックできる数
            },
            "minecraft:hand_equipped": true, // 持ち方をツールっぽくする
            "minecraft:enchantable": {
                "slot": "pickaxe", // エンチャントの種類
                "value": 10 // エンチャントの可能性
            },
            "minecraft:digger": { // 採掘速度の設定
                "use_efficiency": true, // エンチャントの影響を受ける
                "destroy_speeds": [
                    {
                        "block": {
                            "tags": "q.any_tag("stone", "metal","diamond_pick_diggable")" // 石系を指定
                        },
                        "speed": 6
                    }
                ]
            },
            "minecraft:custom_components": [ // カスタムコンポーネント
                "saba:pickaxe_durability" // ピッケルの耐久値を操作するカスタムコンポーネント
            ]
        }
    }
}

custom_item_BP/scripts/main.js

import { system, world } from "@minecraft/server";

// 耐久値が減るかどうかを返す関数
function itemDamageChance(itemStack) {
    // 耐久力エンチャントを取得
    const enchantableComponent = itemStack.getComponent("enchantable");
    const unbreaking = enchantableComponent.getEnchantment("unbreaking");
    if (unbreaking) {
        // 確率計算
        const chance = 1 / (unbreaking.level + 1);
        return Math.random() <= chance;
    }
    return true;
}

// アイテムの耐久値を減らす関数
function durabilityDamage(player, itemStack, damage) {
    const durabilityComponent = itemStack.getComponent("durability");
    const mainhand = player.getComponent("equippable").getEquipmentSlot("Mainhand"); // メインハンド

    if (itemDamageChance(itemStack)) { // 耐久値が減るかのチェック
        if (durabilityComponent.damage + damage >= durabilityComponent.maxDurability) {
            // アイテム破壊
            mainhand.setItem(undefined);
            player.playSound("random.break");
        } else {
            // 耐久値減少
            durabilityComponent.damage += damage;
            mainhand.setItem(itemStack);
        }
    }
}

// カスタムコンポーネントの登録
world.beforeEvents.worldInitialize.subscribe(ev => {

    // ツルハシの耐久値減少のコンポーネントの登録
    ev.itemComponentRegistry.registerCustomComponent("saba:pickaxe_durability", {
        // 採掘時の耐久値減少処理
        onMineBlock: (ev) => { // 採掘したとき
            if (ev.source.getGameMode() !== "creative") { // クリエ以外のとき
                durabilityDamage(ev.source, ev.itemStack, 1); // 耐久値を1減らす
            }
        },

        // 攻撃時の耐久値減少処理
        onBeforeDurabilityDamage: (ev) => { // 攻撃で耐久値が減少したとき
            if (itemDamageChance(ev.itemStack)) {
                ev.durabilityDamage = 2; // 減少値を1にする
            } else {
                ev.durabilityDamage = 0;
            }
        }
    });
});

// ブロックを破壊する関数
function destroyBlock(dimension, location) {
    const block = dimension.getBlock(location); // ブロックの取得
    const blockItem = block.getItemStack(1, true); // ブロックのアイテムを取得

    block.setType("minecraft:air"); // ブロック破壊
    if (blockItem) {
        // アイテムをスポーン
        dimension.spawnItem(blockItem, block.location);
    }}

// 範囲破壊を行う関数
function destroy3x3Blocks(dimension, location, axis) {
    const offsets = [-1, 0, 1];
    for (let i of offsets) {
        for (let j of offsets) {
            let newLocation;
            // 破壊座標を指定する
            if (axis === "x") { // 東西
                newLocation = { x: location.x, y: location.y + i, z: location.z + j };
            } else if (axis === "z") { // 南北
                newLocation = { x: location.x + i, y: location.y + j, z: location.z };
            } else if (axis === "y") { // 上下
                newLocation = { x: location.x + i, y: location.y, z: location.z + j };
            }
            const block = dimension.getBlock(newLocation); // ブロックの取得
            if (block.typeId !== "minecraft:bedrock") { // 岩盤以外の場合
                destroyBlock(dimension, newLocation); // ブロック破壊
            }
        }
    }
}

// プレイヤーがブロックを破壊したとき
world.beforeEvents.playerBreakBlock.subscribe(ev => {
    const { player, itemStack, block, dimension } = ev;
    const location = block.location;

    // 3x3範囲破壊ツルハシの場合
    if (itemStack && itemStack.typeId === "saba:3x3_pickaxe") {
        // 採掘するブロックの面
        const face = player.getBlockFromViewDirection()?.face;
        if (face) {
            system.run(() => {
                if (face === "East" || face === "West") {
                    // 東西
                    destroy3x3Blocks(dimension, location, "x");
                } else if (face === "North" || face === "South") {
                    // 南北
                    destroy3x3Blocks(dimension, location, "z");
                } else {
                    // 上下
                    destroy3x3Blocks(dimension, location, "y");
                }
            });
        }
    }
});

custom_item_BP/recipes/saba_3x3_pickaxe.json

{
    "format_version": "1.17.41",
    "minecraft:recipe_shaped": {
        "description": {
            "identifier": "saba:3x3_pickaxe"
        },
        "tags": ["crafting_table"],
        "pattern": [
            "YXY",
            "XIX",
            " I "
        ],
        "key": {
            "X": {
                "item": "minecraft:netherite_ingot"
            },
            "Y": {
                "item": "minecraft:diamond"
            },
            "I": {
                "item": "minecraft:blaze_rod"
            }
        },
        "result": {
            "item": "saba:3x3_pickaxe"
        }
    }
}

リソースパック

Group-50.png

custom_item_RP/textures/item_texture.json

{
    "resource_pack_name": "custom_item RP",
    "texture_data": {
        "saba:3x3_pickaxe": { // #5 3x3範囲破壊ツルハシ
            "textures": "textures/items/saba_3x3_pickaxe"
        }
    }
}

custom_item_RP/textures/items/saba_3x3_pickaxe.png

saba_3x3_pickaxe.png

動作確認

・石を素早く破壊できる
・採掘したときに耐久値が減る&壊れる
・3×3が破壊される
・レシピ通りに作れる

%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88-2024-08-28-185316-1.png
%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88-2024-08-28-185113-1.png

完成品:https://mega.nz/folder/1FwjCT7b#AhHJHUMowSla4GFdpQZVvw

※投稿記事に含まれるファイルやリンクにより発生した被害についてクラフターズコロニーは責任を取りません
投稿通報

コメント

  1. 関係ないですが
    殺した相手と殺した相手の残りHPを殺された後に表示
    kill/death
    総合kill数
    総合deth数
    今日のkill数
    今日のdeth数
    今日のkill/death
    プレイ時間
    をフォームで確認できるアドオンを作ってほしいです
    pvpで便利なので

コメント通報