blockplaceevent not working

Discussion in 'Plugin Development' started by jstN0body, Mar 26, 2020.

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

    jstN0body

    I'm trying to get it so that when you place a block of tnt it replaces it with a primed tnt so that it explodes without you having to light it but it doesn't seem to work right, I feel like I'm doing everything right, does anyone know where the problem is?


    Code:
    package me.jstN0body.selflightingTNT.event;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;
    
    import me.jstN0body.selflightingTNT.Main;
    
    public class OnPlaceTNT implements Listener {
    
        @SuppressWarnings("unused")
        private Main plugin;
      
      
        public OnPlaceTNT(Main plugin) {
            this.plugin = plugin;
          
            Bukkit.getPluginManager().registerEvents(this, plugin);
        }
      
        @EventHandler
        public void onTntPlaced(BlockPlaceEvent event) {
            Player player = event.getPlayer();
            World world = player.getWorld();
            Block placedBlock = event.getBlock();
            Material blockType = event.getItemInHand().getType();
            EntityType primedTnt = EntityType.PRIMED_TNT;
            if (blockType == Material.TNT) {
                placedBlock.setType(Material.AIR);
                world.spawnEntity(placedBlock.getLocation(), primedTnt);
            }
        }
    }
     
    Last edited: Mar 26, 2020
  2. Offline

    timtower Administrator Administrator Moderator

    @jstN0body Does the event get called?
    Are you making an instance off the class so it starts?
     
  3. Offline

    jstN0body

    @timtower do you mean an instance in the main class because I have that.
     
  4. Offline

    timtower Administrator Administrator Moderator

  5. Offline

    jstN0body

    @timtower It should be, I did a previous plugin with events the same way that I did this one and it works fine.
     
  6. Offline

    timtower Administrator Administrator Moderator

  7. Offline

    jstN0body

    Ok it seems to not be calling the event correctly, I put 2 print statements one in onEnable() and one in the BlockPlaceEvent thing, the one for onEnable displays but not the one in the event
    below is my main class, am I calling the class incorrectly?

    Code:
    package me.jstN0body.selflightingTNT;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    import me.jstN0body.selflightingTNT.event.OnPlaceTNT;
    
    
    
    public class Main extends JavaPlugin {
        @Override
        public void onEnable() {
            System.out.println("onEnable working");
           
            new OnPlaceTNT(this);
        }
    }
    
     
    Last edited: Mar 27, 2020
  8. Offline

    timtower Administrator Administrator Moderator

    @jstN0body I don't see anything wrong with that to be honest.
     
  9. Offline

    jstN0body

  10. Offline

    timtower Administrator Administrator Moderator

    Don't question it, just accept it.
     
Thread Status:
Not open for further replies.

Share This Page