Created
August 15, 2018 06:02
-
-
Save masecla22/d2f77720f3b076f7796da1bd6ea8df7d to your computer and use it in GitHub Desktop.
A class for converting bukkit ItemStack and ItemStacksArrays to Strings and back
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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