[Tutorial] Player Interact Events

Discussion in 'Resources' started by BaconStripzMan, Feb 10, 2014.

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

    BaconStripzMan

    Firstly let's make a class and call it ChatOnClick and make it implements Listener

    So first let's do the event
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event) {
    3.  
    4. }


    Now inside this event let's define some things

    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event) {
    3. //Define the player
    4. Player player = e.getPlayer();
    5. //Define the item in their hand
    6. Material mat = player.getItemInHand().getType();
    7. }


    Okay, so now let's check what interaction they did

    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event) {
    3. //Define the player
    4. Player player = e.getPlayer();
    5. //Define the item in their hand
    6. Material mat = player.getItemInHand().getType();
    7.  
    8. //If the player right clicks air or a block
    9. if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    10.  
    11. }
    12. }


    So now in here let's check the item!

    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event) {
    3. //Define the player
    4. Player player = e.getPlayer();
    5. //Define the item in their hand
    6. Material mat = player.getItemInHand().getType();
    7.  
    8. //If the player right clicks air or a block
    9. if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    10. //Check if it's a stick
    11. if(mat == Material.STICK){
    12.  
    13. }
    14. }
    15. }


    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event) {
    3. //Define the player
    4. Player player = e.getPlayer();
    5. //Define the item in their hand
    6. Material mat = player.getItemInHand().getType();
    7.  
    8. Now make them chat
    9.  
    10. //If the player right clicks air or a block
    11. if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    12. //Check if it's a stick
    13. if(mat == Material.STICK){
    14. //Make them chat
    15. player.chat("I've been forced to say this!");
    16. }
    17. }
    18. }


    I put this in a class for you :) http://pastebin.com/50dJ9A8B
     
    EliasDerBossTV likes this.
  2. Offline

    ccrama

    No offense, this is nothing to write a resource about. This is just basic bukkit API. If you go to this link:
    http://jd.bukkit.org/rb/doxygen/da/..._1event_1_1player_1_1PlayerInteractEvent.html
    it has all the info you posted and more (built right into the Bukkit API). I'm not trying to downplay your tutorials, but tutorials should be cool things you have come across or created, nothing that you can just whip up in a few minutes.
     
    Jentagh, L33m4n123 and Panjab like this.
  3. Offline

    stonebloodtv

    Your example do not work with version build 1.7.2 R0.4 and server on 1.7.2 R0.2... Help me please :(
     
Thread Status:
Not open for further replies.

Share This Page