TheBrokenRail
1c1563bbbf
Some checks failed
ScriptCraft/pipeline/head There was a failure building this commit
65 lines
1.5 KiB
Markdown
65 lines
1.5 KiB
Markdown
# Bridges
|
|
To communicate between Java and JS code, ScriptCraft utilizes a system called bridges.
|
|
|
|
## JS-To-Java Bridges
|
|
|
|
### Define The Bridge
|
|
```java
|
|
import com.thebrokenrail.scriptcraft.core.ScriptCraftCore;
|
|
import com.thebrokenrail.scriptcraft.core.util.ScriptCraftEntryPoint;
|
|
|
|
public class ExampleBridges implements ScriptCraftEntryPoint {
|
|
@Override
|
|
public String getEntryPoint() {
|
|
return "example-bridges";
|
|
}
|
|
|
|
@Override
|
|
public void registerBridges() {
|
|
ScriptCraftCore.addBridge("ExampleBridges.helloWorld", args -> {
|
|
System.out.println("Hello World! " + args[0]);
|
|
return "Return Value";
|
|
});
|
|
}
|
|
}
|
|
```
|
|
|
|
### Use The Bridge
|
|
```javascript
|
|
import { useBridge } from 'scriptcraft-core';
|
|
|
|
console.log(useBridge('ExampleBridges.helloWorld', 'Argument'));
|
|
```
|
|
|
|
## Java-To-JS Bridges
|
|
|
|
### Define The Bridge
|
|
```javascript
|
|
import { addBridge } from 'scriptcraft-core';
|
|
|
|
addBridge('ExampleBridges.helloWorld', arg => {
|
|
console.log('Hello World! ' + args[0]);
|
|
return 'Return Value';
|
|
});
|
|
```
|
|
|
|
### Use The Bridge
|
|
```java
|
|
import com.thebrokenrail.scriptcraft.core.ScriptCraftCore;
|
|
|
|
public class ExampleBridges {
|
|
public static void run() {
|
|
System.out.println((String) ScriptCraftCore.useBridge("ExampleBridges.helloWorld", "Argument"));
|
|
}
|
|
}
|
|
```
|
|
|
|
## Supported Types
|
|
| JS Type | Java Type |
|
|
| --- | --- |
|
|
| Number | ```Number``` (object-form) |
|
|
| Boolean | ```Boolean``` (object-form) |
|
|
| String | ```String``` |
|
|
| Array | ```Object[]``` |
|
|
| Object | ```null``` |
|
|
| ```JavaObject``` | ```Object``` | |