Cloning a block with all block data

Discussion in 'Plugin Development' started by vemacs, Jan 6, 2013.

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

    vemacs

    Suppose I have a block at a Location, and I want to copy that block, block data, faces, and all, to another Location.
    How would I do that?
    Using getTypeID and getData and using those values to setTypeID and setData doesn't seem to do anything.
    It would be nice if somebody could provide a basic example.
    Thanks in advance!
     
  2. Offline

    caseif

    Hmm...
    I wrote up some code to do this, and it works fine for the most part. It's a bit buggy with signs, messing with the collision barrier and sign information. Here's the code I wrote:
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent e){
    3. Block block1 = e.getClickedBlock();
    4. Location l = e.getClickedBlock().getLocation();
    5. l.setY(e.getClickedBlock().getY() - 1);
    6. Block block2 = l.getBlock();
    7. block2.setData(block1.getData());
    8. block2.setType(block1.getType());
    9.  
    10. }

    My guess would be that you would need to move either the state or the tile entity over to the new block after checking if the block is applicable. I'm not sure if there are methods that do this, so you may need to set the information manually for each block type (signs, chests, etc.).
     
  3. Offline

    vemacs

    Can anybody provide any code to get the data from chests, signs, etc...? For signs, Sign.Clone() may be usable, except I can't find anything about usage in the documentation...
     
  4. Offline

    fireblast709

    get the BlockState. Problem solved
     
  5. Offline

    vemacs

    I can't test this right now, so would the code look something like this:

    Code:
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e){
    Block block1 = e.getClickedBlock();
    Location l = e.getClickedBlock().getLocation();
    l.setY(e.getClickedBlock().getY() - 1);
    Block block2 = l.getBlockState().getBlock();
    block2.setData(block1.getBlockState().getData());
    block2.setType(block1.getBlockState().getType());
     
    }
     
  6. Offline

    fireblast709

    vemacs no BlockState was more for storing a copy. If you want to just set the type and data, use setTypeIdAndData(int id, byte data)
     
  7. Offline

    vemacs

    Would you mind providing an example?
     
  8. Offline

    fireblast709

    block2.setTypeIdAndData(block1.getTypeId(), block1.getData());
     
  9. Offline

    vemacs

    So would this also store the data for note blocks, signs, chests, etc..?
     
  10. Offline

    fireblast709

    no. I have no clue how to actually copy over the BlockState. The only thing I know requires a lot of if-statements. Maybe set the x, y and z of the BlockState to the place where it should be copied, and call .update(true)
     
  11. Offline

    vemacs

    I'm currently looking thru the CreeperHeal code, as that does something similar.
     
Thread Status:
Not open for further replies.

Share This Page