-
Notifications
You must be signed in to change notification settings - Fork 2
Storages
The Storages system is a way to store data, such as Radar positions and related data, in a way that does not depend on the chunk to be loaded to access that data. This system was designed with these goals in mind, and allows for stuff like rendering radars that are 1000+ blocks away and in unloaded chunks.
To use Storages, you must be on version 0.15.3.3-rc1 or higher.
The first step of creating a custom storage requires you to figure out what kind of data you need to store. A BlockPos needs to be stored for every kind of StorageData. If you do not need to store a BlockPos, you should look at additional ways of saving your data such as SavedData (which is what Storages uses under the hood) or Data Attachments
To start, create a new class CustomStorageData and extend StorageData. This will automatically handle the BlockPos for you. If you want to handle the BlockPos yourself, implement IStorageData instead.
You need to implement a few methods (mainly #getId and #serializeToNBT). #serializeToNBT is where you will save your data in the form of a CompoundTag. To properly save the BlockPos, you should get the CompoundTag by calling super#serializeToNBT()
In this example, we will save a custom string, and our ID ResourceLocation will be example:custom_sd.
It is recommended you declare your ID ResourceLocation as a public static field so that you can easily reference it for registration.
We now have:
public class CustomStorageData extends StorageData {
public static final ResourceLocation ID = ResourceLocation.fromNamespaceAndPath("example", "custom_sd");
private final String string;
public CustomStorageData(BlockPos pos, String string) {
super(pos);
this.string = string;
}
@Override
public ResourceLocation getId() {
return ID;
}
@Override
public CompoundTag serializeToNBT() {
CompoundTag tag = super.serializeToNBT();
tag.putString("string", string);
return tag;
}
}The last step we need to do for StorageData is to implement the deserialization method. This should be a public static method taking a CompoundTag tag and int version arguments, and return a CustomStorageData.
You can use the #deserializeBlockPos method to deserialize the BlockPos.
public static CustomStorageData deserializeFromNBT(CompoundTag tag, int version) {
BlockPos pos = deserializeBlockPos(tag);
if (pos == null) throw new IllegalArgumentException("Could not read BlockPos in CustomStorageData!");
return new CustomStorageData(pos, tag.getString("string"));
}Putting these two together, we get our full StorageData class:
public class CustomStorageData extends StorageData {
public static final ResourceLocation ID = ResourceLocation.fromNamespaceAndPath("example", "custom_sd");
private final String string;
public CustomStorageData(BlockPos pos, String string) {
super(pos);
this.string = string;
}
@Override
public ResourceLocation getId() {
return ID;
}
@Override
public CompoundTag serializeToNBT() {
CompoundTag tag = super.serializeToNBT();
tag.putString("string", string);
return tag;
}
public static CustomStorageData deserializeFromNBT(CompoundTag tag, int version) {
BlockPos pos = deserializeBlockPos(tag);
if (pos == null) throw new IllegalArgumentException("Could not read BlockPos in CustomStorageData!");
return new CustomStorageData(pos, tag.getString("string"));
}
}