Search in sources :

Example 11 with LivingEntity

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

the class LivingEntityMixin method bridge$damageEntity.

@Override
public boolean bridge$damageEntity(final DamageSource damageSource, float damage) {
    if (this.shadow$isInvulnerableTo(damageSource)) {
        return false;
    }
    final boolean isHuman = (LivingEntity) (Object) this instanceof Player;
    // Sponge Start - Call platform hook for adjusting damage
    damage = this.bridge$applyModDamage((LivingEntity) (Object) this, damageSource, damage);
    // Sponge End
    final float originalDamage = damage;
    if (damage <= 0) {
        return false;
    }
    final List<DamageFunction> originalFunctions = new ArrayList<>();
    final Optional<DamageFunction> hardHatFunction = DamageEventUtil.createHardHatModifier((LivingEntity) (Object) this, damageSource);
    final Optional<DamageFunction> armorFunction = DamageEventUtil.createArmorModifiers((LivingEntity) (Object) this, damageSource);
    final Optional<DamageFunction> resistanceFunction = DamageEventUtil.createResistanceModifier((LivingEntity) (Object) this, damageSource);
    final Optional<List<DamageFunction>> armorEnchantments = DamageEventUtil.createEnchantmentModifiers((LivingEntity) (Object) this, damageSource);
    final Optional<DamageFunction> absorptionFunction = DamageEventUtil.createAbsorptionModifier((LivingEntity) (Object) this);
    final Optional<DamageFunction> shieldFunction = DamageEventUtil.createShieldFunction((LivingEntity) (Object) this, damageSource, damage);
    hardHatFunction.ifPresent(originalFunctions::add);
    shieldFunction.ifPresent(originalFunctions::add);
    armorFunction.ifPresent(originalFunctions::add);
    resistanceFunction.ifPresent(originalFunctions::add);
    armorEnchantments.ifPresent(originalFunctions::addAll);
    absorptionFunction.ifPresent(originalFunctions::add);
    try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
        DamageEventUtil.generateCauseFor(damageSource, frame);
        final DamageEntityEvent event = SpongeEventFactory.createDamageEntityEvent(frame.currentCause(), (org.spongepowered.api.entity.Entity) this, originalFunctions, originalDamage);
        if (damageSource != SpongeDamageSources.IGNORED) {
            // Basically, don't throw an event if it's our own damage source
            SpongeCommon.post(event);
        }
        if (event.isCancelled()) {
            return false;
        }
        damage = (float) event.finalDamage();
        // Sponge Start - Allow the platform to adjust damage before applying armor/etc
        damage = this.bridge$applyModDamageBeforeFunctions((LivingEntity) (Object) this, damageSource, damage);
        // Sponge End
        // Helmet
        final ItemStack helmet = this.shadow$getItemBySlot(EquipmentSlot.HEAD);
        // without using our mixin redirects in EntityFallingBlockMixin.
        if ((damageSource instanceof FallingBlockDamageSource) || damageSource == DamageSource.ANVIL || damageSource == DamageSource.FALLING_BLOCK && !helmet.isEmpty()) {
            helmet.hurtAndBreak((int) (event.baseDamage() * 4.0F + this.random.nextFloat() * event.baseDamage() * 2.0F), (LivingEntity) (Object) this, (entity) -> entity.broadcastBreakEvent(EquipmentSlot.HEAD));
        }
        boolean hurtStack = false;
        // Shield
        if (shieldFunction.isPresent()) {
            this.shadow$hurtCurrentlyUsedShield((float) event.baseDamage());
            hurtStack = true;
            if (!damageSource.isProjectile()) {
                final Entity entity = damageSource.getDirectEntity();
                if (entity instanceof LivingEntity) {
                    this.shadow$blockUsingShield((LivingEntity) entity);
                }
            }
        }
        // Armor
        if (!damageSource.isBypassArmor() && armorFunction.isPresent()) {
            this.shadow$hurtArmor(damageSource, (float) event.baseDamage());
            hurtStack = true;
        }
        // Sponge start - log inventory change due to taking damage
        if (hurtStack && isHuman) {
            PhaseTracker.SERVER.getPhaseContext().getTransactor().logPlayerInventoryChange((Player) (Object) this, PlayerInventoryTransaction.EventCreator.STANDARD);
            // capture
            ((Player) (Object) this).inventoryMenu.broadcastChanges();
        }
        // Resistance modifier post calculation
        if (resistanceFunction.isPresent()) {
            final float f2 = (float) event.damage(resistanceFunction.get().modifier()) - damage;
            if (f2 > 0.0F && f2 < 3.4028235E37F) {
                if (((LivingEntity) (Object) this) instanceof net.minecraft.server.level.ServerPlayer) {
                    ((net.minecraft.server.level.ServerPlayer) ((LivingEntity) (Object) this)).awardStat(Stats.DAMAGE_RESISTED, Math.round(f2 * 10.0F));
                } else if (damageSource.getEntity() instanceof net.minecraft.server.level.ServerPlayer) {
                    ((net.minecraft.server.level.ServerPlayer) damageSource.getEntity()).awardStat(Stats.DAMAGE_DEALT_RESISTED, Math.round(f2 * 10.0F));
                }
            }
        }
        double absorptionModifier = absorptionFunction.map(function -> event.damage(function.modifier())).orElse(0d);
        if (absorptionFunction.isPresent()) {
            absorptionModifier = event.damage(absorptionFunction.get().modifier());
        }
        final float f = (float) event.finalDamage() - (float) absorptionModifier;
        this.shadow$setAbsorptionAmount(Math.max(this.shadow$getAbsorptionAmount() + (float) absorptionModifier, 0.0F));
        if (f > 0.0F && f < 3.4028235E37F && ((LivingEntity) (Object) this) instanceof net.minecraft.server.level.ServerPlayer) {
            ((Player) (Object) this).awardStat(Stats.DAMAGE_DEALT_ABSORBED, Math.round(f * 10.0F));
        }
        if (damage != 0.0F) {
            if (isHuman) {
                ((Player) (Object) this).causeFoodExhaustion(damageSource.getFoodExhaustion());
            }
            final float f2 = this.shadow$getHealth();
            this.shadow$setHealth(f2 - damage);
            this.shadow$getCombatTracker().recordDamage(damageSource, f2, damage);
            if (isHuman) {
                if (damage < 3.4028235E37F) {
                    ((Player) (Object) this).awardStat(Stats.DAMAGE_TAKEN, Math.round(damage * 10.0F));
                }
                return true;
            }
            this.shadow$setAbsorptionAmount(this.shadow$getAbsorptionAmount() - damage);
        }
        return true;
    }
}
Also used : LivingEntity(net.minecraft.world.entity.LivingEntity) Inject(org.spongepowered.asm.mixin.injection.Inject) SpongeDamageSources(org.spongepowered.common.event.cause.entity.damage.SpongeDamageSources) ParticleOptions(net.minecraft.core.particles.ParticleOptions) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) HandType(org.spongepowered.api.data.type.HandType) PlayerBridge(org.spongepowered.common.bridge.world.entity.player.PlayerBridge) SpongeCommonEventFactory(org.spongepowered.common.event.SpongeCommonEventFactory) CallbackInfoReturnable(org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable) CallbackInfo(org.spongepowered.asm.mixin.injection.callback.CallbackInfo) Mixin(org.spongepowered.asm.mixin.Mixin) DamageEntityEvent(org.spongepowered.api.event.entity.DamageEntityEvent) PlayerInventoryTransaction(org.spongepowered.common.event.tracking.context.transaction.inventory.PlayerInventoryTransaction) Living(org.spongepowered.api.entity.living.Living) AttributeMap(net.minecraft.world.entity.ai.attributes.AttributeMap) Transaction(org.spongepowered.api.data.Transaction) At(org.spongepowered.asm.mixin.injection.At) CombatTracker(net.minecraft.world.damagesource.CombatTracker) Collection(java.util.Collection) Sponge(org.spongepowered.api.Sponge) Player(net.minecraft.world.entity.player.Player) DamageEventUtil(org.spongepowered.common.util.DamageEventUtil) List(java.util.List) BlockPos(net.minecraft.core.BlockPos) SoundEvent(net.minecraft.sounds.SoundEvent) Shadow(org.spongepowered.asm.mixin.Shadow) Optional(java.util.Optional) LivingEntityBridge(org.spongepowered.common.bridge.world.entity.LivingEntityBridge) ItemStack(net.minecraft.world.item.ItemStack) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) MoveEntityEvent(org.spongepowered.api.event.entity.MoveEntityEvent) ServerLocation(org.spongepowered.api.world.server.ServerLocation) HumanEntity(org.spongepowered.common.entity.living.human.HumanEntity) SoundSource(net.minecraft.sounds.SoundSource) EventContextKeys(org.spongepowered.api.event.EventContextKeys) ServerWorld(org.spongepowered.api.world.server.ServerWorld) Constants(org.spongepowered.common.util.Constants) Overwrite(org.spongepowered.asm.mixin.Overwrite) ServerLevel(net.minecraft.server.level.ServerLevel) SleepingEvent(org.spongepowered.api.event.action.SleepingEvent) ArrayList(java.util.ArrayList) AttributeInstance(net.minecraft.world.entity.ai.attributes.AttributeInstance) ItemStackUtil(org.spongepowered.common.item.util.ItemStackUtil) DamageSource(net.minecraft.world.damagesource.DamageSource) Stats(net.minecraft.stats.Stats) CauseStackManager(org.spongepowered.api.event.CauseStackManager) LocalCapture(org.spongepowered.asm.mixin.injection.callback.LocalCapture) Nullable(javax.annotation.Nullable) UseItemStackEvent(org.spongepowered.api.event.item.inventory.UseItemStackEvent) Ticks(org.spongepowered.api.util.Ticks) LevelBridge(org.spongepowered.common.bridge.world.level.LevelBridge) FallingBlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.FallingBlockDamageSource) Redirect(org.spongepowered.asm.mixin.injection.Redirect) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) MobEffectInstance(net.minecraft.world.effect.MobEffectInstance) ShouldFire(org.spongepowered.common.event.ShouldFire) SpongeCommon(org.spongepowered.common.SpongeCommon) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) Attribute(net.minecraft.world.entity.ai.attributes.Attribute) Cause(org.spongepowered.api.event.Cause) MovementTypes(org.spongepowered.api.event.cause.entity.MovementTypes) Entity(net.minecraft.world.entity.Entity) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) Vector3d(org.spongepowered.math.vector.Vector3d) PlatformLivingEntityBridge(org.spongepowered.common.bridge.world.entity.PlatformLivingEntityBridge) EquipmentSlot(net.minecraft.world.entity.EquipmentSlot) VecHelper(org.spongepowered.common.util.VecHelper) InteractionHand(net.minecraft.world.InteractionHand) MobEffect(net.minecraft.world.effect.MobEffect) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) LivingEntity(net.minecraft.world.entity.LivingEntity) HumanEntity(org.spongepowered.common.entity.living.human.HumanEntity) Entity(net.minecraft.world.entity.Entity) ArrayList(java.util.ArrayList) LivingEntity(net.minecraft.world.entity.LivingEntity) FallingBlockDamageSource(org.spongepowered.api.event.cause.entity.damage.source.FallingBlockDamageSource) CauseStackManager(org.spongepowered.api.event.CauseStackManager) DamageFunction(org.spongepowered.api.event.cause.entity.damage.DamageFunction) List(java.util.List) ArrayList(java.util.ArrayList) DamageEntityEvent(org.spongepowered.api.event.entity.DamageEntityEvent) Player(net.minecraft.world.entity.player.Player) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) ItemStack(net.minecraft.world.item.ItemStack)

Example 12 with LivingEntity

use of net.minecraft.world.entity.LivingEntity in project MinecraftForge by MinecraftForge.

the class ForgeEventFactory method fireSleepingLocationCheck.

public static boolean fireSleepingLocationCheck(LivingEntity player, BlockPos sleepingLocation) {
    SleepingLocationCheckEvent evt = new SleepingLocationCheckEvent(player, sleepingLocation);
    MinecraftForge.EVENT_BUS.post(evt);
    Result canContinueSleep = evt.getResult();
    if (canContinueSleep == Result.DEFAULT) {
        return player.getSleepingPos().map(pos -> {
            BlockState state = player.level.getBlockState(pos);
            return state.getBlock().isBed(state, player.level, pos, player);
        }).orElse(false);
    } else
        return canContinueSleep == Result.ALLOW;
}
Also used : EntityTeleportEvent(net.minecraftforge.event.entity.EntityTeleportEvent) CommandDispatcher(com.mojang.brigadier.CommandDispatcher) ProjectileImpactEvent(net.minecraftforge.event.entity.ProjectileImpactEvent) GameProfile(com.mojang.authlib.GameProfile) FurnaceFuelBurnTimeEvent(net.minecraftforge.event.furnace.FurnaceFuelBurnTimeEvent) UseHoeEvent(net.minecraftforge.event.entity.player.UseHoeEvent) PlayerList(net.minecraft.server.players.PlayerList) Dist(net.minecraftforge.api.distmarker.Dist) FireworkRocketEntity(net.minecraft.world.entity.projectile.FireworkRocketEntity) Zombie(net.minecraft.world.entity.monster.Zombie) Pose(net.minecraft.world.entity.Pose) MobSpawnSettings(net.minecraft.world.level.biome.MobSpawnSettings) CapabilityDispatcher(net.minecraftforge.common.capabilities.CapabilityDispatcher) EntityPlaceEvent(net.minecraftforge.event.world.BlockEvent.EntityPlaceEvent) Projectile(net.minecraft.world.entity.projectile.Projectile) PreparableReloadListener(net.minecraft.server.packs.resources.PreparableReloadListener) FillBucketEvent(net.minecraftforge.event.entity.player.FillBucketEvent) BonemealEvent(net.minecraftforge.event.entity.player.BonemealEvent) OverlayType(net.minecraftforge.client.event.RenderBlockOverlayEvent.OverlayType) RecipeType(net.minecraft.world.item.crafting.RecipeType) EntityItemPickupEvent(net.minecraftforge.event.entity.player.EntityItemPickupEvent) RenderBlockOverlayEvent(net.minecraftforge.client.event.RenderBlockOverlayEvent) LogicalSide(net.minecraftforge.fml.LogicalSide) PlayerEvent(net.minecraftforge.event.entity.player.PlayerEvent) LivingHealEvent(net.minecraftforge.event.entity.living.LivingHealEvent) BlockEvent(net.minecraftforge.event.world.BlockEvent) ThrownEnderpearl(net.minecraft.world.entity.projectile.ThrownEnderpearl) InteractionResult(net.minecraft.world.InteractionResult) LivingConversionEvent(net.minecraftforge.event.entity.living.LivingConversionEvent) BaseSpawner(net.minecraft.world.level.BaseSpawner) ItemTooltipEvent(net.minecraftforge.event.entity.player.ItemTooltipEvent) PistonEvent(net.minecraftforge.event.world.PistonEvent) ItemStack(net.minecraft.world.item.ItemStack) SoundSource(net.minecraft.sounds.SoundSource) java.util(java.util) OnlyIn(net.minecraftforge.api.distmarker.OnlyIn) CommandSourceStack(net.minecraft.commands.CommandSourceStack) ICapabilityProvider(net.minecraftforge.common.capabilities.ICapabilityProvider) EntityType(net.minecraft.world.entity.EntityType) SleepingLocationCheckEvent(net.minecraftforge.event.entity.player.SleepingLocationCheckEvent) BlockState(net.minecraft.world.level.block.state.BlockState) ArrowLooseEvent(net.minecraftforge.event.entity.player.ArrowLooseEvent) EntityMobGriefingEvent(net.minecraftforge.event.entity.EntityMobGriefingEvent) CreateFluidSourceEvent(net.minecraftforge.event.world.BlockEvent.CreateFluidSourceEvent) ServerPlayer(net.minecraft.server.level.ServerPlayer) Container(net.minecraft.world.Container) ClientChatEvent(net.minecraftforge.client.event.ClientChatEvent) ToolAction(net.minecraftforge.common.ToolAction) PlaySoundAtEntityEvent(net.minecraftforge.event.entity.PlaySoundAtEntityEvent) PlayerDestroyItemEvent(net.minecraftforge.event.entity.player.PlayerDestroyItemEvent) UseOnContext(net.minecraft.world.item.context.UseOnContext) Nullable(javax.annotation.Nullable) ExplosionEvent(net.minecraftforge.event.world.ExplosionEvent) LightningBolt(net.minecraft.world.entity.LightningBolt) Component(net.minecraft.network.chat.Component) PlayerSetSpawnEvent(net.minecraftforge.event.entity.player.PlayerSetSpawnEvent) Animal(net.minecraft.world.entity.animal.Animal) LivingDestroyBlockEvent(net.minecraftforge.event.entity.living.LivingDestroyBlockEvent) File(java.io.File) SaplingGrowTreeEvent(net.minecraftforge.event.world.SaplingGrowTreeEvent) ArrowNockEvent(net.minecraftforge.event.entity.player.ArrowNockEvent) ChunkPos(net.minecraft.world.level.ChunkPos) MinecraftForge(net.minecraftforge.common.MinecraftForge) Result(net.minecraftforge.eventbus.api.Event.Result) AnimalTameEvent(net.minecraftforge.event.entity.living.AnimalTameEvent) EntityDimensions(net.minecraft.world.entity.EntityDimensions) TooltipFlag(net.minecraft.world.item.TooltipFlag) AllowDespawn(net.minecraftforge.event.entity.living.LivingSpawnEvent.AllowDespawn) InteractionHand(net.minecraft.world.InteractionHand) EntityEvent(net.minecraftforge.event.entity.EntityEvent) Mob(net.minecraft.world.entity.Mob) ResourceLocation(net.minecraft.resources.ResourceLocation) LivingEntity(net.minecraft.world.entity.LivingEntity) PlayerWakeUpEvent(net.minecraftforge.event.entity.player.PlayerWakeUpEvent) InteractionResultHolder(net.minecraft.world.InteractionResultHolder) Direction(net.minecraft.core.Direction) ItemExpireEvent(net.minecraftforge.event.entity.item.ItemExpireEvent) MinecraftForgeClient(net.minecraftforge.client.MinecraftForgeClient) PortalShape(net.minecraft.world.level.portal.PortalShape) LootTables(net.minecraft.world.level.storage.loot.LootTables) NeighborNotifyEvent(net.minecraftforge.event.world.BlockEvent.NeighborNotifyEvent) PlayerDataStorage(net.minecraft.world.level.storage.PlayerDataStorage) LivingExperienceDropEvent(net.minecraftforge.event.entity.living.LivingExperienceDropEvent) PlayerSleepInBedEvent(net.minecraftforge.event.entity.player.PlayerSleepInBedEvent) Event(net.minecraftforge.eventbus.api.Event) NonNullList(net.minecraft.core.NonNullList) LivingPackSizeEvent(net.minecraftforge.event.entity.living.LivingPackSizeEvent) WorldEvent(net.minecraftforge.event.world.WorldEvent) PlayerFlyableFallEvent(net.minecraftforge.event.entity.player.PlayerFlyableFallEvent) Commands(net.minecraft.commands.Commands) BedSleepingProblem(net.minecraft.world.entity.player.Player.BedSleepingProblem) GameRules(net.minecraft.world.level.GameRules) PermissionsChangedEvent(net.minecraftforge.event.entity.player.PermissionsChangedEvent) ChatType(net.minecraft.network.chat.ChatType) LevelReader(net.minecraft.world.level.LevelReader) Player(net.minecraft.world.entity.player.Player) Blocks(net.minecraft.world.level.block.Blocks) PotionBrewEvent(net.minecraftforge.event.brewing.PotionBrewEvent) ItemEntity(net.minecraft.world.entity.item.ItemEntity) SoundEvent(net.minecraft.sounds.SoundEvent) BlockPos(net.minecraft.core.BlockPos) BlockToolInteractEvent(net.minecraftforge.event.world.BlockEvent.BlockToolInteractEvent) LevelAccessor(net.minecraft.world.level.LevelAccessor) ClientChatReceivedEvent(net.minecraftforge.client.event.ClientChatReceivedEvent) Level(net.minecraft.world.level.Level) SummonAidEvent(net.minecraftforge.event.entity.living.ZombieEvent.SummonAidEvent) MobSpawnType(net.minecraft.world.entity.MobSpawnType) ServerLevelData(net.minecraft.world.level.storage.ServerLevelData) ServerLevel(net.minecraft.server.level.ServerLevel) EntityMountEvent(net.minecraftforge.event.entity.EntityMountEvent) PoseStack(com.mojang.blaze3d.vertex.PoseStack) MobCategory(net.minecraft.world.entity.MobCategory) LivingEntityUseItemEvent(net.minecraftforge.event.entity.living.LivingEntityUseItemEvent) LootTable(net.minecraft.world.level.storage.loot.LootTable) ServerResources(net.minecraft.server.ServerResources) SleepingTimeCheckEvent(net.minecraftforge.event.entity.player.SleepingTimeCheckEvent) Nonnull(javax.annotation.Nonnull) BlockSnapshot(net.minecraftforge.common.util.BlockSnapshot) EntityMultiPlaceEvent(net.minecraftforge.event.world.BlockEvent.EntityMultiPlaceEvent) ResourceKey(net.minecraft.resources.ResourceKey) Explosion(net.minecraft.world.level.Explosion) EntityStruckByLightningEvent(net.minecraftforge.event.entity.EntityStruckByLightningEvent) Consumer(java.util.function.Consumer) HitResult(net.minecraft.world.phys.HitResult) Entity(net.minecraft.world.entity.Entity) LivingSpawnEvent(net.minecraftforge.event.entity.living.LivingSpawnEvent) SleepFinishedTimeEvent(net.minecraftforge.event.world.SleepFinishedTimeEvent) PlayerBrewedPotionEvent(net.minecraftforge.event.brewing.PlayerBrewedPotionEvent) ChunkWatchEvent(net.minecraftforge.event.world.ChunkWatchEvent) BlockState(net.minecraft.world.level.block.state.BlockState) SleepingLocationCheckEvent(net.minecraftforge.event.entity.player.SleepingLocationCheckEvent) InteractionResult(net.minecraft.world.InteractionResult) Result(net.minecraftforge.eventbus.api.Event.Result) HitResult(net.minecraft.world.phys.HitResult)

Example 13 with LivingEntity

use of net.minecraft.world.entity.LivingEntity in project MyPet by xXKeyleXx.

the class Sprint method shouldStart.

@Override
public boolean shouldStart() {
    if (!myPet.getSkills().isActive(SprintImpl.class)) {
        return false;
    }
    if (petEntity.getMyPet().getDamage() <= 0) {
        return false;
    }
    if (!this.petEntity.hasTarget()) {
        return false;
    }
    LivingEntity targetEntity = ((CraftLivingEntity) this.petEntity.getMyPetTarget()).getHandle();
    if (!targetEntity.isAlive()) {
        return false;
    }
    if (lastTarget == targetEntity) {
        return false;
    }
    if (petEntity.getMyPet().getRangedDamage() > 0 && this.petEntity.distanceToSqr(targetEntity) >= 16) {
        return false;
    }
    this.lastTarget = targetEntity;
    return true;
}
Also used : LivingEntity(net.minecraft.world.entity.LivingEntity) CraftLivingEntity(org.bukkit.craftbukkit.v1_18_R1.entity.CraftLivingEntity) SprintImpl(de.Keyle.MyPet.skill.skills.SprintImpl) CraftLivingEntity(org.bukkit.craftbukkit.v1_18_R1.entity.CraftLivingEntity)

Example 14 with LivingEntity

use of net.minecraft.world.entity.LivingEntity in project MyPet by xXKeyleXx.

the class RangedAttack method shouldStart.

@Override
public boolean shouldStart() {
    if (myPet.getRangedDamage() <= 0) {
        return false;
    }
    if (!entityMyPet.canMove()) {
        return false;
    }
    if (!entityMyPet.hasTarget()) {
        return false;
    }
    LivingEntity target = ((CraftLivingEntity) this.entityMyPet.getMyPetTarget()).getHandle();
    if (target instanceof ArmorStand) {
        return false;
    }
    double meleeDamage = myPet.getDamage();
    if (meleeDamage > 0 && this.entityMyPet.distanceToSqr(target.getX(), target.getBoundingBox().minY, target.getZ()) < 4) {
        Ranged rangedSkill = myPet.getSkills().get(Ranged.class);
        if (meleeDamage > rangedSkill.getDamage().getValue().doubleValue()) {
            return false;
        }
    }
    Behavior behaviorSkill = myPet.getSkills().get(Behavior.class);
    if (behaviorSkill != null && behaviorSkill.isActive()) {
        if (behaviorSkill.getBehavior() == Behavior.BehaviorMode.Friendly) {
            return false;
        }
        if (behaviorSkill.getBehavior() == Behavior.BehaviorMode.Raid) {
            if (target instanceof TamableAnimal && ((TamableAnimal) target).isTame()) {
                return false;
            }
            if (target instanceof EntityMyPet) {
                return false;
            }
            if (target instanceof ServerPlayer) {
                return false;
            }
        }
    }
    this.target = target;
    return true;
}
Also used : LivingEntity(net.minecraft.world.entity.LivingEntity) CraftLivingEntity(org.bukkit.craftbukkit.v1_18_R1.entity.CraftLivingEntity) Ranged(de.Keyle.MyPet.api.skill.skills.Ranged) EntityMyPet(de.Keyle.MyPet.compat.v1_18_R1.entity.EntityMyPet) ArmorStand(net.minecraft.world.entity.decoration.ArmorStand) TamableAnimal(net.minecraft.world.entity.TamableAnimal) ServerPlayer(net.minecraft.server.level.ServerPlayer) CraftLivingEntity(org.bukkit.craftbukkit.v1_18_R1.entity.CraftLivingEntity) Behavior(de.Keyle.MyPet.api.skill.skills.Behavior)

Example 15 with LivingEntity

use of net.minecraft.world.entity.LivingEntity in project MyPet by xXKeyleXx.

the class FollowOwner method applyWalkSpeed.

private void applyWalkSpeed() {
    float walkSpeed = owner.getAbilities().walkingSpeed;
    if (owner.getAbilities().flying) {
        // make the pet faster when the player is flying
        walkSpeed += owner.getAbilities().flyingSpeed;
    } else if (owner.isSprinting()) {
        // make the pet faster when the player is sprinting
        if (owner.getAttributes().getInstance(Attributes.MOVEMENT_SPEED) != null) {
            walkSpeed += owner.getAttributes().getInstance(Attributes.MOVEMENT_SPEED).getValue();
        }
    } else if (owner.isPassenger() && owner.getVehicle() instanceof LivingEntity) {
        // adjust the speed to the pet can catch up with the vehicle the player is in
        AttributeInstance vehicleSpeedAttribute = ((LivingEntity) owner.getVehicle()).getAttributes().getInstance(Attributes.MOVEMENT_SPEED);
        if (vehicleSpeedAttribute != null) {
            walkSpeed = (float) vehicleSpeedAttribute.getValue();
        }
    } else if (owner.hasEffect(MobEffects.MOVEMENT_SPEED)) {
        // make the pet faster when the player is has the SPEED effect
        walkSpeed += owner.getEffect(MobEffects.MOVEMENT_SPEED).getAmplifier() * 0.2 * walkSpeed;
    }
    // make aquatic pets faster - swimming is hard
    if (this.petEntity.isInWaterOrBubble() && this.petEntity.getNavigation() instanceof MyAquaticPetPathNavigation) {
        walkSpeed += 0.6f;
        if (owner.isSwimming()) {
            walkSpeed -= 0.035f;
        }
        if (owner.hasEffect(MobEffects.DOLPHINS_GRACE)) {
            walkSpeed += 0.08f;
        }
    }
    // make the pet a little bit faster than the player so it can catch up
    walkSpeed += 0.07f;
    nav.getParameters().addSpeedModifier("FollowOwner", walkSpeed);
}
Also used : LivingEntity(net.minecraft.world.entity.LivingEntity) MyAquaticPetPathNavigation(de.Keyle.MyPet.compat.v1_18_R1.entity.ai.navigation.MyAquaticPetPathNavigation) AttributeInstance(net.minecraft.world.entity.ai.attributes.AttributeInstance)

Aggregations

LivingEntity (net.minecraft.world.entity.LivingEntity)32 Entity (net.minecraft.world.entity.Entity)15 Player (net.minecraft.world.entity.player.Player)11 DamageSource (net.minecraft.world.damagesource.DamageSource)10 ResourceLocation (net.minecraft.resources.ResourceLocation)9 ArrayList (java.util.ArrayList)8 BlockPos (net.minecraft.core.BlockPos)7 EquipmentSlot (net.minecraft.world.entity.EquipmentSlot)7 CauseStackManager (org.spongepowered.api.event.CauseStackManager)7 Overwrite (org.spongepowered.asm.mixin.Overwrite)7 List (java.util.List)6 Cause (org.spongepowered.api.event.Cause)6 DamageFunction (org.spongepowered.api.event.cause.entity.damage.DamageFunction)6 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)6 ServerWorld (org.spongepowered.api.world.server.ServerWorld)6 ItemStackUtil (org.spongepowered.common.item.util.ItemStackUtil)6 Collection (java.util.Collection)5 ServerLevel (net.minecraft.server.level.ServerLevel)5 ServerPlayer (net.minecraft.server.level.ServerPlayer)5 MobType (net.minecraft.world.entity.MobType)5