Post

silky spawner script

the script of silky mob spawner addon

silk spawner code

just copy this code, and paste it on your behavior pack script. or just download the addon here on mcpedl


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// import minecraft api
import { world, system } from "@minecraft/server";

// addon version
const version = "1.0";

// item use to destroy the spawner
const pickaxe = [
    "minecraft:iron_pickaxe",
    "minecraft:golden_pickaxe",
    "minecraft:diamond_pickaxe",
    "minecraft:netherite_pickaxe"
];

// on block break
world.beforeEvents.playerBreakBlock.subscribe(
    data => {
        // extrack data
        const { block, player, itemStack } = data;

        // verify if hold item match pickaxe list above and has silk touch enchantment
        if (!itemStack || !pickaxe.includes(itemStack.typeId)) return;
        if (
            !itemStack
                .getComponent("minecraft:enchantable")
                ?.getEnchantment("silk_touch")
        )
            return;

        data.cancel = true;

        // what to do after
        const spawnPos = centerBlockToEntity(block.location);
        const item = block.getItemStack(1, true);

        // spawn the item and add feedback, on the next active tick, couse beforeEvents run on idle tick or read only
        system.run(() => {
            pickDurability(player);
            block.setType("minecraft:air");
            block.dimension.spawnItem(item, spawnPos);
            player.playSound("block.mob_spawner.break", {
                pitch: randomRange(0.8, 1.2),
                location: block.location,
                volume: 1
            });
        });
    },
    {
        blockTypes: ["minecraft:mob_spawner"]
    }
);

// convert block position to entity location
function centerBlockToEntity(pos) {
    return {
        x: pos.x + 0.5,
        y: pos.y + 0.5,
        z: pos.z + 0.5
    };
}

// remove durability from pickaxe
function pickDurability(player) {
    const inv = player.getComponent("inventory").container;
    const item = inv.getItem(player.selectedSlotIndex);
    if (!item) return true;
    const durability = item.getComponent("minecraft:durability");
    const unbreakingLv =
        item.getComponent("minecraft:enchantable")?.getEnchantment("unbreaking")
            ?.level || 0;
    const breakChance = durability.getDamageChance(unbreakingLv);

    if (Math.random() * 100 <= breakChance) durability.damage += 1;
    if (durability.damage >= durability.maxDurability) {
        inv.setItem(player.selectedSlotIndex, undefined);
        player.playSound("random.break", {
            pitch: randomRange(0.8, 1.2),
            location: player.location,
            volume: 1
        });
        return true;
    }

    inv.setItem(player.selectedSlotIndex, item);
    return false;
}

// just random function
function randomRange(min = 0, max = 1) {
    return Math.random() * (max - min) + min;
}

// just to check if addon work or not
world.afterEvents.playerSpawn.subscribe(data => {
    if (!data.initialSpawn) return;
    data.player.sendMessage({
        rawtext: [
            {
                text: `§2+ §asilky spawn v${version} is installed`
            }
        ]
    });
});
This post is licensed under CC BY 4.0 by the author.

Trending Tags