Plugin to restore

Discussion in 'Plugin Development' started by Tobirogl, Oct 26, 2015.

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

    Tobirogl

    I´m programming a plugin to restore a arena in a multiverse map.

    I have one problem:
    placed blocks are not disappearing, only broken blocks

    My code for this is:
    Code:
        @EventHandler
        public void onBlock(BlockBreakEvent e) {
            Block b = e.getBlock();
            String block = b.getTypeId() + ":" + b.getData() + ":" + b.getWorld().getName() + ":" + b.getX() + ":" + b.getY() + ":" + b.getZ();
      
            CHANGES.add(block);
        }
      
        @EventHandler
        public void onBlock(BlockPlaceEvent e) {
            Block b = e.getBlock();
            String block = b.getTypeId() + ":" + b.getData() + ":" + b.getWorld().getName() + ":" + b.getX() + ":" + b.getY() + ":" + b.getZ();
      
            CHANGES.add(block);
        }
    I hope you can give me good answers
     
    Last edited by a moderator: Oct 26, 2015
  2. Offline

    timtower Administrator Administrator Moderator

    @Tobirogl Try outputting what you are saving in the CHANGES list
    It might save the new block at blockplace
     
  3. Offline

    Tobirogl

    How?
     
  4. Offline

    timtower Administrator Administrator Moderator

    getLogger().info would do
     
  5. Offline

    Tobirogl

    Ok. i try
     
  6. Offline

    au2001

    @Tobirogl You shouldn't use a string array list for this.
    Try a HashMap<Location, MaterialData> to save the block and it's original form.
    You should also save it in the config not in a variable so that if the server is reloaded/restarted it can still reset the map correctly and you don't loose your map.

    But if I had to do this I would clone a world/area and never touch the original map, or use a schematic.
     
  7. Offline

    Tobirogl

    How can i use the HashMap?
     
  8. Offline

    au2001

    @Tobirogl To create a hashmap, use this :
    Code:
    HashMap<Location, MaterialData> changes = new HashMap<Location, MaterialData>();
    To insert a lines (each time a block is placed/broken), use :
    Code:
    changes.put(b.getLocation(), b.getState().getData());
    And to restore the map :
    Code:
    for (Location b : changes) { // Loop through all the changes made during the game
        b.getBlock().getState().setData(changes.get(b)); // Set the block's data to it's original data stored in the hashmap
    }
    
    changes = new HashMap<Location, MaterialData>(); // Finally reset the hashmap to clear the old lines
    // OR changes.clear();
    But note that if players use TNT/creeper, or if they burn things, etc. your plugin won't restore the map correctly.
    That's why I advise you to use schematics or map cloning even though it is more laggy (if you have separate servers for each game it will be perfectly fine, and you might want to do that without depending on bukkit to restore it while the server is stopped, it will be faster and you can also reset plugins at the same time).
     
Thread Status:
Not open for further replies.

Share This Page