Fractal Softworks Forum

Starsector => Mods => Modding Resources => Topic started by: kazi on December 01, 2015, 12:11:03 AM

Title: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
Post by: kazi on December 01, 2015, 12:11:03 AM
So I know most of the forum have been indoctrinated into the unholy cult of NetBeans, but IntelliJ IDEA is pretty great and has several features that make modding Starsector a breeze (more so than NetBeans). It's also prettier.

If you suck at coding, you will still continue to suck. However, I can guarantee that following this tutorial will make you suck harder, faster, and more creatively than ever before!

By the end of this tutorial, you will have:

+ Configured IntelliJ for editing and compiling your mod's code into a JAR.
+ Setup and learned how to access the Starsector/LazyLib/ShaderLib Javadocs from within IntelliJ while coding.
+ Created a one-click build+run configuration for your mod + Starsector.
+ Setup some miscellaneous IntelliJ settings that I find helpful.
+ Learned a few other IntelliJ tricks.
+ Uninstalled NetBeans.

NOTE: this tutorial is aimed at IntelliJ IDEA versions 14+. Earlier versions are weird.

==============================================================

Download and setup IntelliJ:

+ Download the Java 1.7 SE JDK for your OS (if you don't have it already). Unzip it and store it anywhere.
+ Get IntelliJ IDEA from here (https://www.jetbrains.com/idea/) (the Community Edition is great).
+ Open IntelliJ IDEA

===============================================================

Import your mod:

+ File -> New project from existing sources
+ Select your mod's root folder
+ Create project from existing sources
+ Hit next a couple times, I think the rest of the config settings should be ok. Make sure it makes a module out of your project's src/ directory (or wherever your code lives).

But wait, I don't want to import a mod, I want to start my own from scratch!

Download the source code for an existing mod and follow this tutorial with that mod. Doing this will let you learn the ins and outs of the IDE a bit before you start, and will ensure that you know how to compile a mod correctly. That way, if something doesn't work, you'll know it's your code and not the way you are setting up your project. I will recommend my mod (https://github.com/jstaf/mayorate (https://github.com/jstaf/mayorate)) as an example for this tutorial. I'm making this recommendation not because I'm trying to promote my mod, but because I know it is guaranteed to work with this tutorial. It also functions as an example for most everything you'd want to do, including custom planets/portraits/ships/AI/music/weapons/plugins/sector generation/skills/hullmods/ship systems/etc..

Once you've finished this tutorial with an example mod from the forums, use an existing mod from the forums to create your mod. Take this existing mod and delete everything but the mod_info.json and modPlugin.java files and start from there.

To configure your project:

+ File -> Project Structure

+ Add JDK 1.7 to your project:
   + Platform Settings -> SDKs
   + Hit the "+", select the path to the JDK that you downloaded
   + Project Settings -> Project
   + Project SDK is 1.7
   + Project language level is 7.

+ Add dependencies to your project:
   + Make sure you have LazyLib and ShaderLib downloaded and in your mods folder if your project uses them.
   + Project Settings -> Libraries
   + To add Starsector as a dependency:
      + Hit the "+" sign at the top, select Java
      + Select all of the JARs in Starsector's root directory (as well as starsector.api.zip), hit OK
      + Name the dependency "starsector core" (or whatever you want)
      + Hit the "+" with a little globe on it to add a Javadoc
      + Enter "http://fractalsoftworks.com/starfarer.api/", hit OK
   + To add LazyLib as a dependency:
      + Hit the "+" at the top, select Java
      + Add LazyLib.jar and javadoc.zip, hit OK
   + To add ShaderLib as a dependency:
      + Hit the "+" at the top, select Java
      + Add Shaders.jar
      + Add the javadoc/ folder

+ Configure your compiler output:
   + Project Settings -> Artifacts
   + Hit the "+" at the top, select JAR -> From modules with dependencies
   + Select module that was created on project import
   + Do not set a main class, extract to target JAR
   + Change name to your desired output JAR name (.jar gets automatically added)
   + Select your desired output directory
   + Check "build on make"
   + Remove everything thats not your module's compiled output. Select all of the stuff in the left panel, and hit "-"

+ Hit OK to close the settings dialog

Setup the ultra-fancy one-click build/run configuration:

+ File -> Settings
+ Plugins -> Browse repositories
   + On Windows: Install the "Batch Support" plugin
   + On Mac/Linux: Install the "Bash Support" plugin
+ Restart IntelliJ IDEA to reflect the changes in plugins
+ Run -> Edit Configurations
+ Hit the "+" in the top left, create a new Batch (Windows) or Bash (Mac/Linux) run configuration
+ Set the name to "Starsector"
+ Hit the "..." next to "Script", select either starsector.bat (Windows) or starsector.sh (Mac/Linux)
+ Hit the "..." next to "Working Directory", select your Starsector install directory.
+ In the before launch panel, hit "+", select Build
+ Hit OK to create the configuration

To build and/or run Starsector:

To build your mod's JAR and run Starsector:
Hit the play button in the top right corner to both compile your mod and run Starsector.

To simply build your mod's JAR (without running Starsector):
Hit the little "down pointing arrow with 01010110" icon in the upper right.

Make sure to delete the "out/" directory before distributing your mod, all of the code you want to distribute is packaged in the compiled JAR.

Bask in the glory that is IntelliJ IDEA:

+ Self explanatory.

===================================================================================

Other useful configuration options (Optional):

Disable project autoload (if you have other stuff you want to work on):
File -> Settings -> Appearance & Behavior -> System Settings -> Reopen last project on startup

Enable line numbers in all files:
File -> Settings -> Editor -> Appearance -> Show line numbers

Disable the *** spell check:
File -> Settings -> Editor -> Inspections -> Spelling

=================================================================================

Other neat features with this setup:

When you're running your build configuration, the Starsector log will automatically dump down into the Run panel in the bottom of the screen. If you get an error, that will be the last thing in the console. You'll never need to open starsector.log and scroll all the way to the bottom ever again (unless you're just playing the game normally, of course).

If you need to look up the documentation for something, ctrl+q opens the relevant Javadocs in the editing window. You can also look at the Starsector source files this way.

Whenever you specify a color (e.g. "new Color(r, g, b, a)"), a little colored box pops up next to the line #s. Clicking the colored box gives you a color picker straight out of GIMP or Photoshop that will modify the color specified on that line.

"//TODO <your comment here>" creates a to-do comment in your code that you can go back to anytime/check with the little TODO dock in the bottom (If it isn't there: View -> Tool Windows -> TODO)

Possible autocompletions will popup as you type, which is nice for figuring things out stuff with the APIs.

Shift+F6 renames/refactors a variable/anything in all files of your project.

Ctrl+Enter == autocomplete.

Double-tap Shift searches inside your project for anything.

Ctrl+/ == line comment.

Ctrl+Shift+/ == block comment.

Tools -> Create Javadoc creates Javadocs.

IntelliJ has nice Version Control capabilities with git and whatnot. Not covering them here, but make sure to check out what's possible (also check out the ".gitignore" plugin!).

======================================================================

Troubleshooting:

If on running your configuration results in some weird "xxxxx.so not found" error or something like that, edit the "./jre_your_platform/java" bit in the first part of starsector.bat/starsector.sh to simply "java". Linux users are probably already familiar with this step.

*Updated to use a significantly nicer run configuration
Title: Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
Post by: kazi on December 01, 2015, 12:11:26 AM
Reserved for other stuff
Title: Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
Post by: speeder on December 01, 2015, 09:18:35 PM
Can you add how you create a mod from scratch? (instead of importing your existing mod)
Title: Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
Post by: kazi on December 04, 2015, 09:53:54 PM
Sure, I'll add that in once I get an update out (soon).
Title: Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
Post by: Originem on December 04, 2015, 11:13:49 PM
can you put some pictures for each step? :-\
not everyone has a good eye sight :D
Title: Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
Post by: TJJ on December 05, 2015, 03:08:25 AM
- Attaching & using the debugger?
Title: Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
Post by: Stalker on December 05, 2015, 07:50:50 PM
Spoiler
I set up everything exactly as your guide laid out. Keep in mind I've never worked with pure Java before.

The log in IntelliJ says the build was successful (or "all files are up-to-date"), but no JAR file was created. What happened? I'm not sure what, if anything, I might have done wrong.

There's only one *.java file in the project right now.

The place I want the jar to build to is my mod's root directory.

The source directory is everything under my mod's /jar/ directory.
[close]

EDIT: OK, this is solved. I had two problems preventing this from working:
Title: Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
Post by: shuul on April 20, 2016, 03:42:17 AM
Sure, I'll add that in once I get an update out (soon).

Hi,

Is it still possible to update tutorial on how to set up mod with IntelliJ from scratch?
Title: Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
Post by: kazi on May 06, 2016, 05:04:58 PM
Sure, I'll add that in once I get an update out (soon).

Hi,

Is it still possible to update tutorial on how to set up mod with IntelliJ from scratch?

I suppose, although the easiest way of doing things is to import a skeleton of an existing mod where you've deleted everything but the mod plugin, edit it to your tastes, and work from there. If you did this, it should work using the above instructions.
Title: Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
Post by: Morrokain on July 23, 2017, 06:58:38 PM
Wanted to update this tutorial slightly-

When setting up the Run configuration options two points of the original post are now incorrect. See post here: http://fractalsoftworks.com/forum/index.php?topic=12833.msg217359#msg217359


But to summarize:

1. Under 'script' in the configuration options simply type out starsector.bat
 - do not click "..." as this will put the full file path and that will cause an error when you try to build and run the game

2. For 'working directory' make sure you indicate the starsector-core directory, not the starsector directory.
Title: Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
Post by: pegwymonie on August 26, 2017, 06:37:47 PM
It would be helpful if you could add a section on how to debug a mod using IntelliJ.
Title: Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
Post by: Ava_N on September 29, 2017, 09:18:00 PM
For breakpoint debugging, you can make a run configuration to attach a remote debugger:

1. Make a copy of your starsector-core/starsector.bat file and call it "debug-starsector.bat" (replace .bat with .sh for linux). This is not strictly necessary but highly recommended in case the bat file gets messed up.

2. Open the bat file and configure Java to start in a new process and open a debugger port. The "suspend=y" arg in there makes the JVM not run until a debugger actually connects.

Windows:
Code
start ..\jre\bin\java.exe -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005 <everything that comes after "java.exe" in the original file>

Linux:
Code
..\jre\bin\java.exe -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005 <everything that comes after "java.exe" in the original file> &
Don't forget the & at the end, it's what makes the command run in a separate process.

3. Create a run configuration that points to the new bat file, following kazi's original directions. Alternately, you can just change your existing configuration to point to the new file, but this will mess up the console.

4. Create a new remote debugging run configuration.
  a) Run -> Edit Configurations -> click "Remote" in the top left -> click "+"
  b) Name it "Debug Starsector"
  c) In the "Configuration" tab, in the "Before Launch" tab at the bottom, click "+" and add the configuration from step 3. This will make IntelliJ actually start StarSector before trying to connect to it.
  d) In the Logs tab, hit "+" in the top right and browse to your starsector.log file (should be in starsector-core). Alias it whatever you want, but "starsector.log" is probably a good idea.
  e) Hit "ok" on everything.

5. Set some breakpoints and run the new configuration by clicking the Debug (green bug) button next to the Run button. Easiest way to test if you've done it right is to set a breakpoint in an advance(), init(), or onApplicationLoad() methods in your mod.

Notes:
 - You may get a windows firewall popup when you first run the debugger. Click through it. You don't need to open any ports because it's all going through localhost, but windows firewall will still cry about it.
 - You'll notice that your normal console just says "Connected to the target VM, address: 'localhost:5005', transport: 'socket'". To see StarSector log output, switch to the starsector.log tab next to the console that should conveniently pop up for you.
 - If you see weird cut off logs, there's a drop-down in the top right of that window that lets you pick between "all", "infos","warnings", and "errors". Change to "all" to see the normal log you always see.
 - The StarSector process won't close when you disconnect the debugger.
Title: Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
Post by: TrashMan on January 02, 2019, 06:26:12 AM
This is outdated as hell.

With the new Intellij I can't even figure out where to add/adding the Java SDK

EDIT:
Finally found it!
Title: Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
Post by: Boot Virus on January 22, 2019, 05:20:26 PM
This is indeed somewhat outdated, though all the answers in the topic helped with the setup immensely.

Quote
2. For 'working directory' make sure you indicate the starsector-core directory, not the starsector directory.

In version I have (IntelliJ IDEA Community Edition 2018.3.3) this gave out "Error reading vmparams file", but using the starsector directory itself worked.

Quote
4. Create a new remote debugging run configuration.
  a) Run -> Edit Configurations -> click "Remote" in the top left -> click "+"
  b) Name it "Debug Starsector"
  c) In the "Configuration" tab, in the "Before Launch" tab at the bottom, click "+" and add the configuration from step 3. This will make IntelliJ actually start StarSector before trying to connect to it.
  d) In the Logs tab, hit "+" in the top right and browse to your starsector.log file (should be in starsector-core). Alias it whatever you want, but "starsector.log" is probably a good idea.
  e) Hit "ok" on everything.

For some reason my firewall decided to annoy me and this combination refused to run. The solution was to launch the debug-starsector.bat manually (ignoring point c)) and then run the debugger in the program (clicking the debug button).

Title: Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
Post by: Morrokain on January 28, 2019, 04:18:17 PM
Quote
2. For 'working directory' make sure you indicate the starsector-core directory, not the starsector directory.

In version I have (IntelliJ IDEA Community Edition 2018.3.3) this gave out "Error reading vmparams file", but using the starsector directory itself worked.


Ah yeah I'm actually still using the (IntelliJ IDEA Community Edition 2017.1.4 x64) so it was probably a change made between versions. If you happen to be using the older version then that instruction would probably still hold true.
Title: Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
Post by: Offensive_Name on May 25, 2019, 08:24:27 PM
I Can't find the batch support plugin
Title: Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
Post by: stormbringer951 on May 26, 2019, 03:17:11 AM
I Can't find the batch support plugin

Original post is quite old now. Plugin isn't around any more because batch configurations are built in.
Title: Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
Post by: Imp0815 on August 24, 2019, 07:23:48 AM
I set up all the Jar's and stuff as External library but intelliJ still wont let me "search for usage" in the API files and such :(
Title: Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
Post by: Sundog on August 24, 2019, 02:42:09 PM
I think "search for usage" might now be called "find in path"
(https://i.imgur.com/MPQeVoE.jpg)
Title: Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
Post by: Imp0815 on August 29, 2019, 11:57:01 AM
I think "search for usage" might now be called "find in path"
(https://i.imgur.com/MPQeVoE.jpg)

I only have finde Usage.
It just seems that the API files are not used as libraries:

(https://previews.dropbox.com/p/thumb/AAi3FbxcYQZv4yxCK1iG8s3jnaZFjZrdJAMZr5NU8pwgbgBC2HFPGIGGTMdxIm751R4GbgE-1Fww69HWqaalU4DMsBCHqutkAcK4KJgUkyek_MkJV33CUQWpZf53c0itdIfZChqY38oqv48T6hAIsfSayM5Itew3KK1Cvx4vCMi4DLKNTI0TAzslKFxMgDms9R_cLVFVD-y_-0i855ZQW1oaJ7KkNN5k33mt36h_MfuW6kuwx8T0JGqseOPlYlZj7-GnvFgLf5YSeRjTXkGL3LqBovfp_0RWN1T_nsxmMgo9IAXbW_MlWf9U092h9AkUb0f2VRI0av0mSybO7yAFaxlM/p.png?fv_content=true&size_mode=5)
Title: Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
Post by: Stealth on February 09, 2021, 08:09:48 AM
Does anyone have a direct download link for jdk 1.7? orcales website requires you to make an acc with like you address and stuff and i rly dont want to make one
Title: Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
Post by: lunar_ghost on April 14, 2021, 07:59:15 PM
If you don't want to create an account you can use a burner one here:

http://bugmenot.com/view/oracle.com
Title: Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
Post by: kazi on April 15, 2021, 08:33:35 PM
This guide is ancient. (I don't even use IntelliJ anymore...)

If you use something like Gradle to build you can actually tell JDK 8 (currently supported, no Oracle required) to compile to Java 7 using something like the following in your build.gradle file:

Code
sourceCompatibility = 1.7
targetCompatibility = 1.7
Title: Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
Post by: Raz3w1ll on January 13, 2023, 12:20:10 AM
Legend, was using the previous guide and couldnt launch and then using your batch method now i can start trying out how to mod and debug
Title: Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
Post by: Wispborne on June 10, 2023, 05:47:30 PM
An updated (2023) IntelliJ guide is available on the Wiki: https://starsector.fandom.com/wiki/IntelliJ_IDEA_Setup.
Title: Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
Post by: Damexius on July 24, 2023, 09:19:20 AM
Can you create a step-by-step tutorial video? Without a doubt, I'll subscribe to you.
Title: Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
Post by: V_KJ on August 14, 2023, 04:30:08 AM
Can I assume it works just as it does on Mac? or nah?