Fractal Softworks Forum

Please login or register.

Login with username, password and session length

Author Topic: How to replace a class?  (Read 1246 times)

Curator

  • Ensign
  • *
  • Posts: 5
    • View Profile
How to replace a class?
« on: April 13, 2021, 11:07:16 AM »

Hey!
I want to replace com/fs/starfarer/api/impl/campaign/intel/PersonBountyIntel.class to change the random bounty fleets size and payment calculation.
Made a .jar with my variant of PersonBountyIntel.class (saving the path), made a mod_info.json with "jar" field and even with "replace" field filled with my .jar's name and filepath. Got no effect at all. The mod is in the list, it can be loaded but no changes are applied ingame.

Any advice?

I've attached the mod folder, maybe I did some newbie mistakes?

[attachment deleted by admin]
« Last Edit: April 14, 2021, 07:44:16 AM by Curator »
Logged

Curator

  • Ensign
  • *
  • Posts: 5
    • View Profile
Re: How to replace a class?
« Reply #1 on: April 14, 2021, 07:47:12 AM »

So I got an answer in another thread from @Histidine :
Hey!
There's no information (or I didn't find any) about replacing Vanilla classes.
Some vanilla classes, like industries, abilities and contact missions, are specified in their CSVs, so you just change the classname specified there.

For other things (pirate base generation is one), the class instance is generated by the game's "lifecycle plugin". You have to either replace that plugin (not recommended for compatibility reasons), or else run code that removes the existing script and adds your own.

For detailed help, I'd suggest a new thread in Modding, or the Misc modding questions thread.

What exactly should I do to run the code that removes the existing script and adds my own?
Logged

Histidine

  • Admiral
  • *****
  • Posts: 4682
    • View Profile
    • GitHub profile
Re: How to replace a class?
« Reply #2 on: April 14, 2021, 08:17:53 AM »

You need to replace the PersonBountyManager with a version that generates instances of your person bounty class instead of the vanilla one.

I did some searching and turns out, there's a way to do it easily in the particular case of the PersonBountyManager. In your mod plugin:

Code: java
public void onGameLoad() {
  /* bounty manager is added on every game load, so we need to remove it on every game load */
  Global.getSector().removeScript(PersonBountyManager.getInstance());
  if (!sector.hasScript(MyModifiedPersonBountyManager.class)) {
    Global.getSector().addScript(new MyModifiedPersonBountyManager());
  }
}
Logged

Curator

  • Ensign
  • *
  • Posts: 5
    • View Profile
Re: How to replace a class?
« Reply #3 on: April 14, 2021, 11:17:43 AM »

You need to replace the PersonBountyManager with a version that generates instances of your person bounty class instead of the vanilla one.

I did some searching and turns out, there's a way to do it easily in the particular case of the PersonBountyManager. In your mod plugin:

Code: java
public void onGameLoad() {
  /* bounty manager is added on every game load, so we need to remove it on every game load */
  Global.getSector().removeScript(PersonBountyManager.getInstance());
  if (!sector.hasScript(MyModifiedPersonBountyManager.class)) {
    Global.getSector().addScript(new MyModifiedPersonBountyManager());
  }
}

Thank you for your help!
I'm kinda new to Java and completely new to Starsector modding, so sorry for my noobish questions, but:
1. Do I include this code into my class or I need to create some other script? If another script - what exaclty script or where can I read about it? If into my class - does '!sector' mean that I need to call it from somewhere like 'Global' cause it is marked as error if I put it like it is?
2. Do I need to change the name of my class somehow so it will be not the same as replaced class'es name from Vanilla?
Logged

Histidine

  • Admiral
  • *****
  • Posts: 4682
    • View Profile
    • GitHub profile
Re: How to replace a class?
« Reply #4 on: April 14, 2021, 05:39:46 PM »

1. It goes in a ModPlugin. Make a new class that extends BaseModPlugin and add the needed functions to it. Then, as per the linked wiki page, specify the mod plugin class in your mod_info.json.

Yeah, I made a mistake in copying the code, that 'sector' should be Global.getSector().

2. Yeah, it's easier to work with if they have different names.
Logged