Needing help spawning chickens.

Discussion in 'Plugin Development' started by PuddanatorDev, Aug 29, 2014.

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

    PuddanatorDev

    Hey! I have done several Google searches on this but could not find an answer so I am trying to find out how to spawn a chicken and make it follow a player when they do a command. Here is my current code. Any ideas?

    Code:
    package me.Puddanator.chicken;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Chicken extends JavaPlugin {
     
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String args[]) {
    if (cmd.getName().equalsIgnoreCase("xler")) {
    Player player = (Player) sender;
    if (!sender.hasPermission("chicken.xler")) {
    sender.sendMessage(ChatColor.RED + "You do not have permission to use this command!");
    return true;
    }
     
    }
    }
    }
    
     
  2. Offline

    xJeremyCx

    Spawn a chicken.
    Chicken c = (Chicken)player.getWorld().spawnEntity(EntityType.CHICKEN, player.getLocation));

    Follow player
    c.setTarget(player);
    I think you have to run this code every second to make sure the chicken is following player.
     
  3. Offline

    jojo1541

    Code:java
    1. Chicken C = (Chicken) player.getWorld().spawnEntity(player.getLocation(), EntityType.CHICKEN);

    (Second result in Google. :rolleyes:)

    to make it follow you use the Method
    Code:java
    1. setTarget()


    should be no problem using this with a chicken.

    Hope i could help. :)
     
  4. Offline

    PuddanatorDev

    Here is my current code. It says this with a red line under setTarget: The method setTarget(Player) is undefined for the type Chicken

    Code:
    package me.Puddanator.chicken;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Chicken extends JavaPlugin {
     
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String args[]) {
    if (cmd.getName().equalsIgnoreCase("xler")) {
    Player player = (Player) sender;
    if (!sender.hasPermission("chicken.xler")) {
    sender.sendMessage(ChatColor.RED + "You do not have permission to use this command!");
    } else {
    Chicken c = (Chicken) player.getWorld().spawnEntity(player.getLocation(), EntityType.CHICKEN);
    c.setTarget(player);
    }
    return true;
    }
    }
    }
    
     
  5. Offline

    jojo1541

    try to cast Player to Entity first.
     
  6. Offline

    PuddanatorDev

    How do I do that?
     
  7. Offline

    jojo1541

  8. Offline

    PuddanatorDev

    So is this right?
    Code:
    package me.Puddanator.chicken;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Chicken extends JavaPlugin {
     
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String args[]) {
    if (cmd.getName().equalsIgnoreCase("xler")) {
    Player player = (Player) sender;
    if (!sender.hasPermission("chicken.xler")) {
    sender.sendMessage(ChatColor.RED + "You do not have permission to use this command!");
    } else {
    Chicken c = (Chicken) player.getWorld().spawnEntity(player.getLocation(), EntityType.CHICKEN);
    Entity e = (Entity) player;
    setTarget(e);
    }
    }
    return true;
    }
    }
    
     
  9. Offline

    jojo1541

    I don't think it's a good idea to name your class 'Chicken' when working with the Bukkit class 'Chicken'.
    Your instance 'c' of Chicken is not the bukkit class 'Chicken' but your own class.
    Of course your plugin class has no methot 'setTarget()'.

    Either use the full package name for Chicken
    Code:java
    1. org.bukkit.entity.Chicken c = (org.bukkit.entity.Chicken) player.getWorld().spawnEntity(player.getLocation(), EntityType.CHICKEN);

    or change the name of your class and import 'Chicken' from bukkit.
    Code:java
    1. import org.bukkit.entity.Chicken;


    Btw: there are some great java tutorials out there. If you don't know how to cast i would recommend you one.
     
Thread Status:
Not open for further replies.

Share This Page