| 1 | package net.minecraft.world.entity.projectile.arrow;
|
| 2 |
|
| 3 | import java.util.Collection;
|
| 4 | import java.util.List;
|
| 5 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 6 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 7 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 8 | import net.minecraft.server.level.ServerLevel;
|
| 9 | import net.minecraft.server.level.ServerPlayer;
|
| 10 | import net.minecraft.sounds.SoundEvent;
|
| 11 | import net.minecraft.sounds.SoundEvents;
|
| 12 | import net.minecraft.util.Mth;
|
| 13 | import net.minecraft.world.damagesource.DamageSource;
|
| 14 | import net.minecraft.world.entity.Entity;
|
| 15 | import net.minecraft.world.entity.EntityType;
|
| 16 | import net.minecraft.world.entity.EntityTypes;
|
| 17 | import net.minecraft.world.entity.LivingEntity;
|
| 18 | import net.minecraft.world.entity.player.Player;
|
| 19 | import net.minecraft.world.entity.projectile.ProjectileDeflection;
|
| 20 | import net.minecraft.world.item.ItemStack;
|
| 21 | import net.minecraft.world.item.Items;
|
| 22 | import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
| 23 | import net.minecraft.world.level.Level;
|
| 24 | import net.minecraft.world.level.storage.ValueInput;
|
| 25 | import net.minecraft.world.level.storage.ValueOutput;
|
| 26 | import net.minecraft.world.phys.BlockHitResult;
|
| 27 | import net.minecraft.world.phys.EntityHitResult;
|
| 28 | import net.minecraft.world.phys.Vec3;
|
| 29 | import org.jspecify.annotations.Nullable;
|
| 30 |
|
| 31 | public class ThrownTrident extends AbstractArrow {
|
| 32 | private static final EntityDataAccessor<Byte> ID_LOYALTY = SynchedEntityData.defineId(ThrownTrident.class, EntityDataSerializers.BYTE);
|
| 33 | private static final EntityDataAccessor<Boolean> ID_FOIL = SynchedEntityData.defineId(ThrownTrident.class, EntityDataSerializers.BOOLEAN);
|
| 34 | private static final float WATER_INERTIA = 0.99F;
|
| 35 | private static final boolean DEFAULT_DEALT_DAMAGE = false;
|
| 36 | private boolean dealtDamage = false;
|
| 37 | public int clientSideReturnTridentTickCount;
|
| 38 |
|
| 39 | public ThrownTrident(final EntityType<? extends ThrownTrident> type, final Level level) {
|
| 40 | super(type, level);
|
| 41 | }
|
| 42 |
|
| 43 | public ThrownTrident(final Level level, final LivingEntity owner, final ItemStack tridentItem) {
|
| 44 | super(EntityTypes.TRIDENT, owner, level, tridentItem, null);
|
| 45 | this.entityData.set(ID_LOYALTY, this.getLoyaltyFromItem(tridentItem));
|
| 46 | this.entityData.set(ID_FOIL, tridentItem.hasFoil());
|
| 47 | }
|
| 48 |
|
| 49 | public ThrownTrident(final Level level, final double x, final double y, final double z, final ItemStack tridentItem) {
|
| 50 | super(EntityTypes.TRIDENT, x, y, z, level, tridentItem, tridentItem);
|
| 51 | this.entityData.set(ID_LOYALTY, this.getLoyaltyFromItem(tridentItem));
|
| 52 | this.entityData.set(ID_FOIL, tridentItem.hasFoil());
|
| 53 | }
|
| 54 |
|
| 55 | @Override
|
| 56 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 57 | super.defineSynchedData(entityData);
|
| 58 | entityData.define(ID_LOYALTY, (byte)0);
|
| 59 | entityData.define(ID_FOIL, false);
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | public void tick() {
|
| 64 | if (this.inGroundTime > 4) {
|
| 65 | this.dealtDamage = true;
|
| 66 | }
|
| 67 |
|
| 68 | Entity currentOwner = this.getOwner();
|
| 69 | int loyalty = this.entityData.get(ID_LOYALTY);
|
| 70 | if (loyalty > 0 && (this.dealtDamage || this.isNoPhysics()) && currentOwner != null) {
|
| 71 | if (!this.isAcceptibleReturnOwner()) {
|
| 72 | if (this.level() instanceof ServerLevel level && this.pickup == AbstractArrow.Pickup.ALLOWED) {
|
| 73 | this.spawnAtLocation(level, this.getPickupItem(), 0.1F);
|
| 74 | }
|
| 75 |
|
| 76 | this.discard();
|
| 77 | } else {
|
| 78 | if (!(currentOwner instanceof Player) && this.position().distanceTo(currentOwner.getEyePosition()) < currentOwner.getBbWidth() + 1.0) {
|
| 79 | this.discard();
|
| 80 | return;
|
| 81 | }
|
| 82 |
|
| 83 | this.setNoPhysics(true);
|
| 84 | Vec3 vec = currentOwner.getEyePosition().subtract(this.position());
|
| 85 | this.setPosRaw(this.getX(), this.getY() + vec.y * 0.015 * loyalty, this.getZ());
|
| 86 | double accel = 0.05 * loyalty;
|
| 87 | this.setDeltaMovement(this.getDeltaMovement().scale(0.95).add(vec.normalize().scale(accel)));
|
| 88 | if (this.clientSideReturnTridentTickCount == 0) {
|
| 89 | this.playSound(SoundEvents.TRIDENT_RETURN, 10.0F, 1.0F);
|
| 90 | }
|
| 91 |
|
| 92 | this.clientSideReturnTridentTickCount++;
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | super.tick();
|
| 97 | }
|
| 98 |
|
| 99 | private boolean isAcceptibleReturnOwner() {
|
| 100 | Entity currentOwner = this.getOwner();
|
| 101 | return currentOwner == null || !currentOwner.isAlive() ? false : !(currentOwner instanceof ServerPlayer) || !currentOwner.isSpectator();
|
| 102 | }
|
| 103 |
|
| 104 | public boolean isFoil() {
|
| 105 | return this.entityData.get(ID_FOIL);
|
| 106 | }
|
| 107 |
|
| 108 | @Override
|
| 109 | protected @Nullable EntityHitResult findHitEntity(final Vec3 from, final Vec3 to) {
|
| 110 | return this.dealtDamage ? null : super.findHitEntity(from, to);
|
| 111 | }
|
| 112 |
|
| 113 | @Override
|
| 114 | protected Collection<EntityHitResult> findHitEntities(final Vec3 from, final Vec3 to) {
|
| 115 | EntityHitResult e = this.findHitEntity(from, to);
|
| 116 | return e != null ? List.of(e) : List.of();
|
| 117 | }
|
| 118 |
|
| 119 | @Override
|
| 120 | protected void onHitEntity(final EntityHitResult hitResult) {
|
| 121 | Entity entity = hitResult.getEntity();
|
| 122 | float dmg = 8.0F;
|
| 123 | Entity currentOwner = this.getOwner();
|
| 124 | DamageSource damageSource = this.damageSources().trident(this, currentOwner == null ? this : currentOwner);
|
| 125 | if (this.level() instanceof ServerLevel serverLevel) {
|
| 126 | dmg = EnchantmentHelper.modifyDamage(serverLevel, this.getWeaponItem(), entity, damageSource, dmg);
|
| 127 | }
|
| 128 |
|
| 129 | this.dealtDamage = true;
|
| 130 | boolean wasHurt = entity.hurtOrSimulate(damageSource, dmg);
|
| 131 | if (wasHurt) {
|
| 132 | if (this.level() instanceof ServerLevel serverLevel) {
|
| 133 | EnchantmentHelper.doPostAttackEffectsWithItemSourceOnBreak(serverLevel, entity, damageSource, this.getWeaponItem(), weapon -> this.kill(serverLevel));
|
| 134 | }
|
| 135 |
|
| 136 | if (entity instanceof LivingEntity mob) {
|
| 137 | this.doKnockback(mob, damageSource);
|
| 138 | this.doPostHurtEffects(mob);
|
| 139 | }
|
| 140 | }
|
| 141 |
|
| 142 | if (entity.projectileReceivesSideEffectsOnHit(wasHurt)) {
|
| 143 | this.playSound(SoundEvents.TRIDENT_HIT, 1.0F, 1.0F);
|
| 144 | this.deflect(ProjectileDeflection.REVERSE, entity, this.owner, false, new Vec3(0.02, 0.2, 0.02));
|
| 145 | }
|
| 146 | }
|
| 147 |
|
| 148 | @Override
|
| 149 | protected void hitBlockEnchantmentEffects(final ServerLevel level, final BlockHitResult hitResult, final ItemStack weapon) {
|
| 150 | Vec3 compensatedHitPosition = hitResult.getBlockPos().clampLocationWithin(hitResult.getLocation());
|
| 151 | EnchantmentHelper.onHitBlock(
|
| 152 | level,
|
| 153 | weapon,
|
| 154 | this.getOwner() instanceof LivingEntity livingOwner ? livingOwner : null,
|
| 155 | this,
|
| 156 | null,
|
| 157 | compensatedHitPosition,
|
| 158 | level.getBlockState(hitResult.getBlockPos()),
|
| 159 | item -> this.kill(level)
|
| 160 | );
|
| 161 | }
|
| 162 |
|
| 163 | @Override
|
| 164 | public ItemStack getWeaponItem() {
|
| 165 | return this.getPickupItemStackOrigin();
|
| 166 | }
|
| 167 |
|
| 168 | @Override
|
| 169 | protected boolean tryPickup(final Player player) {
|
| 170 | return super.tryPickup(player) || this.isNoPhysics() && this.ownedBy(player) && player.getInventory().add(this.getPickupItem());
|
| 171 | }
|
| 172 |
|
| 173 | @Override
|
| 174 | protected ItemStack getDefaultPickupItem() {
|
| 175 | return new ItemStack(Items.TRIDENT);
|
| 176 | }
|
| 177 |
|
| 178 | @Override
|
| 179 | protected SoundEvent getDefaultHitGroundSoundEvent() {
|
| 180 | return SoundEvents.TRIDENT_HIT_GROUND;
|
| 181 | }
|
| 182 |
|
| 183 | @Override
|
| 184 | public void playerTouch(final Player player) {
|
| 185 | if (this.ownedBy(player) || this.getOwner() == null) {
|
| 186 | super.playerTouch(player);
|
| 187 | }
|
| 188 | }
|
| 189 |
|
| 190 | @Override
|
| 191 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 192 | super.readAdditionalSaveData(input);
|
| 193 | this.dealtDamage = input.getBooleanOr("DealtDamage", false);
|
| 194 | this.entityData.set(ID_LOYALTY, this.getLoyaltyFromItem(this.getPickupItemStackOrigin()));
|
| 195 | }
|
| 196 |
|
| 197 | @Override
|
| 198 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 199 | super.addAdditionalSaveData(output);
|
| 200 | output.putBoolean("DealtDamage", this.dealtDamage);
|
| 201 | }
|
| 202 |
|
| 203 | private byte getLoyaltyFromItem(final ItemStack tridentItem) {
|
| 204 | return this.level() instanceof ServerLevel serverLevel
|
| 205 | ? (byte)Mth.clamp(EnchantmentHelper.getTridentReturnToOwnerAcceleration(serverLevel, tridentItem, this), 0, 127)
|
| 206 | : 0;
|
| 207 | }
|
| 208 |
|
| 209 | @Override
|
| 210 | public void tickDespawn() {
|
| 211 | int loyalty = this.entityData.get(ID_LOYALTY);
|
| 212 | if (this.pickup != AbstractArrow.Pickup.ALLOWED || loyalty <= 0) {
|
| 213 | super.tickDespawn();
|
| 214 | }
|
| 215 | }
|
| 216 |
|
| 217 | @Override
|
| 218 | protected float getWaterInertia() {
|
| 219 | return 0.99F;
|
| 220 | }
|
| 221 |
|
| 222 | @Override
|
| 223 | public boolean shouldRender(final double camX, final double camY, final double camZ) {
|
| 224 | return true;
|
| 225 | }
|
| 226 | }
|
| 227 |
|