Solved BlockState to config?

Discussion in 'Plugin Development' started by Ganga, Jan 6, 2015.

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

    Ganga

    hey guys!
    Iam doing a plugin for myself but I stuck at one point.
    When I try to save a BlockState List to a config. I can convert it to a string but not the other way round.
    My plugin:
    - When I do a command the player gets into a kind of edit mode. Every placed block is saved to an array.
    - When the player uses another command I want those blocks spawned on his location.

    How do I get this work?

    Any help?
    Thank you very much!!! :)
     
  2. create the list
    Code:
    List<Location> blockLocations = new ArrayList<Location>();
    
    get blocks
    Code:
    Block b1 = world.getBlockAt(100, 100, 100);
    Block b2 = world.getBlockAt(1000, 1000, 1000);
    
    adding the blocklocations to the list
    Code:
    blockLocations.add(b1.getLocation());
    blockLocations.add(b2.getLocation());
    
    saving block locations into the config:
    Code:
    this.getConfig().set("BlockLocations", blockLocations);
    this.saveConfig();
    
    get the location of the block again:
    Code:
    Location l = (Location) this.getConfig().getList("BlockLocations").get(0);
    
    i hope thats want you ment and i hope it helped :)

    greetings
     
  3. Offline

    xTrollxDudex

    Why can't you convert the other way?
     
  4. Code:
    public String serializeLoc(Location l){
        return l.getWorld().getName()+","+l.getBlockX()+","+l.getBlockY()+","+l.getBlockZ();
    }
    public Location deserializeLoc(String s){
        String[] st = s.split(",");
        return new Location(Bukkit.getWorld(st[0]), Integer.parseInt(st[1]), Integer.parseInt(st[2]), Integer.parseInt(st[3]));
    }
    
    to save the location just do this :
    Code:
    getConfig().set("path", serializeLoc(location));
    
    to get a location from the config :
    Code:
    Location location = deserializeLoc("path");
    
     
  5. yes thats also a method to do but he said he already got a list so i think this is an easier way to to
    Code:
    this.getConfig().set("BlockLocations", blockLocations);
    this.saveConfig();
    
    cause else he has to add everything with a for loop
     
  6. Offline

    Ganga

    first off all thank you all four your answers!
    @sn1cko

    Yes I allready tried that but then it gives me an error, because its not able to save a BlockList like this.
    Thats my Array:
    Code:
    public List<BlockState> blocklist = new ArrayList<BlockState>();
    And thats what I tried:
    Code:
        @EventHandler
        public void onBlockBreak(BlockBreakEvent event){
    
        blocklist.add(event.getBlock().getState());
         
        }
        
    And of course I saved it with saveConfig();
    the same way as you said.

    @sn1cko

    sorry It doesnt allows me to load the saved blocks!
    Error:
    ENDLESS nullpointer exception

    EDIT by Timtower: merged posts
     
    Last edited: Jan 8, 2015
  7. why dont you save the Location of the Block or the Block itself ?
     
  8. Offline

    1Rogue

    Location is not ConfigurationSerializable.
     
    sn1cko likes this.
  9. i see but you could save world,x,y,z and create a new Location onEnable() :)

    i think that should work

    try to save the cordinates and the world and also the material
    and if you need it you can build your own block ^^
     
    Last edited by a moderator: Jan 8, 2015
  10. Offline

    1Rogue

    The class I use: https://github.com/CodeLanx/Codelan...codelanx/codelanxlib/serialize/SLocation.java

    Allows making Locations serializable via:

    Code:java
    1. Location loc = /*...*/;
    2. getConfig().set("example", new SLocation(loc));
    3. SLocation fromConf = getConfig().get("example");
    4. loc = fromConf.toLocation();
     
    Ganga and sn1cko like this.
  11. Offline

    SuperOriginal

  12. Offline

    Ganga

    Thanks all! Helped me alot!
     
Thread Status:
Not open for further replies.

Share This Page