Search in sources :

Example 1 with IThrowableEntity

use of net.minecraftforge.fml.common.registry.IThrowableEntity in project GregTech by GregTechCE.

the class FieldProjectorEventHandler method isHostileProjectile.

private static Pair<Boolean, Entity> isHostileProjectile(Entity entity, Entity owner) {
    if (entity instanceof EntityFireball) {
        EntityLivingBase shooter = ((EntityFireball) entity).shootingEntity;
        return Pair.of(!owner.isEntityEqual(shooter), shooter);
    } else if (entity instanceof EntityArrow) {
        Entity shooter = ((EntityArrow) entity).shootingEntity;
        return Pair.of(!owner.isEntityEqual(shooter), shooter);
    } else if (entity instanceof EntityPotion) {
        EntityLivingBase shooter = ((EntityPotion) entity).getThrower();
        ItemStack potionStack = ((EntityPotion) entity).getPotion();
        List<PotionEffect> effectList = PotionUtils.getEffectsFromStack(potionStack);
        boolean hasBadEffects = effectList.stream().anyMatch(it -> it.getPotion().isBadEffect());
        // potions without bad effects are not hostile, so do not touch them
        return Pair.of(!owner.isEntityEqual(shooter) && hasBadEffects, shooter);
    } else if (entity instanceof EntityThrowable) {
        EntityLivingBase shooter = ((EntityThrowable) entity).getThrower();
        return Pair.of(!owner.isEntityEqual(shooter), shooter);
    } else if (entity instanceof IThrowableEntity) {
        Entity shooter = ((IThrowableEntity) entity).getThrower();
        return Pair.of(!owner.isEntityEqual(shooter), shooter);
    } else if (entity instanceof IProjectile) {
        // unknown projectiles are always hostile
        return Pair.of(true, null);
    }
    return Pair.of(false, null);
}
Also used : EntityArrow(net.minecraft.entity.projectile.EntityArrow) IThrowableEntity(net.minecraftforge.fml.common.registry.IThrowableEntity) Entity(net.minecraft.entity.Entity) EntityThrowable(net.minecraft.entity.projectile.EntityThrowable) PotionEffect(net.minecraft.potion.PotionEffect) IThrowableEntity(net.minecraftforge.fml.common.registry.IThrowableEntity) EntityLivingBase(net.minecraft.entity.EntityLivingBase) EntityPotion(net.minecraft.entity.projectile.EntityPotion) ItemStack(net.minecraft.item.ItemStack) EntityFireball(net.minecraft.entity.projectile.EntityFireball) IProjectile(net.minecraft.entity.IProjectile)

Example 2 with IThrowableEntity

use of net.minecraftforge.fml.common.registry.IThrowableEntity in project MinecraftForge by MinecraftForge.

the class EntitySpawnHandler method spawnEntity.

private void spawnEntity(FMLMessage.EntitySpawnMessage spawnMsg) {
    ModContainer mc = Loader.instance().getIndexedModList().get(spawnMsg.modId);
    EntityRegistration er = EntityRegistry.instance().lookupModSpawn(mc, spawnMsg.modEntityTypeId);
    if (er == null) {
        throw new RuntimeException("Could not spawn mod entity ModID: " + spawnMsg.modId + " EntityID: " + spawnMsg.modEntityTypeId + " at ( " + spawnMsg.rawX + "," + spawnMsg.rawY + ", " + spawnMsg.rawZ + ") Please contact mod author or server admin.");
    }
    WorldClient wc = FMLClientHandler.instance().getWorldClient();
    Class<? extends Entity> cls = er.getEntityClass();
    try {
        Entity entity;
        if (er.hasCustomSpawning()) {
            entity = er.doCustomSpawning(spawnMsg);
        } else {
            entity = cls.getConstructor(World.class).newInstance(wc);
            int offset = spawnMsg.entityId - entity.getEntityId();
            entity.setEntityId(spawnMsg.entityId);
            entity.setUniqueId(spawnMsg.entityUUID);
            entity.setLocationAndAngles(spawnMsg.rawX, spawnMsg.rawY, spawnMsg.rawZ, spawnMsg.scaledYaw, spawnMsg.scaledPitch);
            if (entity instanceof EntityLiving) {
                ((EntityLiving) entity).rotationYawHead = spawnMsg.scaledHeadYaw;
            }
            Entity[] parts = entity.getParts();
            if (parts != null) {
                for (int j = 0; j < parts.length; j++) {
                    parts[j].setEntityId(parts[j].getEntityId() + offset);
                }
            }
        }
        EntityTracker.updateServerPosition(entity, spawnMsg.rawX, spawnMsg.rawY, spawnMsg.rawZ);
        EntityPlayerSP clientPlayer = FMLClientHandler.instance().getClientPlayerEntity();
        if (entity instanceof IThrowableEntity) {
            Entity thrower = clientPlayer.getEntityId() == spawnMsg.throwerId ? clientPlayer : wc.getEntityByID(spawnMsg.throwerId);
            ((IThrowableEntity) entity).setThrower(thrower);
        }
        if (spawnMsg.dataWatcherList != null) {
            entity.getDataManager().setEntryValues(spawnMsg.dataWatcherList);
        }
        if (spawnMsg.throwerId > 0) {
            entity.setVelocity(spawnMsg.speedScaledX, spawnMsg.speedScaledY, spawnMsg.speedScaledZ);
        }
        if (entity instanceof IEntityAdditionalSpawnData) {
            ((IEntityAdditionalSpawnData) entity).readSpawnData(spawnMsg.dataStream);
        }
        wc.addEntityToWorld(spawnMsg.entityId, entity);
    } catch (Exception e) {
        FMLLog.log(Level.ERROR, e, "A severe problem occurred during the spawning of an entity at ( " + spawnMsg.rawX + "," + spawnMsg.rawY + ", " + spawnMsg.rawZ + ")");
        throw Throwables.propagate(e);
    }
}
Also used : IEntityAdditionalSpawnData(net.minecraftforge.fml.common.registry.IEntityAdditionalSpawnData) Entity(net.minecraft.entity.Entity) IThrowableEntity(net.minecraftforge.fml.common.registry.IThrowableEntity) ModContainer(net.minecraftforge.fml.common.ModContainer) EntityLiving(net.minecraft.entity.EntityLiving) IThrowableEntity(net.minecraftforge.fml.common.registry.IThrowableEntity) WorldClient(net.minecraft.client.multiplayer.WorldClient) EntityPlayerSP(net.minecraft.client.entity.EntityPlayerSP) EntityRegistration(net.minecraftforge.fml.common.registry.EntityRegistry.EntityRegistration)

Aggregations

Entity (net.minecraft.entity.Entity)2 IThrowableEntity (net.minecraftforge.fml.common.registry.IThrowableEntity)2 EntityPlayerSP (net.minecraft.client.entity.EntityPlayerSP)1 WorldClient (net.minecraft.client.multiplayer.WorldClient)1 EntityLiving (net.minecraft.entity.EntityLiving)1 EntityLivingBase (net.minecraft.entity.EntityLivingBase)1 IProjectile (net.minecraft.entity.IProjectile)1 EntityArrow (net.minecraft.entity.projectile.EntityArrow)1 EntityFireball (net.minecraft.entity.projectile.EntityFireball)1 EntityPotion (net.minecraft.entity.projectile.EntityPotion)1 EntityThrowable (net.minecraft.entity.projectile.EntityThrowable)1 ItemStack (net.minecraft.item.ItemStack)1 PotionEffect (net.minecraft.potion.PotionEffect)1 ModContainer (net.minecraftforge.fml.common.ModContainer)1 EntityRegistration (net.minecraftforge.fml.common.registry.EntityRegistry.EntityRegistration)1 IEntityAdditionalSpawnData (net.minecraftforge.fml.common.registry.IEntityAdditionalSpawnData)1