From 82b00bdaec8e1ecb61dfe407d964f2ac47e73ca2 Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Wed, 29 Apr 2020 20:32:07 -0400 Subject: [PATCH] Finish Examples --- README.md | 3 + examples/README.md | 4 + examples/javascript/.gitignore | 28 +++++++ examples/javascript/README.md | 6 ++ examples/javascript/build.gradle | 67 +++++++++++++++++ examples/javascript/gradle | 1 + examples/javascript/gradle.properties | 13 ++++ examples/javascript/gradlew | 1 + examples/javascript/gradlew.bat | 1 + examples/javascript/settings.gradle | 1 + .../example/JavaScriptExample.java | 15 ++++ examples/javascript/src/main/resources/assets | 1 + .../src/main/resources/fabric.mod.json | 28 +++++++ .../main/resources/scriptcraft/modid/index.ts | 73 +++++++++++++++++++ examples/typescript/README.md | 5 +- .../typescript/src/main/ts/src/modid/index.ts | 2 +- examples/typescript/src/main/ts/tsconfig.json | 2 +- examples/typescript/typescript.build.gradle | 2 +- src/main/resources/fabric.mod.json | 3 +- src/main/ts/package.json | 2 +- src/main/ts/tsconfig.json | 4 +- .../types/scriptcraft-core/index.d.ts | 0 typescript.build.gradle | 8 +- 23 files changed, 253 insertions(+), 17 deletions(-) create mode 100644 examples/README.md create mode 100644 examples/javascript/.gitignore create mode 100644 examples/javascript/README.md create mode 100644 examples/javascript/build.gradle create mode 120000 examples/javascript/gradle create mode 100644 examples/javascript/gradle.properties create mode 120000 examples/javascript/gradlew create mode 120000 examples/javascript/gradlew.bat create mode 120000 examples/javascript/settings.gradle create mode 100644 examples/javascript/src/main/java/com/thebrokenrail/scriptcraft/example/JavaScriptExample.java create mode 120000 examples/javascript/src/main/resources/assets create mode 100644 examples/javascript/src/main/resources/fabric.mod.json create mode 100644 examples/javascript/src/main/resources/scriptcraft/modid/index.ts rename src/main/ts/{src => }/types/scriptcraft-core/index.d.ts (100%) diff --git a/README.md b/README.md index 47022d1..8dbea3a 100644 --- a/README.md +++ b/README.md @@ -4,5 +4,8 @@ JS API for Minecraft ## API [View TypeDoc](https://jenkins.thebrokenrail.com/job/ScriptCraft/job/master/TypeDoc/) +## Example +[View Examples](examples/README.md) + ## Changelog [View Changelog](CHANGELOG.md) diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..1f26343 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,4 @@ +# Examples +[View JavaScript Example](javascript/README.md) + +[View TypeScript Example](typescript/README.md) \ No newline at end of file diff --git a/examples/javascript/.gitignore b/examples/javascript/.gitignore new file mode 100644 index 0000000..3174d3b --- /dev/null +++ b/examples/javascript/.gitignore @@ -0,0 +1,28 @@ +# gradle + +.gradle/ +build/ +out/ +classes/ + +# idea + +.idea/ +*.iml +*.ipr +*.iws + +# vscode + +.settings/ +bin/ +.classpath +.project + +# fabric + +run/ + +remappedSrc/ + +libs/ \ No newline at end of file diff --git a/examples/javascript/README.md b/examples/javascript/README.md new file mode 100644 index 0000000..56e791b --- /dev/null +++ b/examples/javascript/README.md @@ -0,0 +1,6 @@ +# JavaScript Example +This is an example of a Minecraft mod made in JavaScript using ScriptCraft. + +#### Files +- ```src/main/resources/scriptcraft```: Contains JavaScript Code +- ```src/main/java```: Contains Bootstrap Java Code \ No newline at end of file diff --git a/examples/javascript/build.gradle b/examples/javascript/build.gradle new file mode 100644 index 0000000..7c1e0c8 --- /dev/null +++ b/examples/javascript/build.gradle @@ -0,0 +1,67 @@ +plugins { + id 'fabric-loom' version '0.2.7-SNAPSHOT' +} + +compileJava { + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 +} + +archivesBaseName = project.archives_base_name +def mod_version = project.mod_version as Object +version = "${mod_version}+${project.minecraft_version}" +group = project.maven_group as Object + +minecraft { +} + +repositories { + maven { + url 'https://maven.thebrokenrail.com/' + } +} + +dependencies { + minecraft "com.mojang:minecraft:${project.minecraft_version}" + mappings "net.fabricmc:yarn:${project.minecraft_version}+build.${project.yarn_build}:v2" + modImplementation "net.fabricmc:fabric-loader:${project.fabric_loader_version}" + + modImplementation "com.thebrokenrail:scriptcraft:${project.minecraft_version}" + include "com.thebrokenrail:scriptcraft:${project.minecraft_version}" +} + +processResources { + inputs.property 'version', mod_version + + from(sourceSets.main.resources.srcDirs) { + include 'fabric.mod.json' + expand 'version': mod_version + } + + from(sourceSets.main.resources.srcDirs) { + exclude 'fabric.mod.json' + } +} + +// ensure that the encoding is set to UTF-8, no matter what the system default is +// this fixes some edge cases with special characters not displaying correctly +// see http://yodaconditions.net/blog/fix-for-java-file-encoding-problems-with-gradle.html +tasks.withType(JavaCompile) { + options.encoding = 'UTF-8' +} + +// Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task +// if it is present. +// If you remove this task, sources will not be generated. +task sourcesJar(type: Jar, dependsOn: classes) { + classifier 'sources' + from sourceSets.main.allSource +} + +artifacts { + archives sourcesJar +} + +jar { + from 'LICENSE' +} diff --git a/examples/javascript/gradle b/examples/javascript/gradle new file mode 120000 index 0000000..1ce6c4c --- /dev/null +++ b/examples/javascript/gradle @@ -0,0 +1 @@ +../../gradle \ No newline at end of file diff --git a/examples/javascript/gradle.properties b/examples/javascript/gradle.properties new file mode 100644 index 0000000..b04b320 --- /dev/null +++ b/examples/javascript/gradle.properties @@ -0,0 +1,13 @@ +# Done to increase the memory available to gradle. +org.gradle.jvmargs = -Xmx1G + +# Fabric Properties + # check these on https://fabricmc.net/use + minecraft_version = 1.15.2 + yarn_build = 15 + fabric_loader_version = 0.8.2+build.194 + +# Mod Properties + mod_version = 1.0.0 + maven_group = com.thebrokenrail + archives_base_name = scriptcraft-javascript-example diff --git a/examples/javascript/gradlew b/examples/javascript/gradlew new file mode 120000 index 0000000..343e0d2 --- /dev/null +++ b/examples/javascript/gradlew @@ -0,0 +1 @@ +../../gradlew \ No newline at end of file diff --git a/examples/javascript/gradlew.bat b/examples/javascript/gradlew.bat new file mode 120000 index 0000000..cb5a946 --- /dev/null +++ b/examples/javascript/gradlew.bat @@ -0,0 +1 @@ +../../gradlew.bat \ No newline at end of file diff --git a/examples/javascript/settings.gradle b/examples/javascript/settings.gradle new file mode 120000 index 0000000..19fe1e9 --- /dev/null +++ b/examples/javascript/settings.gradle @@ -0,0 +1 @@ +../../settings.gradle \ No newline at end of file diff --git a/examples/javascript/src/main/java/com/thebrokenrail/scriptcraft/example/JavaScriptExample.java b/examples/javascript/src/main/java/com/thebrokenrail/scriptcraft/example/JavaScriptExample.java new file mode 100644 index 0000000..b168ccb --- /dev/null +++ b/examples/javascript/src/main/java/com/thebrokenrail/scriptcraft/example/JavaScriptExample.java @@ -0,0 +1,15 @@ +package com.thebrokenrail.scriptcraft.example; + +import com.thebrokenrail.scriptcraft.core.ScriptCraftEntrypoint; + +public class JavaScriptExample implements ScriptCraftEntrypoint { + @Override + public String getModID() { + return "modid"; + } + + @Override + public String getModIndex() { + return "index.js"; + } +} \ No newline at end of file diff --git a/examples/javascript/src/main/resources/assets b/examples/javascript/src/main/resources/assets new file mode 120000 index 0000000..90bacbe --- /dev/null +++ b/examples/javascript/src/main/resources/assets @@ -0,0 +1 @@ +../../../../../src/main/resources/assets \ No newline at end of file diff --git a/examples/javascript/src/main/resources/fabric.mod.json b/examples/javascript/src/main/resources/fabric.mod.json new file mode 100644 index 0000000..d000a0d --- /dev/null +++ b/examples/javascript/src/main/resources/fabric.mod.json @@ -0,0 +1,28 @@ +{ + "schemaVersion": 1, + "id": "modid", + "version": "${version}", + "name": "ScriptCraft JavaScript Example", + "description": "JavaScript Example for ScriptCraft", + "authors": [ + "TheBrokenRail" + ], + "contact": { + "homepage": "https://thebrokenrail.com/", + "sources": "https://gitea.thebrokenrail.com/TheBrokenRail/ScriptCraft.git", + "issues": "https://gitea.thebrokenrail.com/TheBrokenRail/ScriptCraft/issues" + }, + "license": "MIT", + "icon": "assets/scriptcraft/icon.png", + "environment": "*", + "entrypoints": { + "scriptcraft": [ + "com.thebrokenrail.scriptcraft.example.JavaScriptExample" + ] + }, + "depends": { + "fabricloader": ">=0.7.4", + "minecraft": "1.15.x", + "scriptcraft": "*" + } +} diff --git a/examples/javascript/src/main/resources/scriptcraft/modid/index.ts b/examples/javascript/src/main/resources/scriptcraft/modid/index.ts new file mode 100644 index 0000000..2b42ee7 --- /dev/null +++ b/examples/javascript/src/main/resources/scriptcraft/modid/index.ts @@ -0,0 +1,73 @@ +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) { + 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()); diff --git a/examples/typescript/README.md b/examples/typescript/README.md index 1d2011b..398633a 100644 --- a/examples/typescript/README.md +++ b/examples/typescript/README.md @@ -5,5 +5,6 @@ This is an example of a Minecraft mod made in TypeScript using ScriptCraft. - ```src/main/ts```: Contains TypeScript Code - ```src/main/java```: Contains Bootstrap Java Code -#### Note -When updating Minecraft, make sure to update the ```src/main/ts/package.json``` file as well. \ No newline at end of file +#### Notes +- When updating the Minecraft version, make sure to update it in the ```src/main/ts/package.json``` file as well +- This will also work with JavaScript if you set ```compilerOptions.allowJs``` and optionally ```compilerOptions.checkJs``` in ```src/main/ts/tsconfig.json``` to ```true``` \ No newline at end of file diff --git a/examples/typescript/src/main/ts/src/modid/index.ts b/examples/typescript/src/main/ts/src/modid/index.ts index c4628a2..3002b84 100644 --- a/examples/typescript/src/main/ts/src/modid/index.ts +++ b/examples/typescript/src/main/ts/src/modid/index.ts @@ -70,4 +70,4 @@ class MyItem extends CustomItem { } } -Registry.register(Registry.ITEM, new Identifier('test', 'my_item'), new MyItem()); +Registry.register(Registry.ITEM, new Identifier('modid', 'my_item'), new MyItem()); diff --git a/examples/typescript/src/main/ts/tsconfig.json b/examples/typescript/src/main/ts/tsconfig.json index 62236d9..3dc9a47 100644 --- a/examples/typescript/src/main/ts/tsconfig.json +++ b/examples/typescript/src/main/ts/tsconfig.json @@ -16,6 +16,6 @@ "outDir": "lib/ts" }, "include": [ - "src/**/*.ts" + "src/**/*" ] } \ No newline at end of file diff --git a/examples/typescript/typescript.build.gradle b/examples/typescript/typescript.build.gradle index fd245f9..14c283f 100644 --- a/examples/typescript/typescript.build.gradle +++ b/examples/typescript/typescript.build.gradle @@ -3,7 +3,7 @@ def typescriptRootFile = new File(rootDir.absolutePath, typescriptRoot) def typescriptOut = new File(typescriptRootFile, 'lib/ts') -def nodeModules = new File(typescriptRoot, 'node_modules') +def nodeModules = new File(typescriptRootFile, 'node_modules') task typescriptInstall(group: 'typescript', type: Exec) { inputs.file new File(typescriptRootFile, 'package.json') diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 766cd37..45b8c09 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -20,8 +20,7 @@ "com.thebrokenrail.scriptcraft.core.ScriptCraftCore" ], "scriptcraft": [ - "com.thebrokenrail.scriptcraft.api.ScriptCraftAPI", - "com.thebrokenrail.scriptcraft.Test" + "com.thebrokenrail.scriptcraft.api.ScriptCraftAPI" ] }, "depends": { diff --git a/src/main/ts/package.json b/src/main/ts/package.json index 700e105..ec074d4 100644 --- a/src/main/ts/package.json +++ b/src/main/ts/package.json @@ -1,6 +1,6 @@ { "name": "minecraft", - "version" : "1.0.0-SNAPSHOT", + "version": "1.0.0-SNAPSHOT", "devDependencies": { "@typescript-eslint/eslint-plugin": "latest", "@typescript-eslint/parser": "latest", diff --git a/src/main/ts/tsconfig.json b/src/main/ts/tsconfig.json index aa70bb4..3b194dd 100644 --- a/src/main/ts/tsconfig.json +++ b/src/main/ts/tsconfig.json @@ -7,7 +7,7 @@ "lib": ["es2020"], "module": "es2020", "target": "es2020", - "typeRoots": ["src/types"], + "typeRoots": ["types"], "rootDir": "src", "baseUrl": "src", "outDir": "lib/ts", @@ -15,6 +15,6 @@ "declarationDir": "lib/dts" }, "include": [ - "src/**/*.ts" + "src/**/*" ] } \ No newline at end of file diff --git a/src/main/ts/src/types/scriptcraft-core/index.d.ts b/src/main/ts/types/scriptcraft-core/index.d.ts similarity index 100% rename from src/main/ts/src/types/scriptcraft-core/index.d.ts rename to src/main/ts/types/scriptcraft-core/index.d.ts diff --git a/typescript.build.gradle b/typescript.build.gradle index c6b0db3..0cf5791 100644 --- a/typescript.build.gradle +++ b/typescript.build.gradle @@ -1,7 +1,7 @@ def typescriptRoot = 'src/main/ts' def typescriptRootFile = new File(rootDir.absolutePath, typescriptRoot) -def nodeModules = new File(typescriptRoot, 'node_modules') +def nodeModules = new File(typescriptRootFile, 'node_modules') task typescriptInstall(group: 'typescript', type: Exec) { inputs.file new File(typescriptRootFile, 'package.json') @@ -12,12 +12,6 @@ task typescriptInstall(group: 'typescript', type: Exec) { executable 'npm' args 'install' - - doFirst { - project.delete { - delete nodeModules - } - } } def typescriptOut = new File(typescriptRootFile, 'lib/ts')