Stop transparent-y blocks dropping on update

Discussion in 'Plugin Development' started by thebigdolphin1, Jan 31, 2014.

Thread Status:
Not open for further replies.
  1. I've been stuck on this one, and need a reply within a couple hours.

    I basically need to stop objects such as Doors, Trapdoors & Carpets (and whatever else) from dropping their item when it's updated.

    For example, breaking a block below a door will remove it and drop the item, I just need an answer of how to stop the drop.
     
  2. Offline

    EvilWitchdoctor

    @thebigdolphi1,
    If you call BlockBreakEvent and the block that was broken was the block who's drop you arr are trying to prevent, then you can cancel the drop relatively easily (using .setDrop() or by cancelling the event and setting the block to .AIR).
    However it's a little harder in a situation where the block broken by the player wasn't the blacklisted items itself (like what you said about the door on top of a block) I don't really know what to do with this one, but maybe you could try making a listener for BlockPhysicsEvent or BlockFadeEvent.
    Oh, and I don't think there's currently a way to determine whether a block is solid or not... you'll probably have to just get a list of ids or something for that, sorry :/
     

  3. I just meant any blocks in general that drop when in an 'invalid' location (there are no 'solid' ones that do that), such as crops, grass, carpet, doors.
     
  4. Offline

    EvilWitchdoctor

    thebigdolphin1 my bad, I should have understood you the first time ;)
    I'm afraid I can't think of any solutions not involving long convoluted code at BlockBreakEvent or BlockPhysicsEvent... I hope someone finds a solution to this, this could be pretty useful to some servres

    Alright I came up with something that works and isn't that laggy or over-complex. Pretty much was it does is wait until the block is broken then check in a 2x2x2 area for any drops besides the drop of the block (eg., doors)
    it seemed to work when I compiled it, if you have any problems with it let me know, but currently it's the only solution I can give..

    Code:java
    1. @EventHandler
    2. public void onBlockBreak(final BlockBreakEvent evt)
    3. {
    4.  
    5. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Thread()
    6. {
    7. public void run()
    8. {
    9. Item[] drops = evt.getBlock().getWorld().getEntitiesByClass(Item.class).toArray(new Item[]{});
    10. for(Item item : drops)
    11. {
    12. if(isCloseTo(item.getLocation(), evt.getBlock().getLocation()) &&
    13. item.getItemStack().getType() != evt.getBlock().getType() && item.getTicksLived() < 5)
    14. {
    15. item.remove();
    16. }
    17. }
    18. }
    19. }, 1);
    20. }
    21.  
    22. private boolean isCloseTo(Location loc1, Location loc2)
    23. {
    24. int radius = 2;
    25. if (Math.abs(loc1.getBlockX() - loc2.getBlockX()) <= radius && Math.abs(loc1.getBlockY() - loc2.getBlockY()) <= radius &&
    26. Math.abs(loc1.getBlockZ() - loc2.getBlockZ()) <= radius) return true;
    27. else return false;
    28. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
Thread Status:
Not open for further replies.

Share This Page