Search in sources :

Example 1 with ThrowableProjectile

use of net.minecraft.world.entity.projectile.ThrowableProjectile in project AlexsMobs by Alex-the-666.

the class ServerEvents method onProjectileHit.

@SubscribeEvent
public void onProjectileHit(ProjectileImpactEvent event) {
    if (event.getRayTraceResult() instanceof EntityHitResult && ((EntityHitResult) event.getRayTraceResult()).getEntity() instanceof EntityEmu && !event.getEntity().level.isClientSide) {
        EntityEmu emu = ((EntityEmu) ((EntityHitResult) event.getRayTraceResult()).getEntity());
        if (event.getEntity() instanceof AbstractArrow) {
            // fixes soft crash with vanilla
            ((AbstractArrow) event.getEntity()).setPierceLevel((byte) 0);
        }
        if ((emu.getAnimation() == EntityEmu.ANIMATION_DODGE_RIGHT || emu.getAnimation() == EntityEmu.ANIMATION_DODGE_LEFT) && emu.getAnimationTick() < 7) {
            event.setCanceled(true);
        }
        if (emu.getAnimation() != EntityEmu.ANIMATION_DODGE_RIGHT && emu.getAnimation() != EntityEmu.ANIMATION_DODGE_LEFT) {
            boolean left = true;
            Vec3 arrowPos = event.getEntity().position();
            Vec3 rightVector = emu.getLookAngle().yRot(0.5F * (float) Math.PI).add(emu.position());
            Vec3 leftVector = emu.getLookAngle().yRot(-0.5F * (float) Math.PI).add(emu.position());
            if (arrowPos.distanceTo(rightVector) < arrowPos.distanceTo(leftVector)) {
                left = false;
            } else if (arrowPos.distanceTo(rightVector) > arrowPos.distanceTo(leftVector)) {
                left = true;
            } else {
                left = emu.getRandom().nextBoolean();
            }
            Vec3 vector3d2 = event.getEntity().getDeltaMovement().yRot((float) ((left ? -0.5F : 0.5F) * Math.PI)).normalize();
            emu.setAnimation(left ? EntityEmu.ANIMATION_DODGE_LEFT : EntityEmu.ANIMATION_DODGE_RIGHT);
            emu.hasImpulse = true;
            if (!emu.horizontalCollision) {
                emu.move(MoverType.SELF, new Vec3(vector3d2.x() * 0.25F, 0.1F, vector3d2.z() * 0.25F));
            }
            if (!event.getEntity().level.isClientSide) {
                ServerPlayer serverPlayerEntity = null;
                if (event.getEntity() instanceof Arrow) {
                    Entity thrower = ((Arrow) event.getEntity()).getOwner();
                    if (thrower instanceof ServerPlayer) {
                        serverPlayerEntity = (ServerPlayer) thrower;
                    }
                }
                if (event.getEntity() instanceof ThrowableProjectile) {
                    Entity thrower = ((ThrowableProjectile) event.getEntity()).getOwner();
                    if (thrower instanceof ServerPlayer) {
                        serverPlayerEntity = (ServerPlayer) thrower;
                    }
                }
                if (serverPlayerEntity != null) {
                    AMAdvancementTriggerRegistry.EMU_DODGE.trigger(serverPlayerEntity);
                }
            }
            emu.setDeltaMovement(emu.getDeltaMovement().add(vector3d2.x() * 0.5F, 0.32F, vector3d2.z() * 0.5F));
            event.setCanceled(true);
        }
    }
}
Also used : AbstractArrow(net.minecraft.world.entity.projectile.AbstractArrow) Arrow(net.minecraft.world.entity.projectile.Arrow) ItemEntity(net.minecraft.world.entity.item.ItemEntity) ServerPlayer(net.minecraft.server.level.ServerPlayer) ThrowableProjectile(net.minecraft.world.entity.projectile.ThrowableProjectile) AbstractArrow(net.minecraft.world.entity.projectile.AbstractArrow) SubscribeEvent(net.minecraftforge.eventbus.api.SubscribeEvent)

Example 2 with ThrowableProjectile

use of net.minecraft.world.entity.projectile.ThrowableProjectile in project SpongeCommon by SpongePowered.

the class EntityActivationRange method initializeEntityActivationState.

/**
 * Initialize entity activation state.
 *
 * @param entity Entity to check
 */
public static void initializeEntityActivationState(final Entity entity) {
    final ActivationCapabilityBridge spongeEntity = (ActivationCapabilityBridge) entity;
    if (entity.level.isClientSide()) {
        return;
    }
    // types that should always be active
    if (entity instanceof Player && !((PlatformEntityBridge) entity).bridge$isFakePlayer() || entity instanceof ThrowableProjectile || entity instanceof EnderDragon || entity instanceof EnderDragonPart || entity instanceof WitherBoss || entity instanceof AbstractHurtingProjectile || entity instanceof LightningBolt || entity instanceof PrimedTnt || entity instanceof Painting || entity instanceof EndCrystal || entity instanceof FireworkRocketEntity || // Always tick falling blocks
    entity instanceof FallingBlockEntity) {
        return;
    }
    final InheritableConfigHandle<WorldConfig> configAdapter = SpongeGameConfigs.getForWorld(entity.level);
    final EntityActivationRangeCategory config = configAdapter.get().entityActivationRange;
    final EntityTypeBridge type = (EntityTypeBridge) entity.getType();
    final ResourceLocation key = EntityType.getKey(entity.getType());
    final byte activationType = spongeEntity.activation$getActivationType();
    final String activationTypeName = EntityActivationRange.activationTypeMappings.getOrDefault(activationType, "misc");
    if (!type.bridge$isActivationRangeInitialized()) {
        EntityActivationRange.addEntityToConfig(config.autoPopulate, key, activationType, activationTypeName);
        type.bridge$setActivationRangeInitialized(true);
    }
    final EntityActivationRangeCategory.ModSubCategory entityMod = config.mods.get(key.getNamespace());
    final int defaultActivationRange = config.globalRanges.get(activationTypeName);
    if (entityMod == null) {
        // use default activation range
        spongeEntity.activation$setActivationRange(defaultActivationRange);
        if (defaultActivationRange > 0) {
            spongeEntity.activation$setDefaultActivationState(false);
        }
    } else {
        if (!entityMod.enabled) {
            spongeEntity.activation$setDefaultActivationState(true);
            return;
        }
        final Integer defaultModActivationRange = entityMod.defaultRanges.get(activationTypeName);
        final Integer entityActivationRange = entityMod.entities.get(key.getPath());
        if (defaultModActivationRange != null && entityActivationRange == null) {
            spongeEntity.activation$setActivationRange(defaultModActivationRange);
            if (defaultModActivationRange > 0) {
                spongeEntity.activation$setDefaultActivationState(false);
            }
        } else if (entityActivationRange != null) {
            spongeEntity.activation$setActivationRange(entityActivationRange);
            if (entityActivationRange > 0) {
                spongeEntity.activation$setDefaultActivationState(false);
            }
        }
    }
}
Also used : PrimedTnt(net.minecraft.world.entity.item.PrimedTnt) FallingBlockEntity(net.minecraft.world.entity.item.FallingBlockEntity) Player(net.minecraft.world.entity.player.Player) ServerPlayer(net.minecraft.server.level.ServerPlayer) WorldConfig(org.spongepowered.common.config.inheritable.WorldConfig) EntityTypeBridge(org.spongepowered.common.bridge.world.entity.EntityTypeBridge) EndCrystal(net.minecraft.world.entity.boss.enderdragon.EndCrystal) FireworkRocketEntity(net.minecraft.world.entity.projectile.FireworkRocketEntity) Painting(net.minecraft.world.entity.decoration.Painting) LightningBolt(net.minecraft.world.entity.LightningBolt) EnderDragon(net.minecraft.world.entity.boss.enderdragon.EnderDragon) ActivationCapabilityBridge(org.spongepowered.common.bridge.activation.ActivationCapabilityBridge) WitherBoss(net.minecraft.world.entity.boss.wither.WitherBoss) EnderDragonPart(net.minecraft.world.entity.boss.EnderDragonPart) AbstractHurtingProjectile(net.minecraft.world.entity.projectile.AbstractHurtingProjectile) ResourceLocation(net.minecraft.resources.ResourceLocation) ThrowableProjectile(net.minecraft.world.entity.projectile.ThrowableProjectile) EntityActivationRangeCategory(org.spongepowered.common.config.inheritable.EntityActivationRangeCategory)

Aggregations

ServerPlayer (net.minecraft.server.level.ServerPlayer)2 ThrowableProjectile (net.minecraft.world.entity.projectile.ThrowableProjectile)2 ResourceLocation (net.minecraft.resources.ResourceLocation)1 LightningBolt (net.minecraft.world.entity.LightningBolt)1 EnderDragonPart (net.minecraft.world.entity.boss.EnderDragonPart)1 EndCrystal (net.minecraft.world.entity.boss.enderdragon.EndCrystal)1 EnderDragon (net.minecraft.world.entity.boss.enderdragon.EnderDragon)1 WitherBoss (net.minecraft.world.entity.boss.wither.WitherBoss)1 Painting (net.minecraft.world.entity.decoration.Painting)1 FallingBlockEntity (net.minecraft.world.entity.item.FallingBlockEntity)1 ItemEntity (net.minecraft.world.entity.item.ItemEntity)1 PrimedTnt (net.minecraft.world.entity.item.PrimedTnt)1 Player (net.minecraft.world.entity.player.Player)1 AbstractArrow (net.minecraft.world.entity.projectile.AbstractArrow)1 AbstractHurtingProjectile (net.minecraft.world.entity.projectile.AbstractHurtingProjectile)1 Arrow (net.minecraft.world.entity.projectile.Arrow)1 FireworkRocketEntity (net.minecraft.world.entity.projectile.FireworkRocketEntity)1 SubscribeEvent (net.minecraftforge.eventbus.api.SubscribeEvent)1 ActivationCapabilityBridge (org.spongepowered.common.bridge.activation.ActivationCapabilityBridge)1 EntityTypeBridge (org.spongepowered.common.bridge.world.entity.EntityTypeBridge)1