Created
January 22, 2013 11:31
-
-
Save aadnk/4593947 to your computer and use it in GitHub Desktop.
Serialization without using CraftBukkit
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 com.comphenix.example; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.bukkit.configuration.ConfigurationSection; | |
import org.bukkit.configuration.InvalidConfigurationException; | |
import org.bukkit.configuration.file.YamlConfiguration; | |
import org.bukkit.inventory.Inventory; | |
import org.bukkit.inventory.ItemStack; | |
public class ItemSerialization { | |
public static String saveInventory(Inventory inventory) { | |
YamlConfiguration config = new YamlConfiguration(); | |
// Save every element in the list | |
saveInventory(inventory, config); | |
return config.saveToString(); | |
} | |
public static void saveInventory(Inventory inventory, ConfigurationSection destination) { | |
// Save every element in the list | |
for (int i = 0; i < inventory.getSize(); i++) { | |
ItemStack item = inventory.getItem(i); | |
// Don't store NULL entries | |
if (item != null) { | |
destination.set(Integer.toString(i), item); | |
} | |
} | |
} | |
public static ItemStack[] loadInventory(String data) throws InvalidConfigurationException { | |
YamlConfiguration config = new YamlConfiguration(); | |
// Load the string | |
config.loadFromString(data); | |
return loadInventory(config); | |
} | |
public static ItemStack[] loadInventory(ConfigurationSection source) throws InvalidConfigurationException { | |
List<ItemStack> stacks = new ArrayList<ItemStack>(); | |
try { | |
// Try to parse this inventory | |
for (String key : source.getKeys(false)) { | |
int number = Integer.parseInt(key); | |
// Size should always be bigger | |
while (stacks.size() <= number) { | |
stacks.add(null); | |
} | |
stacks.set(number, (ItemStack) source.get(key)); | |
} | |
} catch (NumberFormatException e) { | |
throw new InvalidConfigurationException("Expected a number.", e); | |
} | |
// Return result | |
return stacks.toArray(new ItemStack[0]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a ton for this! It's really handy for my plugin. :)