74 lines
2.6 KiB
TypeScript
74 lines
2.6 KiB
TypeScript
import { BlockSettings, Identifier, Registry, BlockState, ActionResult, World, Pos, Hand, PlayerEntity, BlockItem, ItemSettings, CustomItem, Direction, LivingEntity, CustomBlockWithEntity, CustomBlockEntity, CompoundTag, NumberType } from 'minecraft';
|
|
|
|
console.log('hello');
|
|
|
|
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();
|
|
}
|
|
}
|
|
|
|
class MyBlock extends CustomBlockWithEntity {
|
|
constructor() {
|
|
super(new BlockSettings('STONE', 'IRON'));
|
|
}
|
|
|
|
onUse(world: World, blockState: BlockState, blockPos: Pos, side: Direction, player: PlayerEntity, hand: Hand): ActionResult {
|
|
const entity = world.getCustomBlockEntity(blockPos);
|
|
if (entity instanceof MyBlockEntity) {
|
|
console.log('Ticks: ' + entity.ticks);
|
|
}
|
|
world.setBlockState(blockPos.offset(side), BlockState.getDefaultState(new Identifier('minecraft:stone')));
|
|
return ActionResult.SUCCESS;
|
|
}
|
|
|
|
createBlockEntity(): CustomBlockEntity {
|
|
return new MyBlockEntity();
|
|
}
|
|
}
|
|
|
|
Registry.register(Registry.BLOCK, new Identifier('test', 'my_block'), new MyBlock());
|
|
Registry.register(Registry.ITEM, new Identifier('test', 'my_block'), new BlockItem(new Identifier('test', 'my_block'), new ItemSettings()));
|
|
|
|
class MyItem extends CustomItem {
|
|
constructor() {
|
|
super(new ItemSettings().setItemGroup('building_blocks').setMaxCount(128));
|
|
}
|
|
|
|
onUse(world: World, player: PlayerEntity, hand: Hand): ActionResult {
|
|
console.log('Item Use: Normal');
|
|
return ActionResult.SUCCESS;
|
|
}
|
|
|
|
onUseOnBlock(world: World, blockPos: Pos, side: Direction, player: PlayerEntity, hand: Hand): ActionResult {
|
|
console.log('Item Use: Block ' + blockPos.toString());
|
|
world.spawnEntity(new Identifier('minecraft:cow'), blockPos.offset(side).add(new Pos(0.5, 0, 0.5))).toString();
|
|
return ActionResult.SUCCESS;
|
|
}
|
|
|
|
onUseOnEntity(player: PlayerEntity, target: LivingEntity, hand: Hand): ActionResult {
|
|
console.log('Item Use: Entity ' + target.toTag().toString());
|
|
console.log('Health: ' + target.toTag().get('Health').toString());
|
|
return ActionResult.SUCCESS;
|
|
}
|
|
}
|
|
|
|
Registry.register(Registry.ITEM, new Identifier('test', 'my_item'), new MyItem());
|