# How do I add a method to a class without editing the source file?

I am trying to add a method to a Minecraft source file, but I have to figure out how to do it without actually editing the source files, as it would be illegal to redistribute Minecraft's source files with the mod I am creating.

I need to add the method setInPortalZub() to the EntityPlayer class located in net.minecraft.entity.player. I am using the MCP/Minecaft Forge API. I have tried creating an EntityPlayer instance, but I'm not exactly sure how to make this work.

• So you mean you can only make instances of EntityPlayer? If that's the case, you could make a wrapper and add your own functions from there.. – Krienie Sep 2 '13 at 22:53
• Nice to see software licensing terms are being studied before coding these days. I know that sounds sarcastic, but I genuinely mean it. Often on mod. projects people dive into the coding before realizing whether what they're doing is legal or not. That said, why do you have to insert something into the source code? Cannot you create a specialized subclass? The only way to really insert something into EntityPlayer without modifying the source code would be to disassemble, insert some Java bytecode and then re-assemble, and that's even less likely to be legal :) – Andon M. Coleman Sep 2 '13 at 23:48
• Welcome to Minecraft modding, where basically you have to work around the crappy modloader/forge apis, and not to mention Minecraft itself. What I usually found myself doing was creating hash maps with player ids and flags/data to store arbitrary data rather than relying on the actual entity player class. It's pretty dodgy but hey it's a mod – Luke Nov 5 '13 at 19:36
• Wait, is the source file available to the legal mod users? Cause if so I have an answer for this question. :) – wolfdawn Mar 15 '14 at 8:33
• Would reflection work? (I've used it sparingly in Java coursework when told "don't change the given source".) – Anko Apr 8 '14 at 19:23

Only way to change a class without extending it (or without access to the source) is by using reflection. For java there is the rather comprehensive Java Reflection API that is made for this task. It might be a bit daunting at first.

Anyway the operation you are looking for is called injection. Here is an example of injecting a method into the class at runtime.

Actually, if you don't need to change any of the minecraft core functionality, I would recommend using a mod API such as Minecraft Forge. It allows you to listen to player events and add blocks/entities. However, if you do need to edit core minecraft functionality, use the Mincraft Coder Pack, or MCP, it allows you to decompile minecraft, as well as recompile and obfusicate the code, and it will give you the compiled files that were modified, if you do that, then you can just, give them those files to put in the minecraft.jar

Extend the class, then make your own method on the extension

MyClass extends TargetClass
{
void myMethod();
}

• For this to work, all existing instantiations of TargetClass (EntityPlayer in the OP's case) would need to be replaced with instantiations of MyClass unless the OP does not need to use his new method on anything except objects he also manually creates (which seems unlikely). How is that accomplished without modifying the code, violating the original constraint? – Josh Apr 23 '14 at 16:38
• @JoshPetrie That's a very good point. For some reason, I took that as 'without modifying the class' rather than 'without modifying the source'. I stand very much corrected. Thanks! – wes Apr 23 '14 at 19:21