Player interact with Entity Problem

Discussion in 'Plugin Development' started by JR.PluginPro, Jul 13, 2012.

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

    JR.PluginPro

    I want to send a player a message when they left click on a pig, but I cant find the right code to do it.


    Code:
    if(event.getRightClicked() instanceof Pig){
            player.sendMessage(ChatColor.GOLD + "Sent");
    I have the above but theres an error on event. Any help would be amazing!
    Thanks.
     
  2. Offline

    ZeusAllMighty11

    You defined e.getPlayer for player, I hope..

    And you forgot the getAction()
     
    JR.PluginPro likes this.
  3. Offline

    JR.PluginPro

    How do you define?
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
        Player player = (Player) sender;
       
        if(event.getRightClicked() instanceof Pig){
            player.sendMessage(ChatColor.GOLD + "Sent");
    I have the above code.
    I seriously just started making plugins, and only know the basics such as teleport and playing commands.
     
  4. Offline

    ZeusAllMighty11

    lol you're doing it all wrong. It should be:

    PHP:
    @EventHandler // means we're about to post some events
    public void onPigClick(PlayerInteractEntityEvent e){ // Defines the event, event variable
    Player player e.getPlayer(); // defines player as the player for the event -- critical
        
    if(e.getAction(RIGHT_CLICK_AIR){
            if ((
    e.getRightClicked() == instanceof pig) || e.getRightClicked() == Pig){
                
    player.sendMessage(ChatColor.GOLD "Sent");
                }
            }
    }
     
    ZomBlade_Shadow and JR.PluginPro like this.
  5. Offline

    JR.PluginPro

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
        Player player = (Player) sender;
               
       
        @EventHandler // means we're about to post some events
        public void onPigClick(PlayerInteractEntityEvent e){ // Defines the event, event variable
        Player player = e.getPlayer(); // defines player as the player for the event -- critical
            if(e.getAction(RIGHT_CLICK_AIR){
                if ((e.getRightClicked() == instanceof pig) || e.getRightClicked() == Pig){
                    player.sendMessage(ChatColor.GOLD + "Sent");
                    }
                }
        }
    It now looks like this with errors on RIGHT_CLICK_AIR, instanceof,pig, Pig . Do you need to get rid of the boolean? If not what do I need to do to fix errors? I imported pig as an entity.
    Thanks for you help :)
     
  6. Offline

    ZeusAllMighty11

    Yes. Remove public boolean stuff and your Player player = (player) sender;
     
  7. Offline

    JR.PluginPro

    Code:
    package me.jag.AnimalName;
     
    import java.awt.Event;
    import java.util.logging.Logger;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Pig;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.block.Action;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.event.player.PlayerEvent;
    import org.bukkit.event.player.PlayerInteractEntityEvent;
     
    public class AnimalName extends JavaPlugin {
    public final Logger logger = Logger.getLogger("Minecraft");
        public static AnimalName plugin;
       
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + "Has Been Disabled");
           
        }
     
    @Override
    public void onEnable() {
        PluginDescriptionFile pdfFile = this.getDescription();
        this.logger.info(pdfFile.getName() + "Version" + pdfFile.getVersion() + "Has Been Enabled");
    }
     
     
               
       
        @EventHandler // means we're about to post some events
        public void onPigClick(PlayerInteractEntityEvent e){ // Defines the event, event variable
        Player player = e.getPlayer(); // defines player as the player for the event -- critical
            if(e.getAction(RIGHT_CLICK_AIR){
                if ((e.getRightClicked() == instanceof pig) || e.getRightClicked() == Pig){
                    player.sendMessage(ChatColor.GOLD + "Sent");
                    }
                }
        }
    }    

    Heres my whole plugin. I'm still recieving errors in the same places.
     
  8. Offline

    ZeusAllMighty11

    On your onEnable() method, register events.

    PluginManager pm = new PluginManager();
    pm.getServer().registerEvent(this, this);

    Don't know the exact code, but something like that.

    And it'd help if you posted the errors

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  9. Offline

    JR.PluginPro

    I said above that I got errors on RIGHT_CLICK_AIR instanceof pig Pig

    How do I find the extact code?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  10. Offline

    ZeusAllMighty11

    Remove instance of pig...try it

    idk... 0_O

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  11. Offline

    JR.PluginPro

    Code:
    @EventHandler // means we're about to post some events
        public void onPigClick(PlayerInteractEntityEvent e){ // Defines the event, event variable
        Player player = e.getPlayer(); // defines player as the player for the event -- critical
            if(e.getAction(RIGHT_CLICK_AIR){
                if ((e.getRightClicked() == ) || e.getRightClicked() == Pig){
                    player.sendMessage(ChatColor.GOLD + "Sent");
                    }
                }
        }
    If I remove instanceof pig what do I make e.getRightClicked() ==

    ZeusAllMighty11

    I am really close! Can you please help me on this one last thing?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  12. Offline

    ZeusAllMighty11

    Digi r0306

    im going to bed but if you post your issue they will help u
     
  13. I recoment your learn better java, it is mutch easier programming then instead of asking here, and it is also faster
     
  14. PlayerInteractEntityEvent doesn't have a getAction() and you're also checking it wrong.
    Then, if you want to check if it's a pig, just do instanceof Pig but without the ==... do you really know basic Java ? If you don't, you should learn, it would make it easier for everybody.

    So your method should look like this:
    Code:
        @EventHandler
        public void onPigClick(PlayerInteractEntityEvent e)
        {
            if(e.getRightClicked() instanceof Pig)
            {
                Player player = e.getPlayer(); // no need to store player earlier than needed, but if you're going to use it once, for a message, it's not needed to store it at all.
                player.sendMessage(ChatColor.GOLD + "Sent");
            }
        }
     
    -_Husky_- likes this.
  15. Offline

    r0306

    JR.PluginPro
    You're putting an event handler inside a command sender method. Remove these lines and it should be fine:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
        Player player = (Player) sender;
     
Thread Status:
Not open for further replies.

Share This Page