Right click item event.

Discussion in 'Plugin Development' started by Sean0402, Aug 25, 2014.

Thread Status:
Not open for further replies.
  1. Hi I have completely forgot how to do this since I have had a while off Java coding.. How do I make it so when you right click the item it sends them a message? Thanks!

    What I have so far:

    Code:java
    1. package me.sean0402.store;
    2.  
    3. import org.bukkit.entity.Player;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.EventPriority;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.block.Action;
    8. import org.bukkit.event.player.PlayerInteractEvent;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class Store extends JavaPlugin implements Listener {
    12.  
    13. public void onEnable(){
    14. getServer().getPluginManager().registerEvents(this, this);
    15. }
    16.  
    17. @EventHandler(priority = EventPriority.NORMAL)
    18. public void onClickEvent(PlayerInteractEvent e){
    19. Player player = e.getPlayer();
    20. if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
    21. if(e.getPlayer().getItemInHand().getType()) {
    22.  
    23. }
    24. }
    25. }
    26. }
     
  2. Offline

    Necrodoom

  3. Offline

    AlphaRLee

    Your code looks good so far. Just add a few pieces more:

    e.getPlayer().getItemInHand().getType() returns a Material (like Material.DIRT or Material.STONE). Now just change your if statement to become a boolean, so it looks more like this:

    Code:
    if(e.getPlayer().getItemInHand().getType() == Material.STONE) {
     
    e.getPlayer().sendMessage(ChatColor.GREEN + "You clicked a stone block!");
    //ChatColor.GREEN is optional
     
    }
    You already created a Player called player, you could replace e.getPlayer() with just the word player, but it's your preference.

    I hope this helps!
     
  4. Offline

    Zupsub

    Do you use an IDE?
    Normally it should show you related methods, if you start typing them. So "player." should show a list of all methods the player class has.
     
  5. Offline

    SmooshCakez

    e is the event, not the player.
     
  6. Offline

    AlphaRLee

    SmooshCakez
    Wait, what? Oops! My bad, just noticed. I misread the line Player player = e.getPlayer();

    Will edit code appropriately now to fix this, thanks, SmooshCakez
     
Thread Status:
Not open for further replies.

Share This Page