From f58750a08aba4db2618310cac4dcc44cbd8d6828 Mon Sep 17 00:00:00 2001 From: ZXMushroom63 Date: Thu, 5 Sep 2024 18:06:42 +0800 Subject: [PATCH] add reflect doc --- docs/apidoc/reflect.md | 70 +++++++++++++++++++++++++++++++++++++++++- postinit.js | 8 ++--- 2 files changed, 72 insertions(+), 6 deletions(-) diff --git a/docs/apidoc/reflect.md b/docs/apidoc/reflect.md index d1dfc30..9db03ad 100644 --- a/docs/apidoc/reflect.md +++ b/docs/apidoc/reflect.md @@ -1 +1,69 @@ -## ModAPI.reflect \ No newline at end of file +## ModAPI.reflect + +ModAPI.reflect supplies a user friendly wrapper for accessing and interacting with java classes from javascript. + +Properties: + +- `classes: ReflectClass[]` + - `ModAPI.reflect.classes` is an array of ReflectClasses, representing (almost) every java class. + +Methods: + +- `ModAPI.reflect.getClassById(classId: String) : ReflectClass` + - This method is used to find a class by its id. + - For example, to get the `Minecraft` class, you can use `ModAPI.reflect.getClassById("net.minecraft.client.Minecraft")` +- `ModAPI.reflect.getClassByName(className: String) : ReflectClass` + - This method is used to find a class by its id. + - For example, to get the `Minecraft` class, you can use `ModAPI.reflect.getClassById("Minecraft")` + - This runs slower than `getClassById` because it has to filter through all classes. Make sure to cache the result rather than calling it over and over again. + +### ReflectClass Definition + +Each `ReflectClass` has the following properties: + +- `binaryName: String?` + - The binary name of the class in question. Eg: `Lnet.minecraft.client.entity.EntityPlayerSP;` + - Will be `null` if `hasMeta` is equal to `false` +- `class: Class?` + - The actual class. Cannot be used easily with `new` keyword, this is mostly useful for extending using `prototype`. + - Will be `null` if `hasMeta` is equal to `false` +- `compiledName: String` + - The TeaVM compiled name of the class. It is equal to the first letters of the package, an underscore, and the class name. Eg: `nmce_EntityPlayerSP` +- `constructors: Function[]` + - The constructors for the class. +- `hasMeta: Boolean` + - Wether or not the class has metadata saved by TeaVM. +- `id: String?` + - The class id of the class. Eg: `net.minecraft.client.entity.EntityPlayerSP` + - Will be `null` if `hasMeta` is equal to `false` +- `name: String` + - The name of the class. Eg: `EntityPlayerSP` +- `methods: Map` +- `staticMethods: Map` +- `staticVariableNames: String[]` + - List of all the static variable names for the class. +- `staticVariables: Map` + - key-value dictionary of all the static variables in a class. +- `superclass: String?` + - The class id of the class's superclass. Eg: `net.minecraft.client.entity.AbstractClientPlayer` + - Will be `null` if `hasMeta` is equal to `false` + +Each `ReflectClass` has the following methods: + +- `instanceOf(object: Object)` + - Checks if the `object` is an instance of the class. + +### ReflectMethod Definition + +Each `ReflectMethod` has the following properties: + +- `methodName: String` + - The compiled method name of the method. Eg: `nmce_EntityPlayerSP_closeScreen`. +- `methodNameShort: String` + - The short name of the method. Eg: `closeScreen`. + +Each `ReflectClass` has the following methods: +- `method(...)` + - This is the java method. + - If it is an instance method (accessed from a ReflectClasses' `methods` property), the first argument should be an instance of the class. Eg: `ModAPI.reflect.getClassByName("EntityPlayerSP").methods.closeScreen.method(ModAPI.player.getRef())` + - If it is a static method (accessed from a ReflectClasses' `staticMethods` property), call the method as usual. Eg: `ModAPI.reflect.getClassById("net.minecraft.init.Items").staticMethods.getRegisteredItem.method(ModAPI.util.str("apple"))` \ No newline at end of file diff --git a/postinit.js b/postinit.js index 17f6161..cf1a229 100644 --- a/postinit.js +++ b/postinit.js @@ -141,10 +141,8 @@ } targetMethodMap[method.replace(compiledName + "_", "")] = { method: ModAPI.hooks.methods[method], - proxiedMethod: function (...args) { - return ModAPI.hooks.methods[method].apply(this, args); - }, - methodName: method + methodName: method, + methodNameShort: method.replace(compiledName + "_", "") }; //Prototype Injection, allows for far easier access to methods @@ -165,7 +163,7 @@ } ModAPI.reflect.getClassByName = function (className) { var classKeys = Object.keys(ModAPI.hooks._classMap); - var key = classKeys.filter(k => {k.endsWith("_" + className)})[0]; + var key = classKeys.filter(k => {return ModAPI.hooks._classMap[k].name === className})[0]; return key ? ModAPI.hooks._classMap[key] : null; } var reloadDeprecationWarnings = 0;