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();
}
}
}
}
Aggregations