Right Clicking Blocks and Spawning Entities in its place?

Discussion in 'Plugin Development' started by TheNoobiestCrafters, Oct 27, 2012.

Thread Status:
Not open for further replies.
  1. I'm still a noob so I don't know how to do this, but how could I spawn primed TNT in a unprimed TNT's place when I right click it?
     
  2. Offline

    oscarshi1995

    Register a playerclickevent at least i think thats what its called, then check if they right clicked and check if the clicked block is TNT. if so then world.spawnEntity(event.getclickedblock.getlocation, Entitytype.PrimedTNT)
     
  3. Offline

    Seadragon91

    Here you have the code, register the listener and look that the class where you add this code implements Listener:
    Code:
    @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            Block b = event.getClickedBlock();
            Action action = event.getAction();
           
            if (action == Action.RIGHT_CLICK_BLOCK && b != null && b.getType() == Material.TNT) {
                player.getWorld().spawnEntity(b.getLocation(), EntityType.PRIMED_TNT);
            }
        }
     
  4. Getting errors with
    Code:
    Action action = event.getAction();
    When I hover over it in Eclipse it the only qfix available is "Change type of 'action' to 'Action'"
    I'm also getting errors with
    Code:
    if (action == Action.RIGHT_CLICK_BLOCK && b != null && b.getType() == Material.TNT) {
    Specifically with
    Code:
     RIGHT_CLICK_BLOCK
    The only suggestion is 'Rename in file'.
     
  5. Offline

    Seadragon91

    I think you have the wrong import for Action. It has to be:
    import org.bukkit.event.block.Action;
     
  6. That's not a valid import, but I just found a way to squash that bug, now my only bug is with "RIGHT_CLICK_BLOCK"
     
  7. Offline

    Seadragon91

    Try:
    org.bukkit.event.block.Action.RIGHT_CLICK_BLOCK
     
  8. Yay! Thank you, I think that solved it! I'll go put it on my server and see if it works.
     
  9. Offline

    Seadragon91

    Add above:
    player.getWorld().spawnEntity(b.getLocation(), EntityType.PRIMED_TNT);
    this line:
    b.setType(Material.AIR);
     
  10. How can I make it so you can only do prime it if you don't have anything in your hand?
     
  11. Offline

    mathiasrocker

    Code
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent event) {
    Player player = event.getPlayer();
    Block b = event.getClickedBlock();
    org.bukkit.event.block.Action action = event.getAction();
    ItemStack tool = player.getItemInHand();
    if(tool.getType() == Material.AIR)
    {
    if (action == org.bukkit.event.block.Action.RIGHT_CLICK_BLOCK && b != null && b.getType() == Material.TNT) {
    b.setType(Material.AIR);
    player.getWorld().spawnEntity(b.getLocation(), EntityType.PRIMED_TNT);
    }
    }
    }
     
Thread Status:
Not open for further replies.

Share This Page