Skip to content

Instantly share code, notes, and snippets.

@gepron1x
Last active June 2, 2021 19:29
Show Gist options
  • Save gepron1x/7bd88457406029e33058b453dbedbc81 to your computer and use it in GitHub Desktop.
Save gepron1x/7bd88457406029e33058b453dbedbc81 to your computer and use it in GitHub Desktop.
itemStack serialization
package me.gepron1x.utils.persistentdatatype;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.persistence.PersistentDataAdapterContext;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
public class ItemStackDataType implements PersistentDataType<byte[], ItemStack> {
private ItemStackDataType() {}
public static final ItemStackDataType INSTANCE = new ItemStackDataType();
private static final ItemStack AIR_ITEM = new ItemStack(Material.AIR);
private static final byte[] AIR = new byte[] {27, 115};
@Override
public @NotNull Class<byte[]> getPrimitiveType() {
return byte[].class;
}
@Override
public @NotNull Class<ItemStack> getComplexType() {
return ItemStack.class;
}
@Override
public @NotNull byte[] toPrimitive(@NotNull ItemStack complex, @NotNull PersistentDataAdapterContext context) {
return complex.getType() == Material.AIR ? AIR : complex.serializeAsBytes();
}
@Override
public @NotNull ItemStack fromPrimitive(@NotNull byte[] primitive, @NotNull PersistentDataAdapterContext context) {
return Arrays.equals(AIR, primitive) ? AIR_ITEM : ItemStack.deserializeBytes(primitive);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment