diff --git a/docs/README.md b/docs/README.md index 640040c..d85bb69 100644 --- a/docs/README.md +++ b/docs/README.md @@ -7,4 +7,6 @@ [View Architecture](ARCHITECTURE.md) +[View Tutorials](tutorials/README.md) + [View TypeDoc](https://jenkins.thebrokenrail.com/job/ScriptCraft/job/master/TypeDoc/) \ No newline at end of file diff --git a/docs/tutorials/BLOCK.md b/docs/tutorials/BLOCK.md new file mode 100644 index 0000000..9d9f673 --- /dev/null +++ b/docs/tutorials/BLOCK.md @@ -0,0 +1,48 @@ +# Create A Block + +## Create Your Block Class +JavaScript: +```javascript +import { Identifier, BlockState, BlockSettings, ActionResult, CustomBlock } from 'minecraft'; + +class MyBlock extends CustomBlock { + constructor() { + super(new BlockSettings('STONE', 'IRON')); + } + + onUse(world, blockState, blockPos, side, player, hand) { + world.setBlockState(blockPos.offset(side), BlockState.getDefaultState(new Identifier('minecraft:stone'))); + return ActionResult.SUCCESS; + } +} +``` + +TypeScript: +```typescript +import { Identifier, BlockState, BlockSettings, ActionResult, World, Pos, Hand, PlayerEntity, Direction, CustomBlock } from 'minecraft'; + +class MyBlock extends CustomBlock { + constructor() { + super(new BlockSettings('STONE', 'IRON')); + } + + 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'))); +``` \ No newline at end of file diff --git a/docs/tutorials/BLOCK_ENTITY.md b/docs/tutorials/BLOCK_ENTITY.md new file mode 100644 index 0000000..2641fda --- /dev/null +++ b/docs/tutorials/BLOCK_ENTITY.md @@ -0,0 +1,112 @@ +# 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 } from 'minecraft'; + +class MyBlock extends CustomBlockWithEntity { + constructor() { + super(new BlockSettings('STONE', 'IRON')); + } + + 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 } from 'minecraft'; + +class MyBlock extends CustomBlock { + constructor() { + super(new BlockSettings('STONE', 'IRON')); + } + + 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'))); +``` \ No newline at end of file diff --git a/docs/tutorials/ITEM.md b/docs/tutorials/ITEM.md new file mode 100644 index 0000000..93608f6 --- /dev/null +++ b/docs/tutorials/ITEM.md @@ -0,0 +1,61 @@ +# Create A Item + +## Create Your Item Class +JavaScript: +```javascript +import { ActionResult, ItemSettings, CustomItem } from 'minecraft'; + +class MyItem extends CustomItem { + constructor() { + super(new ItemSettings().setItemGroup('building_blocks').setMaxCount(128)); + } + + onUse(world, player, hand) { + console.log('Item Use'); + return ActionResult.SUCCESS; + } + + onUseOnBlock(world, blockPos, side, player, hand) { + console.log('Item Use: Block ' + blockPos.toString()); + return ActionResult.SUCCESS; + } + + onUseOnEntity(player, target, hand) { + console.log('Item Use: Entity ' + target.toString()); + return ActionResult.SUCCESS; + } +} +``` + +JavaScript: +```typescript +import { ActionResult, World, Pos, Hand, PlayerEntity, ItemSettings, CustomItem, Direction, LivingEntity } from 'minecraft'; + +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'); + return ActionResult.SUCCESS; + } + + onUseOnBlock(world: World, blockPos: Pos, side: Direction, player: PlayerEntity, hand: Hand): ActionResult { + console.log('Item Use: Block ' + blockPos.toString()); + return ActionResult.SUCCESS; + } + + onUseOnEntity(player: PlayerEntity, target: LivingEntity, hand: Hand): ActionResult { + console.log('Item Use: Entity ' + target.toString()); + return ActionResult.SUCCESS; + } +} +``` + +## Register Your Item +```javascript +import { Registry, Identifier } from 'minecraft'; + +Registry.register(Registry.ITEM, new Identifier('modid', 'my_item'), new MyItem()); +``` \ No newline at end of file diff --git a/docs/tutorials/README.md b/docs/tutorials/README.md new file mode 100644 index 0000000..fad804f --- /dev/null +++ b/docs/tutorials/README.md @@ -0,0 +1,6 @@ +# Tutorials +[Create A Block](BLOCK.md) + +[Create A Block Entity](BLOCK_ENTITY.md) + +[Create A Item](ITEM.md) \ No newline at end of file diff --git a/examples/javascript/src/main/resources/scriptcraft/modid/index.ts b/examples/javascript/src/main/resources/scriptcraft/modid/index.ts index 2b42ee7..0a27509 100644 --- a/examples/javascript/src/main/resources/scriptcraft/modid/index.ts +++ b/examples/javascript/src/main/resources/scriptcraft/modid/index.ts @@ -3,7 +3,10 @@ import { BlockSettings, Identifier, Registry, BlockState, ActionResult, World, P console.log('hello'); class MyBlockEntity extends CustomBlockEntity { - ticks = 0; + constructor() { + super(); + this.ticks = 0; + } toTag(tag) { tag.set('MyTicks', this.ticks, NumberType.INT); diff --git a/plugin/src/main/kotlin/ScriptCraftPlugin.kt b/plugin/src/main/kotlin/ScriptCraftPlugin.kt index 2235510..b634430 100644 --- a/plugin/src/main/kotlin/ScriptCraftPlugin.kt +++ b/plugin/src/main/kotlin/ScriptCraftPlugin.kt @@ -159,11 +159,11 @@ class ScriptCraftPlugin : Plugin { tasks.named("processResources") { dependsOn(typescriptTask) - from(Callable { - File(extension.root, "build/ts") - }) - - into("scriptcraft") + into("scriptcraft") { + from(Callable { + File(extension.root, "build/ts") + }) + } } } } \ No newline at end of file diff --git a/scriptcraft/src/main/java/com/thebrokenrail/scriptcraft/core/quickjs/QuickJSModules.java b/scriptcraft/src/main/java/com/thebrokenrail/scriptcraft/core/quickjs/QuickJSModules.java index b1262fc..59dc04f 100644 --- a/scriptcraft/src/main/java/com/thebrokenrail/scriptcraft/core/quickjs/QuickJSModules.java +++ b/scriptcraft/src/main/java/com/thebrokenrail/scriptcraft/core/quickjs/QuickJSModules.java @@ -43,7 +43,8 @@ public class QuickJSModules { } stream.close(); - return isDirectory(textBuilder.toString(), path, mod.getClass()) ? null : textBuilder.toString(); + String data = textBuilder.toString(); + return isDirectory(data, path, mod.getClass()) ? null : data; } } catch (IOException e) { }