From 5034c122503aac629d38a2a81e12c4931276c918 Mon Sep 17 00:00:00 2001 From: ZXMushroom63 Date: Sun, 1 Sep 2024 17:07:27 +0800 Subject: [PATCH] materials, enchantments, and start of api doc --- docs/apidoc/dedicatedserver.md | 1 + docs/apidoc/hooks.md | 5 +++ docs/apidoc/index.md | 55 +++++++++++++++++++++++++++++++ docs/apidoc/reflect.md | 1 + docs/apidoc/utils.md | 1 + docs/index.md | 2 +- examplemods/no_block_particles.js | 7 +++- postinit.injector.js | 22 +++++++++++-- postinit.js | 5 ++- 9 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 docs/apidoc/dedicatedserver.md create mode 100644 docs/apidoc/hooks.md create mode 100644 docs/apidoc/reflect.md create mode 100644 docs/apidoc/utils.md diff --git a/docs/apidoc/dedicatedserver.md b/docs/apidoc/dedicatedserver.md new file mode 100644 index 0000000..cbdcd2c --- /dev/null +++ b/docs/apidoc/dedicatedserver.md @@ -0,0 +1 @@ +## ModAPI.dedicatedServer \ No newline at end of file diff --git a/docs/apidoc/hooks.md b/docs/apidoc/hooks.md new file mode 100644 index 0000000..3bd910e --- /dev/null +++ b/docs/apidoc/hooks.md @@ -0,0 +1,5 @@ +## ModAPI.hooks +- To replace a function with another, you can use: + - `ModAPI.hooks.methods[ModAPI.util.getMethodFromPackage("com.package.abc.MyClass", "myMethod")] = function () {}` + - To intercept inputs to a function, you can us + - `ModAPI.hooks.methods[ModAPI.util.getMethodFromPackage("com.package.abc.MyClass", "myMethod")] = function () {}` \ No newline at end of file diff --git a/docs/apidoc/index.md b/docs/apidoc/index.md index e69de29..948fe1d 100644 --- a/docs/apidoc/index.md +++ b/docs/apidoc/index.md @@ -0,0 +1,55 @@ +# EaglerForge ModAPI Documentation +The EaglerForge ModAPI is housed in a global JavaScript object stored on `globalThis`, called `ModAPI` or `PluginAPI`. (both are identical) + +The global object has the following properties: + - `ModAPI.player: EntityPlayerSP` + - Only accessible after `ModAPI.require("player")` is called, this is the local player entity. It is regenerated every time the `update` event is called. + - `ModAPI.network: NetHandlerPlayClient` + - Only accessible after `ModAPI.require("network")` is called, this is the client's networking handler. It is regenerated every time the `update` event is called. + - `ModAPI.settings: GameSettings` + - This is the Minecraft client's settings. It is generated upon init. + - `ModAPI.items: Map` + - This is a key-value dictionary of all of the items in the game. It is generated upon init from the static variables of the `Items` class. + - For example, to access the item class for `acacia_door`, you can use `ModAPI.items["acacia_door"]` + - `ModAPI.blocks: Map` + - This is a key-value dictionary of all of the blocks in the game. It is generated upon init from the static variables of the `Blocks` class. + - For example, to access the block class for `bedrock`, you can use `ModAPI.blocks["bedrock"]` + - `ModAPI.materials: Map` + - This is a key-value dictionary of all of the blocks in the game. It is generated upon init from the static variables of the `Material` class. + - For example, to access the material class for `portal`, you can use `ModAPI.materials["portal"]` + - `ModAPI.enchantments: Map` + - This is a key-value dictionary of all of the enchantments in the game. It is generated upon init from the static variables of the `Enchantment` class. + - For example, to access the enchantment class for `knockback`, you can use `ModAPI.enchantments["knockback"]` + - As the enchantment class has other static variables, `Object.keys` will also return non-enchantment keys such as `enchantmentsBookList`. + - `ModAPI.minecraft: Minecraft` + - This is the minecraft instance for the client, generated upon init. + - It can also be accessed using `ModAPI.mc` + - `ModAPI.mcinstance: Raw` + - This is the raw minecraft instance for the client, generated upon init. + - It can also be accessed using `ModAPI.javaClient` + - It can also be accessed using `ModAPI.minecraft.getRef()` + - `ModAPI.server: MinecraftServer` + - This is the dedicated minecraft server in the service worker, generated when the `serverstart`. + - It can only be accessed in the dedicated server's context. (See `ModAPI.dedicatedServer`) + - It can also be accessed using `ModAPI.serverInstance` + - `ModAPI.rawServer: MinecraftServer` + - This is the dedicated minecraft server in the service worker, generated when the `serverstart`. + - It can only be accessed in the dedicated server's context. (See `ModAPI.dedicatedServer`) + - It can also be accessed using `ModAPI.server.getRef()` + - `ModAPI.hooks` + - This is the internal hooking map for ModAPI and can be used to patch, intercept, or rewrite internal functions, and more. + - More: [HooksDocumentation](hooks.md) + - `ModAPI.util` + - This contains utilities for using `ModAPI.hooks`, `ModAPI.reflect`, and more. + - More: [UtilDocumentation](utils.md) + - `ModAPI.reflect` + - This is a wrapper around `ModAPI.hooks`, `ModAPI.hooks._teavm` and `ModAPI.hooks._classMap` that makes accessing and using internal java classes in mods much easier. + - More: [ReflectDocumentation](reflect.md) + - `ModAPI.dedicatedServer` + - This object is used to push code for use in the dedicated server. + - Once the dedicated server worker has started, it is unuseable. + - More: [DedicatedServerDocumentation](dedicatedserver.md) + - `ModAPI.version: String` + - The version of ModAPI. + - `ModAPI.flavour: String` + - The flavour of ModAPI. Hardcoded to be `"injector"`. \ No newline at end of file diff --git a/docs/apidoc/reflect.md b/docs/apidoc/reflect.md new file mode 100644 index 0000000..d1dfc30 --- /dev/null +++ b/docs/apidoc/reflect.md @@ -0,0 +1 @@ +## ModAPI.reflect \ No newline at end of file diff --git a/docs/apidoc/utils.md b/docs/apidoc/utils.md new file mode 100644 index 0000000..5844768 --- /dev/null +++ b/docs/apidoc/utils.md @@ -0,0 +1 @@ +## ModAPI.utils \ No newline at end of file diff --git a/docs/index.md b/docs/index.md index 691ba86..cae7542 100644 --- a/docs/index.md +++ b/docs/index.md @@ -2,7 +2,7 @@ EaglerForge Injector is a tool that uses regular expressions to attatch itself to internal code inside of raw EaglercraftX offline downloads (sounds easy, isn't at all). ### Documentation fragments -[Injector Arcitecture](architecture.md) +[Injector Architecture](architecture.md) [TeaVM quirks](quirks.md) diff --git a/examplemods/no_block_particles.js b/examplemods/no_block_particles.js index 757844a..2148ff2 100644 --- a/examplemods/no_block_particles.js +++ b/examplemods/no_block_particles.js @@ -1 +1,6 @@ -ModAPI.hooks.methods.nmcp_EffectRenderer_addBlockDestroyEffects = ()=>{} \ No newline at end of file +ModAPI.hooks.methods[ + ModAPI.util.getMethodFromPackage( + "net.minecraft.client.particles.EffectRenderer", + "addBlockDestroyEffects" + ) +] = () => {} \ No newline at end of file diff --git a/postinit.injector.js b/postinit.injector.js index fc4f2de..73650ce 100644 --- a/postinit.injector.js +++ b/postinit.injector.js @@ -14,6 +14,7 @@ globalThis.modapi_postinit = `(() => { globalThis.PluginAPI ||= ModAPI; ModAPI.mcinstance ||= {}; ModAPI.javaClient ||= {}; + ModAPI.reflect ||= {}; ModAPI.server = ModAPI.serverInstance = null; ModAPI.dedicatedServer ||= {}; ModAPI.dedicatedServer._data ||= []; @@ -107,6 +108,9 @@ globalThis.modapi_postinit = `(() => { "staticVariableNames": [], "class": item || null, "hasMeta": !!item, + "instanceOf": function (object) { + return ModAPI.hooks._teavm.$rt_isInstance(object, item || null); + }, "compiledName": compiledName } } @@ -145,9 +149,18 @@ globalThis.modapi_postinit = `(() => { } }); }); + ModAPI.reflect.classes = Object.values(ModAPI.hooks._classMap); console.log("[ModAPI] Regenerated hook classmap."); } ModAPI.hooks.regenerateClassMap(); + ModAPI.reflect.getClassById = function (classId) { + return ModAPI.hooks._classMap[ModAPI.util.getCompiledName(classId)]; + } + ModAPI.reflect.getClassByName = function (className) { + var classKeys = Object.keys(ModAPI.hooks._classMap); + var key = classKeys.filter(k => {k.endsWith("_" + className)})[0]; + return key ? ModAPI.hooks._classMap[key] : null; + } var reloadDeprecationWarnings = 0; const TeaVM_to_BaseData_ProxyConf = { get(target, prop, receiver) { @@ -397,6 +410,7 @@ globalThis.modapi_postinit = `(() => { ModAPI.hooks.methods[initMethodName] = function (...args) { var x = originalInit.apply(this, args); //args[0] means $this (ie: minecraft instance). + ModAPI.mc = ModAPI.minecraft = new Proxy(args[0], TeaVM_to_Recursive_BaseData_ProxyConf); ModAPI.mcinstance = ModAPI.javaClient = args[0]; ModAPI.settings = new Proxy(ModAPI.mcinstance.$gameSettings, TeaVM_to_Recursive_BaseData_ProxyConf); @@ -405,8 +419,6 @@ globalThis.modapi_postinit = `(() => { return x; }; - ModAPI.events.newEvent("load", "client"); - var integratedServerStartup = ModAPI.util.getMethodFromPackage("net.lax1dude.eaglercraft.v1_8.sp.internal.ClientPlatformSingleplayer", "loadIntegratedServerSourceInline"); //Integrated server setup has a randomised suffix on the end integratedServerStartup = ModAPI.hooks._rippedMethodKeys.filter(key => { return key.startsWith(integratedServerStartup); })[0]; @@ -418,6 +430,8 @@ globalThis.modapi_postinit = `(() => { return x; }; + ModAPI.events.newEvent("load", "client"); + ModAPI.events.newEvent("sendchatmessage", "client"); const sendChatMessageMethodName = ModAPI.util.getMethodFromPackage("net.minecraft.client.entity.EntityPlayerSP", "sendChatMessage"); const sendChatMessage = ModAPI.hooks.methods[sendChatMessageMethodName]; @@ -453,7 +467,7 @@ globalThis.modapi_postinit = `(() => { const serverStartMethod = ModAPI.hooks.methods[serverStartMethodName]; ModAPI.hooks.methods[serverStartMethodName] = function ($this) { var x = serverStartMethod.apply(this, [$this]); - ModAPI.server = ModAPI.serverInstance = new Proxy($this, ModAPI.util.TeaVM_to_Recursive_BaseData_ProxyConf); + ModAPI.server = new Proxy($this, ModAPI.util.TeaVM_to_Recursive_BaseData_ProxyConf); ModAPI.rawServer = $this; ModAPI.events.callEvent("serverstart", {}); return x; @@ -511,6 +525,8 @@ globalThis.modapi_postinit = `(() => { ModAPI.items = new Proxy(ModAPI.hooks._classMap[ModAPI.util.getCompiledName("net.minecraft.init.Items")].staticVariables, StaticProps_ProxyConf); ModAPI.blocks = new Proxy(ModAPI.hooks._classMap[ModAPI.util.getCompiledName("net.minecraft.init.Blocks")].staticVariables, StaticProps_ProxyConf); + ModAPI.materials = new Proxy(ModAPI.hooks._classMap[ModAPI.util.getCompiledName("net.minecraft.block.material.Material")].staticVariables, StaticProps_ProxyConf); + ModAPI.enchantments = new Proxy(ModAPI.hooks._classMap[ModAPI.util.getCompiledName("net.minecraft.enchantment.Enchantment")].staticVariables, StaticProps_ProxyConf); const originalOptionsInit = ModAPI.hooks.methods[ModAPI.util.getMethodFromPackage("net.minecraft.client.gui.GuiOptions", "initGui")]; diff --git a/postinit.js b/postinit.js index 68fee18..c392128 100644 --- a/postinit.js +++ b/postinit.js @@ -410,6 +410,7 @@ ModAPI.hooks.methods[initMethodName] = function (...args) { var x = originalInit.apply(this, args); //args[0] means $this (ie: minecraft instance). + ModAPI.mc = ModAPI.minecraft = new Proxy(args[0], TeaVM_to_Recursive_BaseData_ProxyConf); ModAPI.mcinstance = ModAPI.javaClient = args[0]; ModAPI.settings = new Proxy(ModAPI.mcinstance.$gameSettings, TeaVM_to_Recursive_BaseData_ProxyConf); @@ -466,7 +467,7 @@ const serverStartMethod = ModAPI.hooks.methods[serverStartMethodName]; ModAPI.hooks.methods[serverStartMethodName] = function ($this) { var x = serverStartMethod.apply(this, [$this]); - ModAPI.server = ModAPI.serverInstance = new Proxy($this, ModAPI.util.TeaVM_to_Recursive_BaseData_ProxyConf); + ModAPI.server = new Proxy($this, ModAPI.util.TeaVM_to_Recursive_BaseData_ProxyConf); ModAPI.rawServer = $this; ModAPI.events.callEvent("serverstart", {}); return x; @@ -524,6 +525,8 @@ ModAPI.items = new Proxy(ModAPI.hooks._classMap[ModAPI.util.getCompiledName("net.minecraft.init.Items")].staticVariables, StaticProps_ProxyConf); ModAPI.blocks = new Proxy(ModAPI.hooks._classMap[ModAPI.util.getCompiledName("net.minecraft.init.Blocks")].staticVariables, StaticProps_ProxyConf); + ModAPI.materials = new Proxy(ModAPI.hooks._classMap[ModAPI.util.getCompiledName("net.minecraft.block.material.Material")].staticVariables, StaticProps_ProxyConf); + ModAPI.enchantments = new Proxy(ModAPI.hooks._classMap[ModAPI.util.getCompiledName("net.minecraft.enchantment.Enchantment")].staticVariables, StaticProps_ProxyConf); const originalOptionsInit = ModAPI.hooks.methods[ModAPI.util.getMethodFromPackage("net.minecraft.client.gui.GuiOptions", "initGui")];