Solved Set a block to different types of log

Discussion in 'Plugin Development' started by Molten, May 14, 2014.

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

    Molten

    As the title says, I want to get a block at the current location and set the block to one of the different types of log, Birch, Oak, etc.

    Right now the only way I can see this is getting a tree type and setting that tree to the right type I want. But once I have that tree type I have no idea how to send it over to a block so it can be set that way. I also have no way of getting the current tree type from a block.

    The code I have is fairly simple, it takes a block, moves it, and then sets it to a LOG. What I really want it to do is to copy over the block type and have that be placed instead (so if it was a birch log, it would place a birch log) however I have copied the block.getType() and set the new location.getBlock().setType(copied type) and it only places oak logs.

    I have looked online for some documentation and the stuff I see, if any is deprecated code. So what is the easiest way to go about doing this?

    I also noticed that in the tree type you can set the direction of the log (laying down, standing up) I really would like to just take a blank tree, set the species to the type of species hit, and then change the direction. Either that or get the tree of the block hit, and change the direction on that. Either way, I have no idea how to convert from tree to material or add a tree to the material etc. I know it's materialData but I am not sure how to modify that.
     
  2. Offline

    Onlineids

    you use the data block.setData(1) will return a birch log
     
  3. Offline

    Molten

    I know it does that, but that is deprecated. I would like to avoid deprecated code where possible.
     
  4. Offline

    Onlineids

    Works fine I use it all the time
     
  5. Offline

    xTigerRebornx

  6. Offline

    Molten

    I have it working to where it gets the block and the tree species, then it tries to set the new blocks species to the old species, but doesn't. Here is the code:

    Code:java
    1. Tree tree = (Tree) log.getState().getData();
    2. TreeSpecies species = tree.getSpecies();
    3.  
    4. //Code here that does some work to move the block
    5.  
    6. tree.setSpecies(species);
    7. e.getPlayer().sendMessage(species.toString());
    8. newLocation.getBlock().setType(logMaterial);
    9. newLocation.getBlock().getState().setData(tree);
    10. newLocation.getBlock().getState().update(true);


    Whenever I hit a birch tree, it correctly outputs birch but then places a generic.

    Edit: After doing some tests I noticed it isn't changing the block data. I printed out the data of the block at newLocation and it is saying data:0 for all logs, which means either my setData(tree) isn't doing what I think it should be doing, or the block isn't getting updated.
     
  7. Offline

    xTigerRebornx

    Molten BlockState isn't a live copy of the block, so those last 2 lines are modifying different copies of the Block, therefor you are setting the data in the 2nd to last (but never updating it), then getting the state of the Block (which was set to 'logMaterial' which I am guessing is just Material.LOG. Try storing the BlockState in its own variable, and then do all modifications to it, then call update on that.
    Pseudo-code
    i.e.
    Code:
    BlockState s = newLocation.getBlock().getState();
    s.setData(tree);
    s.update(true);
     
  8. Offline

    Molten

Thread Status:
Not open for further replies.

Share This Page