Hi, I'm trying to add a skill that gives the captain's ship a hullmod, but I'm having trouble actually getting the hullmod to appear and disappear from the ship as the captain is moved from ship to ship. I'm testing this via a clean campaign with just the bare essential mods and a starter fleet with the captain with the custom skill a part of my starting fleet.
I implemented a simple 1-level skill with implements AfterShipCreationSkillEffect. There were only two real statements in the whole file. The first being applying the hullmod when (I assume) the skill is active, in apply():
@Override
public void apply(final MutableShipStatsAPI stats, final HullSize hullSize, final String id, final float level) {
log.info("CALL APPLY BEFORE");
stats.getVariant().addPermaMod("yrxp_TestSkill");
}
And the second would be to remove it. Originally, I had it in unapply(), but then I was already having a problem - the hullmod was never being removed. So I tried placing it in unapplyEffectsAfterShipCreation():
@Override
public void unapplyEffectsAfterShipCreation(ShipAPI ship, String id) {
log.info("CALL UNAPPLY AFTER");
ship.getVariant().removePermaMod("yrxp_TestSkill");
}
The problem with this now, is that the hullmod is always removed... And worse still, when I reassign the captain to a different ship, that's actually when the hullmod appears - left on the ship where the captain was just removed from. Very strange! So I figured there must something going on with the order in which these functions are being called, and sure enough, the logs showed that weirdness:
294895 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL UNAPPLY AFTER
294895 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL APPLY AFTER
294896 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL APPLY BEFORE
294896 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL APPLY BEFORE
294896 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL APPLY BEFORE
294896 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL APPLY BEFORE
294897 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL UNAPPLY AFTER
294897 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL APPLY AFTER
294897 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL UNAPPLY AFTER
294897 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL APPLY AFTER
294897 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL UNAPPLY BEFORE
294897 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL UNAPPLY AFTER
294898 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL APPLY BEFORE
294898 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL UNAPPLY AFTER
294898 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL APPLY AFTER
294899 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL APPLY BEFORE
294899 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL APPLY BEFORE
294899 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL APPLY BEFORE
294900 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL APPLY BEFORE
294900 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL APPLY BEFORE
294901 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL APPLY BEFORE
294901 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL UNAPPLY AFTER
294901 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL APPLY AFTER
294901 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL UNAPPLY AFTER
294902 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL APPLY AFTER
294902 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL UNAPPLY BEFORE
294902 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL UNAPPLY AFTER
294902 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL APPLY BEFORE
294902 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL UNAPPLY AFTER
294902 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL APPLY AFTER
294903 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL APPLY BEFORE
294903 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL APPLY BEFORE
294904 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL UNAPPLY AFTER
294904 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL APPLY AFTER
294905 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL UNAPPLY AFTER
294905 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL APPLY AFTER
294905 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL UNAPPLY BEFORE
294905 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL UNAPPLY AFTER
294905 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL APPLY BEFORE
294905 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL UNAPPLY AFTER
294906 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL APPLY AFTER
294907 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL APPLY BEFORE
294907 [Thread-3] INFO data.scripts.campaign.skills.yrxp_TestSkill - CALL APPLY BEFORE
Whenever I get the code to refresh - that is, switching the view from a different ship and back to the ship with the captain in question - the above logs appear. It was simply a result of adding a single log line to apply(), unapply(), applyEffectsAfterShipCreation() and unapplyEffectsAfterShipCreation(). Firstly, and what I expected, apply() was being called (though it was being called a heck of a lot which was a bit strange). This means that the call for adding the hullmod was certainly being called. however, what I find then is that, for some reason, the unapply() and unapplyEffectsAfterShipCreation() functions also get called? Did I misundestand that the unapply calls would only when the captain is removed? In any case, they, too are being called in rapid succession, and with seemingly no pattern to it. And that's probably what's causing the weird behavior.
tl;dr: How can I make it so a hullmod is applied to a captain's ship, and is then removed when they are dismissed or sent to a different ship?