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: Simulator Enhancements (03/13/24)

Author Topic: How I start a .jar mod?  (Read 4614 times)

speeder

  • Captain
  • ****
  • Posts: 364
    • View Profile
How I start a .jar mod?
« on: December 03, 2015, 10:54:36 AM »

Well... I found lots of info, like how to setup IntelliJ, API docs, how to do X or Y task, how to draw sprites, how to create a faction mod, how to create a mission using .java files...

But I can't find anywhere how I create my first .jar mod.

So I setup IntelliJ, I found out how to point the json to a jar file... now what? How I create my first java file to be compiled into a .jar file?

Even if the only thing it did was to write a message on the screen I would be happy enough (because it would allow me to know if I setup all my tools and scripts correctly).
Logged

speeder

  • Captain
  • ****
  • Posts: 364
    • View Profile
Re: How I start a .jar mod?
« Reply #1 on: December 03, 2015, 10:55:26 AM »

I would like to also suggest that if someone ever reply to this, to make it a fixed post, either in modding resources, or in the official docs (if Alex that reply to it).
Logged

Zaphide

  • Admiral
  • *****
  • Posts: 799
    • View Profile
Re: How I start a .jar mod?
« Reply #2 on: December 03, 2015, 03:35:32 PM »

Well, a .jar file is pretty much a package of one or more pre-compiled .java files. A '.jar mod' (i.e. a mod with no .java files and just .jar files) is no different to a mod that uses .java files, just that pre-compiling code has a few advantages (faster load times, access to more Java features/methods etc). You can have a mod with both .jar files and .java files (but probably not really advisable, it just makes some things more complicated).

StarSector includes a java compiler (Janino) which is invoked when StarSector loads (after the mod selection screen) to compile any code that exists in .java files. By building a mod with .jar files instead of .java files, you will be skipping this step (which is generally a good thing).

You could structure a mod which contains a .jar like:

Code
YourModFolder
 -> data
 -> graphics
 -> jars
    -> [your .jar file goes here]
 -> sounds
 -> src
    -> [source files (.java files) for your .jar go here]
 -> mod_info.json

You may or may not wish to distribute the source .java files. Some people don't like to but it is generally fairly easy to decompile .jar into their .java source code files anyway.

Some people set things up slightly differently than the above but the main thing is that your mod_info.json references any .jar files included with your mod. This will prompt StarSector to load them.

OK, so if you are interested in using .jar files in your mod you really need to use an IDE. Sure you can compile/package everything by hand but it's a bit pointless when a small amount of configuration in your IDE can make it one click. I use IntelliJIDEA but all IDE's (I assume) have a way to setup .jar package building. The end goal is to have your IDE compile all your .java files located in your mods src folder and place them in a .jar file in your mods jars folder. Something like:

Code
src/data/scripts/MyModPlugin.java
src/data/scripts/world/MyModGen.java
are compiled into
Code
jars/MyModJar.jar

and then you are good to go :)

There are some complexities around class naming and specifying the package for a class but I suggest you follow some of the examples set in other mods.

Generally, the name of your class in the .java file needs to match (case sensitive I think, might depend on OS) the name of the file
(e.g. the MyModPlugin.java file contains a class MyModPlugin)
and the package specified in the file needs to match (again, case sensitive I think, might depend on OS) the files location in the folder structure
(e.g. src/data/scripts/MyModPlugin.java needs to specify package data.scripts;)
Logged

speeder

  • Captain
  • ****
  • Posts: 364
    • View Profile
Re: How I start a .jar mod?
« Reply #3 on: December 03, 2015, 03:43:46 PM »

This helped, but only a little bit.

I already setup IntelliJ, and I've been reading Starsector source and interface files.

But all easy mod examples I found or don't have any custom code (example: only edit .csv files and images) or are bizarrely complex and hard to understand what is going on.

I need an example of a simple .java file to compile into a .jar, like I said, if possible one that just prints something somewhere so that I know that my IntelliJ setup worked.
Logged

Zaphide

  • Admiral
  • *****
  • Posts: 799
    • View Profile
Re: How I start a .jar mod?
« Reply #4 on: December 03, 2015, 04:00:21 PM »

Create a mod folder with structure with the above, and then create a file in src/data/scripts called MyModPlugin.java.

Make it's contents something like (I haven't tested this at all):
Code
package data.scripts;

import com.fs.starfarer.api.BaseModPlugin;
import com.fs.starfarer.api.Global;

public class MyModPlugin extends BaseModPlugin
{
@Override
    public void onNewGame() {
        Global.getLogger(this.getClass()).info("Hooray my mod plugin in a jar is loaded!");
    }
}

This will also ensure you have correctly linked the StarSector API as a source for your IntelliJIDEA (due to the two imports).

Create an artifact definition for your IntelliJIDEA project to compile the contents of src/ into a jar called MyModJar.jar in the jars/ folder, then build your project. This should create a .jar file in the jars/ folder.

Create a mod_info.json file in your mod folder with the following specified:
Code
{
    "id":"mymodtest",
    "name":"MyModTest",
    "author":"speeder",
    "version":"0.0.1",
    "description":"Testing a jar in a StarSector mod",
    "gameVersion":"0.7a",
    "totalConversion":"false",
    "jars":["jars/MyModJar.jar"],
    "modPlugin":"data.scripts.MyModPlugin",
}


This will notify StarSector that if it loads your mod it needs to load the jar file and that your mod has a ModPlugin that StarSector needs to <do stuff with>.

Run StarSector with your mod included and start a new game. If you view the log file, hopefully you will see the message in there :)
« Last Edit: December 03, 2015, 04:09:24 PM by Zaphide »
Logged