Alex Mm
Alex Mm

Reputation: 75

How do I edit Minecraft Mod Class files?

I'm trying to edit a single line in Pam's Harvestcraft mod. The reason is that in the game these "gardens" inside the mod spawn too often. There is a config file for this mod, however the lowest possible value for rarity = 1 is still too high. This rarity is an int, and I want to change it to a float inside this Mod's jar's .class file, so that I may be able to use 0.5 or something less than 1 to decrease the chance of these gardens spawning.

I copy and pasted the .class file into intelliJ, but it is readable only. Writing my own text file and changing the extension to .class obviously corrupts the file.

For the past hour I've been trying to Google the answer. People say that you might need to set up a modding environment, decompile the code, then recompile it after the changes. This seems very long-winded for a simple line of code editing.

Edit: I haven't figured it out yet. Something to add: I downloaded this class file editor http://dirty-joe.com/ It let's me edit the field with the int to float. However there are 2 problems with this;

1, I also need to edit a method that getInt() to getFloat() which I cannot do.

2, When I save the newly edited class file, then try to reopen it using this JOE editor IT IS GIVING ME A FILE ERROR. This indicated to me that if I edit the class file it corrupts it anyways.

Upvotes: 1

Views: 30626

Answers (2)

Go make an issue or Pull Request on HarvestCraft's GitHub page.

Alternatively, you can download the source from there, make the change, and compile up a version that you can use for yourself. You, of course, will have to set up a Forge development environment to do this, but that isn't very hard either:

  1. Go to Forge's download page
  2. Download the MDK for the appropriate version of Minecraft
  3. Unzip into a directory
  4. Run gradlew setupDecompWorkspace and gradlew setupEclipse from the command line in this directory
  5. Download and install Eclipse
  6. Add HarvestCraft's src directory to the Forge src directory (so that the contents of the one ends up in the other Forge/src/java, not Forge/src/src/java).
  7. Run Eclipse, tell it to open an existing workspace, point it at where you installed Forge.
  8. Make your changes
  9. Run gradlew build
  10. Get the resulting jar from Forge/builds/lib

Upvotes: 2

Christopher Schneider
Christopher Schneider

Reputation: 3915

You technically can do this, but it's going to be a pain. If it were just changing a variable, that might be simple, but now you're also talking about changing a field's type.

There are a couple ways to do this, and I don't see one of them listed, so I'll mention that.

First, you can decompile/recompile. I've tried doing this once before, and it really doesn't work well. There were plenty of errors resulting from the recompilation, because it's not going to be a completely accurate decompilation.

The second option, and this would be the easiest if not for needing to change a field type, is editing the machine code. Java is compiled into assembly code that is specifically used by the JVM. You would find the instruction, change the value, and you would be golden. (I say easiest, but if you've never seen assembly before, it'll all look like Greek to you.)

Back to the issue: You're talking about changing a data type. That won't be enough. You not only would need to change the data type, but every spot that data type is used and its get method is called. Every spot that getter is called, its expecting an int return value. If it's a float going into an int, it'll just be automatically converted to an int.

So that is how you would do it, but it's not really worth your time. Simply ask the developer to make an update for you. In fact, if this is the mod you're talking about, it's already open source and you can make the changes yourself.

Upvotes: 2

Related Questions