SetData en SetType not working

Discussion in 'Plugin Development' started by stefvanschie, Aug 5, 2015.

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

    stefvanschie

    Hey,

    I'm making an auto-resetting arena and I have a save and restore method. The save method works fine, but the restore method only sets the data value for every block to zero and isn't setting the type at all.
    This is my code:
    Code:
    package me.stefvanschie;
    
    import java.util.HashMap;
    import java.util.Iterator;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.block.Block;
    
    public class Arena {
       
        static HashMap<Block, String> blocks = new HashMap<Block, String>();
       
        public static void save(String arena) {
            int maxplayers = BuildingGame.main.arenas.getInt(arena + ".maxplayers");
           
            for (int place = 1; place <= maxplayers; place++) {
               
                int minX = BuildingGame.main.arenas.getInt(arena + "." + place + ".low.x");
                int maxX = BuildingGame.main.arenas.getInt(arena + "." + place + ".high.x");
                int minY = BuildingGame.main.arenas.getInt(arena + "." + place + ".low.y");
                int maxY = BuildingGame.main.arenas.getInt(arena + "." + place + ".high.y");
                int minZ = BuildingGame.main.arenas.getInt(arena + "." + place + ".low.z");
                int maxZ = BuildingGame.main.arenas.getInt(arena + "." + place + ".high.z");
    
                for (int x = minX; x <= maxX; x++) {
                    for (int y = minY; y <= maxY; y++) {
                        for (int z = minZ; z <= maxZ; z++) {
                            Location location = new Location(Bukkit.getWorld(BuildingGame.main.arenas.getString(arena + "." + place + ".high.world")), x, y, z);
                            blocks.put(location.getBlock(), arena);
                        }
                    }
                }
            }
        }
    
        @SuppressWarnings("deprecation")
        public static void restore(String arena) {
            Iterator<Block> iterator = blocks.keySet().iterator();
            while (iterator.hasNext()) {
                Block block = iterator.next();
                if (blocks.get(block).equals(arena)) {
                    block.getLocation().getWorld().getBlockAt(block.getLocation()).setType(block.getType());
                    block.getLocation().getWorld().getBlockAt(block.getLocation()).setData(block.getData());
                    iterator.remove();
                }
            }
        }
    }
    
     
  2. Offline

    _Error

    I'm not sure why but just for your information hashmaps gets resets upon plugin disabling
     
  3. Offline

    stefvanschie

    @_Error Yes i know, but the blocks are saved in the same session as the are getting restored. But the restoring isn't working.
     
  4. Offline

    OracleTarget

    @stefvanschie Maybe because the iterator.remove() is inside de while loop?
     
  5. @stefvanschie
    It's because Block#getType() and Block#getData() returns what is currently at that location. You need to make the map's value type BlockState and store each block's BlockState in it (returned using Block#getState()), and when you load you can simply call BlockState#update(true) to make the block exactly as it was before.
     
  6. Offline

    stefvanschie

    @OracleTarget
    The loop is just fine.

    @megamichiel
    The state of a block is already stored inside the block, isn't it? I'm getting the location from my block and get the block that's currently there, so if I changed air into stone, my block is air, but the block on that location is stone. After that I'm getting a block at the location of the stored block and setting the type to the type of the original block.
     
  7. @stefvanschie
    The "Block" class is just some sort of utility, everything it holds is it's chunk and it's position. If you call Block#getType() it returns the type of the current block, not the one when you saved it. With BlockState (Block#getState()) it does save how it was at that moment, that's why you should use that.
     
  8. Offline

    stefvanschie

    @megamichiel How would I set the state of the block, cause I can't find a setState() method. Would I have to get the material from the state and than set it to the block at that location?
     
  9. @stefvanschie
     
  10. Offline

    stefvanschie

    @megamichiel Thanks, for some reason the blockstate.update didn't work, so I got the type and data and setted it to the block.
     
Thread Status:
Not open for further replies.

Share This Page