Solved Executing code when left clicking item

Discussion in 'Plugin Development' started by nelson2tm, Mar 26, 2014.

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

    nelson2tm

    Hello,
    How do i execute a code when left clicking an item?
    And if you click it removes an item until its 1 and then it removes it.
    Thanks for reading
     
  2. Offline

    mickedplay

    nelson2tm
    Use the PlayerInteractEvent:
    Code:
                if((e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK) && p.getItemInHand().getType() == Material.YOUR_ITEM)
                {
                    //do ydkxakdasdflpad
                }
        
     
    nelson2tm likes this.
  3. Offline

    nelson2tm

    mickedplay
    Are there any values needed like you use in a command?
    Code:java
    1. Player player = (Player) sender;

    i mean like this
     
  4. Offline

    mickedplay

    Player p = e.getPlayer();
     
  5. Offline

    Jalau

    nelson2tm
    Code:java
    1. @EventHandler
    2. public void click(PlayerInteractEvent e) {
    3. Player p = e.getPlayer();
    4. if((e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK) && p.getItemInHand().getType() == Material.YOUR_ITEM)
    5. {
    6. if(p.getItemInHand().getAmount() > 1)
    7. p.getItemInHand().setAmount(p.getItemInHand().getAmount() - 1);
    8. else
    9. p.setItemInHand(null);
    10.  
    11. }
    12. }


    Coded out of scratch so i didn't tested it but should work ;)
     
    nelson2tm likes this.
  6. Offline

    nelson2tm

    Jalau And is it possible to make it so, that it does only work with the lore/description (for example): "I like pizza"? and how do i do this?
     
  7. Offline

    Jalau

    nelson2tm

    Code:java
    1. if(p.getItemInHand().hasItemMeta() && p.getItemInHand().getItemMeta().hasLore) {
    2. ArrayList<String> lore = p.getItemInHand().getItemMeta().getLore();
    3. for(String s : lore)
    4. if(s.contains("I like pizza") {
    5. //Your stuff here, if the lore does contains this line two times and you want it to get only executed once add a "break;" at the end of this brackets
    6. }
    7. }
     
    nelson2tm likes this.
  8. Offline

    nelson2tm

    Jalau
    i got an error on @eventhandler and on LEFT_CLICK_AIR and LEFT_CLICK_BLOCK:
    Code:java
    1. package me.nelson2tm.thrower;
    2.  
    3. import net.minecraft.util.io.netty.util.internal.chmv8.ConcurrentHashMapV8.Action;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Sound;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Arrow;
    10. import org.bukkit.entity.EnderPearl;
    11. import org.bukkit.entity.Fireball;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.entity.SmallFireball;
    14. import org.bukkit.entity.Snowball;
    15. import org.bukkit.entity.ThrownExpBottle;
    16. import org.bukkit.entity.ThrownPotion;
    17. import org.bukkit.entity.WitherSkull;
    18. import org.bukkit.event.player.PlayerInteractEvent;
    19. import org.bukkit.plugin.java.JavaPlugin;
    20.  
    21. public class Thrower extends JavaPlugin{
    22.  
    23. public void onEnable(){
    24. getLogger().info("Enabled!");
    25. }
    26.  
    27. public void onDisable(){
    28. getLogger().info("Disabled!");
    29. }
    30.  
    31. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    32. Player player = (Player) sender;
    33. if(cmd.getName().equalsIgnoreCase("command")){
    34. //not opensource
    35. }
    36. return false;
    37. }
    38. @EventHandler
    39. public void click(PlayerInteractEvent e) {
    40. Player p = e.getPlayer();
    41. if((e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK) && p.getItemInHand().getType() == org.bukkit.Material.FIREBALL)
    42. {
    43. if(p.getItemInHand().getAmount() > 1)
    44. p.getItemInHand().setAmount(p.getItemInHand().getAmount() - 1);
    45. else
    46. p.setItemInHand(null);
    47.  
    48. }
    49. }
    50. }
     
  9. Offline

    mickedplay

    nelson2tm
    Learn Bukkit & Java, before asking for every problem.
    Give us the error code and we might help you....
     
  10. Offline

    nelson2tm

    mickedplay What one do you mean? the one from eclipse or the one from the bukkit server console?
     
  11. Offline

    mickedplay

  12. Offline

    Anerdson

    he only has an error from eclipse, he cant run this on a server yet

    try adding "implements listener" to the "public class thrower" part
     
    nelson2tm likes this.
  13. Offline

    GregMC


    You forgot to register the events and implement the listener.

    Code:java
    1.  
    2. public class Name extends JavaPlugin implements Listener {
    3.  
    4. public void onEnable() {
    5. getLogger().info("Enabled");
    6. getServer().getPluginManager().registerEvents(this, this);
    7. }
    8.  
     
    nelson2tm likes this.
  14. Offline

    nelson2tm

    This didn't fix my error :d
    the errors on eclipse are:
    @EventHandler: EventHandler cannot be resolved to a type
    LEFT_CLICK_AIR: LEFT_CLICK_AIR cannot be resolved or is not a field
    LEFT_CLICK_BLOCK: LEFT_CLICK_BLOCK cannot be resolved or is not a field
     
  15. Offline

    The Fancy Whale

    Import eventhandler and action
     
    nelson2tm likes this.
  16. Offline

    nelson2tm

    Thanks for your help everyone who helped my!
    [diamond] for everyone who helped me!
     
    GregMC likes this.
  17. Offline

    Anerdson

    Glad your problems are solved! Just a tip, when your problem gets solved, try to remember to set your thread to "Solved"
     
  18. Offline

    nelson2tm

    Thanks, forgot it ;)
     
    Anerdson likes this.
Thread Status:
Not open for further replies.

Share This Page