[SOLVED] block.setType(material) and BlockPlaceEvent

Discussion in 'Plugin Development' started by fromgate, May 15, 2012.

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

    fromgate

    Hello!

    With help of my plugin player can change some blocks.
    I'm using block.setType(material) method to block changing.

    But I would like to handle the block setting with events - BlockPlaceEvent.
    For example player building something with my plugin. If he trying to build in protected area my plugin override protections and player can grief. Operations of my plugins are not registered with LogBlock.

    Is it possible to place block by plugin and handle it by BlockPlaceEvent?
     
  2. Offline

    Nitnelave

    well, you would have to fire a new blockPlaceEvent every time you place a block, but that will cause a lot of problems, since you can't have a player to pass. You can try and find a way to have a virtual player, maybe?
     
    fromgate likes this.
  3. Offline

    Craftiii4

    fromgate likes this.
  4. Offline

    fromgate

    Thank you... But how I can raise BlockPlaceEvent from inside the PlayerInteractEvent (I think in this time I don't need to create a virtual player)


    I think trying to learning and supporting API of all private and logging plugins is not best way in that moment (we use three private plugin at our server, LogBlock, one plugin that collect statistics and I don't know how much plugins that handles BlockPlaceEvent and BlockBreakEvent...
    I think we need something more universal.

    I'm trying to raise BlockPlaceEvent:

    Code:java
    1.  
    2. public BlockPlaceEvent PlaceBlock(Block clickedBlock, Player p, Material newType, byte newData){
    3. BlockState state = clickedBlock.getState();
    4. state.setType(Material.DIAMOND_BLOCK);
    5. state.getData().setData((byte)0);
    6. Block placedBlock = state.getBlock();
    7. BlockPlaceEvent event = new BlockPlaceEvent(placedBlock, state, clickedBlock, p.getItemInHand(), p, true);
    8. this.getServer().getPluginManager().callEvent(event);
    9. return event;
    10. }
    11.  


    BlockPlaceEvent is executing. But dimonds block are not placed.

    In a BlockPlaceEvent, event returns:
    event.getBlockPlaced() is equal to previous state clicked block and same to event.getBlock();

    But event.getBlockReplacedState() is equal to new state (diamond block).

    How I must use placedBlock variable to get a new block, not old?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  5. Try something like this:
    Code:text
    1. public void onEvent(PlayerInteractEvent event)
    2. {
    3. ...
    4. Block block = getTheBlock();
    5. BlockState oldState = block.getState();
    6. block.setType(Material.DIAMOND);
    7. BlockPlaceEvent event = new BlockPlaceEvent(oldState.getBlock(), oldState, block, p.getItemInHand(), p, true);
    8. Bukkit.getPluginManager().callEvent(event);
    9. if(event.isCancelled())
    10. {
    11. oldState.update(true);
    12. return;
    13. }
    14. ...
    15. }
     
    fromgate likes this.
  6. Offline

    fromgate

    V10lator
    Thank you very much! You really helped me :)

    Here is my final procedures to place any block with raising the BlockPlaceEvent:

    Code:java
    1.  
    2. public void PlaceBlock(Location loc, Player p, Material newType, byte newData){
    3. PlaceBlock (loc.getBlock(),p,newType,newData);
    4. }
    5.  
    6. public void PlaceBlock(Block block, Player p, Material newType, byte newData){
    7. BlockState state = block.getState();
    8. block.setType(newType);
    9. block.setData(newData);
    10. BlockPlaceEvent event = new BlockPlaceEvent(state.getBlock(), state, block, p.getItemInHand(), p, true);
    11. this.getServer().getPluginManager().callEvent(event);
    12. if (event.isCancelled()) state.update(true);
    13. }
    14.  
     
  7. Np. :)
    But maybe you should use block.setTypeIdAndData instead of block.setType and block.setData? ;)
     
  8. Offline

    fromgate

    I did just after posting procedures here ;)
     
Thread Status:
Not open for further replies.

Share This Page