Obtaining block data

Discussion in 'Plugin Development' started by Gravious, Jun 10, 2012.

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

    Gravious

    I have two blocks, one air block ( b1 ) and one stone brick stairs facing one specific direction attached to the block above it ( b2 )

    How can I copy the stone stairs to the other block along with all the other data (the direction, whether it is connected to a block above it or under it, the damage value) to another block? Is this possible with only using Integers?

    My code:

    Code:
    Location loc1 = new Location(p.getWorld(), x, y, z);
    Location loc2 = new Location(p.getWorld(), x + 2, y + 1, z);
    Block b1 = loc1.getBlock();
    Block b2 = loc2.getBlock();
    
    int[] b1Storage = new int[2]; // I don't know how many info needs to be stored, but I'll go for 3
    b1Storage[0] = b1.getTypeId();
    b1Storage[1] = (int) b1.getState().getRawData();
    b1Storage[2] = BlockFaceToInt(b1.getFace()); // Could this be something?
    
    b2.setTypeIdAndData(b1Storage[0], (byte) b1Storage[1], false);
    // b2.setFace? How do you go about doing that?
    
    How do you completely save the block to an integer array and then apply those integer values on another block?
    Any help would be greatly appreciated.

    P.S.: it has to be an integer array because I apparently can't serialize MaterialData and BlockFace, is there another way for this? I'm saving that b1Storage info for later usage using this: http://wiki.bukkit.org/Plugin_Tutorial#Saving.2FLoading_a_HashMap
     
  2. The "new int[2];" is actually size of 2, not 3... use 3 if you want to use indexes 0 to 2.
    You can also use block.getRelative(2, 1, 0); to get the 2nd block.

    If you want to serialize some data, you can make a class that implements Serializable which can store block id and data. (data stores the block's facing direction)
    Still, for special blocks like chests, furnaces, signs, etc, you'll need to also store the inventory, text for it if you want to completly "move" something.... for that you can make another class that extends your initial class with the additional ItemStack array or String array for texts and other stuff for other block types.
     
  3. Offline

    Gravious

    Oh, that explains why I got an ArrayOutOfBounds exception earlier on. I forgot to add, I have implements Serializable on the other class with a default constant of 1L but it still gives a non serializable. So the byte getData also stores the direction? If so I must be doing something wrong.

    The getRelative is not relavant because the blocks are not actually next to eachother; it was just an example to show that they were at different locations.

    Edit: I figured it out now. Thank you for the reply, it really helped me out.
     
Thread Status:
Not open for further replies.

Share This Page