[Tutorial] Block Restoration

Discussion in 'Resources' started by mattrick, Feb 3, 2014.

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

    mattrick

    This is my first tutorial, so bear with me. Have you ever wanted to restore whole areas of blocks? Weather it be from griefers, to minigame arena, it is easier than you think! In this (very basic) I will show you how to do that!

    First off, your going to have to create a list of BlockState's and store it for later (you can even save it to a config file, but I'll leave that out for now). We do that with this simple code:
    Code:java
    1. public List<BlockState> blocklist = new ArrayList<BlockState>();


    This will create a new variable called "blocklist" and will be able to store blocks, and their location. Now we need to populate the list. To do that, we need to add a BlockState whenever a block is broken (it can really be whenever you want, as long as you can get a BlockState).
    Code:java
    1. @EventHandler
    2. public void onBlockBreak(BlockBreakEvent event){
    3. blocklist.add(event.getBlock().getState());
    4. }


    This will add the BlockState to our list whenever a block is broken. If we were doing this in a minigame, we would first check if the player who broke the block is in the minigame.

    Now to get to the meat and potatoes of our lesson, we will restore the blocks. It is really up to you on when you want to restore the blocks, just as long as you can some way access our blocklist variable. This loop will iterate through each one of our BlockStates in our variable, get the location, and set the location to our block.
    Code:java
    1. for (BlockState block: blocklist){
    2. block.update(true);
    3. }
    4. blocklist.clear();

    This code will update the blocks to the BlockState. The "true" paramater forces the block to update, no matter what.

    NOTE: This code doesn't restore MetaData (ie. Sign text, Wool colors, Chest contents)
    Found a way that restores everything, thanks to a certain Troll I know....


    If we want to store this data in a configuration file, we would simply save the blocklist to the config and when we need it, restore the blocks and clear the config and list (there is no use in storing the data anymore, it was already restored). If you have any problems with the code, feel free to Thag me for help :)
     
  2. Offline

    xTrollxDudex

    mattrick
    If you plan on serializing BlockStates, you may need to store the fields of CraftBlockState as BlockState is A) an interface and B) not ConfigurationSerializable
     
  3. Offline

    mattrick

    xTrollxDudex
    Yeah, good point. I'll write it in later, when I'm not being lazy....
     
  4. Offline

    TryB4

  5. Offline

    HariHD

    mattrick Does setting a block to air count at a BlockBreakEvent?? Im making a TNT Run like plugin
     
  6. Offline

    mattrick

    TryB4
    Oh, didn't see that...like xTrollxDudex said, you should just use update, not reset the data and block. I think my code is easier to understand than yours though.

    HariHD
    No, when you set the block to air, add the block you set to air to the blocklist.
     
  7. Offline

    random_username

    mattrick
    Thanks for the tutorial! Maybe you could clear the list after restoring? :D
     
  8. Offline

    mattrick

  9. Offline

    HariHD

    mattrick

    Code:java
    1. blocklist.add(block.getState());
    2. block.setType(Material.AIR);


    ? like this??
     
  10. Offline

    mattrick

  11. Offline

    HariHD

    mattrick Just tested it. It didn't work
     
  12. Offline

    mattrick

    HariHD
    Can I see your whole code?
     
  13. Offline

    HariHD

    mattrick

    Code:java
    1. @EventHandler
    2. public void onPlayerMove2(PlayerMoveEvent e) {
    3.  
    4. final Block block = e.getTo().getBlock().getLocation().add(new Vector(0,-1,0)).getBlock();
    5. if(changeblock == true) {
    6. if (block.getType().equals(Material.GRAVEL)) {
    7. this.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    8. public void run() {
    9. blocklist.add(block.getState());
    10. block.setType(Material.AIR);
    11.  
    12. }
    13.  
    14. },10L);
    15. }
    16. }
    17. }


    mattrick ??

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  14. Offline

    mattrick

    HariHD
    What doesn't work? Can I see the code where you restore it?
     
  15. Offline

    HariHD

    mattrick What you said about.

    Code:java
    1. for (BlockState block: blocklist) {
    2. block.update(true);
    3. }
    4. blocklist.clear();
     
  16. Offline

    mattrick

    HariHD
    Your whole code. Your EventListener, Commands, etc.
     
  17. Offline

    HariHD

    mattrick Does that even matter? Commands and other Events have nothing to do with this..
     
  18. Offline

    mattrick

    HariHD
    You may have done something wrong with the commands. It works fine for me...
     
  19. Offline

    HariHD

    mattrick Send me your code? I can compare because my code is a minigame and there is a lot of classes and code.
     
  20. Offline

    mattrick

    HariHD
    I don't have access to it right now. You can try the link TryB4 posted above to his tutorial if you want.
     
  21. Offline

    mrgreen33gamer

    mattrick
    TryB4

    So if I was to use this BlockState method, how would it work with: BlockPlaceEvent ?
     
  22. Offline

    Plugers11

  23. Offline

    mrgreen33gamer

    Plugers11 It's actually not the same. I tried it my self when restoring when someone builds. It doesn't,
     
Thread Status:
Not open for further replies.

Share This Page