Finish Examples
All checks were successful
ScriptCraft/pipeline/head This commit looks good

This commit is contained in:
TheBrokenRail 2020-04-29 20:32:07 -04:00
parent 7a4b9dd4f8
commit 82b00bdaec
23 changed files with 253 additions and 17 deletions

View File

@ -4,5 +4,8 @@ JS API for Minecraft
## API ## API
[View TypeDoc](https://jenkins.thebrokenrail.com/job/ScriptCraft/job/master/TypeDoc/) [View TypeDoc](https://jenkins.thebrokenrail.com/job/ScriptCraft/job/master/TypeDoc/)
## Example
[View Examples](examples/README.md)
## Changelog ## Changelog
[View Changelog](CHANGELOG.md) [View Changelog](CHANGELOG.md)

4
examples/README.md Normal file
View File

@ -0,0 +1,4 @@
# Examples
[View JavaScript Example](javascript/README.md)
[View TypeScript Example](typescript/README.md)

28
examples/javascript/.gitignore vendored Normal file
View File

@ -0,0 +1,28 @@
# gradle
.gradle/
build/
out/
classes/
# idea
.idea/
*.iml
*.ipr
*.iws
# vscode
.settings/
bin/
.classpath
.project
# fabric
run/
remappedSrc/
libs/

View File

@ -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

View File

@ -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'
}

1
examples/javascript/gradle Symbolic link
View File

@ -0,0 +1 @@
../../gradle

View File

@ -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

1
examples/javascript/gradlew vendored Symbolic link
View File

@ -0,0 +1 @@
../../gradlew

1
examples/javascript/gradlew.bat vendored Symbolic link
View File

@ -0,0 +1 @@
../../gradlew.bat

View File

@ -0,0 +1 @@
../../settings.gradle

View File

@ -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";
}
}

View File

@ -0,0 +1 @@
../../../../../src/main/resources/assets

View File

@ -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": "*"
}
}

View File

@ -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());

View File

@ -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/ts```: Contains TypeScript Code
- ```src/main/java```: Contains Bootstrap Java Code - ```src/main/java```: Contains Bootstrap Java Code
#### Note #### Notes
When updating Minecraft, make sure to update the ```src/main/ts/package.json``` file as well. - 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```

View File

@ -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());

View File

@ -16,6 +16,6 @@
"outDir": "lib/ts" "outDir": "lib/ts"
}, },
"include": [ "include": [
"src/**/*.ts" "src/**/*"
] ]
} }

View File

@ -3,7 +3,7 @@ def typescriptRootFile = new File(rootDir.absolutePath, typescriptRoot)
def typescriptOut = new File(typescriptRootFile, 'lib/ts') 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) { task typescriptInstall(group: 'typescript', type: Exec) {
inputs.file new File(typescriptRootFile, 'package.json') inputs.file new File(typescriptRootFile, 'package.json')

View File

@ -20,8 +20,7 @@
"com.thebrokenrail.scriptcraft.core.ScriptCraftCore" "com.thebrokenrail.scriptcraft.core.ScriptCraftCore"
], ],
"scriptcraft": [ "scriptcraft": [
"com.thebrokenrail.scriptcraft.api.ScriptCraftAPI", "com.thebrokenrail.scriptcraft.api.ScriptCraftAPI"
"com.thebrokenrail.scriptcraft.Test"
] ]
}, },
"depends": { "depends": {

View File

@ -7,7 +7,7 @@
"lib": ["es2020"], "lib": ["es2020"],
"module": "es2020", "module": "es2020",
"target": "es2020", "target": "es2020",
"typeRoots": ["src/types"], "typeRoots": ["types"],
"rootDir": "src", "rootDir": "src",
"baseUrl": "src", "baseUrl": "src",
"outDir": "lib/ts", "outDir": "lib/ts",
@ -15,6 +15,6 @@
"declarationDir": "lib/dts" "declarationDir": "lib/dts"
}, },
"include": [ "include": [
"src/**/*.ts" "src/**/*"
] ]
} }

View File

@ -1,7 +1,7 @@
def typescriptRoot = 'src/main/ts' def typescriptRoot = 'src/main/ts'
def typescriptRootFile = new File(rootDir.absolutePath, typescriptRoot) 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) { task typescriptInstall(group: 'typescript', type: Exec) {
inputs.file new File(typescriptRootFile, 'package.json') inputs.file new File(typescriptRootFile, 'package.json')
@ -12,12 +12,6 @@ task typescriptInstall(group: 'typescript', type: Exec) {
executable 'npm' executable 'npm'
args 'install' args 'install'
doFirst {
project.delete {
delete nodeModules
}
}
} }
def typescriptOut = new File(typescriptRootFile, 'lib/ts') def typescriptOut = new File(typescriptRootFile, 'lib/ts')