[UTIL] Inventory to String/File

Discussion in 'Resources' started by JHG0, Dec 31, 2013.

?

Which of the four item's that are not supported by the plugin would you like added first

  1. Enchanted Books

    10.0%
  2. Written Books

    40.0%
  3. Book and Quills

    10.0%
  4. Fireworks

    40.0%
Thread Status:
Not open for further replies.
  1. Offline

    JHG0

    Hey Guys!
    JHG0 here with a very requested, long awaited, inventory to string parser
    I spent hours upon hours getting everything to work.
    This supports PlayerInventories, Inventories, and DoubleChestInventories (Cast to Inventory)
    This doesn't currently support Enchanted Books, Written Books, Book and Quills, or Fireworks
    Feel free to use it in all your plugins : )


    Registration
    In the Main Class
    Code:
    public Parser par;
    In the onEnable()
    Code:
    par = new Parser(this.getDescription().getName());

    Calling on the parser
    Note: Anything in CAPS is something that you choose; Don't forget file extensions!
    Code:
    par.parsePlayerInventory(FOLDER, FILE NAME, PLAYER INVENTORY);
    par.unparsePlayerInventory(FOLDER, FILE NAME, PLAYER INVENTORY);
    par.parseInventory(FOLDER, FILE NAME, INVENTORY);
    par.unparseInventory(FOLDER, FILE NAME, INVENTORY);

    Parser.java
    Code:
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
     
    import org.bukkit.Material;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Parser extends JavaPlugin{
     
        String pluginName = "";
     
        public Parser(String pluginName){
            this.pluginName = pluginName;
        }
     
        public void parsePlayerInventory(String fileLocation, String fileName, PlayerInventory inv){
            /*Created by JHG0*/
            File f = new File("plugins" + File.separator + this.pluginName + File.separator + fileLocation, fileName);
            if(!f.exists()) {
                try {
                    f.createNewFile();
                } catch (IOException e) {
                }
            }
            f.setWritable(true, false);
            FileConfiguration fConfig = YamlConfiguration.loadConfiguration(f);
            try {
                fConfig.save(f);
            } catch (IOException e) {
            }
            int i = 0;
            for(ItemStack is : inv.getContents()){
                try{
                    fConfig.set(i + ".id", is.getType().toString());
                } catch (NullPointerException e){
                    fConfig.set(i + ".id", "%" + Integer.MAX_VALUE + "%");
                }
                try{
                    fConfig.set(i + ".amount", is.getAmount());
                } catch (NullPointerException e){
                    fConfig.set(i + ".amount", 0);
                }
                try {
                    fConfig.set(i + ".dur", is.getDurability());
                } catch (NullPointerException e){
                    fConfig.set(i + ".dur", 0);
                }
                try{
                    is.getItemMeta().getDisplayName();
                    is.getItemMeta().hasDisplayName();
                    String dn = is.getItemMeta().getDisplayName().toString();
                    fConfig.set(i + ".name", dn);
                } catch (NullPointerException e){
                    fConfig.set(i + ".name", "%noname%");
                }
                try{
                    is.getItemMeta().getLore();
                    is.getItemMeta().hasLore();
                    List<String> lore = is.getItemMeta().getLore();
                    for(int x = 0; x < lore.size(); x++){
                        lore.set(x, lore.get(x));
                    }
                    fConfig.set(i + ".lore", lore);
                } catch (NullPointerException e){
                    fConfig.set(i + ".lore", "%nolore%");
                }
                try{
                    is.getItemMeta().getEnchants();
                    is.getItemMeta().hasEnchants();
                    Map<Enchantment, Integer> enchants = is.getEnchantments();
                    @SuppressWarnings("rawtypes")
                    Iterator it = enchants.entrySet().iterator();
                    int j = 0;
                    fConfig.set(i + ".hasEnchantments", true);
                    fConfig.set(i + ".enchantmentSize", 0);
                    fConfig.set(i + ".enchantments", null);
                    while(it.hasNext()){
                        fConfig.set(i + ".enchantmentSize", j + 1);
                        @SuppressWarnings("rawtypes")
                        Map.Entry pairs = (Map.Entry)it.next();
                        Enchantment ench = (Enchantment) pairs.getKey();
                        fConfig.set(i + ".enchantments." + j + ".name", ench.getName());
                        fConfig.set(i + ".enchantments." + j + ".level", pairs.getValue());
                        j++;
                    }
                } catch (NullPointerException e){
                    fConfig.set(i + ".hasEnchantments", false);
                    fConfig.set(i + ".enchantmentSize", 0);
                    fConfig.set(i + ".enchantments", "%noenchants%");
                }
                i++;
            }
            /* Armor */
            i = 0;
            for(ItemStack is : inv.getArmorContents()){
                try{
                    fConfig.set("ARMOR_" + i + ".id", is.getType().toString());
                } catch (NullPointerException e){
                    fConfig.set("ARMOR_" + i + ".id", "%" + Integer.MAX_VALUE + "%");
                }
                try{
                    fConfig.set("ARMOR_" + i + ".amount", is.getAmount());
                } catch (NullPointerException e){
                    fConfig.set("ARMOR_" + i + ".amount", 0);
                }
                try {
                    fConfig.set("ARMOR_" + i + ".dur", is.getDurability());
                } catch (NullPointerException e){
                    fConfig.set("ARMOR_" + i + ".dur", 0);
                }
                try{
                    is.getItemMeta().getDisplayName();
                    is.getItemMeta().hasDisplayName();
                    String dn = is.getItemMeta().getDisplayName().toString();
                    fConfig.set("ARMOR_" + i + ".name", dn);
                } catch (NullPointerException e){
                    fConfig.set("ARMOR_" + i + ".name", "%noname%");
                }
                try{
                    is.getItemMeta().getLore();
                    is.getItemMeta().hasLore();
                    List<String> lore = is.getItemMeta().getLore();
                    for(int x = 0; x < lore.size(); x++){
                        lore.set(x, lore.get(x));
                    }
                    fConfig.set("ARMOR_" + i + ".lore", lore);
                } catch (NullPointerException e){
                    fConfig.set("ARMOR_" + i + ".lore", "%nolore%");
                }
                try{
                    is.getItemMeta().getEnchants();
                    is.getItemMeta().hasEnchants();
                    Map<Enchantment, Integer> enchants = is.getEnchantments();
                    @SuppressWarnings("rawtypes")
                    Iterator it = enchants.entrySet().iterator();
                    int j = 0;
                    fConfig.set("ARMOR_" + i + ".hasEnchantments", true);
                    fConfig.set("ARMOR_" + i + ".enchantmentSize", 0);
                    fConfig.set("ARMOR_" + i + ".enchantments", null);
                    while(it.hasNext()){
                        fConfig.set("ARMOR_" + i + ".enchantmentSize", j + 1);
                        @SuppressWarnings("rawtypes")
                        Map.Entry pairs = (Map.Entry)it.next();
                        Enchantment ench = (Enchantment) pairs.getKey();
                        fConfig.set("ARMOR_" + i + ".enchantments." + j + ".name", ench.getName());
                        fConfig.set("ARMOR_" + i + ".enchantments." + j + ".level", pairs.getValue());
                        j++;
                    }
                } catch (NullPointerException e){
                    fConfig.set("ARMOR_" + i + ".hasEnchantments", false);
                    fConfig.set("ARMOR_" + i + ".enchantmentSize", 0);
                    fConfig.set("ARMOR_" + i + ".enchantments", "%noenchants%");
                }
                i++;
            }
            try {
                fConfig.save(f);
            } catch (IOException e) {
            }
            /*Created by JHG0*/
        }
     
        public PlayerInventory unparsePlayerInventory(String fileLocation, String fileName, PlayerInventory inv){
            /*Created by JHG0*/
            ItemStack[] is = new ItemStack[inv.getSize()];
            ItemStack[] armor = new ItemStack[inv.getArmorContents().length];
            int i = 0;
            File f = new File("plugins" + File.separator + pluginName + File.separator + fileLocation, fileName);
            if(f.exists()){
                FileConfiguration fConfig = YamlConfiguration.loadConfiguration(f);
                for(i = 0;i < inv.getSize(); i++){
                    int amount = 0;
                    String name = "";
                    int dur = 0;
                    List<String> lore = new ArrayList<String>();
                    String enchantName = "";
                    int enchantLevel = 0;
                    Enchantment ench = null;
                    if(fConfig.getString(i + ".id").equalsIgnoreCase("%" + Integer.MAX_VALUE + "%")){
                        is[i] = new ItemStack(Material.AIR);
                    } else {
                        amount = (Integer) fConfig.get(i + ".amount");
                        dur = (Integer) fConfig.get(i + ".dur");
                        ItemStack item = new ItemStack(Material.getMaterial(fConfig.getString(i + ".id")));
                        item.setDurability((short) dur);
                        item.setAmount(amount);
                        ItemMeta im = item.getItemMeta();
                        try{
                            name = (String) fConfig.get(i + ".name");
                            if(!name.equalsIgnoreCase("%noname%")){
                                im.setDisplayName((String) fConfig.get(i + ".name"));
                            }
                        } catch (NullPointerException e){
                        }
                        if(fConfig.get(i + ".lore") instanceof List && !fConfig.get(i + ".lore").equals("%nolore%")){
                            lore = fConfig.getStringList(i + ".lore");
                            im.setLore(lore);
                        }
                        Map<Enchantment, Integer> enchants = new HashMap<Enchantment, Integer>();
                        if(fConfig.getBoolean(i + ".hasEnchantments") == true) {
                            if(fConfig.getInt(i + ".enchantmentSize") > 0) {
                                for(int j = 0; j < fConfig.getInt(i + ".enchantmentSize"); j++) {
                                    enchantName = fConfig.getString(i + ".enchantments." + j + ".name");
                                    enchantLevel = fConfig.getInt(i + ".enchantments." + j + ".level");
                                    ench = Enchantment.getByName(enchantName);
                                    enchants.put(ench, enchantLevel);
                                }
                            }
                        }
                        item.addUnsafeEnchantments(enchants);
                        item.setItemMeta(im);
                        is[i] = item;
                        is[i].addUnsafeEnchantments(enchants);
                    }
                }
                /* Armor */
                for(i = 0;i < 4; i++){
                    int amount = 0;
                    String name = "";
                    int dur = 0;
                    List<String> lore = new ArrayList<String>();
                    String enchantName = "";
                    int enchantLevel = 0;
                    Enchantment ench = null;
                    try{
                        if(fConfig.get("ARMOR_" + i + ".id").equals("%" + Integer.MAX_VALUE + "%") || fConfig.get("ARMOR_" + i + ".id").equals("AIR") || !(fConfig.get("ARMOR_" + i + ".id") instanceof String)){
                            armor[i] = new ItemStack(Material.AIR);
                        } else {
                            amount = (Integer) fConfig.get("ARMOR_" + i + ".amount");
                            dur = (Integer) fConfig.get("ARMOR_" + i + ".dur");
                            ItemStack item = new ItemStack(Material.getMaterial(fConfig.getString("ARMOR_" + i + ".id")));
                            item.setDurability((short) dur);
                            item.setAmount(amount);
                            ItemMeta im = item.getItemMeta();
                            try{
                                name = (String) fConfig.get("ARMOR_" + i + ".name");
                                if(!name.equalsIgnoreCase("%noname%")){
                                    im.setDisplayName((String) fConfig.get("ARMOR_" + i + ".name"));
                                }
                            } catch (NullPointerException e){
                            }
                            if(fConfig.get("ARMOR_" + i + ".lore") instanceof List && !fConfig.get("ARMOR_" + i + ".lore").equals("%nolore%")){
                                lore = fConfig.getStringList("ARMOR_" + i + ".lore");
                                im.setLore(lore);
                            }
                            Map<Enchantment, Integer> enchants = new HashMap<Enchantment, Integer>();
                            if(fConfig.getBoolean("ARMOR_" + i + ".hasEnchantments") == true) {
                                if(fConfig.getInt("ARMOR_" + i + ".enchantmentSize") > 0) {
                                    for(int j = 0; j < fConfig.getInt("ARMOR_" + i + ".enchantmentSize"); j++) {
                                        enchantName = fConfig.getString("ARMOR_" + i + ".enchantments." + j + ".name");
                                        enchantLevel = fConfig.getInt("ARMOR_" + i + ".enchantments." + j + ".level");
                                        ench = Enchantment.getByName(enchantName);
                                        enchants.put(ench, enchantLevel);
                                    }
                                }
                            }
                            item.addUnsafeEnchantments(enchants);
                            item.setItemMeta(im);
                            armor[i] = item;
                            armor[i].addUnsafeEnchantments(enchants);
                        }
                    } catch (NullPointerException exe){
                        armor[i] = new ItemStack(Material.AIR);
                    }
                }
            }
            inv.setArmorContents(armor);
            inv.setContents(is);
            return inv;
            /*Created by JHG0*/
        }
     
        public void parseInventory(String fileLocation, String fileName, Inventory inv){
            /*Created by JHG0*/
            File f = new File("plugins" + File.separator + pluginName + File.separator + fileLocation, fileName);
            if(!f.exists()) {
                try {
                    f.createNewFile();
                } catch (IOException e) {
                }
            }
            f.setWritable(true, false);
            FileConfiguration fConfig = YamlConfiguration.loadConfiguration(f);
            try {
                fConfig.save(f);
            } catch (IOException e) {
            }
            int i = 0;
            for(ItemStack is : inv.getContents()){
                try{
                    fConfig.set(i + ".id", is.getType().toString());
                } catch (NullPointerException e){
                    fConfig.set(i + ".id", "%" + Integer.MAX_VALUE + "%");
                }
                try{
                    fConfig.set(i + ".amount", is.getAmount());
                } catch (NullPointerException e){
                    fConfig.set(i + ".amount", 0);
                }
                try {
                    fConfig.set(i + ".dur", is.getDurability());
                } catch (NullPointerException e){
                    fConfig.set(i + ".dur", 0);
                }
                try{
                    is.getItemMeta().getDisplayName();
                    is.getItemMeta().hasDisplayName();
                    String dn = is.getItemMeta().getDisplayName().toString();
                    fConfig.set(i + ".name", dn);
                } catch (NullPointerException e){
                    fConfig.set(i + ".name", "%noname%");
                }
                try{
                    is.getItemMeta().getLore();
                    is.getItemMeta().hasLore();
                    List<String> lore = is.getItemMeta().getLore();
                    for(int x = 0; x < lore.size(); x++){
                        lore.set(x, lore.get(x));
                    }
                    fConfig.set(i + ".lore", lore);
                } catch (NullPointerException e){
                    fConfig.set(i + ".lore", "%nolore%");
                }
                try{
                    is.getItemMeta().getEnchants();
                    is.getItemMeta().hasEnchants();
                    Map<Enchantment, Integer> enchants = is.getEnchantments();
                    @SuppressWarnings("rawtypes")
                    Iterator it = enchants.entrySet().iterator();
                    int j = 0;
                    fConfig.set(i + ".hasEnchantments", true);
                    fConfig.set(i + ".enchantmentSize", 0);
                    fConfig.set(i + ".enchantments", null);
                    while(it.hasNext()){
                        fConfig.set(i + ".enchantmentSize", j + 1);
                        @SuppressWarnings("rawtypes")
                        Map.Entry pairs = (Map.Entry)it.next();
                        Enchantment ench = (Enchantment) pairs.getKey();
                        fConfig.set(i + ".enchantments." + j + ".name", ench.getName());
                        fConfig.set(i + ".enchantments." + j + ".level", pairs.getValue());
                        j++;
                    }
                } catch (NullPointerException e){
                    fConfig.set(i + ".hasEnchantments", false);
                    fConfig.set(i + ".enchantmentSize", 0);
                    fConfig.set(i + ".enchantments", "%noenchants%");
                }
                i++;
            }
            try {
                fConfig.save(f);
            } catch (IOException e) {
            }
            /*Created by JHG0*/
        }
     
        public Inventory unparseInventory(String fileLocation, String fileName, Inventory inv){
            /*Created by JHG0*/
            ItemStack[] is = new ItemStack[inv.getSize()];
            File f = new File("plugins" + File.separator + pluginName + File.separator + fileLocation, fileName);
            if(f.exists()){
                FileConfiguration fConfig = YamlConfiguration.loadConfiguration(f);
                for(int i = 0;i < inv.getSize(); i++){
                    int amount = 0;
                    String name = "";
                    int dur = 0;
                    List<String> lore = new ArrayList<String>();
                    String enchantName = "";
                    int enchantLevel = 0;
                    Enchantment ench = null;
                    if(fConfig.getString(i + ".id").equalsIgnoreCase("%" + Integer.MAX_VALUE + "%")){
                        is[i] = new ItemStack(Material.AIR);
                    } else {
                        amount = (Integer) fConfig.get(i + ".amount");
                        dur = (Integer) fConfig.get(i + ".dur");
                        ItemStack item = new ItemStack(Material.getMaterial(fConfig.getString(i + ".id")));
                        item.setDurability((short) dur);
                        item.setAmount(amount);
                        ItemMeta im = item.getItemMeta();
                        try{
                            name = (String) fConfig.get(i + ".name");
                            if(!name.equalsIgnoreCase("%noname%")){
                                im.setDisplayName((String) fConfig.get(i + ".name"));
                            }
                        } catch (NullPointerException e){
                        }
                        if(fConfig.get(i + ".lore") instanceof List && !fConfig.get(i + ".lore").equals("%nolore%")){
                            lore = fConfig.getStringList(i + ".lore");
                            im.setLore(lore);
                        }
                        Map<Enchantment, Integer> enchants = new HashMap<Enchantment, Integer>();
                        if(fConfig.getBoolean(i + ".hasEnchantments") == true) {
                            if(fConfig.getInt(i + ".enchantmentSize") > 0) {
                                for(int j = 0; j < fConfig.getInt(i + ".enchantmentSize"); j++) {
                                    enchantName = fConfig.getString(i + ".enchantments." + j + ".name");
                                    enchantLevel = fConfig.getInt(i + ".enchantments." + j + ".level");
                                    ench = Enchantment.getByName(enchantName);
                                    enchants.put(ench, enchantLevel);
                                }
                            }
                        }
                        item.addUnsafeEnchantments(enchants);
                        item.setItemMeta(im);
                        is[i] = item;
                        is[i].addUnsafeEnchantments(enchants);
                    }
                }
            }
            inv.setContents(is);
            return inv;
            /*Created by JHG0*/
        }
    }

    I also uploaded the Parser.java file to the paste...Click here for a simpler view

    Enjoy and leave a like if this was useful : )
    - JHG0
     
    ArthurMaker likes this.
  2. Offline

    DarkBladee12

    JHG0 Did you even know that ItemStack implements ConfigurationSerializable which allows you to save it to the config with CONFIG.set(PATH, ITEMSTACK) and get it from the config with CONFIG.getItemStack(PATH)? (These methods can be used for every kind of item and it also saves the meta of the item)
     
    Stealth2800 likes this.
  3. Offline

    JHG0

  4. Offline

    DarkBladee12

    JHG0 Sorry, I just wanted to let you know that :(
     
    MTN likes this.
  5. Offline

    Compressions

    DarkBladee12 No, you make a good point. Bukkit's built-in methods need more recognition. When you find things I think are missing from the Bukkit API, making a pull request is always a great option.
     
  6. Offline

    BungeeTheCookie

    At least he tried his best. I will try to make this better and post some code with a better version.
     
  7. Offline

    Necrodoom

    Removed flaming and useless posts.
    If you think a tutorial is not good, explain why, attacking the OP however is not acceptable and will net you a warning.
     
  8. Offline

    JHG0

Thread Status:
Not open for further replies.

Share This Page