Skip to content

Transient data

Lukas22041 edited this page Aug 29, 2026 · 3 revisions

Sometimes you need to store per-instance transient data. For that, you can make use of context.getData. This provides you with a PatchData object. This is essentially a HashMap that PatchLib ensures is garbage collected when the instance it is associated with no longer exists.

@Patch(target = @ClassMatch(subtype = TemporalShellStats.class))
public class TempShellPatch {

    @After(target = @MethodMatch(methodName = "apply"))
    public static void afterApply(AfterContext context) {
        MutableShipStatsAPI stats = (MutableShipStatsAPI) context.getArg(0);
        ShipAPI ship = (ShipAPI) stats.getEntity();

        PatchData data = context.getData();
        IntervalUtil interval = data.get("message_timer");
        if (interval == null) {
            interval = new IntervalUtil(5, 5);
            data.put("message_timer", interval);
        }
        interval.advance(Global.getCombatEngine().getElapsedInLastFrame());
        if (interval.intervalElapsed()) {
            Global.getCombatEngine().addFloatingText(ship.getLocation(), "Text from the patch!", 50f, Misc.getHighlightColor(), ship, 1f, 1f);
        }
    }
}

Screenshot

Use case

There are many other options you can use, like managing your own global data class or using utilities like ship.getCustomData() where they exist. The getData map is available for cases where those aren't easily accessible.

Clone this wiki locally