2020-06-01 23:07:01 +00:00
# Create A Block
## Create Your Block Class
JavaScript:
```javascript
2020-08-25 01:32:13 +00:00
import { Identifier, BlockState, BlockSettings, ActionResult, CustomBlock, BuiltinRegistry } from 'minecraft';
const ironBlock = BuiltinRegistry.BLOCK.get(new Identifier('minecraft:iron_block'));
2020-06-01 23:07:01 +00:00
class MyBlock extends CustomBlock {
constructor() {
2020-08-25 01:32:13 +00:00
super(new BlockSettings(ironBlock.getMaterial(), ironBlock.getMaterialColor()));
2020-06-01 23:07:01 +00:00
}
onUse(world, blockState, blockPos, side, player, hand) {
world.setBlockState(blockPos.offset(side), BlockState.getDefaultState(new Identifier('minecraft:stone')));
return ActionResult.SUCCESS;
}
}
```
TypeScript:
```typescript
2020-08-25 01:32:13 +00:00
import { Identifier, BlockState, BlockSettings, ActionResult, World, Pos, Hand, PlayerEntity, Direction, CustomBlock, BuiltinRegistry } from 'minecraft';
const ironBlock = BuiltinRegistry.BLOCK.get(new Identifier('minecraft:iron_block'));
2020-06-01 23:07:01 +00:00
class MyBlock extends CustomBlock {
constructor() {
2020-08-25 01:32:13 +00:00
super(new BlockSettings(ironBlock.getMaterial(), ironBlock.getMaterialColor()));
2020-06-01 23:07:01 +00:00
}
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
```javascript
import { Registry, Identifier } from 'minecraft';
Registry.register(Registry.BLOCK, new Identifier('modid', 'my_block'), new MyBlock());
```
## Register Your Block Item
```javascript
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')));
2020-08-25 01:32:13 +00:00
```