Serializes Entities, Basically converting them to namespacedkeys that can be used later, not exactly serializing with its common definition.
Note:
- This will only work for 1.17.1+
- Only use classes EntitySerializer, and classes inside api package.
Easily turn an entity into a namespacedkey of the name of your choice, then turn it back into an entity.
Credits to: gizmonster for giving me the idea of using StructuresAPI Introduced in 1.17.1
For MAVEN: Add this to your repositories
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
Then add this to your dependencies
<dependency>
<groupId>com.github.Dqmino</groupId>
<artifactId>EntitySerializer</artifactId>
<version>v1.1</version>
</dependency>
For GRADLE:
Use this:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
And the dependency:
dependencies {
implementation 'com.github.Dqmino:EntitySerializer:v1.1'
}
First, you have to create an object of EntitySerializer in your onEnable:
EntitySerializer entitySerializer = EntitySerializer.create(this);
Then make a variable, referencing the class EntityHandler:
private static EntityHandler handler;
then on your onEnable again:
handler = entitySerializer.getEntityHandler();
Then make a static getter:
public static EntityHandler getHandler() {
return handler;
}
Then later in whatever class, you can use
NamespacedKey namespacedkey = new NamespacedKey(pluginInstance, "your-key-here");
YourMainClass.getHandler().serialize(entity, namespackedkey);
EntityHolder entityDeserialized = YourMainClass.getHandler().deserialize(namespacedkey);
Note that you should have a unique namespacedkey for each unique entity serialization.
You can invoke spawnAndGet()
(which will return the entity and spawn it) on the EntityHolder later, or spawn
.
and after that, you need to teleport it to your desired location, since the location you give to the spawn/spawnAndGet() methods is only used as a reference for internals.
And to Destroy a serialization (Remove its access, and its place in server storage) Use:
YourMainClass.getHandler().destroy(namespacedkey)