Java – how to add a method to a source file without editing the source file?

I'm trying to add a method to the minecraft source file, but I have to figure out how to do it without actually editing the source file, because redistributing the source file is illegal, and these source files must be included in the mod I'm creating I need to add the method setinportalzub () to the location at net minecraft. entity. The file "entityplayer" in the player. I'm using the MCP / minecraft forge API. I tried to create an instance of entityplayer, but I'm not sure how

Solution

You are about to enter the wonderful and frustrating world of coremods! Coremod (forge modloader) in FML is the "simplest" method to inject arbitrary code into the vanilla minecraft class without distributing modified versions of these class files

This is achieved by using the ASM bytecode operation framework published by Object Web This framework allows you to write java code that can read and manipulate the bytecode of classes at load time

I can't explain every step you have to take to accomplish this feat, so I'll publish a link to my own coremod injection class and try to explain each

Here is the CorePlugin class.

The coreplugin class tells forge modloader where to find the converter class that performs code injection

FML according to your The jar file manifest found this class:

Manifest-Version: 1.0
FMLCorePlugin: bspkrs.treecapitator.fml.asm.TreeCapitatorCorePlugin
FMLCorePluginContainsFMLMod: bspkrs.treecapitator.fml.TreeCapitatorMod
Created-By: 1.7.0 (Oracle Corporation)

Fmlcoreplugin specifies the fully qualified path of the coreplugin class If you are The jar file should contain a normal @ mod style forge modloader mod class. You also need to specify fmlcoreplugincontainsfmlmod (although the expected value is unknown; I don't think it actually matters. You must specify the value there, but the key must be there)

Now for the interesting part... The actual bytecode converter Here is a link to the transformer for Treecapitator

Without a detailed introduction, I wrote this class to handle execution in eclipse and "production" That's why there are two hashmaps; One is used for MCP execution and the other is used for obfuscation execution Each loaded class is first passed to the transform () method The code checks whether it is the class we want to convert, and if so, convert it

The end result of all this is that whenever an instance of iteminworldmanager is created, the transformer runs and adds a row to a specific location in the removeblock () method This line acts as a cheap block interrupt hook, allowing the code to execute when the player breaks a block

Tips:

>Get eclipse's bytecode outline plug-in You can find it on the object web site. > Sometimes, the class structure in eclipse / MCP is different from that in confusion environment > these are not easy to understand concepts Use the bytecode outline plug-in to view the class to be converted, whether it contains the code to be included or not This should help you find the "tag" to find and the ASM node you need to insert

I hope to help!

Edit: fix broken links to reference old branches that still contain classes

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>