How to use ICopyableBlock
Learn how to use the ICopyableBlock interface to improve compatibility with assembly, schematics, and splitting.
Introduction
ICopyableBlock is an interface which can be implemented by any custom class that extends Block.
The methods you implement will be run by Valkyrien Skies itself (as well as addons like VMod) to improve compatibility with transitions such as schematic copy/pasting or ship splitting.
It can be used for a wide variety of blocks to improve compat. Examples include:
Bearing block being pasted in schematics without losing its joint
Cross-ship fluid pipe being split off into a second ship without losing its connection
Control seat persisting its linked blocks after being pasted as a new ship
If your blocks are having issues with VMod schematics, you're in the right place.
Steps
Implementing the interface
The first step is to add the interface to your custom block class. You can also add it to another mods block using a mixin, but that won't be covered in this guide.
Then, you must implement two functions, onCopy and onPaste. (Your IDE will often provide the method signature for you)
Understanding the interface
The Javadoc comments on these methods are rather technical, so it can be useful to see a high level overview of how they work.
When your block is copied, onCopy is called. The CompoundTag that onCopy returns will be saved.
If onCopy returns null, then the CompoundTag from the block entity saveAdditional is saved. Most blocks choose to return null from onCopy for simplicity, saving the block entity data.
When the block is pasted, onPaste is called. The tag you returned from onCopy or saveAdditional will be given to onPaste, as well as some additional information like a mapping of old ship ids (the ids from when your block was copied) to new ship ids (the ids of the ships your block is being pasted onto).
Whatever compound tag you return from onPaste is loaded into the newly placed block, and is usually parsed by the block entity's load function. If null is returned from onPaste, it will load the unmodified compound tag into the block.
This means you can use onPaste as a form of "pre-processor" for the block entities data. You take in the data that was saved (for example the ship your bearing is jointed to), and map it to new data (like the new ship ids you have been pasted on).
This may sound complicated but once you wrap your head around it, it isn't that bad.
Example
Below is an example of a simple block which stores a position on another ship, and fully supports being duplicated to a new ship (e.g. when pasted in a schematic).