/** (abstract) Protected helper method to write subclass entity data to NBT. */ @Override public void writeEntityToNBT(NBTTagCompound par1NBTTagCompound) { super.writeEntityToNBT(par1NBTTagCompound); if (potionStack != null) { par1NBTTagCompound.setTag("Potion", potionStack.writeToNBT(new NBTTagCompound())); } }
/** (abstract) Protected helper method to read subclass entity data from NBT. */ @Override public void readEntityFromNBT(NBTTagCompound par1NBTTagCompound) { super.readEntityFromNBT(par1NBTTagCompound); if (par1NBTTagCompound.hasKey("Potion")) { potionStack = ItemStack.loadItemStackFromNBT(par1NBTTagCompound.getCompoundTag("Potion")); } if (potionStack == null) { this.setDead(); } }
public void attackEntityWithRangedAttack(EntityLivingBase par1EntityLiving, float par2) { if (!this.getAggressive()) { EntityPotion var2 = new EntityPotion(this.worldObj, this, 32732); var2.rotationPitch -= -20.0F; double var3 = par1EntityLiving.posX + par1EntityLiving.motionX - this.posX; double var5 = par1EntityLiving.posY + (double) par1EntityLiving.getEyeHeight() - 1.100000023841858D - this.posY; double var7 = par1EntityLiving.posZ + par1EntityLiving.motionZ - this.posZ; float var9 = MathHelper.sqrt_double(var3 * var3 + var7 * var7); if (var9 >= 8.0F && !par1EntityLiving.isPotionActive(Potion.moveSlowdown)) { var2.setPotionDamage(32698); } else if (par1EntityLiving.getHealth() >= 8.0F && !par1EntityLiving.isPotionActive(Potion.poison)) { var2.setPotionDamage(32660); } else if (var9 <= 3.0F && !par1EntityLiving.isPotionActive(Potion.weakness) && this.rand.nextFloat() < 0.25F) { var2.setPotionDamage(32696); } var2.setThrowableHeading(var3, var5 + (double) (var9 * 0.2F), var7, 0.75F, 8.0F); this.worldObj.spawnEntityInWorld(var2); } }
@Override public Optional<Entity> createEntity(EntityType type, Vector3d position) { checkNotNull(type, "The entity type cannot be null!"); checkNotNull(position, "The position cannot be null!"); Entity entity = null; Class<? extends Entity> entityClass = type.getEntityClass(); double x = position.getX(); double y = position.getY(); double z = position.getZ(); if (entityClass.isAssignableFrom(EntityPlayerMP.class) || entityClass.isAssignableFrom(EntityDragonPart.class)) { // Unable to construct these return Optional.empty(); } net.minecraft.world.World world = (net.minecraft.world.World) (Object) this; // Not all entities have a single World parameter as their constructor if (entityClass.isAssignableFrom(EntityLightningBolt.class)) { entity = (Entity) new EntityLightningBolt(world, x, y, z); } else if (entityClass.isAssignableFrom(EntityEnderPearl.class)) { EntityArmorStand tempEntity = new EntityArmorStand(world, x, y, z); tempEntity.posY -= tempEntity.getEyeHeight(); entity = (Entity) new EntityEnderPearl(world, tempEntity); ((EnderPearl) entity).setShooter(ProjectileSource.UNKNOWN); } // Some entities need to have non-null fields (and the easiest way to // set them is to use the more specialised constructor). if (entityClass.isAssignableFrom(EntityFallingBlock.class)) { entity = (Entity) new EntityFallingBlock(world, x, y, z, Blocks.sand.getDefaultState()); } else if (entityClass.isAssignableFrom(EntityItem.class)) { entity = (Entity) new EntityItem(world, x, y, z, new ItemStack(Blocks.stone)); } if (entity == null) { try { entity = ConstructorUtils.invokeConstructor(entityClass, this); ((net.minecraft.entity.Entity) entity).setPosition(x, y, z); } catch (Exception e) { SpongeImpl.getLogger().error(ExceptionUtils.getStackTrace(e)); } } // TODO - replace this with an actual check /* if (entity instanceof EntityHanging) { if (((EntityHanging) entity).facingDirection == null) { // TODO Some sort of detection of a valid direction? // i.e scan immediate blocks for something to attach onto. ((EntityHanging) entity).facingDirection = EnumFacing.NORTH; } if (!((EntityHanging) entity).onValidSurface()) { return Optional.empty(); } }*/ // Last chance to fix null fields if (entity instanceof EntityPotion) { // make sure EntityPotion.potionDamage is not null ((EntityPotion) entity).getPotionDamage(); } else if (entity instanceof EntityPainting) { // This is default when art is null when reading from NBT, could // choose a random art instead? ((EntityPainting) entity).art = EnumArt.KEBAB; } return Optional.ofNullable(entity); }