Launching entities (cows, chickens) with knockback/block changing

Discussion in 'Plugin Development' started by The__Master_Coder, Oct 6, 2014.

Thread Status:
Not open for further replies.
  1. I am currently working on a plugin in which you launch a chicken from your eye height. So far when I right click a shovel, a chicken is launched from me, but it is launched from my feet. I also want to make it so that if the chicken hits a block, then that block disappears, and if the chicken hits a player then the player gets knocked back a fair distance. I do not know of an event in which it would allow me to make blocks disappear or deal knock back to players from a flying chicken. Can anyone help me out? I also don't know how to make the chicken i launched go in the exact direction im looking.
     
  2. Offline

    RingOfStorms

    Start by posting your current code so we have some base code to help you from.
     
  3. Offline

    Skionz

    Player#getEyeLocation();
    Block#setType(Material);
    Vectors
     
  4. Offline

    xTrollxDudex

    Set entity velocity
     
  5. Sorry I didn't post code, this was from my iPhone, but thanks for the suggestions guys I will try these things when I get home. :)

    Code:java
    1. public void spawnMOB(Player player) {
    2. Location l = new Location(player.getWorld(), player.getLocation().getX(),
    3. player.getLocation().add(0, 1, 0).getY(), player.getLocation().getZ());
    4. Chicken cow = (Chicken) player.getLocation().getWorld().spawn(l, Chicken.class);
    5. cow.setCustomName(ChatColor.AQUA + "" + ChatColor.BOLD + "Flying Chicken");
    6. cow.setCustomNameVisible(true);
    7. cow.setBaby();
    8. cow.setAgeLock(true);
    9. Vector dir = cow.getLocation().getDirection();
    10. dir.distance(dir);
    11. dir.multiply(2); //speed
    12. cow.setVelocity(dir);
    13. }

    This is my current method for spawning the chicken and setting the velocity. Did I do the location right? Also I do not know of a event that I can listen for when the chicken hits a block, so that it will remove the block or an event that is called when the chicken hits a player, so it can deal knockback. Any ideas?

    Also I would like to make it so that gravity does not affect the chickens, how do you do that?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  6. Offline

    RingOfStorms

    You never called the getEyeLocation so you didn't use that properly, for the hitting a block you'll have to make a repeater that periodically checks for a block adjacent to the chicken, there are no events.

    Here is some cleanup to your current method:
    Code:java
    1.  
    2. public void spawnMOB(Player player) {
    3. //Don't have a bunch of unecessary code to get individual x,y,z when you already have the location
    4. Location l = player.getEyeLocation();
    5. //No need to call getLocation as player also has getWorld method
    6. //Also, use meaningful names that make sense, don't use the name cow for a chicken
    7. Chicken chicken = (Chicken) player.getLocation().getWorld().spawn(l, Chicken.class);
    8.  
    9. chicken.setCustomName(ChatColor.AQUA + "" + ChatColor.BOLD + "Flying Chicken");
    10. chicken.setCustomNameVisible(true);
    11. chicken.setBaby();
    12. chicken.setAgeLock(true);
    13.  
    14. //Changed to player.getLocation as the chicken location's direction may get changed when it is created
    15. Vector dir = player.getLocation().getDirection();
    16. //Removed random distance check to itself, why?
    17. dir.multiply(2); //speed
    18. chicken.setVelocity(dir);
    19. }
    20.  
     

  7. Thanks for the code cleanup, and location fix, but I am unaware of how to make a repeater that is checking for a block adjacent to the chicken every once in a while. Also the reason it was cow, was because I wanted to change the entity being launched to cow ( yes I know it was a little messy ) because the chicken would flap its wings. But, I do not know how to make a repeater.
     
  8. Offline

    RingOfStorms

  9. Not only, RingOfStorms, but if anybody can help me on how to disable gravity when you spawn in a baby cow/chicken, that would be very helpful. Thanks!
     
Thread Status:
Not open for further replies.

Share This Page