diff --git a/docs/apidoc/index.md b/docs/apidoc/index.md index 5336311..77d5e78 100644 --- a/docs/apidoc/index.md +++ b/docs/apidoc/index.md @@ -141,4 +141,26 @@ For example, take the method `setRenderViewEntity()` on `ModAPI.mcinstance`. Ins ```javascript var entityIndex = 1; //Index of the entity to look for. 0 means first, which is usually the player, so 1 is usually a natural entity. ModAPI.mc.setRenderViewEntity(ModAPI.world.loadedEntityList.get(entityIndex).getRef()); -``` \ No newline at end of file +``` + +## Corrective Proxies +By default, accessing a global like `ModAPI.player` will return a proxy to the original player that removes $ prefixes, as well as making instance methods callable. TeaVM has a quirk where it adds numerical suffixes to some properties. For example `ModAPI.player.inGround0` instead of `ModAPI.player.inGround`. As this is a large issue due to these suffixes changing for every eaglercraft update, you can now bypass this by obtaining a corrective version of `ModAPI.player`, using `ModAPI.player.getCorrective()`. + +For example: +```javascript +ModAPI.player.inGround //returns undefined +ModAPI.player.inGround0 //returns 1 or 0, the correct value + +ModAPI.player.isCorrective() //returns false + +var correctedPlayer = ModAPI.player.getCorrective(); + +correctedPlayer.inGround //returns 1 or 0, the correct value +correctedPlayer.inGround0 //returns 1 or 0, the correct value + +correctedPlayer.isCorrective() //returns true +``` + +You can check if an object is corrective using `.isCorrective()`; + +Accessing children of a corrective object will also make them corrective. `correctedPlayer.fishEntity.isCorrective(); //true` \ No newline at end of file diff --git a/docs/quirks.md b/docs/quirks.md index 92e3643..c719645 100644 --- a/docs/quirks.md +++ b/docs/quirks.md @@ -2,7 +2,7 @@ When TeaVM compiles code, it sometimes does strange things. #### Property Suffixes -TeaVM will add suffixes to some variables, seemingly randomly. An example is the property `inGround` of any entity. When accessing this on the `ModAPI.player.fishEntity` object, TeaVM has renamed it to `inGround2`. Can be mitigated with `ModAPI.util.getNearestProperty`. +TeaVM will add suffixes to some variables, seemingly randomly. An example is the property `inGround` of any entity. When accessing this on the `ModAPI.player.fishEntity` object, TeaVM has renamed it to `inGround2`. Can be mitigated with `ModAPI.util.getNearestProperty`, or, even better, using a corrective version of ModAPI.player. #### Collapsing Methods When I was trying to hook into the server-side processing of chat messages, I found that chat packets were handled by the method `processChatMessage` in `NetHandlerPlayServer`. However, in the compiled JavaScript, this method no longer exists. This is because it is only used once, in the `processPacket` method of `C01PacketChatMessage`. TeaVM automatically saw this, and collapsed one method into the other. diff --git a/patches.js b/patches.js index 46e23c2..7d59a59 100644 --- a/patches.js +++ b/patches.js @@ -1,11 +1,17 @@ class PatchesRegistry { - static patchedEventNames = [] + static patchFns = [] static patchedEventNames = [] static getEventInjectorCode() { - return globalThis.modapi_specialevents = "[" + PatchesRegistry.patchedEventNames.flatMap(x=>`\`${x}\``).join(",") + "]" + return "globalThis.modapi_specialevents = [" + PatchesRegistry.patchedEventNames.flatMap(x=>`\`${x}\``).join(",") + "]" + } + static addPatch(fn) { + patchFns.push(fn); + } + static addSpecialEvent(x) { + PatchesRegistry.patchedEventNames.push(x); } } -function addPatch(params) { +function addPatch() { } function addSpecialEvent(eventName) {