TheBrokenRail
624193103e
Some checks failed
ScriptCraft/pipeline/head There was a failure building this commit
117 lines
3.0 KiB
Markdown
117 lines
3.0 KiB
Markdown
# Create A Block
|
|
|
|
## Create Your Block Entity Class
|
|
JavaScript:
|
|
```javascript
|
|
import { CustomBlockEntity, NumberType } from 'minecraft';
|
|
|
|
class MyBlockEntity extends CustomBlockEntity {
|
|
constructor() {
|
|
super();
|
|
this.ticks = 0;
|
|
}
|
|
|
|
toTag(tag) {
|
|
tag.set('MyTicks', this.ticks, NumberType.INT);
|
|
return tag;
|
|
}
|
|
|
|
fromTag(tag) {
|
|
const obj = tag.get('MyTicks');
|
|
if (typeof obj === 'number') {
|
|
this.ticks = obj;
|
|
} else {
|
|
this.ticks = 0;
|
|
}
|
|
}
|
|
|
|
tick() {
|
|
this.ticks++;
|
|
this.markDirty();
|
|
}
|
|
}
|
|
```
|
|
|
|
TypeScript:
|
|
```typescript
|
|
import { CustomBlockEntity, CompoundTag, NumberType } from 'minecraft';
|
|
|
|
class MyBlockEntity extends CustomBlockEntity {
|
|
ticks = 0;
|
|
|
|
toTag(tag: CompoundTag): CompoundTag {
|
|
tag.set('MyTicks', this.ticks, NumberType.INT);
|
|
return tag;
|
|
}
|
|
|
|
fromTag(tag: CompoundTag): void {
|
|
const obj = tag.get('MyTicks');
|
|
if (typeof obj === 'number') {
|
|
this.ticks = obj;
|
|
} else {
|
|
this.ticks = 0;
|
|
}
|
|
}
|
|
|
|
tick(): void {
|
|
this.ticks++;
|
|
this.markDirty();
|
|
}
|
|
}
|
|
```
|
|
|
|
## Create Your Block Class
|
|
JavaScript:
|
|
```javascript
|
|
import { Identifier, BlockState, BlockSettings, ActionResult, CustomBlockWithEntity, BuiltinRegistry } from 'minecraft';
|
|
|
|
const ironBlock = BuiltinRegistry.BLOCK.get(new Identifier('minecraft:iron_block'));
|
|
|
|
class MyBlock extends CustomBlockWithEntity {
|
|
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;
|
|
}
|
|
|
|
createBlockEntity() {
|
|
return new MyBlockEntity();
|
|
}
|
|
}
|
|
```
|
|
|
|
TypeScript:
|
|
```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 CustomBlockWithEntity {
|
|
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
|
|
```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')));
|
|
```
|