[Question] Block click action

Discussion in 'BungeeCord Plugin Development' started by Hangar555, Jun 5, 2016.

  1. How do I do when a player right click a block (ex: StoneBlock) it performs a command?

    My code is like this (it doesn't work, and, yeah, I'm at the moment a noob)

    Code (Java):
    package eu.hangar.seconds;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;



    public class Atributes extends JavaPlugin {
       

        @EventHandler
          public void playerInteract(PlayerInteractEvent e){
            Player p = e.getPlayer();
            Block b = e.getClickedBlock();
            if (e.getAction() == Action.RIGHT_CLICK_BLOCK &&
              (e.getClickedBlock().getType() == Material.DIAMOND_BLOCK) )
            {
                 p.performCommand("TeleStep2");
          {
               
          {;
               
                 

                      }
            }
        }
    }
    }
     
     
  2. Instead of extending JavaPlugin you need to implement Listener. Then you need to register the listener in your main class. Also, you might want to clean up your code. I see an unneeded semicolon.
     
  3. Code (Text):
    package eu.hangar.seconds;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;



    public class Atributes extends JavaPlugin implements Listener {
       
       
        public void onEnable(){
            //Here you register your plugin,
            Bukkit.getPluginManager().registerEvents(this,this);
        }


    @EventHandler
    public void playerInteract(PlayerInteractEvent e) {
      Player p = e.getPlayer();
      Block b = e.getClickedBlock();
      if (e.getAction() == Action.RIGHT_CLICK_BLOCK &&
       (e.getClickedBlock().getType() == Material.DIAMOND_BLOCK)) {
       p.performCommand("TeleStep2");
       }
    }
    }
    .....Besides, wrong section.
     
  4. Please don't spoonfeed.
     
    #4 MinionDoesMC, Jun 5, 2016
    Last edited: Jun 5, 2016
  5. Welp, you are right. Shouldnt spoonfeed. Still, this is the BungeeCord Section. And this is clearly Bukkit / Spigot related.
     
  6. My bad. I thought he/she was using BungeeCord. Should've looked at the imports beforehand.
     
  7. Yeah, don't spoonFeed me, please, and, yes, this is the wrong section.