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.

  • \$\begingroup\$ 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.. \$\endgroup\$
    – Krienie
    Sep 2, 2013 at 22:53
  • 5
    \$\begingroup\$ 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 :) \$\endgroup\$ Sep 2, 2013 at 23:48
  • 3
    \$\begingroup\$ 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 \$\endgroup\$
    – Luke
    Nov 5, 2013 at 19:36
  • \$\begingroup\$ Wait, is the source file available to the legal mod users? Cause if so I have an answer for this question. :) \$\endgroup\$
    – AturSams
    Mar 15, 2014 at 8:33
  • \$\begingroup\$ Would reflection work? (I've used it sparingly in Java coursework when told "don't change the given source".) \$\endgroup\$
    – Anko
    Apr 8, 2014 at 19:23

3 Answers 3


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();
  • \$\begingroup\$ 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? \$\endgroup\$
    – user1430
    Apr 23, 2014 at 16:38
  • \$\begingroup\$ @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! \$\endgroup\$
    – wes
    Apr 23, 2014 at 19:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .