Save HashSet data after restart

Discussion in 'Plugin Development' started by Snowshovel, Jul 3, 2013.

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

    Snowshovel

    I created a HashSet which saves Blocks like so:

    Code:
        HashSet<Block> set = new HashSet<Block>();
    //Later on in the program
        set.add(event.getBlockPlaced());
    //Even later
        if (set.contains(event.getClickedBlock()) {
    //Do something
    }
    The HashSet works fine, but when my server restarts, all of the data in the HashSet is lost. How do I save the data so that when the server restarts, the block will behave the same as it would if the restart never took place?
     
  2. Offline

    Sessional

    Snowshovel
    Several ways to do it - you can use an Object Output Stream
    http://docs.oracle.com/javase/1.4.2/docs/api/java/io/ObjectOutputStream.html
    Buffered writer is capable of it - but it requires a little more effort then the ObjectOutputStream.
    I'm personally a big fan of BufferedWriter because I can just write text and if anything goes wrong it is easy to fix and debug because it's a text file. Certainly not efficient compared to other options though.
    And there is other options too.
     
  3. Offline

    Snowshovel

    Sessional

    What's the simplest way to save it over a restart?
     
  4. Offline

    Sessional

    Snowshovel
    Personal preference - I like text files personally using BufferedWriter and then a BufferedReader to read them in.
     
  5. Offline

    Snowshovel

  6. Offline

    pasow

    are buffered writers (textfiles) a plausible alternative to databases (mysql?) for custom server plugins?
     
  7. Offline

    AmShaegar

    I don't think you can just save and restore blocks into and from a file. You schouldl have mentioned that in your other thread too. In this case, because you only need the location of the block, I'd recommend to save String instead of Block in the HashSet. Do this by serializing them.

    Code:
    private String serializeBlock(Block b) {
        StringBuilder s = new StringBuilder();
        s.append(b.getWorld().getName());
        s.append(":");
        s.append(b.getX());
        s.append(":");
        s.append(b.getY());
        s.append(":");
        s.append(b.getZ());
        return s.toString();
    }
     
        HashSet<String> set = new HashSet<String>();
    //Later on in the program
        set.add(serializeBlock(event.getBlockPlaced()));
    //Even later
        if (set.contains(serializeBlock(event.getClickedBlock())) {
    //Do something
    }
     
  8. Offline

    Sessional

    https://github.com/Sessional/Waypoints/tree/master/src/waypoints
    Check out this code, it's hard to explain, but it can get you started, and will work for flat files.

    As for the mysql comment - any database is more efficient, but server owners rarely want to deal with setting one up if they can just use flat files.
     
Thread Status:
Not open for further replies.

Share This Page