Rats, I thought that would be clean and "just work", Axle.
So, let's look at the case where it's invoked. The only place in the API where that occurs is for Stations in CoreAutoFitPlugin, specifically this function:
protected Set<String> availableMods;
protected Set<String> slotsToSkip = new HashSet<String>();
protected Set<Integer> baysToSkip = new HashSet<Integer>();
protected boolean fittingModule = false;
protected int missilesWithAmmoOnCurrent = 0;
public void doFit(ShipVariantAPI current, ShipVariantAPI target, AutofitPluginDelegate delegate) {
if (!fittingModule) {
fittedWeapons.clear();
fittedFighters.clear();
randomize = isChecked(RANDOMIZE);
availableMods = new LinkedHashSet<String>(delegate.getAvailableHullmods());
}
current.getStationModules().putAll(target.getStationModules());
int index = 0;
for (String slotId : current.getStationModules().keySet()) {
ShipVariantAPI moduleCurrent = current.getModuleVariant(slotId);
if (moduleCurrent == null) continue;
if (moduleCurrent.isStockVariant()) {
moduleCurrent = moduleCurrent.clone();
moduleCurrent.setSource(VariantSource.REFIT);
moduleCurrent.setHullVariantId(moduleCurrent.getHullVariantId() + "_" + index);
}
index++;
// String variantId = current.getStationModules().get(slotId);
// ShipVariantAPI moduleTarget = Global.getSettings().getVariant(variantId);
ShipVariantAPI moduleTarget = target.getModuleVariant(slotId);
if (moduleTarget == null) continue;
fittingModule = true;
doFit(moduleCurrent, moduleTarget, delegate);
fittingModule = false;
current.setModuleVariant(slotId, moduleCurrent);
}
current.setSource(VariantSource.REFIT);
weaponFilterSeed = random.nextLong();
emptyWingTarget = null;
if (delegate.getAvailableFighters().size() > 0) {
emptyWingTarget = delegate.getAvailableFighters().get(random.nextInt(delegate.getAvailableFighters().size())).getId();
}
altWeaponCats.clear();
altFighterCats.clear();
slotsToSkip.clear();
baysToSkip.clear();
missilesWithAmmoOnCurrent = 0;
boolean strip = isChecked(STRIP);
if (strip) {
stripWeapons(current, delegate);
stripFighters(current, delegate);
current.setNumFluxCapacitors(0);
current.setNumFluxVents(0);
if (delegate.isPlayerCampaignRefit()) {
for (String modId : current.getNonBuiltInHullmods()) {
boolean canRemove = delegate.canAddRemoveHullmodInPlayerCampaignRefit(modId);
if (canRemove) {
current.removeMod(modId);
}
}
} else {
current.clearHullMods();
}
} else {
slotsToSkip.addAll(current.getFittedWeaponSlots());
for (int i = 0; i < 20; i++) {
String wingId = current.getWingId(i);
if (wingId != null && !wingId.isEmpty()) {
baysToSkip.add(i);
}
}
}
//boolean randomize = isChecked(RANDOMIZE);
boolean reinforcedHull = isChecked(ALWAYS_REINFORCED_HULL);
boolean blastDoors = isChecked(ALWAYS_BLAST_DOORS);
if (reinforcedHull) {
addHullmods(current, delegate, HullMods.REINFORCEDHULL);
}
if (blastDoors) {
addHullmods(current, delegate, HullMods.BLAST_DOORS);
}
addHullmods(current, delegate, target.getNonBuiltInHullmods().toArray(new String[0]));
int addedRandomHullmodPts = 0;
if (randomize) {
addedRandomHullmodPts = addRandomizedHullmodsPre(current, delegate);
}
fitFighters(current, target, false, delegate);
fitWeapons(current, target, false, delegate);
if (current.hasHullMod(HullMods.FRAGILE_SUBSYSTEMS) &&
(current.getHullSize() == HullSize.FRIGATE || current.getHullSize() == HullSize.DESTROYER)) {
addHullmods(current, delegate, HullMods.HARDENED_SUBSYSTEMS);
}
float addedMax = current.getHullSpec().getOrdnancePoints(stats) * 0.1f;
if (randomize && addedRandomHullmodPts <= addedMax) {
addRandomizedHullmodsPost(current, delegate);
}
float ventsCapsFraction = 1f;
boolean upgrade = isChecked(UPGRADE);
if (upgrade) {
ventsCapsFraction = 0.5f;
//ventsCapsFraction = 0f;
}
addVentsAndCaps(current, target, ventsCapsFraction);
// now that we're at the target level of vents and caps
// see if we can upgrade some weapons
if (upgrade) {
fitFighters(current, target, true, delegate);
fitWeapons(current, target, true, delegate);
addVentsAndCaps(current, target, 1f - ventsCapsFraction);
}
// float dissipation = current.getHullSpec().getFluxDissipation() + current.getNumFluxVents() * 10f;
// float generation = 0f;
// for (String slotId : current.getFittedWeaponSlots()) {
// WeaponSpecAPI spec = current.getWeaponSpec(slotId);
// generation += spec.getDerivedStats().getSustainedFluxPerSecond();
// }
addHullmods(current, delegate, HullMods.REINFORCEDHULL, HullMods.BLAST_DOORS, HullMods.HARDENED_SUBSYSTEMS);
addExtraVentsAndCaps(current, target);
addModsWithSpareOPIfAny(current, target, delegate);
current.setVariantDisplayName(target.getDisplayName());
current.getWeaponGroups().clear();
for (WeaponGroupSpec group : target.getWeaponGroups()) {
WeaponGroupSpec copy = new WeaponGroupSpec(group.getType());
copy.setAutofireOnByDefault(group.isAutofireOnByDefault());
for (String slotId : group.getSlots()) {
if (current.getWeaponId(slotId) != null) {
copy.addSlot(slotId);
}
}
if (!copy.getSlots().isEmpty()) {
current.addWeaponGroup(copy);
}
}
boolean player = fleetCommander != null && fleetCommander.isPlayer();
if (player) {
if (current.getWeaponGroups().isEmpty() || randomize || current.hasUnassignedWeapons()) {
current.autoGenerateWeaponGroups();
}
//current.assignUnassignedWeapons();
} else {
current.getWeaponGroups().clear(); // will get auto-assigned when deployed in combat; until then don't care
}
if (!fittingModule) {
delegate.syncUIWithVariant();
}
}
There's a lot going here; it'll take a me a little bit to unpack. But we know it actually works here, if not in your context.
But it's calling for AutofitPluginDelegate, is in AutofitPlugin; it might be necessary to have that in this, since it appears there are some UI calls, etc. going on when a new Module is installed.
AutofitPluginDelegate:
public interface AutofitPluginDelegate {
void fitFighterInSlot(int index, AvailableFighter fighter, ShipVariantAPI variant);
void clearFighterSlot(int index, ShipVariantAPI variant);
void fitWeaponInSlot(WeaponSlotAPI slot, AvailableWeapon weapon, ShipVariantAPI variant);
void clearWeaponSlot(WeaponSlotAPI slot, ShipVariantAPI variant);
List<AvailableWeapon> getAvailableWeapons();
List<AvailableFighter> getAvailableFighters();
boolean isPriority(WeaponSpecAPI weapon);
boolean isPriority(FighterWingSpecAPI wing);
List<String> getAvailableHullmods();
void syncUIWithVariant();
ShipAPI getShip();
FactionAPI getFaction();
boolean isPlayerCampaignRefit();
boolean canAddRemoveHullmodInPlayerCampaignRefit(String modId);
}