Skip to content

Commit

Permalink
add more storage docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Charismara committed Aug 29, 2024
1 parent 1fedf19 commit 9d69a8e
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 4 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,10 @@ public static class ExampleStorage extends Storage {
public int getExampleInt() {
return exampleInt;
}

public void setExampleInt(int exampleInt) {
this.exampleInt = exampleInt;
markDirty(); // Tells the system that the storage has been modified
}
}
```
File renamed without changes.
27 changes: 27 additions & 0 deletions docs/storage/04-accessing-storage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Accessing a Storage
Storages can be accessed using the `StorageHolder#manasCore$getStorageOptional` method and your `StorageKey` instance you get when registering a storage.

```java
entity.manasCore$getStorageOptional(STORAGE_KEY);
level.manasCore$getStorageOptional(STORAGE_KEY);
chunk.manasCore$getStorageOptional(STORAGE_KEY);
```

## Modifying Data
To modify data inside of a storage you can just mutate the storage instance you get from the `StorageHolder#manasCore$getStorageOptional` method.

It is recommended to create a getter and setter method for each field you want to modify within your storage to ensure that changes mark the storage as dirty:

```java
entity.manasCore$getStorageOptional(STORAGE_KEY).ifPresent(storage -> {
storage.setExampleInt(420);
})
```
It is also possible to directly modify the storage data without using a setter method, but you have to call `markDirty` manually to ensure that the storage is marked as dirty:

```java
entity.manasCore$getStorageOptional(STORAGE_KEY).ifPresent(storage -> {
storage.exampleInt = 420;
storage.markDirty();
})
```
38 changes: 38 additions & 0 deletions docs/storage/05-optimizing-update-performance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Optimize update Package performance
By default ManasCore Storages fully deserialize and serialize the data on every update.
This can be a performance bottleneck if you have a lot of data in your storage.
To optimize the performance, you can override the `Storage#saveOutdated` method in your custom Storage class to only apply the changed data to the update tag.

```java
public static class ExampleStorage extends Storage {
private int exampleInt = 0;

public ExampleStorage(StorageHolder holder) {
super(holder);
}

@Override
public void save(CompoundTag data) {
data.putInt("exampleInt", exampleInt);
}

@Override
public void load(CompoundTag data) {
exampleInt = data.getInt("exampleInt");
}

public int getExampleInt() {
return exampleInt;
}

public void setExampleInt(int exampleInt) {
this.exampleInt = exampleInt;
markDirty(); // Tells the system that the storage has been modified
}

@Override
public void saveOutdated(CompoundTag data) {
data.putInt("exampleInt", exampleInt);
}
}
```
4 changes: 0 additions & 4 deletions docs/storage/getting-started/_category_.json

This file was deleted.

0 comments on commit 9d69a8e

Please sign in to comment.