add a bunch of new utility functions

This commit is contained in:
ZXMushroom63 2024-12-27 22:45:56 +08:00
parent f59f9cc7cb
commit 5a9b4905cb
3 changed files with 52 additions and 2 deletions

View File

@ -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()`.

View File

@ -57,4 +57,28 @@ Methods:
});
console.log(multiply(2, 3));
//Logs 6
```
```
- `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.

View File

@ -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) {