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;
}
use of net.minecraft.world.entity.LivingEntity in project MyPet by xXKeyleXx.
the class BehaviorDuelTarget method shouldFinish.
@Override
public boolean shouldFinish() {
if (!petEntity.canMove()) {
return true;
} else if (!petEntity.hasTarget()) {
return true;
}
LivingEntity target = ((CraftLivingEntity) this.petEntity.getMyPetTarget()).getHandle();
Behavior behaviorSkill = myPet.getSkills().get(Behavior.class);
if (behaviorSkill.getBehavior() != BehaviorMode.Duel) {
return true;
} else if (myPet.getDamage() <= 0 && myPet.getRangedDamage() <= 0) {
return true;
} else if (target.level != petEntity.level) {
return true;
} else if (petEntity.distanceToSqr(target) > 400) {
return true;
} else
return petEntity.distanceToSqr(((CraftPlayer) petEntity.getOwner().getPlayer()).getHandle()) > 600;
}
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);
}
use of net.minecraft.world.entity.LivingEntity in project SpongeCommon by SpongePowered.
the class EntityData method register.
// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
registrator.asMutable(Entity.class).create(Keys.AGE).get(h -> h.tickCount).setAnd((h, v) -> {
if (v < 0) {
return false;
}
h.tickCount = v;
return true;
}).create(Keys.BASE_SIZE).get(h -> (double) h.getBbWidth()).create(Keys.BASE_VEHICLE).get(h -> {
final Entity rootVehicle = h.getRootVehicle();
if (rootVehicle == h) {
return null;
}
return (org.spongepowered.api.entity.Entity) rootVehicle;
}).create(Keys.CUSTOM_NAME).get(h -> h.hasCustomName() ? SpongeAdventure.asAdventure(h.getCustomName()) : null).set((h, v) -> h.setCustomName(SpongeAdventure.asVanilla(v))).delete(h -> {
h.setCustomName(null);
h.setCustomNameVisible(false);
}).create(Keys.DISPLAY_NAME).get(h -> SpongeAdventure.asAdventure(h.getDisplayName())).create(Keys.EYE_HEIGHT).get(h -> (double) h.getEyeHeight()).create(Keys.EYE_POSITION).get(h -> VecHelper.toVector3d(h.getEyePosition(1f))).create(Keys.FALL_DISTANCE).get(h -> (double) h.fallDistance).setAnd((h, v) -> {
if (v < 0) {
return false;
}
h.fallDistance = v.floatValue();
return true;
}).create(Keys.FIRE_DAMAGE_DELAY).get(h -> new SpongeTicks(((EntityAccessor) h).invoker$getFireImmuneTicks())).setAnd((h, v) -> {
final int ticks = (int) v.ticks();
if (ticks < 1 || ticks > Short.MAX_VALUE) {
return false;
}
((EntityBridge) h).bridge$setFireImmuneTicks(ticks);
return ((EntityAccessor) h).invoker$getFireImmuneTicks() == ticks;
}).create(Keys.FIRE_TICKS).get(h -> ((EntityAccessor) h).accessor$remainingFireTicks() > 0 ? Ticks.of(((EntityAccessor) h).accessor$remainingFireTicks()) : null).set((h, v) -> {
final int ticks = (int) v.ticks();
((EntityAccessor) h).accessor$remainingFireTicks(Math.max(ticks, Constants.Entity.MINIMUM_FIRE_TICKS));
}).deleteAndGet(h -> {
final EntityAccessor accessor = (EntityAccessor) h;
final int ticks = accessor.accessor$remainingFireTicks();
if (ticks < Constants.Entity.MINIMUM_FIRE_TICKS) {
return DataTransactionResult.failNoData();
}
final DataTransactionResult.Builder dtrBuilder = DataTransactionResult.builder();
dtrBuilder.replace(Value.immutableOf(Keys.FIRE_TICKS, new SpongeTicks(ticks)));
dtrBuilder.replace(Value.immutableOf(Keys.FIRE_DAMAGE_DELAY, new SpongeTicks(((EntityAccessor) h).invoker$getFireImmuneTicks())));
h.clearFire();
return dtrBuilder.result(DataTransactionResult.Type.SUCCESS).build();
}).create(Keys.HEIGHT).get(h -> (double) h.getBbHeight()).create(Keys.INVULNERABILITY_TICKS).get(h -> new SpongeTicks(h.invulnerableTime)).setAnd((h, v) -> {
final int ticks = (int) v.ticks();
if (ticks < 0) {
return false;
}
h.invulnerableTime = ticks;
if (h instanceof LivingEntity) {
((LivingEntity) h).hurtTime = ticks;
}
return true;
}).create(Keys.IS_CUSTOM_NAME_VISIBLE).get(Entity::isCustomNameVisible).set(Entity::setCustomNameVisible).create(Keys.IS_FLYING).get(h -> h.hasImpulse).set((h, v) -> h.hasImpulse = v).supports(h -> !(h instanceof Player)).create(Keys.IS_GLOWING).get(Entity::isGlowing).set(Entity::setGlowing).create(Keys.IS_GRAVITY_AFFECTED).get(h -> !h.isNoGravity()).set((h, v) -> h.setNoGravity(!v)).create(Keys.IS_SNEAKING).get(Entity::isShiftKeyDown).set(Entity::setShiftKeyDown).create(Keys.IS_SPRINTING).get(Entity::isSprinting).set(Entity::setSprinting).create(Keys.IS_SILENT).get(Entity::isSilent).set(Entity::setSilent).create(Keys.IS_WET).get(Entity::isInWaterOrRain).create(Keys.ON_GROUND).get(Entity::isOnGround).create(Keys.PASSENGERS).get(h -> h.getPassengers().stream().map(org.spongepowered.api.entity.Entity.class::cast).collect(Collectors.toList())).set((h, v) -> {
h.ejectPassengers();
v.forEach(v1 -> ((Entity) v1).startRiding(h, true));
}).create(Keys.REMAINING_AIR).get(h -> Math.max(0, h.getAirSupply())).setAnd((h, v) -> {
if (v < 0 || v > h.getMaxAirSupply()) {
return false;
}
if (v == 0 && h.getAirSupply() < 0) {
return false;
}
h.setAirSupply(v);
return true;
}).create(Keys.SCALE).get(h -> 1d).create(Keys.SCOREBOARD_TAGS).get(Entity::getTags).set((h, v) -> {
h.getTags().clear();
h.getTags().addAll(v);
}).create(Keys.TRANSIENT).get(h -> ((EntityAccessor) h).invoker$getEncodeId() == null).set((h, v) -> ((EntityBridge) h).bridge$setTransient(v)).create(Keys.VEHICLE).get(h -> (org.spongepowered.api.entity.Entity) h.getVehicle()).set((h, v) -> h.startRiding((Entity) v, true)).create(Keys.VELOCITY).get(h -> VecHelper.toVector3d(h.getDeltaMovement())).set((h, v) -> h.setDeltaMovement(VecHelper.toVanillaVector3d(v))).create(Keys.SWIFTNESS).get(m -> m.getDeltaMovement().length()).set((m, v) -> m.setDeltaMovement(m.getDeltaMovement().normalize().scale(v))).supports(m -> m.getDeltaMovement().lengthSqr() > 0).asMutable(EntityMaxAirBridge.class).create(Keys.MAX_AIR).get(EntityMaxAirBridge::bridge$getMaxAir).set(EntityMaxAirBridge::bridge$setMaxAir);
registrator.spongeDataStore(ResourceKey.sponge("max_air"), EntityMaxAirBridge.class, Keys.MAX_AIR);
registrator.newDataStore(SpongeEntitySnapshot.class, SpongeEntityArchetype.class).dataStore(Keys.CUSTOM_NAME, (dv, v) -> dv.set(Constants.Entity.CUSTOM_NAME, GsonComponentSerializer.gson().serialize(v)), dv -> dv.getString(Constants.Entity.CUSTOM_NAME).map(GsonComponentSerializer.gson()::deserialize));
// @formatter:on
}
use of net.minecraft.world.entity.LivingEntity in project SpongeCommon by SpongePowered.
the class ExplosionMixin method explode.
/**
* @author gabizou
* @author zidane
* @reason Fire ExplosionEvent.Detonate
*/
@Overwrite
public void explode() {
// Sponge Start - Do not run calculation logic on client thread
if (this.level.isClientSide) {
return;
}
// Sponge Start - If the explosion should not break blocks, don't bother calculating it on server thread
if (this.impl$shouldBreakBlocks) {
final Set<BlockPos> set = Sets.newHashSet();
final int i = 16;
for (int j = 0; j < 16; ++j) {
for (int k = 0; k < 16; ++k) {
for (int l = 0; l < 16; ++l) {
if (j == 0 || j == 15 || k == 0 || k == 15 || l == 0 || l == 15) {
double d0 = (double) ((float) j / 15.0F * 2.0F - 1.0F);
double d1 = (double) ((float) k / 15.0F * 2.0F - 1.0F);
double d2 = (double) ((float) l / 15.0F * 2.0F - 1.0F);
final double d3 = Math.sqrt(d0 * d0 + d1 * d1 + d2 * d2);
d0 = d0 / d3;
d1 = d1 / d3;
d2 = d2 / d3;
float f = this.radius * (0.7F + this.level.random.nextFloat() * 0.6F);
double d4 = this.x;
double d6 = this.y;
double d8 = this.z;
for (final float f1 = 0.3F; f > 0.0F; f -= 0.22500001F) {
final BlockPos blockpos = new BlockPos(d4, d6, d8);
final BlockState blockstate = this.level.getBlockState(blockpos);
final FluidState fluidstate = this.level.getFluidState(blockpos);
Optional<Float> optional = this.damageCalculator.getBlockExplosionResistance((net.minecraft.world.level.Explosion) (Object) this, this.level, blockpos, blockstate, fluidstate);
if (optional.isPresent()) {
f -= (optional.get() + 0.3F) * 0.3F;
}
if (f > 0.0F && this.damageCalculator.shouldBlockExplode((net.minecraft.world.level.Explosion) (Object) this, this.level, blockpos, blockstate, f)) {
set.add(blockpos);
}
d4 += d0 * (double) 0.3F;
d6 += d1 * (double) 0.3F;
d8 += d2 * (double) 0.3F;
}
}
}
}
}
this.toBlow.addAll(set);
}
// Sponge End
final float f3 = this.radius * 2.0F;
final int k1 = Mth.floor(this.x - (double) f3 - 1.0D);
final int l1 = Mth.floor(this.x + (double) f3 + 1.0D);
final int i2 = Mth.floor(this.y - (double) f3 - 1.0D);
final int i1 = Mth.floor(this.y + (double) f3 + 1.0D);
final int j2 = Mth.floor(this.z - (double) f3 - 1.0D);
final int j1 = Mth.floor(this.z + (double) f3 + 1.0D);
// Sponge Start - Only query for entities if we're to damage them
final List<Entity> list = this.impl$shouldDamageEntities ? this.level.getEntities(this.source, new AABB((double) k1, (double) i2, (double) j2, (double) l1, (double) i1, (double) j1)) : Collections.emptyList();
if (ShouldFire.EXPLOSION_EVENT_DETONATE) {
final List<ServerLocation> blockPositions = new ArrayList<>(this.toBlow.size());
final List<org.spongepowered.api.entity.Entity> entities = new ArrayList<>(list.size());
for (final BlockPos pos : this.toBlow) {
blockPositions.add(ServerLocation.of((org.spongepowered.api.world.server.ServerWorld) this.level, pos.getX(), pos.getY(), pos.getZ()));
}
for (final Entity entity : list) {
// Make sure to check the entity is immune first.
if (!entity.ignoreExplosion()) {
entities.add((org.spongepowered.api.entity.Entity) entity);
}
}
final Cause cause = PhaseTracker.getCauseStackManager().currentCause();
final ExplosionEvent.Detonate detonate = SpongeEventFactory.createExplosionEventDetonate(cause, blockPositions, entities, (Explosion) this, (org.spongepowered.api.world.server.ServerWorld) this.level);
SpongeCommon.post(detonate);
// Clear the positions so that they can be pulled from the event
this.toBlow.clear();
if (detonate.isCancelled()) {
return;
}
if (this.impl$shouldBreakBlocks) {
for (final ServerLocation worldLocation : detonate.affectedLocations()) {
this.toBlow.add(VecHelper.toBlockPos(worldLocation));
}
}
// Clear the list of entities so they can be pulled from the event.
list.clear();
if (this.impl$shouldDamageEntities) {
for (final org.spongepowered.api.entity.Entity entity : detonate.entities()) {
try {
list.add((Entity) entity);
} catch (final Exception e) {
// Do nothing, a plugin tried to use the wrong entity somehow.
}
}
}
}
// Sponge End
final Vec3 vec3d = new Vec3(this.x, this.y, this.z);
for (int k2 = 0; k2 < list.size(); ++k2) {
final Entity entity = list.get(k2);
if (!entity.ignoreExplosion()) {
final double d12 = (double) (Mth.sqrt(entity.distanceToSqr(vec3d)) / f3);
if (d12 <= 1.0D) {
double d5 = entity.getX() - this.x;
double d7 = entity.getEyeY() - this.y;
double d9 = entity.getZ() - this.z;
final double d13 = (double) Mth.sqrt(d5 * d5 + d7 * d7 + d9 * d9);
if (d13 != 0.0D) {
d5 = d5 / d13;
d7 = d7 / d13;
d9 = d9 / d13;
final double d14 = (double) net.minecraft.world.level.Explosion.getSeenPercent(vec3d, entity);
final double d10 = (1.0D - d12) * d14;
entity.hurt(this.shadow$getDamageSource(), (float) ((int) ((d10 * d10 + d10) / 2.0D * 7.0D * (double) f3 + 1.0D)));
double d11 = d10;
if (entity instanceof LivingEntity) {
d11 = ProtectionEnchantment.getExplosionKnockbackAfterDampener((LivingEntity) entity, d10);
}
// Sponge Start - Honor our knockback value from event
entity.setDeltaMovement(entity.getDeltaMovement().add(d5 * d11 * this.impl$knockback, d7 * d11 * this.impl$knockback, d9 * d11 * this.impl$knockback));
if (entity instanceof Player) {
final Player playerentity = (Player) entity;
if (!playerentity.isSpectator() && (!playerentity.isCreative() || !playerentity.abilities.flying)) {
this.hitPlayers.put(playerentity, new Vec3(d5 * d10 * this.impl$knockback, d7 * d10 * this.impl$knockback, d9 * d10 * this.impl$knockback));
}
}
// Sponge End
}
}
}
}
}
Aggregations