This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
ScriptCraft/docs/JAVA_OBJECT_WRAPPERS.md

7 lines
1.1 KiB
Markdown

# Java Object Wrappers
When the JS environment receives a Java object through a [bridge](BRIDGES.md), it is converted into a ```JavaObject```. A ```JavaObject``` cannot be used directly in the JS environment, but it can be stored and passed back to the Java environment using another [bridge](BRIDGES.md).
To make interacting wit Java object easier, they are placed in a wrapper class. An example of a wrapper class is ```World```, it can be interacted with like any other JS object, but internally it uses its ```JavaObject``` to execute code in the Java environment.
## Casting
In Java, if you are given a ```LivingEntity```, you can use ```instanceof``` to check if it is a ```PlayerEntity``` and then cast it. However, because the JS environment only knows the class of the ```JavaObject```'s wrapper, it can't do this. However, you can manually cast a JS object wrapper to another wrapper type using the new wrapper type's constructor, like ```new PlayerEntity(livingEntity);```, if this operation is not possible (ie. The ```LivingEntity``` is not a ```PlayerEntity```), it will throw an ```Error```.