How do i make an inventory go into config

Discussion in 'Spigot Plugin Development' started by Bailey Crowe, May 20, 2015.

  1. I need some help with my plugin: StarCache
    Currently you have to add items into the config, so it will make different crates fall. Well currently you have to add them into the config, how could i make it so you can just type a command i'll make "/starcache input" and it'll add all the items in your inventory as a new crate
    Config:
    Code (Text):
    general:
    ##################################################
    # Time Between each Star Cache Event. #
    # Expressed in Seconds #
    ##################################################
    eventCooldown: 43200
    ##################################################
    # Radius is caclulated in a square #
    # Values will be +- the defined #
    ##################################################
    world:
    radius: 5000
    ##################################################
    # DO NOT TOUCH #
    ##################################################
    lastEvent: 0
    ##################################################
    # Are we using Factions to determine #
    # safe areas? #
    ##################################################
    useFactions: true
    ##################################################
    # How should we broadcast coordinates? #
    # By CHUNK or EXACT? #
    ##################################################
    announcer:
    method: CHUNK
    caches:
    netherstar:
    - 399%1
    blocks:
    - 57%32
    - 41%32
    - 42%32
    the end where it says "caches:" is where i want to add the command so i can input items into the starcache, i've never worked with something like this in a plugin so some help would be nice!
    If someone is confused on what i mean just message me and i'll try to explain better
    Here is the caches part in my config.class
    Code (Text):
            if (config.contains("caches")) {
                Set<String> caches = config.getConfigurationSection("caches").getKeys(false);
                for (String cache : caches) {
                    System.out.println(cache);
                    List<String> items = config.getStringList("caches." + cache);
                    cacheList.add(new Cache(items));
                }
            }
        }
    So for the netherstar cache it would be caches.netherstar
    So to add my inventory it'll just take my inventory and add a
    caches.[name] then input inventory into the config then i can reload the config
    Also i've been messing with it, how could i add an item with an enchantment? considering 311%1
    would be 1 diamond chestplate how could i add an enchantment? i've been searching around and
    can't get this to work :(
     
    #1 Bailey Crowe, May 20, 2015
    Last edited: May 20, 2015
  2. To save an inventory to config, I would do something like,

    for (int slot = 0; slot < inventorySize; slot++)
    {
    ItemStack stack = inv.getItem(slot);
    if (stack != null) {
    items.set(String.valueOf(slot), stack);
    }
    }

    items being a configurationsection and inv being the inventory.
     
  3. konsolas

    Supporter

    Iterate through every item in an inventory and save it to a list. You can then easily save the list in the config.
     
  4. How exactly would i do this?
     
  5. konsolas

    Supporter

    Inventory extends Iterable, so you do it the same way as you would iterate over a list.
     
  6. Code (Java):
            for (ItemStack item : p.getInventory().getContents()) {
                getConfig().set(p.getUniqueId().toString() + ".inventory",
                        p.getInventory().getContents());
                getConfig().set(p.getUniqueId().toString() + ".armor",
                        p.getInventory().getArmorContents());
                saveConfig();
                Object a = getConfig().get(p.getName() + ".inventory");
                Object b = getConfig().get(p.getName() + ".armor");
                if (a == null || b == null) {
                    return;
                }
                ItemStack[] inventory = null;
                ItemStack[] armor = null;
                if (a instanceof ItemStack[]) {
                    inventory = (ItemStack[]) a;
                } else if (a instanceof List) {
                    List lista = (List) a;
                    inventory = (ItemStack[]) lista.toArray(new ItemStack[0]);
                }
                if (b instanceof ItemStack[]) {
                    armor = (ItemStack[]) b;
                } else if (b instanceof List) {
                    List listb = (List) b;
                    armor = (ItemStack[]) listb.toArray(new ItemStack[0]);
                }
                            // Keep the below code if you would like to load their inventory from the file in your method
                p.getInventory().clear();
                p.getInventory().setContents(inventory);
                p.getInventory().setArmorContents(armor);
    Ends up looking like this: http://pastebin.com/0gtatGB4

    Credit: https://bukkit.org/threads/saving-loading-inventory-to-a-file.133578/
     
  7. This will not work for my config... but thanks for the help
     
  8. why does this not work?
     
  9. That puts a players inventory in it's own config and in a different format, see that's why i'm having issues i don't want to have to redo my whole config, see how mine is the id%amount
     
  10. Would you mind pasting your configuration here so I could just take a quick look?
     
  11. Code (Text):
    package com.division.starcache.core;

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Set;
    import org.bukkit.configuration.file.YamlConfiguration;

    public class SCConfig {

        private File configFile;
        private YamlConfiguration config = new YamlConfiguration();
        private long eventCooldown;
        private long lastEvent;
        private int worldRadius;
        private String announcerMethod;
        private boolean usingFactions;
        private List<Cache> cacheList = new ArrayList<Cache>();

        public SCConfig(StarCache instance) {
            if (!instance.getDataFolder().exists()) {
                instance.getDataFolder().mkdirs();
            }
            configFile = new File(instance.getDataFolder(), "config.yml");
            if (!configFile.exists()) {
                try {
                    configFile.createNewFile();
                    setup();
                } catch (IOException e) {
                }
            }
        }

        private void setup() {
            try {
                FileOutputStream fos = new FileOutputStream(configFile);
                InputStream in = this.getClass().getResourceAsStream("/config.yml");
                byte[] bytesRec = new byte[4096];
                int bytes;
                while ((bytes = in.read(bytesRec, 0, bytesRec.length)) != -1) {
                    fos.write(bytesRec, 0, bytes);
                }
                in.close();
                fos.flush();
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public void load() {
            try {
                System.out.println("[StarCache] Loading config..");
                config.load(configFile);
            } catch (Exception ex) {
                System.out.println("[StarCache] generating config...");
                setup();
                load();
                return;
            }
            this.eventCooldown = config.getLong("general.eventCooldown");
            this.worldRadius = config.getInt("general.world.radius");
            this.lastEvent = config.getLong("general.lastEvent");
            this.announcerMethod = config.getString("general.announcer.method");
            if (config.contains("general.useFactions")) {
                this.usingFactions = config.getBoolean("general.useFactions");
            } else {
                this.usingFactions = true;
            }
            if (config.contains("caches")) {
                Set<String> caches = config.getConfigurationSection("caches").getKeys(false);
                for (String cache : caches) {
                    System.out.println(cache);
                    List<String> items = config.getStringList("caches." + cache);
                    cacheList.add(new Cache(items));
                }
            }
        }

        public long getLastEvent() {
            return lastEvent;
        }

        public void setLastEvent(long lastEvent) {
            this.lastEvent = lastEvent;
            config.set("general.lastEvent", lastEvent);
            try {
                config.save(configFile);
            } catch (IOException ex) {
            }
        }

        public int getWorldRadius() {
            return worldRadius;
        }

        public long getEventCooldown() {
            return eventCooldown;
        }

        public String getAnnouncerMethod() {
            return announcerMethod;
        }

        public List<Cache> getCacheList() {
            return cacheList;
        }

        public boolean isUsingFactions() {
            return usingFactions;
        }
    }
     
     
  12. It's already in the post
    Code (Text):
    general:
    ##################################################
    # Time Between each Star Cache Event. #
    # Expressed in Seconds #
    ##################################################
    eventCooldown: 43200
    ##################################################
    # Radius is caclulated in a square #
    # Values will be +- the defined #
    ##################################################
    world:
    radius: 5000
    ##################################################
    # DO NOT TOUCH #
    ##################################################
    lastEvent: 0
    ##################################################
    # Are we using Factions to determine #
    # safe areas? #
    ##################################################
    useFactions: true
    ##################################################
    # How should we broadcast coordinates? #
    # By CHUNK or EXACT? #
    ##################################################
    announcer:
    method: CHUNK
    caches:
    netherstar:
    - 399%1
    blocks:
    - 57%32
    - 41%32
    - 42%32
     
  13. Wouldn't this work?

    Code (Java):
            for (ItemStack item : p.getInventory().getContents()) {
                config.set("caches." + cache,
                        item);
                config.saveConfig();
                Object a = config.get("caches." + cache);
                if (a == null) {
                    return;
                }
                ItemStack[] inventory = null;
                if (a instanceof ItemStack[]) {
                    inventory = (ItemStack[]) a;
                } else if (a instanceof List) {
                    List lista = (List) a;
                    inventory = (ItemStack[]) lista.toArray(new ItemStack[0]);
                }
     
  14. How would i put this into a command like this "/starcache cache [name]" To put it as:
    caches:
    [name]
    [item]%[amount]
     
    #14 Bailey Crowe, May 21, 2015
    Last edited: May 21, 2015