From 0f63f63d8655aacde4537f8253b292a9f39e35e2 Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Tue, 2 Jun 2020 16:01:26 -0400 Subject: [PATCH] Update API and Docs --- README.md | 4 +-- docs/README.md | 2 +- docs/tutorials/EVENT.md | 25 +++++++++++++++++++ docs/tutorials/README.md | 4 ++- ...l_scriptcraft_core_quickjs_QuickJSNative.c | 6 ++--- .../scriptcraft/api/block/CustomBlock.java | 2 +- .../api/block/CustomBlockEntity.java | 3 +-- .../scriptcraft/api/bridge/EventBridges.java | 4 +-- .../api/bridge/ItemStackBridges.java | 4 +-- .../scriptcraft/api/item/CustomItem.java | 2 +- scriptcraft/src/main/ts/src/minecraft/core.ts | 2 +- .../src/main/ts/src/minecraft/index.ts | 2 +- 12 files changed, 43 insertions(+), 17 deletions(-) create mode 100644 docs/tutorials/EVENT.md diff --git a/README.md b/README.md index aa97675..560dc52 100644 --- a/README.md +++ b/README.md @@ -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/) JS API for Minecraft -## API -[View API](docs/README.md) +## Documentation +[View Documentation](docs/README.md) ## Examples [View Examples](examples) \ No newline at end of file diff --git a/docs/README.md b/docs/README.md index 8f4e0a2..0b85d9c 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1,4 +1,4 @@ -# API +# Documentation [View Module Resolution](MODULE_RESOLUTION.md) [View Entry-Points](ENTRY_POINTS.md) diff --git a/docs/tutorials/EVENT.md b/docs/tutorials/EVENT.md new file mode 100644 index 0000000..f81cbff --- /dev/null +++ b/docs/tutorials/EVENT.md @@ -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..trigger(); +``` \ No newline at end of file diff --git a/docs/tutorials/README.md b/docs/tutorials/README.md index fad804f..665be6a 100644 --- a/docs/tutorials/README.md +++ b/docs/tutorials/README.md @@ -3,4 +3,6 @@ [Create A Block Entity](BLOCK_ENTITY.md) -[Create A Item](ITEM.md) \ No newline at end of file +[Create A Item](ITEM.md) + +[Use An Event](EVENT.md) \ No newline at end of file diff --git a/scriptcraft/src/main/c/com_thebrokenrail_scriptcraft_core_quickjs_QuickJSNative.c b/scriptcraft/src/main/c/com_thebrokenrail_scriptcraft_core_quickjs_QuickJSNative.c index 5fe8ded..43e2e6c 100644 --- a/scriptcraft/src/main/c/com_thebrokenrail_scriptcraft_core_quickjs_QuickJSNative.c +++ b/scriptcraft/src/main/c/com_thebrokenrail_scriptcraft_core_quickjs_QuickJSNative.c @@ -205,7 +205,7 @@ static JSValue java_object_to_js_object(JNIEnv *env, JSContext *ctx, jobject obj JSValue out; 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 array_clazz = (*env)->FindClass(env, "[Ljava/lang/Object;"); 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"); jboolean val = (*env)->CallBooleanMethod(env, obj, boolean_methodID); out = JS_NewBool(ctx, val); - } else if ((*env)->IsInstanceOf(env, obj, double_clazz)) { - jmethodID double_methodID = (*env)->GetMethodID(env, double_clazz, "doubleValue", "()D"); + } else if ((*env)->IsInstanceOf(env, obj, number_clazz)) { + jmethodID double_methodID = (*env)->GetMethodID(env, number_clazz, "doubleValue", "()D"); jdouble val = (*env)->CallDoubleMethod(env, obj, double_methodID); out = JS_NewFloat64(ctx, val); } else if ((*env)->IsInstanceOf(env, obj, array_clazz)) { diff --git a/scriptcraft/src/main/java/com/thebrokenrail/scriptcraft/api/block/CustomBlock.java b/scriptcraft/src/main/java/com/thebrokenrail/scriptcraft/api/block/CustomBlock.java index 449db37..4e6fa16 100644 --- a/scriptcraft/src/main/java/com/thebrokenrail/scriptcraft/api/block/CustomBlock.java +++ b/scriptcraft/src/main/java/com/thebrokenrail/scriptcraft/api/block/CustomBlock.java @@ -23,6 +23,6 @@ public class CustomBlock extends Block { @Override 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); } } diff --git a/scriptcraft/src/main/java/com/thebrokenrail/scriptcraft/api/block/CustomBlockEntity.java b/scriptcraft/src/main/java/com/thebrokenrail/scriptcraft/api/block/CustomBlockEntity.java index 7db2083..e710420 100644 --- a/scriptcraft/src/main/java/com/thebrokenrail/scriptcraft/api/block/CustomBlockEntity.java +++ b/scriptcraft/src/main/java/com/thebrokenrail/scriptcraft/api/block/CustomBlockEntity.java @@ -1,7 +1,6 @@ package com.thebrokenrail.scriptcraft.api.block; import com.thebrokenrail.scriptcraft.core.ScriptCraftCore; -import com.thebrokenrail.scriptcraft.core.quickjs.QuickJSManager; import net.fabricmc.fabric.api.block.entity.BlockEntityClientSerializable; import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntityType; @@ -54,7 +53,7 @@ public class CustomBlockEntity extends BlockEntity implements BlockEntityClientS @Override public void setLocation(World world, BlockPos 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") diff --git a/scriptcraft/src/main/java/com/thebrokenrail/scriptcraft/api/bridge/EventBridges.java b/scriptcraft/src/main/java/com/thebrokenrail/scriptcraft/api/bridge/EventBridges.java index d944ba3..a88e636 100644 --- a/scriptcraft/src/main/java/com/thebrokenrail/scriptcraft/api/bridge/EventBridges.java +++ b/scriptcraft/src/main/java/com/thebrokenrail/scriptcraft/api/bridge/EventBridges.java @@ -13,8 +13,8 @@ import net.minecraft.util.TypedActionResult; class EventBridges { static void register() { 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)); - 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)); + 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(), hitResult.getBlockPos().getX(), hitResult.getBlockPos().getY(), hitResult.getBlockPos().getZ(), hitResult.getSide().name()), ActionResult.PASS)); UseItemCallback.EVENT.register((playerEntity, world, hand) -> { ActionResult result = ValueUtil.getEnumValue(ActionResult.class, (String) ScriptCraftCore.useBridge("Event.useItem", playerEntity, world, hand.name()), ActionResult.PASS); ItemStack stack = playerEntity.getStackInHand(hand); diff --git a/scriptcraft/src/main/java/com/thebrokenrail/scriptcraft/api/bridge/ItemStackBridges.java b/scriptcraft/src/main/java/com/thebrokenrail/scriptcraft/api/bridge/ItemStackBridges.java index 2ced408..4ee20d0 100644 --- a/scriptcraft/src/main/java/com/thebrokenrail/scriptcraft/api/bridge/ItemStackBridges.java +++ b/scriptcraft/src/main/java/com/thebrokenrail/scriptcraft/api/bridge/ItemStackBridges.java @@ -17,13 +17,13 @@ class ItemStackBridges { ((ItemStack) args[0]).setCount((int) ValueUtil.toDouble(args[1], 0)); 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 -> { ((ItemStack) args[0]).setDamage((int) ValueUtil.toDouble(args[1], 0)); 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.getTag", args -> ((ItemStack) args[0]).getTag()); diff --git a/scriptcraft/src/main/java/com/thebrokenrail/scriptcraft/api/item/CustomItem.java b/scriptcraft/src/main/java/com/thebrokenrail/scriptcraft/api/item/CustomItem.java index a98e092..fa58352 100644 --- a/scriptcraft/src/main/java/com/thebrokenrail/scriptcraft/api/item/CustomItem.java +++ b/scriptcraft/src/main/java/com/thebrokenrail/scriptcraft/api/item/CustomItem.java @@ -30,7 +30,7 @@ public class CustomItem extends Item { @Override 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 diff --git a/scriptcraft/src/main/ts/src/minecraft/core.ts b/scriptcraft/src/main/ts/src/minecraft/core.ts index b0ba931..3e4811d 100644 --- a/scriptcraft/src/main/ts/src/minecraft/core.ts +++ b/scriptcraft/src/main/ts/src/minecraft/core.ts @@ -33,7 +33,7 @@ export class JavaObjectWrapper { protected assertValidJavaObject(type: string): void { const valid = useBridge(type + '.isValid', this.getJavaObject()) as boolean; if (!valid) { - throw new Error('Provided Java Object Is Not A Valid ' + type); + throw new Error('Provided Java Object Does Not Match Type: ' + type); } } } diff --git a/scriptcraft/src/main/ts/src/minecraft/index.ts b/scriptcraft/src/main/ts/src/minecraft/index.ts index 26bd268..96e1cf9 100644 --- a/scriptcraft/src/main/ts/src/minecraft/index.ts +++ b/scriptcraft/src/main/ts/src/minecraft/index.ts @@ -16,4 +16,4 @@ export { LivingEntity, PlayerEntity } from './entity'; export { CompoundTag, ListTag, NumberType } from './tag'; export { Registry } from './registry'; export { Inventory } from './inventory'; -export { Events } from './event'; +export { Events, Event, BlockEvent, ItemEvent, WorldEvent } from './event';