From 5a9b4905cbf670b71b3151ea833e3e8eb790633f Mon Sep 17 00:00:00 2001 From: ZXMushroom63 Date: Fri, 27 Dec 2024 22:45:56 +0800 Subject: [PATCH] add a bunch of new utility functions --- docs/apidoc/index.md | 4 ++++ docs/apidoc/utils.md | 26 +++++++++++++++++++++++++- postinit.js | 24 +++++++++++++++++++++++- 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/docs/apidoc/index.md b/docs/apidoc/index.md index e88d90f..8f1d0dd 100644 --- a/docs/apidoc/index.md +++ b/docs/apidoc/index.md @@ -148,6 +148,10 @@ For example, take the method `setRenderViewEntity()` on `ModAPI.mcinstance`. Ins 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()); ``` +Note that an entry for `getRef` will not appear from `Object.keys` and similar web APIs. + +## Checking if an object is a proxy +All proxied objects have a property `isModProxy` which is equal to true. Note that an entry for `isModProxy` will not appear from `Object.keys` and similar web APIs. ## 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()`. diff --git a/docs/apidoc/utils.md b/docs/apidoc/utils.md index 0d3a635..054469e 100644 --- a/docs/apidoc/utils.md +++ b/docs/apidoc/utils.md @@ -57,4 +57,28 @@ Methods: }); console.log(multiply(2, 3)); //Logs 6 - ``` \ No newline at end of file + ``` +- `ModAPI.util.modifyFunction(fn: Function, patcherFunction: Function) : string` + - Returns a modifies version of a function, where the patcher function can be used to modify the contents of a function. Example: + ```javascript + function add(a, b) { + return a + b; + } + var multiply = ModAPI.util.modifyFunction(add, (code)=>{ + return code.replaceAll("a + b", "a * b"); + }); + console.log(multiply(2, 3)); + //Logs 6 + ``` +- `ModAPI.util.getIdFromItem(item: Item) : number` + - Gets the ID of an item +- `ModAPI.util.getItemById(id: number) : Item` + - Gets an item by it's ID +- `ModAPI.util.getItemFromBlock(block: Block) : ItemBlock` + - Gets an item from a block. +- `ModAPI.util.getIdFromBlock(block: Block) : number` + - Gets the ID of a block +- `ModAPI.util.getBlockById(id: number) : Block` + - Gets a block by it's ID +- `ModAPI.util.getBlockFromItem(item: Item) : Block` + - Gets a block from an ItemBlock instance. \ No newline at end of file diff --git a/postinit.js b/postinit.js index ed53278..c70fb07 100644 --- a/postinit.js +++ b/postinit.js @@ -1,4 +1,4 @@ -globalThis.ModAPIVersion = "v2.3.4"; +globalThis.ModAPIVersion = "v2.3.5"; globalThis.modapi_postinit = "(" + (() => { //EaglerForge post initialization code. //This script cannot contain backticks, escape characters, or backslashes in order to inject into the dedicated server code. @@ -35,6 +35,17 @@ globalThis.modapi_postinit = "(" + (() => { return x; } } + function easyStaticMethod(classId, methodName, autoUnpack) { + var method = ModAPI.reflect.getClassById(classId).staticMethods[methodName].method; + return function easyImpl(...args) { + return method(...(autoUnpack ? args.map(x=>{ + if ((typeof x === "object") && (x.isModProxy === true)) { + return x.getRef(); + } + return x; + }) : args)) + } + } ModAPI.meta.title = function (title) { if (!document.currentScript || document.currentScript.getAttribute("data-isMod") !== "true") { return console.log("[ModAPIMeta] Cannot set meta for non-mod script."); @@ -121,6 +132,14 @@ globalThis.modapi_postinit = "(" + (() => { }); return name; } + + ModAPI.util.getIdFromItem = easyStaticMethod("net.minecraft.item.Item", "getIdFromItem", true); + ModAPI.util.getItemById = easyStaticMethod("net.minecraft.item.Item", "getItemById", false); + ModAPI.util.getItemFromBlock = easyStaticMethod("net.minecraft.item.Item", "getItemFromBlock", true); + ModAPI.util.getBlockById = easyStaticMethod("net.minecraft.block.Block", "getBlockById", false); + ModAPI.util.getBlockFromItem = easyStaticMethod("net.minecraft.block.Block", "getBlockFromItem", true); + ModAPI.util.getIdFromBlock = easyStaticMethod("net.minecraft.block.Block", "getIdFromBlock", true); + ModAPI.util.wrap = function (outputValue, target, corrective, disableFunctions) { target ||= {}; corrective ||= false; @@ -427,6 +446,9 @@ globalThis.modapi_postinit = "(" + (() => { } } } + if (prop === "isModProxy") { + return true + } var outProp = "$" + prop; if (this._corrective) {