Skip to content

Commit

Permalink
Add item_projectile and fake_arrow DenizenEntityTypes
Browse files Browse the repository at this point in the history
The item_projectile works like a snowball and fires hit and damage
events (set to 0 damage) for the entire Bukkit server.
  • Loading branch information
Morphan1 committed Feb 12, 2015
1 parent d2bbceb commit 9a14353
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/main/java/net/aufdemrand/denizen/Denizen.java
Expand Up @@ -24,6 +24,9 @@
import net.aufdemrand.denizen.objects.properties.item.*;
import net.aufdemrand.denizen.tags.BukkitTagContext;
import net.aufdemrand.denizen.tags.core.*;
import net.aufdemrand.denizen.utilities.entity.CraftFakeArrow;
import net.aufdemrand.denizen.utilities.entity.CraftItemProjectile;
import net.aufdemrand.denizen.utilities.entity.DenizenEntityType;
import net.aufdemrand.denizencore.events.OldEventManager;
import net.aufdemrand.denizen.events.bukkit.SavesReloadEvent;
import net.aufdemrand.denizen.events.bukkit.ScriptReloadEvent;
Expand Down Expand Up @@ -407,6 +410,10 @@ public void onEnable() {
Depends.citizens.registerCommandClass(NPCCommandHandler.class);
}

// Register DenizenEntityTypes
DenizenEntityType.registerEntityType("ITEM_PROJECTILE", CraftItemProjectile.class);
DenizenEntityType.registerEntityType("FAKE_ARROW", CraftFakeArrow.class);

// Track all player names for quick dPlayer matching
for (OfflinePlayer player: Bukkit.getOfflinePlayers()) {
dPlayer.notePlayer(player);
Expand Down
Expand Up @@ -15,7 +15,7 @@

import java.util.Set;

public class CraftFakeArrow extends CraftArrow implements Vehicle {
public class CraftFakeArrow extends CraftArrow implements DenizenCustomEntity, Vehicle {
public CraftFakeArrow(CraftServer craftServer, EntityArrow entityArrow) {
super(craftServer, entityArrow);
}
Expand All @@ -32,6 +32,7 @@ public void remove() {
super.remove();
}

@CreateEntity
public static Arrow createArrow(Location location) {
CraftWorld world = (CraftWorld) location.getWorld();
EntityArrow arrow = new FakeArrowEntity(world, location);
Expand Down Expand Up @@ -113,4 +114,9 @@ public void recalculatePermissions() {

public void removeAttachment(PermissionAttachment attachment) {
}

@Override
public String getEntityTypeName() {
return "FAKE_ARROW";
}
}
@@ -0,0 +1,77 @@
package net.aufdemrand.denizen.utilities.entity;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_8_R1.CraftServer;
import org.bukkit.craftbukkit.v1_8_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_8_R1.entity.CraftHumanEntity;
import org.bukkit.craftbukkit.v1_8_R1.entity.CraftItem;
import org.bukkit.craftbukkit.v1_8_R1.entity.CraftLivingEntity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.projectiles.ProjectileSource;

public class CraftItemProjectile extends CraftItem implements DenizenCustomEntity, ItemProjectile {

private boolean doesBounce;

public CraftItemProjectile(CraftServer server, EntityItemProjectile entity) {
super(server, entity);
}

@CreateEntity
public static ItemProjectile createItemProjectile(Location location) {
CraftWorld world = (CraftWorld) location.getWorld();
EntityItemProjectile entity = new EntityItemProjectile(world, location);
return (ItemProjectile) entity.getBukkitEntity();
}

@Override
public EntityItemProjectile getHandle() {
return (EntityItemProjectile) super.getHandle();
}

@Override
public String getEntityTypeName() {
return "ITEM_PROJECTILE";
}

@Override
public ProjectileSource getShooter() {
return getHandle().projectileSource;
}

@Override
public void setShooter(ProjectileSource shooter) {
if (shooter instanceof CraftLivingEntity) {
getHandle().shooter = ((CraftLivingEntity) shooter).getHandle();
if (shooter instanceof CraftHumanEntity) {
getHandle().shooterName = ((CraftHumanEntity) shooter).getName();
}
} else {
getHandle().shooter = null;
getHandle().shooterName = null;
}
getHandle().projectileSource = shooter;
}

@Override
public boolean doesBounce() {
return doesBounce;
}

@Override
public void setBounce(boolean doesBounce) {
this.doesBounce = doesBounce;
}


@Override
public void _INVALID_setShooter(LivingEntity livingEntity) {
// Do nothing
}

@Override
public LivingEntity _INVALID_getShooter() {
return null;
}
}
@@ -0,0 +1,15 @@
package net.aufdemrand.denizen.utilities.entity;

import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.util.Vector;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

public interface DenizenCustomEntity extends Entity {
@Retention(RetentionPolicy.RUNTIME)
public @interface CreateEntity {}

public String getEntityTypeName();
}
@@ -0,0 +1,145 @@
package net.aufdemrand.denizen.utilities.entity;

import net.minecraft.server.v1_8_R1.*;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_8_R1.CraftServer;
import org.bukkit.craftbukkit.v1_8_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_8_R1.event.CraftEventFactory;

import java.util.List;

public class EntityItemProjectile extends EntityItem implements IProjectile {

public EntityLiving shooter;
public String shooterName;
private int age;

public EntityItemProjectile(CraftWorld craftWorld, Location location) {
super(craftWorld.getHandle());
this.pickupDelay = Integer.MAX_VALUE;
setPositionRotation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
this.a(0.25F, 0.25F);
world.addEntity(this);
bukkitEntity = new CraftItemProjectile((CraftServer) Bukkit.getServer(), this);
}

@Override
public void s_() {
K();
BlockPosition blockposition = new BlockPosition(locX, locY, locZ);
IBlockData iblockdata = world.getType(blockposition);
Block block = iblockdata.getBlock();

if (block.getMaterial() != Material.AIR) {
AxisAlignedBB axisalignedbb = block.a(world, blockposition, iblockdata);

if ((axisalignedbb != null) && (axisalignedbb.a(new Vec3D(locX, locY, locZ)))) {
CraftEventFactory.callProjectileHitEvent(this);
this.die();
}
}
age += 1;
Vec3D vec3d = new Vec3D(locX, locY, locZ);
Vec3D vec3d1 = new Vec3D(locX + motX, locY + motY, locZ + motZ);
MovingObjectPosition movingobjectposition = world.rayTrace(vec3d, vec3d1, false, true, false);

vec3d = new Vec3D(locX, locY, locZ);
vec3d1 = new Vec3D(locX + motX, locY + motY, locZ + motZ);
if (movingobjectposition != null) {
vec3d1 = new Vec3D(movingobjectposition.pos.a, movingobjectposition.pos.b, movingobjectposition.pos.c);
}

Entity entity = null;
List list = world.getEntities(this, getBoundingBox().a(motX, motY, motZ).grow(1.0D, 1.0D, 1.0D));
double d0 = 0.0D;

for (Object aList : list) {
Entity entity1 = (Entity) aList;

if ((entity1.ad()) && ((entity1 != shooter) || (age >= 5))) {
float f1 = 0.3F;
AxisAlignedBB axisalignedbb1 = entity1.getBoundingBox().grow(f1, f1, f1);
MovingObjectPosition movingobjectposition1 = axisalignedbb1.a(vec3d, vec3d1);

if (movingobjectposition1 != null) {
double d1 = vec3d.distanceSquared(movingobjectposition1.pos);

if ((d1 < d0) || (d0 == 0.0D)) {
entity = entity1;
d0 = d1;
}
}
}
}
if (entity != null) {
movingobjectposition = new MovingObjectPosition(entity);
}
if ((movingobjectposition != null) && (movingobjectposition.entity != null) && ((movingobjectposition.entity instanceof EntityHuman))) {
EntityHuman entityhuman = (EntityHuman) movingobjectposition.entity;
if ((entityhuman.abilities.isInvulnerable) || (((shooter instanceof EntityHuman)) && (!((EntityHuman) shooter).a(entityhuman)))) {
movingobjectposition = null;
}
}
if (movingobjectposition != null) {
if (movingobjectposition.entity != null && movingobjectposition.entity instanceof EntityLiving) {
movingobjectposition.entity.damageEntity(DamageSource.projectile(this, this.getShooter()), 0F);
this.die();
} else if (movingobjectposition.a() != null) {
if (block.getMaterial() != Material.AIR) {
motX = ((float) (movingobjectposition.pos.a - locX));
motY = ((float) (movingobjectposition.pos.b - locY));
motZ = ((float) (movingobjectposition.pos.c - locZ));
float f3 = MathHelper.sqrt(motX * motX + motY * motY + motZ * motZ);
locX -= motX / f3 * 0.0500000007450581D;
locY -= motY / f3 * 0.0500000007450581D;
locZ -= motZ / f3 * 0.0500000007450581D;
CraftEventFactory.callProjectileHitEvent(this);
this.die();
}
}
}

locX += motX;
locY += motY;
locZ += motZ;
float f3 = 0.99F;
float f1 = 0.05F;
motX *= f3;
motY *= f3;
motZ *= f3;
motY -= f1;
setPosition(locX, locY, locZ);
checkBlockCollisions();
}

@Override
public void shoot(double d0, double d1, double d2, float f, float f1) {
float f2 = MathHelper.sqrt(d0 * d0 + d1 * d1 + d2 * d2);

d0 /= f2;
d1 /= f2;
d2 /= f2;
d0 += random.nextGaussian() * 0.007499999832361937D * f1;
d1 += random.nextGaussian() * 0.007499999832361937D * f1;
d2 += random.nextGaussian() * 0.007499999832361937D * f1;
d0 *= f;
d1 *= f;
d2 *= f;
motX = d0;
motY = d1;
motZ = d2;
float f3 = MathHelper.sqrt(d0 * d0 + d2 * d2);

lastYaw = yaw = (float) (Math.atan2(d0, d2) * 180.0D / 3.1415927410125732D);
lastPitch = pitch = (float) (Math.atan2(d1, f3) * 180.0D / 3.1415927410125732D);
}

public EntityLiving getShooter() {
if (this.shooter == null && this.shooterName != null && this.shooterName.length() > 0) {
this.shooter = this.world.a(this.shooterName);
}

return this.shooter;
}
}
@@ -0,0 +1,7 @@
package net.aufdemrand.denizen.utilities.entity;

import org.bukkit.entity.Item;
import org.bukkit.entity.Projectile;

public interface ItemProjectile extends Item, Projectile {
}

0 comments on commit 9a14353

Please sign in to comment.