| 1 | package net.minecraft.world.entity.projectile.arrow;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
|
| 6 | import java.util.ArrayList;
|
| 7 | import java.util.Collection;
|
| 8 | import java.util.Comparator;
|
| 9 | import java.util.List;
|
| 10 | import java.util.Objects;
|
| 11 | import net.minecraft.advancements.triggers.CriteriaTriggers;
|
| 12 | import net.minecraft.core.BlockPos;
|
| 13 | import net.minecraft.core.component.DataComponents;
|
| 14 | import net.minecraft.core.particles.ParticleTypes;
|
| 15 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 16 | import net.minecraft.network.protocol.game.ClientboundGameEventPacket;
|
| 17 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 18 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 19 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 20 | import net.minecraft.server.level.ServerLevel;
|
| 21 | import net.minecraft.server.level.ServerPlayer;
|
| 22 | import net.minecraft.sounds.SoundEvent;
|
| 23 | import net.minecraft.sounds.SoundEvents;
|
| 24 | import net.minecraft.tags.EntityTypeTags;
|
| 25 | import net.minecraft.util.Mth;
|
| 26 | import net.minecraft.util.Unit;
|
| 27 | import net.minecraft.world.damagesource.DamageSource;
|
| 28 | import net.minecraft.world.entity.Entity;
|
| 29 | import net.minecraft.world.entity.EntityType;
|
| 30 | import net.minecraft.world.entity.LivingEntity;
|
| 31 | import net.minecraft.world.entity.MoverType;
|
| 32 | import net.minecraft.world.entity.OminousItemSpawner;
|
| 33 | import net.minecraft.world.entity.SlotAccess;
|
| 34 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 35 | import net.minecraft.world.entity.player.Player;
|
| 36 | import net.minecraft.world.entity.projectile.Projectile;
|
| 37 | import net.minecraft.world.entity.projectile.ProjectileDeflection;
|
| 38 | import net.minecraft.world.entity.projectile.ProjectileUtil;
|
| 39 | import net.minecraft.world.item.ItemStack;
|
| 40 | import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
| 41 | import net.minecraft.world.level.ClipContext;
|
| 42 | import net.minecraft.world.level.Level;
|
| 43 | import net.minecraft.world.level.block.state.BlockState;
|
| 44 | import net.minecraft.world.level.storage.ValueInput;
|
| 45 | import net.minecraft.world.level.storage.ValueOutput;
|
| 46 | import net.minecraft.world.phys.AABB;
|
| 47 | import net.minecraft.world.phys.BlockHitResult;
|
| 48 | import net.minecraft.world.phys.EntityHitResult;
|
| 49 | import net.minecraft.world.phys.HitResult;
|
| 50 | import net.minecraft.world.phys.Vec3;
|
| 51 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 52 | import org.jspecify.annotations.Nullable;
|
| 53 |
|
| 54 | public abstract class AbstractArrow extends Projectile {
|
| 55 | private static final double ARROW_BASE_DAMAGE = 2.0;
|
| 56 | private static final int SHAKE_TIME = 7;
|
| 57 | private static final float WATER_INERTIA = 0.6F;
|
| 58 | private static final float INERTIA = 0.99F;
|
| 59 | private static final short DEFAULT_LIFE = 0;
|
| 60 | private static final byte DEFAULT_SHAKE = 0;
|
| 61 | private static final boolean DEFAULT_IN_GROUND = false;
|
| 62 | private static final boolean DEFAULT_CRIT = false;
|
| 63 | private static final byte DEFAULT_PIERCE_LEVEL = 0;
|
| 64 | private static final EntityDataAccessor<Byte> ID_FLAGS = SynchedEntityData.defineId(AbstractArrow.class, EntityDataSerializers.BYTE);
|
| 65 | private static final EntityDataAccessor<Byte> PIERCE_LEVEL = SynchedEntityData.defineId(AbstractArrow.class, EntityDataSerializers.BYTE);
|
| 66 | private static final EntityDataAccessor<Boolean> IN_GROUND = SynchedEntityData.defineId(AbstractArrow.class, EntityDataSerializers.BOOLEAN);
|
| 67 | private static final int FLAG_CRIT = 1;
|
| 68 | private static final int FLAG_NOPHYSICS = 2;
|
| 69 | private @Nullable BlockState lastState;
|
| 70 | protected int inGroundTime;
|
| 71 | public AbstractArrow.Pickup pickup = AbstractArrow.Pickup.DISALLOWED;
|
| 72 | public int shakeTime = 0;
|
| 73 | private int life = 0;
|
| 74 | private double baseDamage = 2.0;
|
| 75 | private SoundEvent soundEvent = this.getDefaultHitGroundSoundEvent();
|
| 76 | private @Nullable IntOpenHashSet piercingIgnoreEntityIds;
|
| 77 | private @Nullable List<Entity> piercedAndKilledEntities;
|
| 78 | private ItemStack pickupItemStack = this.getDefaultPickupItem();
|
| 79 | private @Nullable ItemStack firedFromWeapon = null;
|
| 80 |
|
| 81 | protected AbstractArrow(final EntityType<? extends AbstractArrow> type, final Level level) {
|
| 82 | super(type, level);
|
| 83 | }
|
| 84 |
|
| 85 | protected AbstractArrow(
|
| 86 | final EntityType<? extends AbstractArrow> type,
|
| 87 | final double x,
|
| 88 | final double y,
|
| 89 | final double z,
|
| 90 | final Level level,
|
| 91 | final ItemStack pickupItemStack,
|
| 92 | final @Nullable ItemStack firedFromWeapon
|
| 93 | ) {
|
| 94 | this(type, level);
|
| 95 | this.pickupItemStack = pickupItemStack.copy();
|
| 96 | this.applyComponentsFromItemStack(pickupItemStack);
|
| 97 | Unit intangible = pickupItemStack.remove(DataComponents.INTANGIBLE_PROJECTILE);
|
| 98 | if (intangible != null) {
|
| 99 | this.pickup = AbstractArrow.Pickup.CREATIVE_ONLY;
|
| 100 | }
|
| 101 |
|
| 102 | this.setPos(x, y, z);
|
| 103 | if (firedFromWeapon != null && level instanceof ServerLevel serverLevel) {
|
| 104 | if (firedFromWeapon.isEmpty()) {
|
| 105 | throw new IllegalArgumentException("Invalid weapon firing an arrow");
|
| 106 | }
|
| 107 |
|
| 108 | this.firedFromWeapon = firedFromWeapon.copy();
|
| 109 | int pierceLevel = EnchantmentHelper.getPiercingCount(serverLevel, firedFromWeapon, this.pickupItemStack);
|
| 110 | if (pierceLevel > 0) {
|
| 111 | this.setPierceLevel((byte)pierceLevel);
|
| 112 | }
|
| 113 | }
|
| 114 | }
|
| 115 |
|
| 116 | protected AbstractArrow(
|
| 117 | final EntityType<? extends AbstractArrow> type,
|
| 118 | final LivingEntity mob,
|
| 119 | final Level level,
|
| 120 | final ItemStack pickupItemStack,
|
| 121 | final @Nullable ItemStack firedFromWeapon
|
| 122 | ) {
|
| 123 | this(type, mob.getX(), mob.getEyeY() - 0.1F, mob.getZ(), level, pickupItemStack, firedFromWeapon);
|
| 124 | this.setOwner(mob);
|
| 125 | }
|
| 126 |
|
| 127 | public void setSoundEvent(final SoundEvent soundEvent) {
|
| 128 | this.soundEvent = soundEvent;
|
| 129 | }
|
| 130 |
|
| 131 | @Override
|
| 132 | public boolean shouldRenderAtSqrDistance(final double distance) {
|
| 133 | double size = this.getBoundingBox().getSize() * 10.0;
|
| 134 | if (Double.isNaN(size)) {
|
| 135 | size = 1.0;
|
| 136 | }
|
| 137 |
|
| 138 | size *= 64.0 * getViewScale();
|
| 139 | return distance < size * size;
|
| 140 | }
|
| 141 |
|
| 142 | @Override
|
| 143 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 144 | entityData.define(ID_FLAGS, (byte)0);
|
| 145 | entityData.define(PIERCE_LEVEL, (byte)0);
|
| 146 | entityData.define(IN_GROUND, false);
|
| 147 | }
|
| 148 |
|
| 149 | @Override
|
| 150 | public void shoot(final double xd, final double yd, final double zd, final float pow, final float uncertainty) {
|
| 151 | super.shoot(xd, yd, zd, pow, uncertainty);
|
| 152 | this.life = 0;
|
| 153 | }
|
| 154 |
|
| 155 | @Override
|
| 156 | public void lerpMotion(final Vec3 movement) {
|
| 157 | super.lerpMotion(movement);
|
| 158 | this.life = 0;
|
| 159 | if (this.isInGround() && movement.lengthSqr() > 0.0) {
|
| 160 | this.setInGround(false);
|
| 161 | }
|
| 162 | }
|
| 163 |
|
| 164 | @Override
|
| 165 | public void onSyncedDataUpdated(final EntityDataAccessor<?> accessor) {
|
| 166 | super.onSyncedDataUpdated(accessor);
|
| 167 | if (!this.firstTick && this.shakeTime <= 0 && accessor.equals(IN_GROUND) && this.isInGround()) {
|
| 168 | this.shakeTime = 7;
|
| 169 | }
|
| 170 | }
|
| 171 |
|
| 172 | @Override
|
| 173 | public void tick() {
|
| 174 | boolean physicsEnabled = !this.isNoPhysics();
|
| 175 | Vec3 movement = this.getDeltaMovement();
|
| 176 | BlockPos blockPos = this.blockPosition();
|
| 177 | BlockState blockState = this.level().getBlockState(blockPos);
|
| 178 | if (!blockState.isAir() && physicsEnabled) {
|
| 179 | VoxelShape shape = blockState.getCollisionShape(this.level(), blockPos);
|
| 180 | if (!shape.isEmpty()) {
|
| 181 | Vec3 position = this.position();
|
| 182 |
|
| 183 | for (AABB aabb : shape.toAabbs()) {
|
| 184 | if (aabb.move(blockPos).contains(position)) {
|
| 185 | this.setDeltaMovement(Vec3.ZERO);
|
| 186 | this.setInGround(true);
|
| 187 | break;
|
| 188 | }
|
| 189 | }
|
| 190 | }
|
| 191 | }
|
| 192 |
|
| 193 | if (this.shakeTime > 0) {
|
| 194 | this.shakeTime--;
|
| 195 | }
|
| 196 |
|
| 197 | if (this.isInWaterOrRain()) {
|
| 198 | this.clearFire();
|
| 199 | }
|
| 200 |
|
| 201 | if (this.isInGround() && physicsEnabled) {
|
| 202 | if (!this.level().isClientSide()) {
|
| 203 | if (this.lastState != blockState && this.shouldFall()) {
|
| 204 | this.startFalling();
|
| 205 | } else {
|
| 206 | this.tickDespawn();
|
| 207 | }
|
| 208 | }
|
| 209 |
|
| 210 | this.inGroundTime++;
|
| 211 | if (this.isAlive()) {
|
| 212 | this.applyEffectsFromBlocks();
|
| 213 | }
|
| 214 |
|
| 215 | if (!this.level().isClientSide()) {
|
| 216 | this.setSharedFlagOnFire(this.getRemainingFireTicks() > 0);
|
| 217 | }
|
| 218 | } else {
|
| 219 | this.inGroundTime = 0;
|
| 220 | Vec3 originalPosition = this.position();
|
| 221 | if (this.isInWater()) {
|
| 222 | this.applyInertia(this.getWaterInertia());
|
| 223 | this.addBubbleParticles(originalPosition);
|
| 224 | }
|
| 225 |
|
| 226 | if (this.isCritArrow()) {
|
| 227 | for (int i = 0; i < 4; i++) {
|
| 228 | this.level()
|
| 229 | .addParticle(
|
| 230 | ParticleTypes.CRIT,
|
| 231 | originalPosition.x + movement.x * i / 4.0,
|
| 232 | originalPosition.y + movement.y * i / 4.0,
|
| 233 | originalPosition.z + movement.z * i / 4.0,
|
| 234 | -movement.x,
|
| 235 | -movement.y + 0.2,
|
| 236 | -movement.z
|
| 237 | );
|
| 238 | }
|
| 239 | }
|
| 240 |
|
| 241 | float yRot;
|
| 242 | if (!physicsEnabled) {
|
| 243 | yRot = (float)(Mth.atan2(-movement.x, -movement.z) * 180.0F / (float)Math.PI);
|
| 244 | } else {
|
| 245 | yRot = (float)(Mth.atan2(movement.x, movement.z) * 180.0F / (float)Math.PI);
|
| 246 | }
|
| 247 |
|
| 248 | float xRot = (float)(Mth.atan2(movement.y, movement.horizontalDistance()) * 180.0F / (float)Math.PI);
|
| 249 | this.setXRot(lerpRotation(this.getXRot(), xRot));
|
| 250 | this.setYRot(lerpRotation(this.getYRot(), yRot));
|
| 251 | this.checkLeftOwner();
|
| 252 | if (physicsEnabled) {
|
| 253 | BlockHitResult blockHitResult = this.level()
|
| 254 | .clipIncludingBorder(new ClipContext(originalPosition, originalPosition.add(movement), ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, this));
|
| 255 | this.stepMoveAndHit(blockHitResult);
|
| 256 | } else {
|
| 257 | this.setPos(originalPosition.add(movement));
|
| 258 | this.applyEffectsFromBlocks();
|
| 259 | }
|
| 260 |
|
| 261 | if (!this.isInWater()) {
|
| 262 | this.applyInertia(this.getAirDrag());
|
| 263 | }
|
| 264 |
|
| 265 | if (physicsEnabled && !this.isInGround()) {
|
| 266 | this.applyGravity();
|
| 267 | }
|
| 268 |
|
| 269 | super.tick();
|
| 270 | }
|
| 271 | }
|
| 272 |
|
| 273 | @Override
|
| 274 | protected float getAirDrag() {
|
| 275 | return 0.99F;
|
| 276 | }
|
| 277 |
|
| 278 | private void stepMoveAndHit(final BlockHitResult blockHitResult) {
|
| 279 | while (this.isAlive()) {
|
| 280 | Vec3 initialPosition = this.position();
|
| 281 | ArrayList<EntityHitResult> entitiesHit = new ArrayList<>(this.findHitEntities(initialPosition, blockHitResult.getLocation()));
|
| 282 | entitiesHit.sort(Comparator.comparingDouble(c -> initialPosition.distanceToSqr(c.getEntity().position())));
|
| 283 | EntityHitResult firstEntityHit = entitiesHit.isEmpty() ? null : entitiesHit.getFirst();
|
| 284 | Vec3 nextLocation = Objects.requireNonNullElse(firstEntityHit, blockHitResult).getLocation();
|
| 285 | this.setPos(nextLocation);
|
| 286 | this.applyEffectsFromBlocks(initialPosition, nextLocation);
|
| 287 | if (this.portalProcess != null && this.portalProcess.isInsidePortalThisTick()) {
|
| 288 | this.handlePortal();
|
| 289 | }
|
| 290 |
|
| 291 | if (entitiesHit.isEmpty()) {
|
| 292 | if (this.isAlive() && blockHitResult.getType() != HitResult.Type.MISS) {
|
| 293 | this.hitTargetOrDeflectSelf(blockHitResult);
|
| 294 | this.needsSync = true;
|
| 295 | }
|
| 296 | break;
|
| 297 | } else if (this.isAlive() && !this.noPhysics) {
|
| 298 | ProjectileDeflection deflection = this.hitTargetsOrDeflectSelf(entitiesHit);
|
| 299 | this.needsSync = true;
|
| 300 | if (this.getPierceLevel() > 0 && deflection == ProjectileDeflection.NONE) {
|
| 301 | continue;
|
| 302 | }
|
| 303 | break;
|
| 304 | }
|
| 305 | }
|
| 306 | }
|
| 307 |
|
| 308 | private ProjectileDeflection hitTargetsOrDeflectSelf(final Collection<EntityHitResult> entityHitResults) {
|
| 309 | for (EntityHitResult e : entityHitResults) {
|
| 310 | ProjectileDeflection deflection = this.hitTargetOrDeflectSelf(e);
|
| 311 | if (!this.isAlive() || deflection != ProjectileDeflection.NONE) {
|
| 312 | return deflection;
|
| 313 | }
|
| 314 | }
|
| 315 |
|
| 316 | return ProjectileDeflection.NONE;
|
| 317 | }
|
| 318 |
|
| 319 | private void applyInertia(final float inertia) {
|
| 320 | Vec3 movement = this.getDeltaMovement();
|
| 321 | this.setDeltaMovement(movement.scale(inertia));
|
| 322 | }
|
| 323 |
|
| 324 | private void addBubbleParticles(final Vec3 position) {
|
| 325 | Vec3 movement = this.getDeltaMovement();
|
| 326 |
|
| 327 | for (int i = 0; i < 4; i++) {
|
| 328 | float s = 0.25F;
|
| 329 | this.level()
|
| 330 | .addParticle(
|
| 331 | ParticleTypes.BUBBLE, position.x - movement.x * 0.25, position.y - movement.y * 0.25, position.z - movement.z * 0.25, movement.x, movement.y, movement.z
|
| 332 | );
|
| 333 | }
|
| 334 | }
|
| 335 |
|
| 336 | @Override
|
| 337 | protected double getDefaultGravity() {
|
| 338 | return 0.05;
|
| 339 | }
|
| 340 |
|
| 341 | private boolean shouldFall() {
|
| 342 | return this.isInGround() && this.level().noCollision(new AABB(this.position(), this.position()).inflate(0.06));
|
| 343 | }
|
| 344 |
|
| 345 | private void startFalling() {
|
| 346 | this.setInGround(false);
|
| 347 | Vec3 deltaMovement = this.getDeltaMovement();
|
| 348 | this.setDeltaMovement(deltaMovement.multiply(this.random.nextFloat() * 0.2F, this.random.nextFloat() * 0.2F, this.random.nextFloat() * 0.2F));
|
| 349 | this.life = 0;
|
| 350 | }
|
| 351 |
|
| 352 | protected boolean isInGround() {
|
| 353 | return this.entityData.get(IN_GROUND);
|
| 354 | }
|
| 355 |
|
| 356 | protected void setInGround(final boolean inGround) {
|
| 357 | this.entityData.set(IN_GROUND, inGround);
|
| 358 | }
|
| 359 |
|
| 360 | @Override
|
| 361 | public boolean isPushedByFluid() {
|
| 362 | return !this.isInGround();
|
| 363 | }
|
| 364 |
|
| 365 | @Override
|
| 366 | public void move(final MoverType moverType, final Vec3 delta) {
|
| 367 | super.move(moverType, delta);
|
| 368 | if (moverType != MoverType.SELF && this.shouldFall()) {
|
| 369 | this.startFalling();
|
| 370 | }
|
| 371 | }
|
| 372 |
|
| 373 | protected void tickDespawn() {
|
| 374 | this.life++;
|
| 375 | if (this.life >= 1200) {
|
| 376 | this.discard();
|
| 377 | }
|
| 378 | }
|
| 379 |
|
| 380 | private void resetPiercedEntities() {
|
| 381 | if (this.piercedAndKilledEntities != null) {
|
| 382 | this.piercedAndKilledEntities.clear();
|
| 383 | }
|
| 384 |
|
| 385 | if (this.piercingIgnoreEntityIds != null) {
|
| 386 | this.piercingIgnoreEntityIds.clear();
|
| 387 | }
|
| 388 | }
|
| 389 |
|
| 390 | @Override
|
| 391 | public void onItemBreak(final ItemStack item) {
|
| 392 | this.firedFromWeapon = null;
|
| 393 | }
|
| 394 |
|
| 395 | @Override
|
| 396 | public void onAboveBubbleColumn(final boolean dragDown, final BlockPos pos) {
|
| 397 | if (!this.isInGround()) {
|
| 398 | super.onAboveBubbleColumn(dragDown, pos);
|
| 399 | }
|
| 400 | }
|
| 401 |
|
| 402 | @Override
|
| 403 | public void onInsideBubbleColumn(final boolean dragDown) {
|
| 404 | if (!this.isInGround()) {
|
| 405 | super.onInsideBubbleColumn(dragDown);
|
| 406 | }
|
| 407 | }
|
| 408 |
|
| 409 | @Override
|
| 410 | public void push(final double xa, final double ya, final double za) {
|
| 411 | if (!this.isInGround()) {
|
| 412 | super.push(xa, ya, za);
|
| 413 | }
|
| 414 | }
|
| 415 |
|
| 416 | @Override
|
| 417 | protected void onHitEntity(final EntityHitResult hitResult) {
|
| 418 | super.onHitEntity(hitResult);
|
| 419 | Entity entity = hitResult.getEntity();
|
| 420 | float pow = (float)this.getDeltaMovement().length();
|
| 421 | double arrowDamage = this.baseDamage;
|
| 422 | Entity currentOwner = this.getOwner();
|
| 423 | DamageSource damageSource = this.damageSources().arrow(this, currentOwner != null ? currentOwner : this);
|
| 424 | if (this.getWeaponItem() != null && this.level() instanceof ServerLevel serverLevel) {
|
| 425 | arrowDamage = EnchantmentHelper.modifyDamage(serverLevel, this.getWeaponItem(), entity, damageSource, (float)arrowDamage);
|
| 426 | }
|
| 427 |
|
| 428 | int damage = Mth.ceil(Mth.clamp(pow * arrowDamage, 0.0, 2.147483647E9));
|
| 429 | if (this.getPierceLevel() > 0) {
|
| 430 | if (this.piercingIgnoreEntityIds == null) {
|
| 431 | this.piercingIgnoreEntityIds = new IntOpenHashSet(5);
|
| 432 | }
|
| 433 |
|
| 434 | if (this.piercedAndKilledEntities == null) {
|
| 435 | this.piercedAndKilledEntities = Lists.newArrayListWithCapacity(5);
|
| 436 | }
|
| 437 |
|
| 438 | if (this.piercingIgnoreEntityIds.size() >= this.getPierceLevel() + 1) {
|
| 439 | this.discard();
|
| 440 | return;
|
| 441 | }
|
| 442 |
|
| 443 | this.piercingIgnoreEntityIds.add(entity.getId());
|
| 444 | }
|
| 445 |
|
| 446 | if (this.isCritArrow()) {
|
| 447 | long dmgIncrease = this.random.nextInt(damage / 2 + 2);
|
| 448 | damage = (int)Math.min(dmgIncrease + damage, 2147483647L);
|
| 449 | }
|
| 450 |
|
| 451 | if (currentOwner instanceof LivingEntity livingOwner) {
|
| 452 | livingOwner.setLastHurtMob(entity);
|
| 453 | }
|
| 454 |
|
| 455 | int remainingFireTicks = entity.getRemainingFireTicks();
|
| 456 | if (this.isOnFire()) {
|
| 457 | entity.igniteForSeconds(5.0F);
|
| 458 | }
|
| 459 |
|
| 460 | if (entity.hurtOrSimulate(damageSource, damage)) {
|
| 461 | if (entity instanceof LivingEntity mob) {
|
| 462 | if (!this.level().isClientSide() && this.getPierceLevel() <= 0) {
|
| 463 | mob.setArrowCount(mob.getArrowCount() + 1);
|
| 464 | }
|
| 465 |
|
| 466 | this.doKnockback(mob, damageSource);
|
| 467 | if (this.level() instanceof ServerLevel serverLevel) {
|
| 468 | EnchantmentHelper.doPostAttackEffectsWithItemSource(serverLevel, mob, damageSource, this.getWeaponItem());
|
| 469 | }
|
| 470 |
|
| 471 | this.doPostHurtEffects(mob);
|
| 472 | if (mob instanceof Player && currentOwner instanceof ServerPlayer ownerPlayer && !this.isSilent() && mob != ownerPlayer) {
|
| 473 | ownerPlayer.connection.send(new ClientboundGameEventPacket(ClientboundGameEventPacket.PLAY_ARROW_HIT_SOUND, 0.0F));
|
| 474 | }
|
| 475 |
|
| 476 | if (!entity.isAlive() && this.piercedAndKilledEntities != null) {
|
| 477 | this.piercedAndKilledEntities.add(mob);
|
| 478 | }
|
| 479 |
|
| 480 | if (!this.level().isClientSide() && currentOwner instanceof ServerPlayer player) {
|
| 481 | if (this.piercedAndKilledEntities != null) {
|
| 482 | CriteriaTriggers.KILLED_BY_ARROW.trigger(player, this.piercedAndKilledEntities, this.firedFromWeapon);
|
| 483 | } else if (!entity.isAlive()) {
|
| 484 | CriteriaTriggers.KILLED_BY_ARROW.trigger(player, List.of(entity), this.firedFromWeapon);
|
| 485 | }
|
| 486 | }
|
| 487 | }
|
| 488 |
|
| 489 | this.playSound(this.soundEvent, 1.0F, 1.2F / (this.random.nextFloat() * 0.2F + 0.9F));
|
| 490 | if (this.getPierceLevel() <= 0) {
|
| 491 | this.discard();
|
| 492 | }
|
| 493 | } else if (entity.projectileReceivesSideEffectsOnHit(false)) {
|
| 494 | entity.setRemainingFireTicks(remainingFireTicks);
|
| 495 | this.deflect(ProjectileDeflection.REVERSE, entity, this.owner, false, 0.2);
|
| 496 | if (this.level() instanceof ServerLevel level && this.getDeltaMovement().lengthSqr() < 1.0E-7) {
|
| 497 | if (this.pickup == AbstractArrow.Pickup.ALLOWED) {
|
| 498 | this.spawnAtLocation(level, this.getPickupItem(), 0.1F);
|
| 499 | }
|
| 500 |
|
| 501 | this.discard();
|
| 502 | }
|
| 503 | }
|
| 504 | }
|
| 505 |
|
| 506 | protected void doKnockback(final LivingEntity mob, final DamageSource damageSource) {
|
| 507 | double knockback = this.firedFromWeapon != null && this.level() instanceof ServerLevel serverLevel
|
| 508 | ? EnchantmentHelper.modifyKnockback(serverLevel, this.firedFromWeapon, mob, damageSource, 0.0F)
|
| 509 | : 0.0F;
|
| 510 | if (knockback > 0.0) {
|
| 511 | double knockbackResistance = Math.max(0.0, 1.0 - mob.getAttributeValue(Attributes.KNOCKBACK_RESISTANCE));
|
| 512 | Vec3 movement = this.getDeltaMovement().multiply(1.0, 0.0, 1.0).normalize().scale(knockback * 0.6 * knockbackResistance);
|
| 513 | if (movement.lengthSqr() > 0.0) {
|
| 514 | mob.push(movement.x, 0.1, movement.z);
|
| 515 | }
|
| 516 | }
|
| 517 | }
|
| 518 |
|
| 519 | @Override
|
| 520 | protected void onHitBlock(final BlockHitResult hitResult) {
|
| 521 | this.lastState = this.level().getBlockState(hitResult.getBlockPos());
|
| 522 | super.onHitBlock(hitResult);
|
| 523 | ItemStack weaponItem = this.getWeaponItem();
|
| 524 | if (this.level() instanceof ServerLevel serverLevel && weaponItem != null) {
|
| 525 | this.hitBlockEnchantmentEffects(serverLevel, hitResult, weaponItem);
|
| 526 | }
|
| 527 |
|
| 528 | Vec3 movement = this.getDeltaMovement();
|
| 529 | Vec3 offsetDirection = new Vec3(Math.signum(movement.x), Math.signum(movement.y), Math.signum(movement.z));
|
| 530 | Vec3 scaledMovement = offsetDirection.scale(0.05F);
|
| 531 | this.setPos(this.position().subtract(scaledMovement));
|
| 532 | this.setDeltaMovement(Vec3.ZERO);
|
| 533 | this.playSound(this.getHitGroundSoundEvent(), 1.0F, 1.2F / (this.random.nextFloat() * 0.2F + 0.9F));
|
| 534 | this.setInGround(true);
|
| 535 | this.shakeTime = 7;
|
| 536 | this.setCritArrow(false);
|
| 537 | this.setPierceLevel((byte)0);
|
| 538 | this.setSoundEvent(SoundEvents.ARROW_HIT);
|
| 539 | this.resetPiercedEntities();
|
| 540 | }
|
| 541 |
|
| 542 | protected void hitBlockEnchantmentEffects(final ServerLevel serverLevel, final BlockHitResult hitResult, final ItemStack weapon) {
|
| 543 | Vec3 compensatedHitPosition = hitResult.getBlockPos().clampLocationWithin(hitResult.getLocation());
|
| 544 | EnchantmentHelper.onHitBlock(
|
| 545 | serverLevel,
|
| 546 | weapon,
|
| 547 | this.getOwner() instanceof LivingEntity livingOwner ? livingOwner : null,
|
| 548 | this,
|
| 549 | null,
|
| 550 | compensatedHitPosition,
|
| 551 | serverLevel.getBlockState(hitResult.getBlockPos()),
|
| 552 | item -> this.firedFromWeapon = null
|
| 553 | );
|
| 554 | }
|
| 555 |
|
| 556 | @Override
|
| 557 | public @Nullable ItemStack getWeaponItem() {
|
| 558 | return this.firedFromWeapon;
|
| 559 | }
|
| 560 |
|
| 561 | protected SoundEvent getDefaultHitGroundSoundEvent() {
|
| 562 | return SoundEvents.ARROW_HIT;
|
| 563 | }
|
| 564 |
|
| 565 | protected final SoundEvent getHitGroundSoundEvent() {
|
| 566 | return this.soundEvent;
|
| 567 | }
|
| 568 |
|
| 569 | protected void doPostHurtEffects(final LivingEntity mob) {
|
| 570 | }
|
| 571 |
|
| 572 | protected @Nullable EntityHitResult findHitEntity(final Vec3 from, final Vec3 to) {
|
| 573 | return ProjectileUtil.getEntityHitResult(
|
| 574 | this.level(), this, from, to, this.getBoundingBox().expandTowards(this.getDeltaMovement()).inflate(1.0), this::canHitEntity
|
| 575 | );
|
| 576 | }
|
| 577 |
|
| 578 | protected Collection<EntityHitResult> findHitEntities(final Vec3 from, final Vec3 to) {
|
| 579 | return ProjectileUtil.getManyEntityHitResult(
|
| 580 | this.level(), this, from, to, this.getBoundingBox().expandTowards(this.getDeltaMovement()).inflate(1.0), this::canHitEntity, false, false
|
| 581 | );
|
| 582 | }
|
| 583 |
|
| 584 | @Override
|
| 585 | protected boolean canHitEntity(final Entity entity) {
|
| 586 | return entity instanceof Player playerEntity && this.getOwner() instanceof Player player && !player.canHarmPlayer(playerEntity)
|
| 587 | ? false
|
| 588 | : super.canHitEntity(entity) && (this.piercingIgnoreEntityIds == null || !this.piercingIgnoreEntityIds.contains(entity.getId()));
|
| 589 | }
|
| 590 |
|
| 591 | @Override
|
| 592 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 593 | super.addAdditionalSaveData(output);
|
| 594 | output.putShort("life", (short)this.life);
|
| 595 | output.storeNullable("inBlockState", BlockState.CODEC, this.lastState);
|
| 596 | output.putByte("shake", (byte)this.shakeTime);
|
| 597 | output.putBoolean("inGround", this.isInGround());
|
| 598 | output.store("pickup", AbstractArrow.Pickup.LEGACY_CODEC, this.pickup);
|
| 599 | output.putDouble("damage", this.baseDamage);
|
| 600 | output.putBoolean("crit", this.isCritArrow());
|
| 601 | output.putByte("PierceLevel", this.getPierceLevel());
|
| 602 | output.store("SoundEvent", BuiltInRegistries.SOUND_EVENT.byNameCodec(), this.soundEvent);
|
| 603 | output.store("item", ItemStack.CODEC, this.pickupItemStack);
|
| 604 | output.storeNullable("weapon", ItemStack.CODEC, this.firedFromWeapon);
|
| 605 | }
|
| 606 |
|
| 607 | @Override
|
| 608 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 609 | super.readAdditionalSaveData(input);
|
| 610 | this.life = input.getShortOr("life", (short)0);
|
| 611 | this.lastState = input.read("inBlockState", BlockState.CODEC).orElse(null);
|
| 612 | this.shakeTime = input.getByteOr("shake", (byte)0) & 255;
|
| 613 | this.setInGround(input.getBooleanOr("inGround", false));
|
| 614 | this.baseDamage = input.getDoubleOr("damage", 2.0);
|
| 615 | this.pickup = input.read("pickup", AbstractArrow.Pickup.LEGACY_CODEC).orElse(AbstractArrow.Pickup.DISALLOWED);
|
| 616 | this.setCritArrow(input.getBooleanOr("crit", false));
|
| 617 | this.setPierceLevel(input.getByteOr("PierceLevel", (byte)0));
|
| 618 | this.soundEvent = input.read("SoundEvent", BuiltInRegistries.SOUND_EVENT.byNameCodec()).orElse(this.getDefaultHitGroundSoundEvent());
|
| 619 | this.setPickupItemStack(input.read("item", ItemStack.CODEC).orElse(this.getDefaultPickupItem()));
|
| 620 | this.firedFromWeapon = input.read("weapon", ItemStack.CODEC).orElse(null);
|
| 621 | }
|
| 622 |
|
| 623 | @Override
|
| 624 | public void setOwner(final @Nullable Entity owner) {
|
| 625 | super.setOwner(owner);
|
| 626 |
|
| 627 | this.pickup = switch (owner) {
|
| 628 | case Player ignored when this.pickup == AbstractArrow.Pickup.DISALLOWED -> AbstractArrow.Pickup.ALLOWED;
|
| 629 | case OminousItemSpawner ignored -> AbstractArrow.Pickup.DISALLOWED;
|
| 630 | case null, default -> this.pickup;
|
| 631 | };
|
| 632 | }
|
| 633 |
|
| 634 | @Override
|
| 635 | public void playerTouch(final Player player) {
|
| 636 | if (!this.level().isClientSide() && (this.isInGround() || this.isNoPhysics()) && this.shakeTime <= 0) {
|
| 637 | if (this.tryPickup(player)) {
|
| 638 | player.take(this, 1);
|
| 639 | this.discard();
|
| 640 | }
|
| 641 | }
|
| 642 | }
|
| 643 |
|
| 644 | protected boolean tryPickup(final Player player) {
|
| 645 | return switch (this.pickup) {
|
| 646 | case DISALLOWED -> false;
|
| 647 | case ALLOWED -> player.getInventory().add(this.getPickupItem());
|
| 648 | case CREATIVE_ONLY -> player.hasInfiniteMaterials();
|
| 649 | };
|
| 650 | }
|
| 651 |
|
| 652 | protected ItemStack getPickupItem() {
|
| 653 | return this.pickupItemStack.copy();
|
| 654 | }
|
| 655 |
|
| 656 | protected abstract ItemStack getDefaultPickupItem();
|
| 657 |
|
| 658 | @Override
|
| 659 | protected Entity.MovementEmission getMovementEmission() {
|
| 660 | return Entity.MovementEmission.NONE;
|
| 661 | }
|
| 662 |
|
| 663 | public ItemStack getPickupItemStackOrigin() {
|
| 664 | return this.pickupItemStack;
|
| 665 | }
|
| 666 |
|
| 667 | public void setBaseDamage(final double baseDamage) {
|
| 668 | this.baseDamage = baseDamage;
|
| 669 | }
|
| 670 |
|
| 671 | @Override
|
| 672 | public boolean isAttackable() {
|
| 673 | return this.is(EntityTypeTags.REDIRECTABLE_PROJECTILE);
|
| 674 | }
|
| 675 |
|
| 676 | public void setCritArrow(final boolean critArrow) {
|
| 677 | this.setFlag(1, critArrow);
|
| 678 | }
|
| 679 |
|
| 680 | private void setPierceLevel(final byte pieceLevel) {
|
| 681 | this.entityData.set(PIERCE_LEVEL, pieceLevel);
|
| 682 | }
|
| 683 |
|
| 684 | private void setFlag(final int flag, final boolean value) {
|
| 685 | byte flags = this.entityData.get(ID_FLAGS);
|
| 686 | if (value) {
|
| 687 | this.entityData.set(ID_FLAGS, (byte)(flags | flag));
|
| 688 | } else {
|
| 689 | this.entityData.set(ID_FLAGS, (byte)(flags & ~flag));
|
| 690 | }
|
| 691 | }
|
| 692 |
|
| 693 | protected void setPickupItemStack(final ItemStack itemStack) {
|
| 694 | if (!itemStack.isEmpty()) {
|
| 695 | this.pickupItemStack = itemStack;
|
| 696 | } else {
|
| 697 | this.pickupItemStack = this.getDefaultPickupItem();
|
| 698 | }
|
| 699 | }
|
| 700 |
|
| 701 | public boolean isCritArrow() {
|
| 702 | byte flags = this.entityData.get(ID_FLAGS);
|
| 703 | return (flags & 1) != 0;
|
| 704 | }
|
| 705 |
|
| 706 | public byte getPierceLevel() {
|
| 707 | return this.entityData.get(PIERCE_LEVEL);
|
| 708 | }
|
| 709 |
|
| 710 | public void setBaseDamageFromMob(final float power) {
|
| 711 | this.setBaseDamage(power * 2.0F + this.random.triangle(this.level().getDifficulty().getId() * 0.11, 0.57425));
|
| 712 | }
|
| 713 |
|
| 714 | protected float getWaterInertia() {
|
| 715 | return 0.6F;
|
| 716 | }
|
| 717 |
|
| 718 | public void setNoPhysics(final boolean noPhysics) {
|
| 719 | this.noPhysics = noPhysics;
|
| 720 | this.setFlag(2, noPhysics);
|
| 721 | }
|
| 722 |
|
| 723 | public boolean isNoPhysics() {
|
| 724 | return !this.level().isClientSide() ? this.noPhysics : (this.entityData.get(ID_FLAGS) & 2) != 0;
|
| 725 | }
|
| 726 |
|
| 727 | @Override
|
| 728 | public boolean isPickable() {
|
| 729 | return super.isPickable() && !this.isInGround();
|
| 730 | }
|
| 731 |
|
| 732 | @Override
|
| 733 | public @Nullable SlotAccess getSlot(final int slot) {
|
| 734 | return slot == 0 ? SlotAccess.of(this::getPickupItemStackOrigin, this::setPickupItemStack) : super.getSlot(slot);
|
| 735 | }
|
| 736 |
|
| 737 | @Override
|
| 738 | protected boolean shouldBounceOnWorldBorder() {
|
| 739 | return true;
|
| 740 | }
|
| 741 |
|
| 742 | public enum Pickup {
|
| 743 | DISALLOWED,
|
| 744 | ALLOWED,
|
| 745 | CREATIVE_ONLY;
|
| 746 |
|
| 747 | public static final Codec<AbstractArrow.Pickup> LEGACY_CODEC = Codec.BYTE.xmap(AbstractArrow.Pickup::byOrdinal, p -> (byte)p.ordinal());
|
| 748 |
|
| 749 | public static AbstractArrow.Pickup byOrdinal(int ordinal) {
|
| 750 | if (ordinal < 0 || ordinal > values().length) {
|
| 751 | ordinal = 0;
|
| 752 | }
|
| 753 |
|
| 754 | return values()[ordinal];
|
| 755 | }
|
| 756 | }
|
| 757 | }
|
| 758 |
|