I am writing this post regarding an issue I stumbled upon recently. About a week ago, I downloaded a small mod called Villager Names from Natamus for 1.15. As the name of the mod suggests, it generates random names to NPC villagers. It is overall a nice mod, but I noticed one very annoying feature about this mod - it gives female names to the villagers significantly more often than the male ones. I decided to fix this issue, and since I am no stranger to Java coding, it didn't take me long to figure out, using Java Decompiler, where the problem in the mod code is and how to fix it. The only problem is I can't find out how to actually edit the original mod code. I tried writing my own modified class file using NetBeans and then replacing the respective file in the mod folder, but it didn't work since NetBeans refused to compile the modified file. Any ideas? Thanks for reply.
(Note: For those who might think modifiing someone else's mods is unethical, I am not doing this for any questionable reasons. I have no interest in posting my modified version of the mod, I just want my villages to be more gender balanced.)
Depending on what needs to be changed it may be simple to edit the bytecode; for example, until 1.13 I maintained a mod that reverted the changes to cave generation in 1.7, which could be done by simply changing a couple values in the bytecode, which looked like this:
// Decompiled source
int i = this.b.nextInt(this.b.nextInt(this.b.nextInt(15) + 1) + 1);
if (this.b.nextInt(7) != 0) i = 0;
// Bytecode; note that the parameter to nextInt comes before the method call; bipush 15 corresponds to nextInt(15) and bipush 7 corresponds to nextInt(7), and 1.6.4 cave generation uses 40 and 15 respectively
getfield apn/b Ljava/util/Random;
Likewise, if the mod uses a simple random call to determine the chance of a name you can change the value, which may be a float (e.g. nextFloat() < chance) but the same principle applies. More complex edits are much more difficult to do; one thing I once did was to compile the method I wanted to change and copy the bytecode over (this won't help though if it contains references to Minecraft and/or Forge and/or external libraries, which will error since they can't be resolved without a proper setup that includes all the necessary libraries).
Note that the first search result for "java bytecode editor" is the one that I used but it was only designed for Java 5 and earlier so be sure you use one designed for newer versions (it apparently does work if the method being edited only contains Java <= 5 bytecode but only simpler methods can be edited).
Thanks for all replies. Unfortunately, only changing strings won't be enough in this case. I need to partially rewrite an entire method, including adding two new lists and a new block of code which will randomly choose whether to use male or female name. Is there any software that allows the user to decompile the code, perform nesscasary changes and then recompile it again, or any tutorial how to perform that in the bytecode format?
Is there any software that allows the user to decompile the code, perform nesscasary changes and then recompile it again, or any tutorial how to perform that in the bytecode format?
No, you cannot decompile and recompile just that class. You will get compiler linker errors (compiling class not "existing"). Bytecode is the only way, unless you plan on decompiling the entire mod and recompiling it.
If you want to do it in bytecode (assembly) which is the easiest method, TheMasterCaver has already given advice on how to go about doing it (second paragraph).
Another method you could use, I guess, is making your own mod and hijacking that class during runtime with the Sponge Mixin library.