[SOLVED] BlockPlace Listener

Discussion in 'Plugin Development' started by Milkywayz, Feb 21, 2012.

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

    Milkywayz

    Hey, i want to make it when players place tnt, it logs it, i have that in a different event and it works fine. Now i need to make TNT not yield a drop, i attempted to it with
    Code:
            if(event.getBlock().getWorld() != null) {   
                event.getBlock().breakNaturally();
                if(event.getBlock().equals(Material.TNT) && event.getPlayer().hasPermission("milkyprotect.tnt")) {           
                    event.getBlock().setType(Material.AIR);
    This code is BAD! people can destroy blocks inside protected areas. Therefore i:
    Code:
    package net.milkycraft;
     
    import org.bukkit.Material;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
     
    public class MyTntdropListener implements Listener {
        @EventHandler
        public void onBlockExplode(BlockBreakEvent event)   
        {
            if(event.getBlock().getWorld() != null) {   
                if(event.getBlock().equals(Material.TNT)) {           
                    event.getBlock().setType(Material.AIR);           
                }
            }
            }
        }
    
    This code doesn't set the TNT to AIR when they break a TNT. Im inferring I need to make a } else {, but what do I put under the else..
     
  2. Offline

    dillyg10

    What are you trying to do with this plugin?
     
  3. Offline

    Milkywayz

    1. When user places tnt, it broadcasts it and its coordinates (already taken care of)
    2. When user tries to break tnt, it doesn't drop ( to help prevent spammers)
    - Wait. i just realized when typing this. Do i need to cancel the drop for .getBlock().getType(Material.TNT)?
     
  4. Offline

    dillyg10

    You can try that :)
     
  5. Offline

    Milkywayz

    Yeah im lost. I just want to cancel the drop for tnt :(
     
  6. Offline

    s1mpl3x

    event.setCancelled(true);

    inside of your if (TNT) ?
     
  7. Offline

    CainFoool

    public void onBlockBreak(BlockBreakEvent e) {
    if(e.getBlock().getType() == Material.TNT) {
    e.getBlock().setType(Material.AIR);
    }
    return;
    }

    Should do it.
     
  8. Offline

    Milkywayz

    Nevermind, this is [SOLVED] here my code:
    Code:
    package net.milkycraft;
     
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
     
    public class MyTntdropListener implements Listener {
        @EventHandler
        public void onBlockExplode(BlockBreakEvent event)   
        {
            if(event.getBlock().getWorld() != null) {
                if(event.getBlock().getTypeId() == 46) {
                    event.getBlock().setTypeId(0);           
                    return;
                }
            }
            }
        }
    
    A studied a little bit of the java docs and I threw this together and it tested fine.

    :p You posted the second i did, thanks for the help everyone!

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

Share This Page