Solved getting overwritten block (Logging BlockPlacement)

Discussion in 'Plugin Development' started by Jusi, Jan 3, 2013.

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

    Jusi

    Hey guys,
    It's a new year and I have decided to finally finish my PVP server plugin. One of the things I tried to do is saving Blocks ofBlockBreak and BlockPlace events in HashMapsto reset them later.This works fine for BlockBreak events but I have no clue how to get the block that is overwritten by the new one.
    Here's some code of the EventHandlers.

    Code:
    //********************************BlockPlace*******************************************
        @EventHandler
        public void onBlockPlace(final BlockPlaceEvent event)
        {       
            Player player = event.getPlayer();
            Block block = event.getBlockPlaced();
            int blockOver = event.getBlockReplacedState().getBlock().getTypeId();
            byte blockOverData = event.getBlockReplacedState().getBlock().getData();
            Location location = block.getLocation();
            String CurrentWorld = player.getLocation().getWorld().toString();
            if (player == null || block == null)
            {return;}
    //stuff
            if(event.isCancelled() == false)    //only placed blocks
            {
                if(BlocksChanged.containsKey(location) == false)        //do not overwrite
                {
                    getServer().broadcastMessage(ChatColor.RED + prefix + " " +  blockOver + "  " + blockOverData);      //debug
                    BlocksChanged.put(location, blockOver);        //remember the old block, probably air
                    DataChanged.put(location, blockOverData);   
                }
            }
        }
    //***********************************BlockBreak***********************************************
        @EventHandler
        public void onBlockBreak(final BlockBreakEvent event)
        {
            Player player = event.getPlayer();
            Block block = event.getBlock();
            String CurrentWorld = player.getLocation().getWorld().toString();
            Location location = block.getLocation();
            if (player == null || block == null)
            {return;}
    //stuff
            if(event.isCancelled() == false)    //only broken blocks
            {
                if(BlocksChanged.containsKey(location) == false)        //do not overwrite
                {
                    BlocksChanged.put(location, block.getTypeId());        //remember the broken block
                    DataChanged.put(location, block.getData());
                }
            }
        }
    That's what I've guessed it should work like butevent.getBlockReplacedState().getBlock().getTypeId() and event.getBlockReplacedState().getBlock().getData() are block id and data of the new block.
    Does anyone have an idea what the right functions would be?
    Thanks for your help!
     
  2. Offline

    fireblast709

    event.getBlock().getTypeId() and event.getBlock().getData()
     
  3. Offline

    Jusi

    Well this actually is not right. I've tried it:
    Code:
    //[...]
    getServer().broadcastMessage(ChatColor.RED + prefix + " " + event.getBlock().getTypeId() + "  " + event.getBlock().getData());
                    getServer().broadcastMessage(ChatColor.RED + prefix + " " +  blockOver + "  " + blockOverData);
                    BlocksChanged.put(location, blockOver);        //remember the old block, probably air
                    DataChanged.put(location, blockOverData);    
    does print out the placed block twice.
     
  4. Offline

    fireblast709

    Jusi maybe because you are broadcasting twice?
     
  5. Offline

    Jusi

    Yes I am but as I mentioned what I am lookingfor was the replaced block (which is typically air but i can't do this assumption in that case) and not the placed block.
     
  6. Offline

    fireblast709

    which event.getBlock() is actually getting... getBlock() returns the Block at that location in the world before it was replaced by the block you are placing
     
  7. Offline

    Jusi

    Weird... I now have gotten the newest beta build andupdated the plugin but nothing has changed...
    If I place stone at some random (air) place, the output i get is (1 0), (1 0) - which is stone.
    That means event.getBlock().getTypeId() seems to be equal to event.getBlockReplacedState().getBlock().getTypeId() - at least in this case - and gets the placed block. I really don't know what I'm doing wrong here:( .
     
  8. Offline

    fireblast709

    After some more research, apparently the block is placed before the event fires :confused:. Use the event.getBlockReplacedState(), but without calling the .getBlock() method (as this gets you the updated block).

    Use the .getTypeId() and .getRawData() (latter gets you the byte damagavalue)
     
  9. Offline

    Jusi

    Thank you for your work, it works as intended now.
    I guess it makes sense to usegetBlockReplacedState().getTypeId() instead ofgetBlockReplacedState().getBlock().getTypeId() since the block is replaced already.
    Well that at least turned out to be quite easy, things like exploded blocks, falling sand etc. will probably take even more time to be figured out :rolleyes:. But that will be another topic... (closed).
     
Thread Status:
Not open for further replies.

Share This Page