SimplySerializations
This utilitity API will let you serialize objects into a byte array without any effort.
By creating two simple methods, you will be able to serialize and deserialize any object!
Setup
Add a maven dependency or download the jar and add it to the build path of your plugin.
maven:
Code (Text):
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<dependency>
<groupId>com.github.ODINN555</groupId>
<artifactId>SerializationsPlus</artifactId>
<version>v1.0.3</version>
</dependency>
Usage
Currently listed serializers:
- ItemStack
- List
- Map
Example of how to add and use serializers:
Code (Java):
package
me.example.plugin
;
import
com.github.SimplySerialize.Serializations
;
import
com.github.SimplySerialize.Serializer
;
import
org.bukkit.plugin.java.JavaPlugin
;
import
java.util.HashMap
;
import
java.util.Map
;
public
class ExamplePlugin
extends JavaPlugin
{
@Override
public
void onEnable
(
)
{
// Register serializers on the enable of the plugin
Serializations.
registerSerializer
(
new Serializer
<ExampleObject
>
(
"ExampleObject",ExampleObject.
class
)
{
@Override
public Map
<
String, Object
> serializeValue
(ExampleObject exampleObject
)
{
// Always instance a new map here! do not use any short version of creating a map!
Map
<
String,Object
> map
=
new HashMap
<>
(
)
;
// Put the values of the object.
// With this values you need to be able to create a new object.
map.
put
(
"exampleKey",
"exampleValue"
)
;
return map
;
}
@Override
public ExampleObject deserializeValue
(Map
<
String, Object
> map
)
{
// Retrieve the values and create a new object with them
String example
=
(
String
) map.
get
(
"exampleKey"
)
;
return
new ExampleObject
(
)
;
}
}
)
;
// Example of Simply Serializing an object, it is that simple!
ExampleObject object
=
new ExampleObject
(
)
;
byte
[
] arr
= Serializations.
serialize
(object
)
;
ExampleObject deserializedObject
=
(ExampleObject
) Serializations.
deserialize
(arr
)
;
}
}
Persistent Data Container
If you wish to store the serialized object into a persistent data container. simply use this class below for Byte[] data type.
Code (Java):
import
org.bukkit.persistence.PersistentDataAdapterContext
;
import
org.bukkit.persistence.PersistentDataType
;
import
org.bukkit.util.io.BukkitObjectInputStream
;
import
org.bukkit.util.io.BukkitObjectOutputStream
;
import
java.io.ByteArrayInputStream
;
import
java.io.ByteArrayOutputStream
;
/**
* A PersistentDataType class which represents a byte[] data type since any object is first serialized to a byte[] through Serializations
*/
public
class StoredData
implements PersistentDataType
<
byte
[
] ,
byte
[
]
>
{
public StoredData
(
)
{
}
@Override
public Class
<
byte
[
]
> getComplexType
(
)
{
return
byte
[
].
class
;
}
@Override
public Class
<
byte
[
]
> getPrimitiveType
(
)
{
return
byte
[
].
class
;
}
@Override
public
byte
[
] toPrimitive
(
byte
[
] arg0, PersistentDataAdapterContext arg1
)
{
try
{
ByteArrayOutputStream byteOut
=
new
ByteArrayOutputStream
(
)
;
BukkitObjectOutputStream bukkitOut
=
new BukkitObjectOutputStream
(byteOut
)
;
bukkitOut.
writeObject
(arg0
)
;
bukkitOut.
flush
(
)
;
return byteOut.
toByteArray
(
)
;
}
catch
(
Exception e
)
{
e.
printStackTrace
(
)
;
return
null
;
}
}
@Override
public
byte
[
] fromPrimitive
(
byte
[
] arg0, PersistentDataAdapterContext arg1
)
{
try
{
ByteArrayInputStream byteIn
=
new
ByteArrayInputStream
(arg0
)
;
BukkitObjectInputStream bukkitIn
=
new BukkitObjectInputStream
(byteIn
)
;
return
(
byte
[
]
) bukkitIn.
readObject
(
)
;
}
catch
(
Exception e
)
{
return
null
;
}
}
}
Contact
If you need any help with the plugin, suggestion, review or you just want to ask me something. Post it on this thread or DM me through discord (ODINN#1010).
Contribute
If you wish to contribute, please like the thread or post a gode review on it, it helps more than you think!
If your a developer and you wish to contribute, Create a pull request on Github and ill review it.