Skip to content

Instantly share code, notes, and snippets.

@602723113
Forked from masecla22/ItemStackSerializer.java
Created August 17, 2018 12:09
Show Gist options
  • Save 602723113/342970a07ccf01267e2454ee0d4a6022 to your computer and use it in GitHub Desktop.
Save 602723113/342970a07ccf01267e2454ee0d4a6022 to your computer and use it in GitHub Desktop.
A class for converting bukkit ItemStack and ItemStacksArrays to Strings and back
package masecla.practicepvp.serializers;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import org.bukkit.inventory.ItemStack;
public class ItemStackSerializer {
/**
*
* @param what - The ItemStack to be converted into a string
* @return The String that contains the ItemStack (will return null if anything goes wrong)
*/
public static String convertItemStackToString(ItemStack what){
// serialize the object
String serializedObject = "";
try {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream so = new ObjectOutputStream(bo);
so.writeObject(what);
so.flush();
serializedObject = bo.toString();
return serializedObject;
} catch (Exception e) {
System.out.println(e);
}
return null;
}
/**
*
* @param data - The String to be converted into an ItemStack
* @return The ItemStack Array obtained from the string (will return void should anything go wrong)
*/
public static ItemStack convertStringToItemStack(String data){
// deserialize the object
try {
byte b[] = data.getBytes();
ByteArrayInputStream bi = new ByteArrayInputStream(b);
ObjectInputStream si = new ObjectInputStream(bi);
ItemStack obj = (ItemStack) si.readObject();
return obj;
} catch (Exception e) {
System.out.println(e);
}
return null;
}
/**
*
* @param what - The ItemStack to be converted into a string
* @return The String that contains the ItemStack (will return null if anything goes wrong)
*/
public static String convertItemStackArrayToString(ItemStack what[]){
// serialize the object
String serializedObject = "";
try {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream so = new ObjectOutputStream(bo);
so.writeObject(what);
so.flush();
serializedObject = bo.toString();
return serializedObject;
} catch (Exception e) {
System.out.println(e);
}
return null;
}
/**
*
* @param data - The String to be converted into an ItemStack Array
* @return The ItemStack Array obtained from the string (will return void should anything go wrong)
*/
public static ItemStack[] convertStringToItemStackArray(String data){
// deserialize the object
try {
byte b[] = data.getBytes();
ByteArrayInputStream bi = new ByteArrayInputStream(b);
ObjectInputStream si = new ObjectInputStream(bi);
ItemStack[] obj = (ItemStack[]) si.readObject();
return obj;
} catch (Exception e) {
System.out.println(e);
}
return null;
}
}
@conorb314
Copy link

ty this has been super helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment