Update API and Docs
Some checks failed
ScriptCraft/pipeline/head There was a failure building this commit

This commit is contained in:
TheBrokenRail 2020-06-02 16:01:26 -04:00
parent 8c356af7a2
commit 0f63f63d86
12 changed files with 43 additions and 17 deletions

View File

@ -1,8 +1,8 @@
# ScriptCraft [![Build Status](https://jenkins.thebrokenrail.com/job/ScriptCraft/job/master/badge/icon?style=flat-square)](https://jenkins.thebrokenrail.com/job/ScriptCraft/job/master/) # ScriptCraft [![Build Status](https://jenkins.thebrokenrail.com/job/ScriptCraft/job/master/badge/icon?style=flat-square)](https://jenkins.thebrokenrail.com/job/ScriptCraft/job/master/)
JS API for Minecraft JS API for Minecraft
## API ## Documentation
[View API](docs/README.md) [View Documentation](docs/README.md)
## Examples ## Examples
[View Examples](examples) [View Examples](examples)

View File

@ -1,4 +1,4 @@
# API # Documentation
[View Module Resolution](MODULE_RESOLUTION.md) [View Module Resolution](MODULE_RESOLUTION.md)
[View Entry-Points](ENTRY_POINTS.md) [View Entry-Points](ENTRY_POINTS.md)

25
docs/tutorials/EVENT.md Normal file
View File

@ -0,0 +1,25 @@
# Use An Event
## Example
```javascript
import { Events } from 'minecraft';
Events.WORLD_TICK.addListener(e => {
// Use e.getWorld()
});
```
## All Events
| Name | Description | Input Type | Output Type |
| --- | --- | --- | --- |
| ```WORLD_TICK``` | Triggered on every world tick. | ```WorldEvent``` | None |
| ```ATTACK_BLOCK``` | Triggered when breaking a block. | ```BlockEvent``` | ```ActionResult``` |
| ```USE_BLOCK``` | Triggered when using a block. | ```BlockEvent``` | ```ActionResult``` |
| ```USE_ITEM``` | Triggered when using an item. | ```ItemEvent``` | ```ActionResult``` |
## Manually Trigger An Event
```javascript
import { Events } from 'minecraft';
const result = Events.<Event Name>.trigger(<Input Value>);
```

View File

@ -4,3 +4,5 @@
[Create A Block Entity](BLOCK_ENTITY.md) [Create A Block Entity](BLOCK_ENTITY.md)
[Create A Item](ITEM.md) [Create A Item](ITEM.md)
[Use An Event](EVENT.md)

View File

@ -205,7 +205,7 @@ static JSValue java_object_to_js_object(JNIEnv *env, JSContext *ctx, jobject obj
JSValue out; JSValue out;
jclass boolean_clazz = (*env)->FindClass(env, "java/lang/Boolean"); jclass boolean_clazz = (*env)->FindClass(env, "java/lang/Boolean");
jclass double_clazz = (*env)->FindClass(env, "java/lang/Double"); jclass number_clazz = (*env)->FindClass(env, "java/lang/Number");
jclass string_clazz = (*env)->FindClass(env, "java/lang/String"); jclass string_clazz = (*env)->FindClass(env, "java/lang/String");
jclass array_clazz = (*env)->FindClass(env, "[Ljava/lang/Object;"); jclass array_clazz = (*env)->FindClass(env, "[Ljava/lang/Object;");
if (!obj) { if (!obj) {
@ -218,8 +218,8 @@ static JSValue java_object_to_js_object(JNIEnv *env, JSContext *ctx, jobject obj
jmethodID boolean_methodID = (*env)->GetMethodID(env, boolean_clazz, "booleanValue", "()Z"); jmethodID boolean_methodID = (*env)->GetMethodID(env, boolean_clazz, "booleanValue", "()Z");
jboolean val = (*env)->CallBooleanMethod(env, obj, boolean_methodID); jboolean val = (*env)->CallBooleanMethod(env, obj, boolean_methodID);
out = JS_NewBool(ctx, val); out = JS_NewBool(ctx, val);
} else if ((*env)->IsInstanceOf(env, obj, double_clazz)) { } else if ((*env)->IsInstanceOf(env, obj, number_clazz)) {
jmethodID double_methodID = (*env)->GetMethodID(env, double_clazz, "doubleValue", "()D"); jmethodID double_methodID = (*env)->GetMethodID(env, number_clazz, "doubleValue", "()D");
jdouble val = (*env)->CallDoubleMethod(env, obj, double_methodID); jdouble val = (*env)->CallDoubleMethod(env, obj, double_methodID);
out = JS_NewFloat64(ctx, val); out = JS_NewFloat64(ctx, val);
} else if ((*env)->IsInstanceOf(env, obj, array_clazz)) { } else if ((*env)->IsInstanceOf(env, obj, array_clazz)) {

View File

@ -23,6 +23,6 @@ public class CustomBlock extends Block {
@Override @Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
return ValueUtil.getEnumValue(ActionResult.class, (String) ScriptCraftCore.useBridge("CustomBlock.onUse", id.toString(), world, state, (double) pos.getX(), (double) pos.getY(), (double) pos.getZ(), hit.getSide().name(), player, hand.name()), ActionResult.PASS); return ValueUtil.getEnumValue(ActionResult.class, (String) ScriptCraftCore.useBridge("CustomBlock.onUse", id.toString(), world, state, pos.getX(), pos.getY(), pos.getZ(), hit.getSide().name(), player, hand.name()), ActionResult.PASS);
} }
} }

View File

@ -1,7 +1,6 @@
package com.thebrokenrail.scriptcraft.api.block; package com.thebrokenrail.scriptcraft.api.block;
import com.thebrokenrail.scriptcraft.core.ScriptCraftCore; import com.thebrokenrail.scriptcraft.core.ScriptCraftCore;
import com.thebrokenrail.scriptcraft.core.quickjs.QuickJSManager;
import net.fabricmc.fabric.api.block.entity.BlockEntityClientSerializable; import net.fabricmc.fabric.api.block.entity.BlockEntityClientSerializable;
import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType; import net.minecraft.block.entity.BlockEntityType;
@ -54,7 +53,7 @@ public class CustomBlockEntity extends BlockEntity implements BlockEntityClientS
@Override @Override
public void setLocation(World world, BlockPos pos) { public void setLocation(World world, BlockPos pos) {
super.setLocation(world, pos); super.setLocation(world, pos);
ScriptCraftCore.useBridge("CustomBlockEntity.setLocation", objID, world, (double) pos.getX(), (double) pos.getY(), (double) pos.getZ()); ScriptCraftCore.useBridge("CustomBlockEntity.setLocation", objID, world, pos.getX(), pos.getY(), pos.getZ());
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")

View File

@ -13,8 +13,8 @@ import net.minecraft.util.TypedActionResult;
class EventBridges { class EventBridges {
static void register() { static void register() {
WorldTickCallback.EVENT.register(world -> ScriptCraftCore.useBridge("Event.worldTick", world)); WorldTickCallback.EVENT.register(world -> ScriptCraftCore.useBridge("Event.worldTick", world));
AttackBlockCallback.EVENT.register((playerEntity, world, hand, blockPos, direction) -> ValueUtil.getEnumValue(ActionResult.class, (String) ScriptCraftCore.useBridge("Event.attackBlock", playerEntity, world, hand.name(), (double) blockPos.getX(), (double) blockPos.getY(), (double) blockPos.getZ(), direction.name()), ActionResult.PASS)); AttackBlockCallback.EVENT.register((playerEntity, world, hand, blockPos, direction) -> ValueUtil.getEnumValue(ActionResult.class, (String) ScriptCraftCore.useBridge("Event.attackBlock", playerEntity, world, hand.name(), blockPos.getX(), blockPos.getY(), blockPos.getZ(), direction.name()), ActionResult.PASS));
UseBlockCallback.EVENT.register((playerEntity, world, hand, hitResult) -> ValueUtil.getEnumValue(ActionResult.class, (String) ScriptCraftCore.useBridge("Event.useBlock", playerEntity, world, hand.name(), (double) hitResult.getBlockPos().getX(), (double) hitResult.getBlockPos().getY(), (double) hitResult.getBlockPos().getZ(), hitResult.getSide().name()), ActionResult.PASS)); UseBlockCallback.EVENT.register((playerEntity, world, hand, hitResult) -> ValueUtil.getEnumValue(ActionResult.class, (String) ScriptCraftCore.useBridge("Event.useBlock", playerEntity, world, hand.name(), hitResult.getBlockPos().getX(), hitResult.getBlockPos().getY(), hitResult.getBlockPos().getZ(), hitResult.getSide().name()), ActionResult.PASS));
UseItemCallback.EVENT.register((playerEntity, world, hand) -> { UseItemCallback.EVENT.register((playerEntity, world, hand) -> {
ActionResult result = ValueUtil.getEnumValue(ActionResult.class, (String) ScriptCraftCore.useBridge("Event.useItem", playerEntity, world, hand.name()), ActionResult.PASS); ActionResult result = ValueUtil.getEnumValue(ActionResult.class, (String) ScriptCraftCore.useBridge("Event.useItem", playerEntity, world, hand.name()), ActionResult.PASS);
ItemStack stack = playerEntity.getStackInHand(hand); ItemStack stack = playerEntity.getStackInHand(hand);

View File

@ -17,13 +17,13 @@ class ItemStackBridges {
((ItemStack) args[0]).setCount((int) ValueUtil.toDouble(args[1], 0)); ((ItemStack) args[0]).setCount((int) ValueUtil.toDouble(args[1], 0));
return null; return null;
}); });
ScriptCraftCore.addBridge("ItemStack.getCount", args -> (double) ((ItemStack) args[0]).getCount()); ScriptCraftCore.addBridge("ItemStack.getCount", args -> ((ItemStack) args[0]).getCount());
ScriptCraftCore.addBridge("ItemStack.setDamage", args -> { ScriptCraftCore.addBridge("ItemStack.setDamage", args -> {
((ItemStack) args[0]).setDamage((int) ValueUtil.toDouble(args[1], 0)); ((ItemStack) args[0]).setDamage((int) ValueUtil.toDouble(args[1], 0));
return null; return null;
}); });
ScriptCraftCore.addBridge("ItemStack.getDamage", args -> (double) ((ItemStack) args[0]).getDamage()); ScriptCraftCore.addBridge("ItemStack.getDamage", args -> ((ItemStack) args[0]).getDamage());
ScriptCraftCore.addBridge("ItemStack.isDamageable", args -> ((ItemStack) args[0]).isDamageable()); ScriptCraftCore.addBridge("ItemStack.isDamageable", args -> ((ItemStack) args[0]).isDamageable());
ScriptCraftCore.addBridge("ItemStack.getTag", args -> ((ItemStack) args[0]).getTag()); ScriptCraftCore.addBridge("ItemStack.getTag", args -> ((ItemStack) args[0]).getTag());

View File

@ -30,7 +30,7 @@ public class CustomItem extends Item {
@Override @Override
public ActionResult useOnBlock(ItemUsageContext context) { public ActionResult useOnBlock(ItemUsageContext context) {
return ValueUtil.getEnumValue(ActionResult.class, (String) ScriptCraftCore.useBridge("CustomItem.onUseOnBlock", id.toString(), context.getWorld(), (double) context.getBlockPos().getX(), (double) context.getBlockPos().getY(), (double) context.getBlockPos().getZ(), context.getSide().name(), context.getPlayer(), context.getHand().name()), ActionResult.PASS); return ValueUtil.getEnumValue(ActionResult.class, (String) ScriptCraftCore.useBridge("CustomItem.onUseOnBlock", id.toString(), context.getWorld(), context.getBlockPos().getX(), context.getBlockPos().getY(), context.getBlockPos().getZ(), context.getSide().name(), context.getPlayer(), context.getHand().name()), ActionResult.PASS);
} }
@Override @Override

View File

@ -33,7 +33,7 @@ export class JavaObjectWrapper {
protected assertValidJavaObject(type: string): void { protected assertValidJavaObject(type: string): void {
const valid = useBridge(type + '.isValid', this.getJavaObject()) as boolean; const valid = useBridge(type + '.isValid', this.getJavaObject()) as boolean;
if (!valid) { if (!valid) {
throw new Error('Provided Java Object Is Not A Valid ' + type); throw new Error('Provided Java Object Does Not Match Type: ' + type);
} }
} }
} }

View File

@ -16,4 +16,4 @@ export { LivingEntity, PlayerEntity } from './entity';
export { CompoundTag, ListTag, NumberType } from './tag'; export { CompoundTag, ListTag, NumberType } from './tag';
export { Registry } from './registry'; export { Registry } from './registry';
export { Inventory } from './inventory'; export { Inventory } from './inventory';
export { Events } from './event'; export { Events, Event, BlockEvent, ItemEvent, WorldEvent } from './event';