Fractal Softworks Forum

Please login or register.

Login with username, password and session length
Advanced search  

News:

Starsector 0.97a is out! (02/02/24); New blog post: Planet Search Overhaul (07/13/24)

Pages: [1] 2

Author Topic: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE  (Read 40227 times)

kazi

  • Admiral
  • *****
  • Posts: 714
    • View Profile
[Tutorial] How to use IntelliJ IDEA as a Starsector IDE
« 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 (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) 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
« Last Edit: April 23, 2017, 08:54:15 AM by kazi »
Logged

kazi

  • Admiral
  • *****
  • Posts: 714
    • View Profile
Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
« Reply #1 on: December 01, 2015, 12:11:26 AM »

Reserved for other stuff
Logged

speeder

  • Captain
  • ****
  • Posts: 365
    • View Profile
Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
« Reply #2 on: December 01, 2015, 09:18:35 PM »

Can you add how you create a mod from scratch? (instead of importing your existing mod)
Logged

kazi

  • Admiral
  • *****
  • Posts: 714
    • View Profile
Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
« Reply #3 on: December 04, 2015, 09:53:54 PM »

Sure, I'll add that in once I get an update out (soon).
Logged

Originem

  • Purple Principle
  • Captain
  • ****
  • Posts: 432
  • Dancing like a boss.
    • View Profile
Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
« Reply #4 on: December 04, 2015, 11:13:49 PM »

can you put some pictures for each step? :-\
not everyone has a good eye sight :D
Logged
My mods


TJJ

  • Admiral
  • *****
  • Posts: 1905
    • View Profile
Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
« Reply #5 on: December 05, 2015, 03:08:25 AM »

- Attaching & using the debugger?
Logged

Stalker

  • Lieutenant
  • **
  • Posts: 65
    • View Profile
Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
« Reply #6 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:
  • My JAR output directory was a parent directory above where my *.java files were. IntelliJ auto-excludes those directories from being sources.
  • I had to actually right-click my intended source directory in the left-panel of the main interface, and set that directory to the type of "Source". Then, the directory icon turned blue and all my files were available for compiling.
« Last Edit: December 09, 2015, 07:18:42 AM by Stalker »
Logged

shuul

  • Ensign
  • *
  • Posts: 42
    • View Profile
Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
« Reply #7 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?
Logged

kazi

  • Admiral
  • *****
  • Posts: 714
    • View Profile
Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
« Reply #8 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.
Logged

Morrokain

  • Admiral
  • *****
  • Posts: 2143
  • Megalith Dreadnought - Archean Order
    • View Profile
Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
« Reply #9 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.
Logged

pegwymonie

  • Ensign
  • *
  • Posts: 13
    • View Profile
Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
« Reply #10 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.
Logged

Ava_N

  • Ensign
  • *
  • Posts: 3
    • View Profile
Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
« Reply #11 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.
« Last Edit: September 29, 2017, 09:22:47 PM by Ava_N »
Logged

TrashMan

  • Admiral
  • *****
  • Posts: 1327
    • View Profile
Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
« Reply #12 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!
« Last Edit: January 02, 2019, 06:29:47 AM by TrashMan »
Logged

Boot Virus

  • Ensign
  • *
  • Posts: 5
    • View Profile
Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
« Reply #13 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).

Logged

Morrokain

  • Admiral
  • *****
  • Posts: 2143
  • Megalith Dreadnought - Archean Order
    • View Profile
Re: [Tutorial] How to use IntelliJ IDEA as a Starsector IDE
« Reply #14 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.
Logged
Pages: [1] 2