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: Java for Dummies: modplugin.java 101 (help)  (Read 2530 times)

Grizzlyadamz

  • Commander
  • ***
  • Posts: 101
    • View Profile
Java for Dummies: modplugin.java 101 (help)
« on: August 06, 2016, 01:10:16 PM »

OK so, I'm a complete noob & looking to make a small campaign mod. After asking for/getting some help from another user, (Thank you!), I've found I still need some more hand-holding.
Here're my impressions after going through some instructions/examples/guides:

The first step to getting a mod into the game is the mod_info.json file.
     This tells the game (and the user) details about the mod, points to the .jar (a pre-compiled version of the .java files; having a .jar is optional but saves on in-game load-time), and finally it points the game to the modplugin.java file.
     Simple enough.

The second step is the modplugin.java (this would be contained in the .jar if you were to compile one (using an IDE)).
   
     The purpose of this plugin is 'to tell starsector to load the campaign/combat plugin'.

But, being a noob, I'd like to know how exactly it does this?
   
     Looking at the SS+ modplugin as an example, it seems to import a bunch of other files & 'extends' the vanilla 'modplugin', (aka it loads everything *that* loads as well).
  
What does importing do, precisely? Does it just enable references to the variables present in the imported files? What's a rule-of-thumb as far as what gets imported?
   
     It then seems to add some new variables, (seems straight-forward), and then if I understand correctly it checks several variables & sets up contingencies for those, for example returning an error message if you're trying to add SS+ to an already-existing game.

Question here is, if there aren't any variables or contingencies to consider (unlike in a large mod, say SS+), could the modplugin.java be just a few lines long?
The person who helped me pointed out this line of code in the SS+ modplugin.
Code
Global.getSector().registerPlugin(new SSP_CampaignPlugin());
Context
Would one simply need to 'registerplugin' on game-load or newgame?
What are some variables & contingencies that should always be checked?
Are there special considerations for registering a new plugin in an existing game?

Aand that's as far as I've gotten.

If some generous soul has the time to outline the different parts of a simple modplugin.java in Starsector, how each line does what it does, (or knows of a heavily-commented example which does exactly that), I'd be much obliged!
« Last Edit: August 06, 2016, 01:37:17 PM by Grizzlyadamz »
Logged

kazi

  • Admiral
  • *****
  • Posts: 714
    • View Profile
Re: Java for Dummies: modplugin.java 101 (help)
« Reply #1 on: August 06, 2016, 03:56:07 PM »

Don't use SS+ as an example. That's quite a complex mod.

When Starsector loads a mod, it loads a mod plugin script that you specify. This is how you get Starsector to begin executing your code on the campaign map. Your mod plugin script needs to extend the BaseModPlugin class. The one thing you are required to do in the BaseModPlugin is override (replace) onNewGame() with code that calls another script used to initialize faction relationships/generate your star systems/add persistent scripts and whatnot.

In the case of the Mayorate (using my own example since I know how that one works haha), the game calls onNewGame() upon starting a new game in MayorateModPlugin- this method calls another method (initMayorate()) that creates a new instance of mayorateGen. Inside mayorateGen, faction relationships are set, then it creates a new instance of ilk_RashtGen that generates the Mayorate home system.

If you don't understand much of what I wrote here, I highly recommend learning some additional Java before trying to take things further. Otherwise you will fall prey to the unforgiving cycle of "copy, paste, despair". As far as imports are concerned, you need to import any classes that you use in your code. I recommend using an IDE like IntelliJ IDEA because it will handle all of the imports for you, check for errors, etc.
Logged

Histidine

  • Admiral
  • *****
  • Posts: 4661
    • View Profile
    • GitHub profile
Re: Java for Dummies: modplugin.java 101 (help)
« Reply #2 on: August 06, 2016, 07:33:04 PM »

If you don't understand much of what I wrote here, I highly recommend learning some additional Java before trying to take things further. Otherwise you will fall prey to the unforgiving cycle of "copy, paste, despair". As far as imports are concerned, you need to import any classes that you use in your code. I recommend using an IDE like IntelliJ IDEA because it will handle all of the imports for you, check for errors, etc.
This.

As for (some of) the questions:
  • [This is what import does/li]
    • You need to import even if the extended class already did it
    • When registering a plugin on game load, either check if the plugin is already running (like SS+ writing a key to sector's persistent data), or make the plugin transient
Logged