Saving/loading hasmaps with locations?

Discussion in 'Plugin Development' started by CevinWa, Jul 15, 2012.

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


    Im pretty new to coding and i've got a problem.
    When i try to save my HashMap<Location, ArrayList<Block>> it won't go using slapi. i think i need some method on converting Location to string and block to object but im not shure about how to do so and i know nothing about sereilizeable or what is it called. Really helpful if someone took the time and helped me with this. :D
    And im thanking you just for wathing this.
  2. Offline


    you cant actually convert an streing to an location reliable, becayuse the world may be unloaded at the moment of your atempt, the same goes for block, you actually need to store the x,y,z,and the world name, and then you have al objects that arew serezizeable, so you can use the slapi to store them to a file
  3. Offline


    How do i store the x,y,z and the world name?
    And don't you have to get them back later and get them back into the location hashmap or something. thank u for helping me :d
  4. Offline


    He just told you, in a string.... separated by a character like ; or something.
    And then just use string.split(";") to get an array of them and then just re-create the location... pretty easy.
  5. Offline


    Then use another character to separate different blocks like "|"

    example file "worldname;14;80;22|worldname;73;97;14" and so on,
  6. Offline


    But im like noob on this. Don't understand, is there a tutorial on doing this? cuz I really whan't to learn it.
    searched google but found nothing.
  7. Offline


    It's alot of data to be manually stored and loaded, we were suggesting on how to make Location be serialized for use in object writer.

    You also shouldn't store Block there... you'll need a string that holds X/Y/Z too, without world because I assume it's the same world as the key locatino's world.

    And you've never made strings before ? It's too easy... let me provide an example to use with a method so you don't have to manually do it over and over:
    private HashMap<String, ArrayList<String>> blocksHashMap = new HashMap<String, ArrayList<String>>();
    private String locationToString(Location loc)
        return loc.getWorld().getName() + ";" + loc.getBlockX() + ";" + loc.getBlockY() + ";" + loc.getBlockZ();
  8. Offline


    And then then i'l just add loc to the hashmap with hashmap.put(loc);?
    and this should be within the class where i store the block in the hashmap with?
    public void onBlockPlace(BlockPlaceEvent event){
    Material block = event.getBlock().getType();
    Player player = event.getPlayer();
    Location loc = event.getBlockPlaced().getLocation();?
  9. Offline


    put() has 2 arguments.... the 2nd argument must be the ArrayList containing the blocks... which I recommend you swap with String or even Vector if that's serializable.
    And why are you asking how to use that, isn't it pretty obvious what it gets and what it returns ? :/ it only has 4 lines which 2 are only brackets...
  10. Offline


    I think you would want to see my post here.
  11. Offline


    Like this?
  12. Offline


    for the saving/loading part

    That's the best tut i have seen for this.
Thread Status:
Not open for further replies.

Share This Page