Solved Can't seem to acknowledge PlayerInteractEvent?

Discussion in 'Plugin Development' started by Caedus, Dec 28, 2015.

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

    Caedus

    Essentially, when I right or left click the air in minecraft, nothing happens. In the following code, it doesn't print "1", meaning it doesn't even get that far. What have I done wrong?

    Code:
    package me.Caedus;
    
    import java.util.Arrays;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    
    public class Weed extends JavaPlugin implements Listener{
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if(cmd.getName().equalsIgnoreCase("weed")){
                Player p = (Player) sender;
               
                ItemStack bonemeal = new ItemStack(Material.INK_SACK, 1, (short) 15);
                ItemMeta im = bonemeal.getItemMeta();
                im.setDisplayName(ChatColor.RED+"Weed");
                bonemeal.setItemMeta(im);
               
                ItemStack weed = setMeta(bonemeal, ChatColor.RED+"Weed", Arrays.asList("First", "Second"));
               
               
                p.getInventory().addItem(weed);
               
               
               
                return true;
            }
            return false;
        }
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e){
            if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){
                Bukkit.broadcastMessage("1");
                if(e.getPlayer().getItemInHand().hasItemMeta()){
                    Bukkit.broadcastMessage("2");
                    if(e.getPlayer().getItemInHand().getItemMeta().getDisplayName().equals("Weed")){
                        Bukkit.broadcastMessage("3");
                        double newHealth = e.getPlayer().getHealth() + 2;
                        ItemStack remove = new ItemStack(e.getPlayer().getItemInHand().getType());
                        e.getPlayer().setHealth(newHealth);
                        e.getPlayer().sendMessage("You have consumed weed");
                        e.getPlayer().getInventory().remove(remove);
                    }
                }
            }
        }
       
       
       
        public ItemStack setMeta(ItemStack material, String name, List<String> lore){
            if(((material == null) || material.getType() == Material.AIR) || ((name == null) && lore == null))
                return null;
           
            ItemMeta im = material.getItemMeta();
            if(name != null)
                im.setDisplayName(name);
            if(lore != null)
                im.setLore(lore);
           
           
            material.setItemMeta(im);
            return material;
        }
       
    
    }
    
     
  2. Offline

    Corndogoz

    you forgot getServer().getPluginManager().registerEvents(this, this); in your onEnable()
    Code:
    public void onEnable(){
    getServer().getPluginManager().registerEvents(this, this);
    }
     
  3. Offline

    Caedus

    You are correct, I am an idiot. Cheers.

    EDIT: Now it prints "1" & "2", if someone can help me work out why it's not reading the item meta as "Weed", that would be great.
     
    Last edited: Dec 28, 2015
  4. Offline

    Corndogoz

    not sure, but u made the display name red
     
    elian1203 likes this.
  5. Offline

    elian1203

    @Corndogoz Like he said, you have to check if it equals ChatColor.RED + "Weed" or "§cWeed"
     
    Caedus likes this.
  6. Offline

    Xerox262

    @elian1203 Always use ChatColor over §. (Although it's unlikely, the color symbol could be changed)

    @Caedus You can also use ChatColor.stripColor to check it's name, it means it will work no matter what color you decide to name it.
     
    Caedus likes this.
  7. Offline

    elian1203

    Oh and by the way, you're not safely setting the players new health. If they are at 19 health and consume the weed, you will get an error and their health won't be boosted. Make sure to check that their health + 2 isn't above 20. Alternatively you could change the health scale, but same thing goes.
     
    Caedus and Xerox262 like this.
  8. Offline

    Caedus

    Thank you all for your help, you guys are great!
     
  9. Offline

    Zombie_Striker

    @elian1203
    Yes, bukkit already takes care of "if health + number > max health". The health will be reverted back to "max health" if the "set health" is greater than the max health

    [edit] Mark as solved if solved.
     
  10. Offline

    Xerox262

Thread Status:
Not open for further replies.

Share This Page