Need help with Item Right Click 1.7.10

Discussion in 'Plugin Development' started by Sponge RoboMonkey, Sep 1, 2014.

Thread Status:
Not open for further replies.
  1. Hi, I'm new to the Bukkit API and I need help testing an item Right Click. I ransacked the forums and google but cant find anything. Here is my code:

    Code:java
    1. if(player.getInventory().getItemInHand().getType() == Material.DIAMOND_PICKAXE) {
    2. if(e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    3. player.sendMessage(ChatColor.MAGIC + "Right Clicked!");
    4. Arrow damage = player.shootArrow();
    5. damage.setShooter(player);
    6. damage.setVelocity(damage.getVelocity().multiply(2));
    7. }
    8. }


    Thanks!
     
  2. Offline

    hintss

    This looks fine, paste some code from above/below this?
     
  3. @SpongeRoboMonkey like hintss said the whole listener class or at least the whole method which you showed us a part from would be nice. Also you should tell us what does not work. do you get an exception or does it just not send the message?

    Also i ve a presumption. You said you're new to the api, maybe you forgot to adress the method to the eventhandler. In a bukkitlistener you must address every method which shall be listening to the eventhandler and it must contain a bukkitevent as a parameter. to address the method write @EventHandler direct over it in your code
     
  4. Ok hintss Shmobi here is my whole EventsHandler class:
    Code:java
    1. package me.RoboMonkey2000.EventsHandler;
    2.  
    3. import java.util.HashMap;
    4.  
    5. import net.minecraft.server.v1_7_R4.DamageSource;
    6. import net.minecraft.server.v1_7_R4.EntityDamageSource;
    7.  
    8. import org.bukkit.Bukkit;
    9. import org.bukkit.ChatColor;
    10. import org.bukkit.Effect;
    11. import org.bukkit.Location;
    12. import org.bukkit.Material;
    13. import org.bukkit.OfflinePlayer;
    14. import org.bukkit.World;
    15. import org.bukkit.entity.Arrow;
    16. import org.bukkit.entity.Entity;
    17. import org.bukkit.entity.Player;
    18. import org.bukkit.entity.Projectile;
    19. import org.bukkit.entity.Snowball;
    20. import org.bukkit.entity.TNTPrimed;
    21. import org.bukkit.event.Event;
    22. import org.bukkit.event.EventHandler;
    23. import org.bukkit.event.EventPriority;
    24. import org.bukkit.event.Listener;
    25. import org.bukkit.event.block.Action;
    26. import org.bukkit.event.block.BlockBreakEvent;
    27. import org.bukkit.event.block.SignChangeEvent;
    28. import org.bukkit.event.entity.EntityDamageEvent;
    29. import org.bukkit.event.entity.ExplosionPrimeEvent;
    30. import org.bukkit.event.entity.FoodLevelChangeEvent;
    31. import org.bukkit.event.entity.ProjectileHitEvent;
    32. import org.bukkit.event.inventory.ClickType;
    33. import org.bukkit.event.inventory.InventoryClickEvent;
    34. import org.bukkit.event.player.PlayerDropItemEvent;
    35. import org.bukkit.event.player.PlayerInteractEvent;
    36. import org.bukkit.event.player.PlayerTeleportEvent;
    37. import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
    38. import org.bukkit.plugin.Plugin;
    39. import org.bukkit.plugin.PluginManager;
    40. import org.bukkit.plugin.java.JavaPlugin;
    41. import org.bukkit.potion.PotionEffect;
    42. import org.bukkit.potion.PotionEffectType;
    43.  
    44. public class EventsHandler extends JavaPlugin implements Listener {
    45.  
    46. public void onEnable() {
    47. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    48. }
    49.  
    50. public void onDisable() {
    51.  
    52. }
    53.  
    54. final EventsHandler plugin;
    55.  
    56. public EventsHandler(EventsHandler plugin) {
    57. this.plugin = plugin;
    58. }
    59.  
    60. @EventHandler(priority = EventPriority.HIGHEST)
    61. public void onPlayerInteract(final PlayerInteractEvent e) {
    62. Player player = e.getPlayer();
    63.  
    64. if(player.getInventory().getItemInHand().getType() == Material.CLAY_BALL) {
    65. if(e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    66. double max = player.getMaxHealth();
    67. double heal = 5;
    68. if(player.getHealth() != max) {
    69. player.setHealth(player.getHealth() + heal);
    70. }
    71. }
    72. }
    73.  
    74. if(player.getItemInHand() != null || player.getItemInHand().getType() == Material.REDSTONE) {
    75. for(Player players : Bukkit.getOnlinePlayers()) {
    76. players.hidePlayer(player);
    77. }
    78. }
    79. if(player.getInventory().getItemInHand().getType() == Material.DIAMOND_PICKAXE) {
    80. if(e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    81. player.sendMessage(ChatColor.MAGIC + "Right Clicked!");
    82. Arrow damage = player.shootArrow();
    83. damage.setShooter(player);
    84. damage.setVelocity(damage.getVelocity().multiply(2));
    85. }
    86. }
    87.  
    88. if(player.getInventory().getItemInHand().getType() == Material.WOOD_AXE) {
    89. if(e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    90. Arrow firearrow = player.shootArrow();
    91. firearrow.setFireTicks(1200);
    92. firearrow.setShooter(player);
    93. firearrow.setVelocity(firearrow.getVelocity().multiply(2));
    94. }
    95. }
    96.  
    97. if(player.getInventory().getItemInHand().getType() == Material.GOLD_SPADE) {
    98. if(e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    99. player.addPotionEffect(new PotionEffect(PotionEffectType.HEAL, 1, 1));
    100. player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 5, 99));
    101. }
    102. }
    103. }
    104.  
    105. @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
    106. public void onFoodChange(final FoodLevelChangeEvent e) {
    107. if(e.getEntity() instanceof Player) {
    108. Player player = (Player) e.getEntity();
    109. e.setCancelled(true);
    110. player.setFoodLevel(20);
    111. }
    112. }
    113.  
    114. @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
    115. public void onBlockBreak(final BlockBreakEvent e) {
    116. Player player = e.getPlayer();
    117. if(!player.isOp()) {
    118. e.setCancelled(true);
    119. }
    120. }
    121.  
    122. @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = false)
    123. public void onPlayerTeleport(final PlayerTeleportEvent e) {
    124. if(e.getCause().equals(TeleportCause.ENDER_PEARL)) {
    125. e.setCancelled(true);
    126. }
    127. }
    128.  
    129. @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = false)
    130. public void onDrop(PlayerDropItemEvent e) {
    131. e.setCancelled(true);
    132. }
    133. }
    134.  


    And Shmobi I don't understand that code. Could you explain it to me?
    Thanks!
     
  5. @SpongeRoboMonkey what do you not understand? i won't explain you the whole code, that would be the dead for my fingers, because i would've to type 10 pages to explain that
     
  6. Offline

    hintss

    Why are you making a constructor for your main class...
     
  7. hintss i dont think that he/she made anything of that. that's why he/she asked me to explain it. It's probably copy paste
     
  8. Offline

    hintss

    no shit :p
     
  9. Offline

    xize

    make sure the item in hand in the player interact events aren't null, else you will get NullPointerExceptions these are common mistakes I see with ItemMeta, ItemStacks in hand and display names always make sure to check it if it exists and not empty or null before continue the code.

    also why is your PlayerInteractEvent final?, if you don't use schedulers/runnables or want to stop overwriting there is not really a reason to use it there and also a scheduler is not always a good solution to put in a event it could hang some things.
     
  10. xize I am new to the API, so I don't know that much about those events and that is why I made this post.
     
  11. hintss Shmobi xize @anybody could someone post some code that tests for right click? I really need to know this!
     
  12. Offline

    hintss

    learn java
     
  13. god will anyone actually help me
     
  14. Offline

    xTigerRebornx

    Sponge RoboMonkey Considering you only want people to write your code for you, you'll get more "help" in Plugin Requests.
     
    mkezar and BetaNyan like this.
  15. Offline

    mkezar

    if you need help, go to this website that has all of your answers and explains everything!
    http://jd.bukkit.org/dev/apidocs/
     
  16. Sponge RoboMonkey i can tell you how to but not give you the code. you got an eventobject which contains informations about the event and why it's happening etc... look if you can find the reason why the event was thrown. in other words, what the player did do to throw the event. check if the reason was a right click and if so, do your thing
     
Thread Status:
Not open for further replies.

Share This Page