Search in sources :

Example 1 with PlayerEggThrowEvent

use of org.bukkit.event.player.PlayerEggThrowEvent in project Glowstone by GlowstoneMC.

the class GlowEgg method randomHatchSpawning.

/**
 * Handle spawning entities when the egg breaks.
 *
 * @param location The location the egg breaks
 */
private void randomHatchSpawning(Location location) {
    ThreadLocalRandom random = ThreadLocalRandom.current();
    // There is a 1/8 chance for egg to hatch and spawn at least 1 entity
    boolean hatching = random.nextInt(8) == 0;
    // ...and if the egg is hatching, there is now
    // 1/32 chance to spawn 3 more entities.
    byte amount;
    if (hatching) {
        amount = (byte) ((random.nextInt(32) == 0) ? 4 : 1);
    } else {
        amount = 0;
    }
    EntityType hatchingType = EntityType.CHICKEN;
    final ProjectileSource shooter = getShooter();
    if (shooter instanceof GlowPlayer) {
        PlayerEggThrowEvent event = EventFactory.getInstance().callEvent(new PlayerEggThrowEvent((GlowPlayer) shooter, this, hatching, amount, hatchingType));
        amount = event.getNumHatches();
        hatching = event.isHatching();
        hatchingType = event.getHatchingType();
    }
    if (hatching) {
        for (int i = 0; i < amount; i++) {
            GlowEntity entity = (GlowEntity) location.getWorld().spawnEntity(location.clone().add(0, 0.3, 0), hatchingType);
            if (entity instanceof GlowAgeable) {
                ((GlowAgeable) entity).setBaby();
            }
        }
    }
}
Also used : EntityType(org.bukkit.entity.EntityType) GlowPlayer(net.glowstone.entity.GlowPlayer) PlayerEggThrowEvent(org.bukkit.event.player.PlayerEggThrowEvent) GlowEntity(net.glowstone.entity.GlowEntity) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) GlowAgeable(net.glowstone.entity.GlowAgeable) ProjectileSource(org.bukkit.projectiles.ProjectileSource)

Aggregations

ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 GlowAgeable (net.glowstone.entity.GlowAgeable)1 GlowEntity (net.glowstone.entity.GlowEntity)1 GlowPlayer (net.glowstone.entity.GlowPlayer)1 EntityType (org.bukkit.entity.EntityType)1 PlayerEggThrowEvent (org.bukkit.event.player.PlayerEggThrowEvent)1 ProjectileSource (org.bukkit.projectiles.ProjectileSource)1