1212
1313public final class ItemStackConverter {
1414
15- public final static boolean hasByteSerializeSupport ;
15+ private final static boolean hasByteSerializeSupport ;
1616
1717 static {
1818 hasByteSerializeSupport = Try .run (() -> ItemStack .class .getMethod ("deserializeBytes" , byte [].class ))
@@ -27,6 +27,10 @@ public static void init(MultiverseInventories plugin) {
2727 config = plugin .getServiceLocator ().getService (InventoriesConfig .class );
2828 }
2929
30+ public static boolean isEmptyItemStack (@ Nullable ItemStack itemStack ) {
31+ return itemStack == null || itemStack .getType () == Material .AIR || itemStack .getAmount () == 0 ;
32+ }
33+
3034 @ Nullable
3135 public static ItemStack deserialize (Object obj ) {
3236 if (obj instanceof ItemStack itemStack ) {
@@ -42,16 +46,20 @@ public static ItemStack deserialize(Object obj) {
4246
4347 @ Nullable
4448 public static Object serialize (ItemStack itemStack ) {
45- if (config != null && config .getUseByteSerializationForInventoryData () && hasByteSerializeSupport ) {
46- if (itemStack .getType () == Material .AIR ) {
47- return null ;
48- }
49- return Try .of (() -> Base64 .getEncoder ().encodeToString (itemStack .serializeAsBytes ()))
50- .onFailure (e -> Logging .severe ("Could not serialize item stack: %s" , e .getMessage ()))
51- .getOrNull ();
49+ if (isEmptyItemStack (itemStack )) {
50+ return null ;
51+ }
52+ if (config == null || !config .getUseByteSerializationForInventoryData () || !hasByteSerializeSupport ) {
53+ // let ConfigurationSerialization handle it
54+ return itemStack ;
5255 }
53- // let ConfigurationSerialization handle it
54- return itemStack ;
56+ return Try .of (() -> Base64 .getEncoder ().encodeToString (itemStack .serializeAsBytes ()))
57+ .onFailure (e -> Logging .severe ("Could not byte serialize item stack: %s" , e .getMessage ()))
58+ .getOrNull ();
59+ }
60+
61+ public static boolean hasByteSerializeSupport () {
62+ return hasByteSerializeSupport ;
5563 }
5664
5765 private ItemStackConverter () {
0 commit comments