Prior to today I had been coding my mods in NotePad++, occasionally calling the java compiler from the command line when I needed more detailed error messages. Not exactly ideal. So I decided to pick up a real Java IDE, and ended up choosing NetBeans. It's been such a massive improvement that I figured I would help pave the way for any others who want to try it out. Here's a tutorial to get an existing mod working in NetBeans. If I explained something poorly, click the spoiler tag underneath that paragraph for pictures.
(Keep in mind I've only been using NetBeans for a few hours and figured it out as I wrote this tutorial, so it's likely some of this information might be wrong. Code at your own risk!
"This looks like a really difficult tutorial. I mean, it's like four pages long. Is this really worth it?"It's not as hard as it looks, I'm just terrible at writing tutorials
. Plus, it has lots of pretty pictures to look at
. As for why you would want to work in a real IDE, there are a few benefits: most importantly, you will get instant feedback on any bugs you make. If you've been editing the java files in Notepad++ or something so far, you're probably intimately familiar with the following: trying to launch Starfarer, having it crash, then reading the end of a logfile to get a uselessly cryptic error message about what went wrong. With NetBeans you get
real error messages, ones you can Google for help with, and you get them as you type. Bugs become much easier to squash. It also has a host of other features that I won't bore you with.
Let's begin. First up, you will need the Java Development Kit if you don't already have it (if you have to ask, you probably don't have it). That can be downloaded
here (it's the download called JDK, if that wasn't obvious). Java 6 or 7, doesn't really matter. Compatability isn't an issue since you won't be using what you compile with NetBeans (Starfarer compiles the mods itself when it launches); NetBeans just makes working with the source files a lot easier.
Next, you'll need NetBeans itself. NetBeans can be downloaded from
netbeans.org. It's up to you if you grab the beta version or not; I've tried both and either should work just fine. You only need the Java SE version for making Starfarer mods.
Once you've installed and run the program, this is close to what you should see (I've already customized my windows, sorry!):
Go to File - New Project. Make sure the category is set to Java and select "Java Project with Existing Sources" in the Projects box. Hit Next. Name your project anything you like, and put it anywhere
except in your mod folder. We're not interested in the project files NetBeans generates.
You'll now see two empty boxes. We're interested in the top one, Source Package Folders. Hit "Add Folder" to the right of this box. Browse to the mods directory and select your mod folder (ignore the image below showing the data folder, it's wrong). Hit OK, then Next.
Now we come to the Includes & Excludes menu. Chances are that your mod isn't pure code, so we'll have do to a bit of work here. Change the text in the Includes box from "**" to "**/*.java". If you did this right, only your .java files are in the Included Files box now, with everything else under Excluded Files. Hit Finish.
Spoiler
(Ignore the background in this one. I forgot to take a screenshot, so had to go back later.
You should now be able to see your project's folder structure under the Projects tab on the left of the main IDE screen. But what's with all the red exclamation point signs?
Well, you're still missing something very important. Your code is referencing all these Starfarer APIs, but NetBeans has no idea what you are talking about. To fix this, we need to add the Starfarer API to the Project Libraries. Right click on Libraries in the Project tab, and select "Add Jar/Folder". Navigate to the Starfarer install directory, and enter the folder starfarer-core. Find starfarer.api.jar and select it. You might also want to add lwjgl_util.jar, as that is where the Vector2f class is defined.
Note that at this point you're probably still going to have a bunch of compile errors due to missing dependencies. Remember, Starfarer mods work by adding your mod on top of what's in starfarer-core, and NetBeans doesn't know about those files. The
laziest easiest way to fix this is to just look at the compile errors, find out what files are missing, and copy them over from the Starfarer/starfarer-core/data directory (for most current mods, BaseSpawnPoint will be the big one). After you're done, go to Source - Scan for External Changes. That should allow NetBeans to notice the files you just added. Don't worry about cluttering up your mod, you should only need a few files; even if you did somehow require every core file for compatability, it would add less than 100 kilobytes to your mod size.
I'm sure there's a better way of doing this, does anyone who uses NetBeans know of one? I'm aware that you can add starfarer-core to your project source, but then you run the risk of editing core files.Most of the compile error warnings should have vanished now (you might still have to do a bit of cleanup), and as an added bonus you can now browse the API at your leisure in the Libraries tree.
Although you might notice something strange...
You're looking at a generated source file. Not very intuitive, is it? Don't worry, we can fix this. Right click on starfarer.api.jar in your Projects tab, and click Edit. Hit the Browse button next to Sources and head back to starfarer-core. This time, select starfarer.api.zip.
You might have to reload the file before any changes show up, but it should now look something like this:
Isn't this:
SectorEntityToken addPlanet(SectorEntityToken focus, String name, String type,
float angle, float radius, float orbitRadius, float orbitDays);
Much more intuitive than this:
public SectorEntityToken addPlanet(SectorEntityToken set, String string, String string1, float f, float f1, float f2, float f3);
You're pretty much ready to go at this point. Happy coding!
(If this tutorial was missing anything, let me know and I'll fix it)