Skip to content

Commit

Permalink
Fine Tune Egg And Snowball (#858)
Browse files Browse the repository at this point in the history
  • Loading branch information
XuZhen86 authored and mastercoms committed Mar 6, 2018
1 parent 830d851 commit f140097
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 26 deletions.
8 changes: 4 additions & 4 deletions src/main/java/net/glowstone/entity/GlowPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2150,10 +2150,10 @@ public <T> void spawnParticle(Particle particle, Location location, int count, d
int[] particleData = GlowParticle.getExtData(particle, data);

if (distance <= 1024.0D || isLongDistance && distance <= 262144.0D) {
getSession().send(new PlayParticleMessage(particleId, isLongDistance, (float) location
.getX(), (float) location.getY(), (float) location
.getZ(), (float) offsetX, (float) offsetY, (float) offsetZ, (float) extra,
count, particleData));
getSession().send(new PlayParticleMessage(particleId, isLongDistance,
(float) location.getX(), (float) location.getY(), (float) location.getZ(),
(float) offsetX, (float) offsetY, (float) offsetZ,
(float) extra, count, particleData));
}
}

Expand Down
73 changes: 56 additions & 17 deletions src/main/java/net/glowstone/entity/projectile/GlowEgg.java
Original file line number Diff line number Diff line change
@@ -1,56 +1,95 @@
package net.glowstone.entity.projectile;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import net.glowstone.entity.passive.GlowChicken;
import net.glowstone.net.message.play.entity.SpawnObjectMessage;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Particle;
import org.bukkit.block.Block;
import org.bukkit.entity.Egg;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.projectiles.ProjectileSource;
import org.bukkit.util.Vector;

public class GlowEgg extends GlowProjectile implements Egg {
private static final double VERTICAL_GRAVITY_ACCEL = -0.03;

/**
* Creates an egg entity.
* Creates a thrown egg with default speed.
*
* @param location the initial location
* @param location the position and facing of the thrower
*/
public GlowEgg(Location location) {
super(location);
setGravityAccel(new Vector(0, -0.3, 0));
setDrag(0.99, false);
setDrag(0.99, true);
setHorizontalAirDrag(1);
setGravityAccel(new Vector(0, VERTICAL_GRAVITY_ACCEL, 0));
setVelocity(location.getDirection().multiply(3));
setBoundingBox(0.25, 0.25);
}

/**
* Process random spawn chicks when collide with a block.
*
* @param block the block that the egg collides with
*/
@Override
public void collide(Block block) {
randomSpawnChicken(getLocation().add(0, 1, 0));
randomSpawnChicken(getLocation().clone());
getWorld().spawnParticle(
Particle.ITEM_CRACK, location, 5,
0, 0, 0, 0.05, new ItemStack(Material.EGG));
remove();
}

/**
* Process random spawn chicks when collide with a living entity.
*
* @param entity the eneity that the egg collides with
*/
@Override
public void collide(LivingEntity entity) {
randomSpawnChicken(getLocation());
entity.damage(0);
remove();
ProjectileSource source = getShooter();
// the entity receives fake damage.
if (entity instanceof Entity) {
entity.damage(0, (Entity) source, EntityDamageEvent.DamageCause.PROJECTILE);
} else {
entity.damage(0, EntityDamageEvent.DamageCause.PROJECTILE);
}

collide(entity.getLocation().getBlock());
}

/**
* Handle spawning chicks when the egg breaks.
* There is a 1/8 chance to spawn 1 chick,
* and if that happends, there is a 1/32 chance to spawn 3 more chicks.
*
* @param location The location the egg breaks
*/
private void randomSpawnChicken(Location location) {
Random random = ThreadLocalRandom.current();
ThreadLocalRandom random = ThreadLocalRandom.current();
int amount = 0;

if (random.nextInt(8) == 0) {
int count = 1;
amount = 1;
if (random.nextInt(32) == 0) {
count = 4;
}
for (int i = 0; i < count; i++) {
GlowChicken chicken = (GlowChicken)
location.getWorld().spawnEntity(location.clone().add(0, 1, 0),
EntityType.CHICKEN);
chicken.setAge(-24000);
amount = 4;
}
}

for (int i = 0; i < amount; i++) {
GlowChicken chicken =
(GlowChicken) location.getWorld().spawnEntity(
location.clone().add(0, 0.3, 0), EntityType.CHICKEN);
chicken.setBaby();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ protected void pulsePhysics() {
Vector size = boundingBox.getSize();
for (Entity entity : world.getNearbyEntities(
location, size.getX(), size.getY(), size.getZ())) {
if (entity instanceof LivingEntity && entity != this && !(entity.equals(shooter))) {
collide((LivingEntity) entity);
break;
if (entity != this && !(entity.equals(shooter))) {
if (entity instanceof LivingEntity) {
collide((LivingEntity) entity);
break;
}
}
}
}
Expand Down
42 changes: 40 additions & 2 deletions src/main/java/net/glowstone/entity/projectile/GlowSnowball.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,65 @@
package net.glowstone.entity.projectile;

import net.glowstone.entity.monster.GlowBlaze;
import net.glowstone.net.message.play.entity.SpawnObjectMessage;
import org.bukkit.Location;
import org.bukkit.Particle;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Snowball;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.projectiles.ProjectileSource;
import org.bukkit.util.Vector;

public class GlowSnowball extends GlowProjectile implements Snowball {
private static final double VERTICAL_GRAVITY_ACCEL = -0.03;

/**
* Creates a thrown snowball with default speed.
*
* @param location the position and facing of the thrower
*/
public GlowSnowball(Location location) {
super(location);
setDrag(0.99, false);
setDrag(0.99, true);
setHorizontalAirDrag(1);
setGravityAccel(new Vector(0, VERTICAL_GRAVITY_ACCEL, 0));
setVelocity(location.getDirection().multiply(3));
setBoundingBox(0.25, 0.25);
}

/**
* Process collide with a block.
*
* @param block the block that the snowball collides with
*/
@Override
public void collide(Block block) {
getWorld().spawnParticle(Particle.SNOWBALL, location, 5);
remove();
}

/**
* Process collide with a living entity.
*
* @param entity the eneity that the snowball collides with
*/
@Override
public void collide(LivingEntity entity) {
entity.damage(0);
remove();
ProjectileSource source = getShooter();
// the entity receives fake damage.
if (source instanceof Entity) {
if (entity instanceof GlowBlaze) {
entity.damage(3, (Entity) source, EntityDamageEvent.DamageCause.PROJECTILE);
} else {
entity.damage(0, (Entity) source, EntityDamageEvent.DamageCause.PROJECTILE);
}
} else {
entity.damage(0, EntityDamageEvent.DamageCause.PROJECTILE);
}
collide(location.getBlock());
}

@Override
Expand Down

0 comments on commit f140097

Please sign in to comment.