How should I format my config to save/get the following data?

Discussion in 'Spigot Plugin Development' started by Drtro, May 15, 2015.

  1. When a player right clicks the block with a key (tripwire hook) in hand, it creates an inventory that can be later accessed by right clicking that same block.

    I'm wondering what would be the best way to do this, to determine if the block already has an inventory, or if it needs to create a new one.

    I was thinking of something like this.

    Locations:
    1: world,x,y,z
    2: world,x,y,z
    2: world,x,y,z
    Items:
    1:
    - ItemStack list
    2:
    - ItemStack list
    3:
    - ItemStack list

    So it would be Locations.Key#, location value and Items.Key#, ItemStack list value.

    I could load the Locations section to a HashMap, and if the block location matches one of value, I get the key and load the ItemStack list of the same number. So if the block location matched #3, it would then get ItemStack list from Items.3 and load it in the inventory on open.
     
  2. Location#key sounds like a good bet to me. Is your question how you'd implement this?
     
  3. Not exactly how, but when should I save/load inventories?
     
  4. If you want it to update live, for saving, a good idea might be every time the inventory is closed. For loading, you can do it as soon as it is opened.

    If you're using chests as the block that keeps the items, a good idea might be to initialize the inventories when the server starts, and saving them when the server closes.

    It's also worth noting that if you create Inventory objects and find a way to match them up with the blocks (using your Location system, for example), you don't even need to use chests if you had something else in mind.
     
  5. There are no chests involved. It just creates an inventory at the block, and that inventory can be accessed by right clicking it, whether the block is stone or a torch. Only materials I blacklisted so far is water, air, and lava. If I made the inventory type chest, would I have to save the item stacks, or would spigot save it?
     
  6. If you made the inventory a chest, yes, Spigot would save it with the world.
     
  7. Code (Text):

    public void onDisable(){
    config().save;
     


    JK I am not going to spoonfeed
     
  8. You should do config.save() when the Plugin is disabled
     
  9. Code (Text):

    public void onEnable(){

    config.load();
    }
     
    Code (Text):

    public void onDisable(){

    config.save();
    }
     
     
  10. Make the inventory a chest type? It's not a physical chest.
     
  11. portable inventory or something?
     
  12. Oh, sorry, misinterpreted the post.

    Setting the inventory type to chest will not save it. If it actually is a chest, it will be saved. In your case, just load and save the inventories on startup/shutdown (respectively).
     
  13. Could I change the block to a chest, but change what it looks like without changing the block type? Like a chest block but it looks like stone?
     
  14. Like how GriefPrevention shows a border but doesnt really change the blocks, or anti x-ray. Can I do that with chests?
     
  15. I suppose so... You could send a packet or sendBlockChange, but I think the chest updates would mess it up. Just save the inventory.
     
  16. Hm, okay.
     
  17. I looked at it and just thought List<Location> and List<ItemStack> as soon as I saw this, if I remember correctly Location already generates x y etc in the YamlConfiguration
     
  18. String blockLoc = event.getClickedBlock().getWorld().getName() + "," + event.getClickedBlock().getX() + "," + event.getClickedBlock().getY() + "," + event.getClickedBlock().getZ();



    HashMap<String, String> savedLocs = new HashMap<String, String>();
    for(String key : plugin.getConfig().getConfigurationSection("Locations").getKeys(false))
    {
    savedLocs.put(key, plugin.getConfig().getString("Locations." + key));
    }
    if(savedLocs.containsValue(blockLoc))
    {
    //get key, create inventory, load inventory from Items.key
    }
    else
    {
    //add blockLoc to config, create new inventory, save inventory key in config, etc.
    }
    }
     
  19. Does sendBlockChange still work?
     
  20. So by HashMap<String, String> are you trying to save a location under some directory? Instead I suggest just making a list for each location then when loading back onto the HashMap just create a new location with that information
    indexName:
    x: x
    y: y
    z: z
    world: worldName

    as for the itemstacks being in a list i suggest just making a new YamlConfiguration data.yml and directly save/load from there