How to setup an attachment
Learn how to make a basic attachment class and add it to a ship
Introduction
Say you want to store data on a particular ship, for example a boolean value on whether its in "turbo mode".
To store this data, you need an attachment. (Why?)
Luckily, setting one up is relatively straightforward.
Steps
Setup a public, final class
The attachment class must be public (so it can be registered), and final (so it cannot be extended).
To make a kotlin class final, simply omit the open keyword
Add information to the class
Now that you have your class, lets make it actually useful by adding the boolean we want to store.
We can do this by simply storing it in a property and adding a constructor parameter.
Keep in mind these values may be accessed on different threads, often the gamethread and/or the physics thread. You may need to make it volatile or a Concurrent class.
Registering the class as an attachment
Attempting to add the attachment to a ship will crash if the attachment has not been registered beforehand.
It's recommended to register your attachments in your mod's constructor.
Adding the attachment to a ship
Now that you have your attachment class registered and ready to go, it's time to add it to a ship.
You can only add attachments to LoadedServerShips. You may need to use getLoadedShipManagingPos instead of getShipManagingPos, or do a (safe) cast.
Getting the attachment from a ship
A LoadedServerShip is also required for getting your attachment back, for example to check the turboMode of a ship.
Updating the attachment of a ship
To update the data of an attachment, simply get the attachment, change the value you want to change, then add back the attachment with the updated instance.
For example, to toggle the value of our turboMode: