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/examples/javascript/src/main/resources/scriptcraft/modid/index.js

77 lines
2.4 KiB
JavaScript

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 {
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();
}
}
class MyBlock extends CustomBlockWithEntity {
constructor() {
super(new BlockSettings('STONE', 'IRON'));
}
onUse(world, blockState, blockPos, side, player, hand) {
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() {
return new MyBlockEntity();
}
}
Registry.register(Registry.BLOCK, new Identifier('modid', 'my_block'), new MyBlock());
Registry.register(Registry.ITEM, new Identifier('modid', 'my_block'), new BlockItem(new Identifier('modid', 'my_block'), new ItemSettings()));
class MyItem extends CustomItem {
constructor() {
super(new ItemSettings().setItemGroup('building_blocks').setMaxCount(128));
}
onUse(world, player, hand) {
console.log('Item Use: Normal');
return ActionResult.SUCCESS;
}
onUseOnBlock(world, blockPos, side, player, hand) {
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, target, hand) {
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('modid', 'my_item'), new MyItem());