Still spawns at Random places behind me and they still die and respawn.
New code:
package com.cruelar.cruelars_triforcemod.entities.projectiles;
import io.netty.buffer.ByteBuf;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.MoverType;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.datasync.DataParameter;
import net.minecraft.network.datasync.DataSerializers;
import net.minecraft.network.datasync.EntityDataManager;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.*;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.registry.IEntityAdditionalSpawnData;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import java.util.Iterator;
import java.util.List;
public class Clawshot_Head extends Entity implements IEntityAdditionalSpawnData{
private EntityPlayer entityPlayer;
public Entity caughtEntity;
public Clawshot_Head.State currentState;
private static final DataParameter<Integer> DATA_HOOKED_ENTITY;
private boolean inGround;
private int ticksInAir;
private int ticksInGround;
private boolean shouldRetarct=false;
public static ResourceLocation RESOURCE_LOCATION=new ResourceLocation("cruelars_triforcemod:textures/entity/projectiles/clawshot_head.png");
public Clawshot_Head(World world){
super(world);
}
public Clawshot_Head(World world, EntityPlayer entityPlayer){
super(world);
this.init(entityPlayer);
this.currentState = Clawshot_Head.State.FLYING;
this.setPosition((double)entityPlayer.posX,(double)entityPlayer.posY+entityPlayer.getEyeHeight(),(double)entityPlayer.posX);
this.shoot(entityPlayer);
}
@SideOnly(Side.CLIENT)
public Clawshot_Head(World world, EntityPlayer entityPlayer, double posX, double posY, double posZ) {
super(world);
this.init(entityPlayer);
this.setPosition(posX,posY,posZ);
this.prevPosX = this.posX;
this.prevPosY = this.posY;
this.prevPosZ = this.posZ;
}
public void notifyDataManagerChange(DataParameter<?> p_notifyDataManagerChange_1_) {
if (DATA_HOOKED_ENTITY.equals(p_notifyDataManagerChange_1_)) {
int i = (Integer)this.getDataManager().get(DATA_HOOKED_ENTITY);
this.caughtEntity = i > 0 ? this.world.getEntityByID(i - 1) : null;
}
super.notifyDataManagerChange(p_notifyDataManagerChange_1_);
}
public void onUpdate()
{
super.onUpdate();
if (this.entityPlayer == null)
{
this.setDead();
}
else if (this.world.isRemote )
{
if (this.inGround)
{
++this.ticksInGround;
if (this.ticksInGround >= 1200)
{
this.setDead();
return;
}
}
BlockPos blockpos = new BlockPos(this);
IBlockState iblockstate = this.world.getBlockState(blockpos);
if (this.currentState == Clawshot_Head.State.FLYING)
{
if (this.caughtEntity != null)
{
this.motionX = 0.0D;
this.motionY = 0.0D;
this.motionZ = 0.0D;
this.currentState = Clawshot_Head.State.HOOKED_IN_ENTITY;
return;
}
if (!this.world.isRemote)
{
this.checkCollision();
}
if (!this.inGround && !this.onGround && !this.collidedHorizontally)
{
++this.ticksInAir;
}
else
{
this.ticksInAir = 0;
this.motionX = 0.0D;
this.motionY = 0.0D;
this.motionZ = 0.0D;
}
if (ticksInAir>=100){
this.shouldRetarct=true;
}
}
else
{
if (this.currentState == Clawshot_Head.State.HOOKED_IN_ENTITY)
{
if (this.caughtEntity != null)
{
if (this.caughtEntity.isDead)
{
this.caughtEntity = null;
this.currentState = Clawshot_Head.State.FLYING;
}
else
{
this.posX = this.caughtEntity.posX;
double d2 = (double)this.caughtEntity.height;
this.posY = this.caughtEntity.getEntityBoundingBox().minY + d2 * 0.8D;
this.posZ = this.caughtEntity.posZ;
this.setPosition(this.posX, this.posY, this.posZ);
}
}
return;
}
}
if (iblockstate.getMaterial() != Material.WATER)
{
this.motionY -= 0.03D;
}
this.move(MoverType.SELF, this.motionX, this.motionY, this.motionZ);
this.updateRotation();
this.setPosition(this.posX, this.posY, this.posZ);
}
}
private void updateRotation()
{
float f = MathHelper.sqrt(this.motionX * this.motionX + this.motionZ * this.motionZ);
this.rotationYaw = (float)(MathHelper.atan2(this.motionX, this.motionZ) * (180D / Math.PI));
for (this.rotationPitch = (float)(MathHelper.atan2(this.motionY, (double)f) * (180D / Math.PI)); this.rotationPitch - this.prevRotationPitch < -180.0F; this.prevRotationPitch -= 360.0F)
{
;
}
while (this.rotationPitch - this.prevRotationPitch >= 180.0F)
{
this.prevRotationPitch += 360.0F;
}
while (this.rotationYaw - this.prevRotationYaw < -180.0F)
{
this.prevRotationYaw -= 360.0F;
}
while (this.rotationYaw - this.prevRotationYaw >= 180.0F)
{
this.prevRotationYaw += 360.0F;
}
this.rotationPitch = this.prevRotationPitch + (this.rotationPitch - this.prevRotationPitch) * 0.2F;
this.rotationYaw = this.prevRotationYaw + (this.rotationYaw - this.prevRotationYaw) * 0.2F;
}
public void shoot(Entity entity) {
System.out.println(entity);
this.motionX=0.6D*entity.getLookVec().x;
this.motionY=0.6D*entity.getLookVec().y;
this.motionZ=0.6D*entity.getLookVec().z;
}
public void shoot(double p_shoot_1_, double p_shoot_3_, double p_shoot_5_, float p_shoot_7_, float p_shoot_8_){}
public int handleHookRetraction()
{
if (!this.world.isRemote && this.entityPlayer != null)
{
int i = 0;
if (this.caughtEntity != null)
{
this.bringInHookedEntity();
this.world.setEntityState(this, (byte)31);
i = this.caughtEntity instanceof EntityItem ? 3 : 5;
}
else if (this.inGround)
{
i = 2;
}
this.setDead();
return i;
}
else
{
return 0;
}
}
protected void bringInHookedEntity()
{
if (this.entityPlayer != null)
{
double d0 = this.entityPlayer.posX - this.posX;
double d1 = this.entityPlayer.posY - this.posY;
double d2 = this.entityPlayer.posZ - this.posZ;
double d3 = 0.1D;
this.caughtEntity.motionX += d0 * 0.1D;
this.caughtEntity.motionY += d1 * 0.1D;
this.caughtEntity.motionZ += d2 * 0.1D;
}
}
public void setDead()
{
super.setDead();
}
public EntityPlayer getAngler()
{
return this.entityPlayer;
}
private void init(EntityPlayer p_init_1_) {
this.setSize(0.25F, 0.25F);
this.ignoreFrustumCheck = true;
this.entityPlayer = p_init_1_;
}
private void checkCollision() {
Vec3d vec3d = new Vec3d(this.posX, this.posY, this.posZ);
Vec3d vec3d1 = new Vec3d(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);
RayTraceResult raytraceresult = this.world.rayTraceBlocks(vec3d, vec3d1, false, true, false);
vec3d = new Vec3d(this.posX, this.posY, this.posZ);
vec3d1 = new Vec3d(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);
if (raytraceresult != null) {
vec3d1 = new Vec3d(raytraceresult.hitVec.x, raytraceresult.hitVec.y, raytraceresult.hitVec.z);
this.motionX=0;
this.motionY=0;
this.motionZ=0;
entityPlayer.posX=this.posX;
entityPlayer.posY=this.posY;
entityPlayer.posZ=this.posZ;
}
Entity entity = null;
List<Entity> list = this.world.getEntitiesWithinAABBExcludingEntity(this, this.getEntityBoundingBox().expand(this.motionX, this.motionY, this.motionZ).grow(1.0D));
double d0 = 0.0D;
Iterator var8 = list.iterator();
while(true) {
Entity entity1;
double d1;
do {
RayTraceResult raytraceresult1;
do {
do {
do {
if (!var8.hasNext()) {
if (entity != null) {
raytraceresult = new RayTraceResult(entity);
}
if (raytraceresult != null && raytraceresult.typeOfHit != RayTraceResult.Type.MISS) {
if (raytraceresult.typeOfHit == RayTraceResult.Type.ENTITY) {
this.caughtEntity = raytraceresult.entityHit;
this.setHookedEntity();
} else {
this.inGround = true;
}
}
return;
}
entity1 = (Entity)var8.next();
} while(!this.canBeHooked(entity1));
} while(entity1 == this.entityPlayer && this.ticksInAir < 5);
AxisAlignedBB axisalignedbb = entity1.getEntityBoundingBox().grow(0.30000001192092896D);
raytraceresult1 = axisalignedbb.calculateIntercept(vec3d, vec3d1);
} while(raytraceresult1 == null);
d1 = vec3d.squareDistanceTo(raytraceresult1.hitVec);
} while(d1 >= d0 && d0 != 0.0D);
entity = entity1;
d0 = d1;
}
}
private void setHookedEntity() {
this.getDataManager().set(DATA_HOOKED_ENTITY, this.caughtEntity.getEntityId() + 1);
}
protected boolean canBeHooked(Entity p_canBeHooked_1_) {
return p_canBeHooked_1_.canBeCollidedWith() || p_canBeHooked_1_ instanceof EntityItem;
}
protected void entityInit() {
this.getDataManager().register(DATA_HOOKED_ENTITY, 0);
}
@Override
protected void readEntityFromNBT(NBTTagCompound nbtTagCompound) {
}
@Override
protected void writeEntityToNBT(NBTTagCompound nbtTagCompound) {
}
static {
DATA_HOOKED_ENTITY = EntityDataManager.createKey(Clawshot_Head.class, DataSerializers.VARINT);
}
@Override
public void writeSpawnData(ByteBuf buffer) {
buffer.writeBoolean(entityPlayer==null);
if (entityPlayer!=null) {
buffer.writeInt(entityPlayer.getEntityId());
}
}
@Override
public void readSpawnData(ByteBuf additionalData) {
boolean nullPlayer = additionalData.readBoolean();
if (!nullPlayer){
EntityPlayer entityByID=(EntityPlayer) world.getEntityByID(additionalData.readInt());
if (entityByID!=null){
entityPlayer=entityByID;
}
this.shoot(entityPlayer);
}
}
static enum State {
FLYING,
HOOKED_IN_ENTITY;
private State() {
}
}
}