1.17.x Spawn eggs in random directions

Discussion in 'Spigot Plugin Development' started by TheDraconGG, Jun 22, 2021.

  1. I'm really new at Spigot, Java and plugin Development. And I want to code a plugin that makes that when you throw an egg, and it hits something, it "bounce" in a random direction whiteout it gets stuck or something like that. Here is my code


    public class Egg implements Listener {
    @EventHandler
    public void onEggHit(ProjectileHitEvent e) {
    Projectile p = e.getEntity();

    if (p.getType() == EntityType.EGG) {
    Location egglocation = p.getLocation();


    float yaw = (int)(Math.random()*((-180-180)+1))+180;
    float pitch = (int)(Math.random()*((-90-(-40))+1))+1;
    System.out.println("HIT" + yaw);
    egglocation.setPitch(pitch);
    egglocation.setYaw(yaw);



    Entity egg = egglocation.getWorld().spawnEntity(egglocation, EntityType.EGG);
    egg.setVelocity(p.getLocation().getDirection().multiply(0.5));

    }
    }



    }
     
  2. I don't understand what the problem is, could you send a screenshot of what is going on in game or some errors in console? I also would suggest canceling the event using this method:

    Code (Java):
    event.setCancelled(true);
    You can find the javadocs for that method right here