Skip to content

Instantly share code, notes, and snippets.

@pedroagrs
Last active February 6, 2025 11:46
Show Gist options
  • Save pedroagrs/ece014ae567481df848eca042d7580a5 to your computer and use it in GitHub Desktop.
Save pedroagrs/ece014ae567481df848eca042d7580a5 to your computer and use it in GitHub Desktop.
ItemStack Serializer - Base64
package yourpackage;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.io.BukkitObjectInputStream;
import org.bukkit.util.io.BukkitObjectOutputStream;
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
public class ItemSerializer {
public static String write(ItemStack... items) {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream)) {
dataOutput.writeInt(items.length);
for (ItemStack item : items)
dataOutput.writeObject(item);
return Base64Coder.encodeLines(outputStream.toByteArray());
} catch (Exception ignored) {
return "";
}
}
public static ItemStack[] read(String source) {
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(source));
BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream)) {
ItemStack[] items = new ItemStack[dataInput.readInt()];
for (int i = 0; i < items.length; i++)
items[i] = (ItemStack) dataInput.readObject();
return items;
} catch (Exception ignored) {
return new ItemStack[0];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment