Skip to content

Instantly share code, notes, and snippets.

@zh32
Created October 15, 2013 13:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zh32/6991460 to your computer and use it in GitHub Desktop.
Save zh32/6991460 to your computer and use it in GitHub Desktop.
public class DamageUtil {
public static void createExplosion(Projectile pro, WeaponType weapontype, LivingEntity shooter) {
int radius = weapontype.getDamage();
Location loc = pro.getLocation();
List<Entity> en = pro.getNearbyEntities(radius, radius, radius);
pro.getLocation().getWorld().createExplosion(loc, 0.0F, false);
for (Entity ent : en) {
if (ent instanceof LivingEntity) {
HumanEntity le = (HumanEntity) ent;
Location eyeloc = le.getEyeLocation();
int distance = (int) loc.distanceSquared(ent.getLocation());
if (canDamage(loc, eyeloc)) {
int damage = getExplosionDamage(radius, distance);
le.damage(damage, shooter);
}
}
}
}
private static boolean canDamage(Location loc1, Location loc2) {
loc1.setY(loc1.getY() + 1.5F);
World w = loc1.getWorld();
int minx = Math.min(loc1.getBlockX(), loc2.getBlockX()),
miny = Math.min(loc1.getBlockY(), loc2.getBlockY()),
minz = Math.min(loc1.getBlockZ(), loc2.getBlockZ()),
maxx = Math.max(loc1.getBlockX(), loc2.getBlockX()),
maxy = Math.max(loc1.getBlockY(), loc2.getBlockY()),
maxz = Math.max(loc1.getBlockZ(), loc2.getBlockZ());
for (int x = minx; x<=maxx;x++) {
for (int y = miny; y<=maxy;y++) {
for (int z = minz; z<=maxz;z++) {
Block b = w.getBlockAt(x, y, z);
if (!b.getType().equals(Material.AIR)) {
return false;
}
}
}
}
return true;
}
private static int getExplosionDamage(int r, int d) {
float akk = (float)r*(float)r;
float abb = (float) d;
return Math.round((((10F/akk)*abb)+(-10F))*(-1F));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment