This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
ScriptCraft/docs/tutorials/BLOCK.md
TheBrokenRail 624193103e
Some checks failed
ScriptCraft/pipeline/head There was a failure building this commit
Update Docs
2020-08-24 21:32:13 -04:00

1.8 KiB

Create A Block

Create Your Block Class

JavaScript:

import { Identifier, BlockState, BlockSettings, ActionResult, CustomBlock, BuiltinRegistry } from 'minecraft';

const ironBlock = BuiltinRegistry.BLOCK.get(new Identifier('minecraft:iron_block'));

class MyBlock extends CustomBlock {
    constructor() {
        super(new BlockSettings(ironBlock.getMaterial(), ironBlock.getMaterialColor()));
    }

    onUse(world, blockState, blockPos, side, player, hand) {
        world.setBlockState(blockPos.offset(side), BlockState.getDefaultState(new Identifier('minecraft:stone')));
        return ActionResult.SUCCESS;
    }
}

TypeScript:

import { Identifier, BlockState, BlockSettings, ActionResult, World, Pos, Hand, PlayerEntity, Direction, CustomBlock, BuiltinRegistry } from 'minecraft';

const ironBlock = BuiltinRegistry.BLOCK.get(new Identifier('minecraft:iron_block'));

class MyBlock extends CustomBlock {
    constructor() {
        super(new BlockSettings(ironBlock.getMaterial(), ironBlock.getMaterialColor()));
    }

    onUse(world: World, blockState: BlockState, blockPos: Pos, side: Direction, player: PlayerEntity, hand: Hand): ActionResult {
        world.setBlockState(blockPos.offset(side), BlockState.getDefaultState(new Identifier('minecraft:stone')));
        return ActionResult.SUCCESS;
    }
}

Register Your Block

import { Registry, Identifier } from 'minecraft';

Registry.register(Registry.BLOCK, new Identifier('modid', 'my_block'), new MyBlock());

Register Your Block Item

import { Registry, Identifier, BlockItem, ItemSettings } from 'minecraft';

Registry.register(Registry.ITEM, new Identifier('modid', 'my_block'), new BlockItem(new Identifier('modid', 'my_block'), new ItemSettings().setItemGroup('building_blocks')));