Skip to content

Instantly share code, notes, and snippets.

@masecla22
Created August 15, 2018 06:02
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save masecla22/d2f77720f3b076f7796da1bd6ea8df7d to your computer and use it in GitHub Desktop.
Save masecla22/d2f77720f3b076f7796da1bd6ea8df7d 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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment