Skip to content

Instantly share code, notes, and snippets.

@aadnk
Created January 22, 2013 11:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aadnk/4593947 to your computer and use it in GitHub Desktop.
Save aadnk/4593947 to your computer and use it in GitHub Desktop.
Serialization without using CraftBukkit
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]);
}
}
@TechTeller
Copy link

Thanks a ton for this! It's really handy for my plugin. :)

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