Code help

Discussion in 'Spigot Plugin Development' started by Alweer, Jul 13, 2018.

  1. How can I do this:
    If player throws egg in region, dont spawn chicken

    I have tried events such a ProjectileHitEvent event, PlayerEggThrowEvent event and many more I also tried to google it but I was not successful
     
  2. Can we see some actual code? What you have attempted? etc...
     
  3. You throw this egg by holding it or by having a custom "gun"
     
  4. All you need to do is listen to the PlayerEggThrowEvent. From there you don't cancel the event, you simply set the hatching to false. This is fully documented on the JavaDocs.

    Try the following code, it should work fine:
    Code (Text):
    @EventHandler
    public void onEgg(PlayerEggThrowEvent event) {
        event.setHatching(false);
    }
    If this doesn't work, it means your listener is not properly registered. Put a Bukkit.broadcastMessage(String) somewhere in your code to make sure the event is actually being call properly.
     
    • Agree Agree x 1
  5. You guys missed the point of thrown egg into a region. I'm guessing he means WorldGuard region.
    For this, you will need to know where the egg hit and/or the chicken hatched. Then you can check if that location is in a region or not.

    Easiest way to do this is to listen to the CreatureSpawnEvent and check if the SpawnReason is from an EGG (not SPAWNER_EGG). If it is, check if the Location of the Entity is inside a region and cancel if it is.
     
    • Like Like x 3
  6. x
    How can I check who threw the egg?
     
  7. If you absolutely need the shooter, then use the ProjectileHitEvent and check if the Entity is an instanceof an Egg. If it is, get the Shooter. Then you have both the player entity and the egg entity so you can check either (or both if wanted) for if their location(s) are in a region. The player might have moved since they shot the egg, so thats not too reliable. But the egg location is the exact location it collided with either a block or an entity.