| 1 | package net.minecraft.world.entity.projectile;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.util.Collections;
|
| 5 | import java.util.List;
|
| 6 | import net.minecraft.advancements.triggers.CriteriaTriggers;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.core.particles.ParticleTypes;
|
| 9 | import net.minecraft.network.protocol.Packet;
|
| 10 | import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
| 11 | import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
|
| 12 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 13 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 14 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 15 | import net.minecraft.server.level.ServerEntity;
|
| 16 | import net.minecraft.server.level.ServerLevel;
|
| 17 | import net.minecraft.server.level.ServerPlayer;
|
| 18 | import net.minecraft.sounds.SoundEvents;
|
| 19 | import net.minecraft.stats.Stats;
|
| 20 | import net.minecraft.tags.FluidTags;
|
| 21 | import net.minecraft.tags.ItemTags;
|
| 22 | import net.minecraft.util.Mth;
|
| 23 | import net.minecraft.util.RandomSource;
|
| 24 | import net.minecraft.world.entity.Entity;
|
| 25 | import net.minecraft.world.entity.EntityEvent;
|
| 26 | import net.minecraft.world.entity.EntityType;
|
| 27 | import net.minecraft.world.entity.EntityTypes;
|
| 28 | import net.minecraft.world.entity.ExperienceOrb;
|
| 29 | import net.minecraft.world.entity.InterpolationHandler;
|
| 30 | import net.minecraft.world.entity.LinearInterpolationHandler;
|
| 31 | import net.minecraft.world.entity.MoverType;
|
| 32 | import net.minecraft.world.entity.item.ItemEntity;
|
| 33 | import net.minecraft.world.entity.player.Player;
|
| 34 | import net.minecraft.world.item.ItemStack;
|
| 35 | import net.minecraft.world.item.Items;
|
| 36 | import net.minecraft.world.level.Level;
|
| 37 | import net.minecraft.world.level.block.Blocks;
|
| 38 | import net.minecraft.world.level.block.state.BlockState;
|
| 39 | import net.minecraft.world.level.material.FluidState;
|
| 40 | import net.minecraft.world.level.storage.ValueInput;
|
| 41 | import net.minecraft.world.level.storage.ValueOutput;
|
| 42 | import net.minecraft.world.level.storage.loot.BuiltInLootTables;
|
| 43 | import net.minecraft.world.level.storage.loot.LootParams;
|
| 44 | import net.minecraft.world.level.storage.loot.LootTable;
|
| 45 | import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
|
| 46 | import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
|
| 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 org.jspecify.annotations.Nullable;
|
| 52 | import org.slf4j.Logger;
|
| 53 |
|
| 54 | /**
|
| 55 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 56 | */
|
| 57 | public class FishingHook extends Projectile {
|
| 58 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 59 | private final RandomSource syncronizedRandom = RandomSource.create();
|
| 60 | private boolean biting;
|
| 61 | private int outOfWaterTime;
|
| 62 | private static final int MAX_OUT_OF_WATER_TIME = 10;
|
| 63 | private static final EntityDataAccessor<Integer> DATA_HOOKED_ENTITY = SynchedEntityData.defineId(FishingHook.class, EntityDataSerializers.INT);
|
| 64 | private static final EntityDataAccessor<Boolean> DATA_BITING = SynchedEntityData.defineId(FishingHook.class, EntityDataSerializers.BOOLEAN);
|
| 65 | private int life;
|
| 66 | private int nibble;
|
| 67 | private int timeUntilLured;
|
| 68 | private int timeUntilHooked;
|
| 69 | private float fishAngle;
|
| 70 | private boolean openWater = true;
|
| 71 | private static final float GRAVITY = 0.03F;
|
| 72 | private @Nullable Entity hookedIn;
|
| 73 | private FishingHook.FishHookState currentState = FishingHook.FishHookState.FLYING;
|
| 74 | private final int luck;
|
| 75 | private final int lureSpeed;
|
| 76 |
|
| 77 | /**
|
| 78 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 79 | */
|
| 80 | public FishingHook(final EntityType<? extends FishingHook> type, final Level level, final int luck, final int lureSpeed) {
|
| 81 | super(type, level);
|
| 82 | this.luck = Math.max(0, luck);
|
| 83 | this.lureSpeed = Math.max(0, lureSpeed);
|
| 84 | }
|
| 85 |
|
| 86 | public FishingHook(final EntityType<? extends FishingHook> type, final Level level) {
|
| 87 | this(type, level, 0, 0);
|
| 88 | }
|
| 89 |
|
| 90 | public FishingHook(final Player player, final Level level, final int luck, final int lureSpeed) {
|
| 91 | this(EntityTypes.FISHING_BOBBER, level, luck, lureSpeed);
|
| 92 | this.setOwner(player);
|
| 93 | float xRot1 = player.getXRot();
|
| 94 | float yRot1 = player.getYRot();
|
| 95 | float yCos = Mth.cos(-yRot1 * (float) (Math.PI / 180.0) - (float) Math.PI);
|
| 96 | float ySin = Mth.sin(-yRot1 * (float) (Math.PI / 180.0) - (float) Math.PI);
|
| 97 | float xCos = -Mth.cos(-xRot1 * (float) (Math.PI / 180.0));
|
| 98 | float xSin = Mth.sin(-xRot1 * (float) (Math.PI / 180.0));
|
| 99 | double x1 = player.getX() - ySin * 0.3;
|
| 100 | double y1 = player.getEyeY();
|
| 101 | double z1 = player.getZ() - yCos * 0.3;
|
| 102 | this.snapTo(x1, y1, z1, yRot1, xRot1);
|
| 103 | Vec3 newMovement = new Vec3(-ySin, Mth.clamp(-(xSin / xCos), -5.0F, 5.0F), -yCos);
|
| 104 | double dist = newMovement.length();
|
| 105 | newMovement = newMovement.multiply(
|
| 106 | 0.6 / dist + this.random.triangle(0.5, 0.0103365), 0.6 / dist + this.random.triangle(0.5, 0.0103365), 0.6 / dist + this.random.triangle(0.5, 0.0103365)
|
| 107 | );
|
| 108 | this.setDeltaMovement(newMovement);
|
| 109 | this.setYRot((float)(Mth.atan2(newMovement.x, newMovement.z) * 180.0F / (float)Math.PI));
|
| 110 | this.setXRot((float)(Mth.atan2(newMovement.y, newMovement.horizontalDistance()) * 180.0F / (float)Math.PI));
|
| 111 | this.yRotO = this.getYRot();
|
| 112 | this.xRotO = this.getXRot();
|
| 113 | }
|
| 114 |
|
| 115 | @Override
|
| 116 | protected InterpolationHandler createInterpolationHandler() {
|
| 117 | return LinearInterpolationHandler.create(this);
|
| 118 | }
|
| 119 |
|
| 120 | @Override
|
| 121 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 122 | entityData.define(DATA_HOOKED_ENTITY, 0);
|
| 123 | entityData.define(DATA_BITING, false);
|
| 124 | }
|
| 125 |
|
| 126 | @Override
|
| 127 | protected boolean shouldBounceOnWorldBorder() {
|
| 128 | return true;
|
| 129 | }
|
| 130 |
|
| 131 | @Override
|
| 132 | public void onSyncedDataUpdated(final EntityDataAccessor<?> accessor) {
|
| 133 | if (DATA_HOOKED_ENTITY.equals(accessor)) {
|
| 134 | int id = this.getEntityData().get(DATA_HOOKED_ENTITY);
|
| 135 | this.hookedIn = id > 0 ? this.level().getEntity(id - 1) : null;
|
| 136 | }
|
| 137 |
|
| 138 | if (DATA_BITING.equals(accessor)) {
|
| 139 | this.biting = this.getEntityData().get(DATA_BITING);
|
| 140 | if (this.biting) {
|
| 141 | this.setDeltaMovement(this.getDeltaMovement().x, -0.4F * Mth.nextFloat(this.syncronizedRandom, 0.6F, 1.0F), this.getDeltaMovement().z);
|
| 142 | }
|
| 143 | }
|
| 144 |
|
| 145 | super.onSyncedDataUpdated(accessor);
|
| 146 | }
|
| 147 |
|
| 148 | @Override
|
| 149 | public boolean shouldRenderAtSqrDistance(final double distance) {
|
| 150 | double size = 64.0;
|
| 151 | return distance < 4096.0;
|
| 152 | }
|
| 153 |
|
| 154 | @Override
|
| 155 | public void tick() {
|
| 156 | this.syncronizedRandom.setSeed(this.getUUID().getLeastSignificantBits() ^ this.level().getGameTime());
|
| 157 | super.tick();
|
| 158 | Player owner = this.getPlayerOwner();
|
| 159 | if (owner == null) {
|
| 160 | this.discard();
|
| 161 | } else if (this.level().isClientSide() || !this.shouldStopFishing(owner)) {
|
| 162 | if (this.onGround()) {
|
| 163 | this.life++;
|
| 164 | if (this.life >= 1200) {
|
| 165 | this.discard();
|
| 166 | return;
|
| 167 | }
|
| 168 | } else {
|
| 169 | this.life = 0;
|
| 170 | }
|
| 171 |
|
| 172 | float liquidHeight = 0.0F;
|
| 173 | BlockPos blockPos = this.blockPosition();
|
| 174 | FluidState fluidState = this.level().getFluidState(blockPos);
|
| 175 | if (fluidState.is(FluidTags.WATER)) {
|
| 176 | liquidHeight = fluidState.getHeight(this.level(), blockPos);
|
| 177 | }
|
| 178 |
|
| 179 | boolean isInWater = liquidHeight > 0.0F;
|
| 180 | if (this.currentState == FishingHook.FishHookState.FLYING) {
|
| 181 | if (this.hookedIn != null) {
|
| 182 | this.setDeltaMovement(Vec3.ZERO);
|
| 183 | this.currentState = FishingHook.FishHookState.HOOKED_IN_ENTITY;
|
| 184 | return;
|
| 185 | }
|
| 186 |
|
| 187 | if (isInWater) {
|
| 188 | this.setDeltaMovement(this.getDeltaMovement().multiply(0.3, 0.2, 0.3));
|
| 189 | this.currentState = FishingHook.FishHookState.BOBBING;
|
| 190 | return;
|
| 191 | }
|
| 192 |
|
| 193 | this.checkCollision();
|
| 194 | } else {
|
| 195 | if (this.currentState == FishingHook.FishHookState.HOOKED_IN_ENTITY) {
|
| 196 | if (this.hookedIn != null) {
|
| 197 | if (!this.hookedIn.isRemoved() && this.hookedIn.canInteractWithLevel() && this.hookedIn.level().dimension() == this.level().dimension()) {
|
| 198 | this.setPos(this.hookedIn.getX(), this.hookedIn.getY(0.8), this.hookedIn.getZ());
|
| 199 | } else {
|
| 200 | this.setHookedEntity(null);
|
| 201 | this.currentState = FishingHook.FishHookState.FLYING;
|
| 202 | }
|
| 203 | }
|
| 204 |
|
| 205 | return;
|
| 206 | }
|
| 207 |
|
| 208 | if (this.currentState == FishingHook.FishHookState.BOBBING) {
|
| 209 | Vec3 movement = this.getDeltaMovement();
|
| 210 | double force = this.getY() + movement.y - blockPos.getY() - liquidHeight;
|
| 211 | if (Math.abs(force) < 0.01) {
|
| 212 | force += Math.signum(force) * 0.1;
|
| 213 | }
|
| 214 |
|
| 215 | this.setDeltaMovement(movement.x * 0.9, movement.y - force * this.random.nextFloat() * 0.2, movement.z * 0.9);
|
| 216 | if (this.nibble <= 0 && this.timeUntilHooked <= 0) {
|
| 217 | this.openWater = true;
|
| 218 | } else {
|
| 219 | this.openWater = this.openWater && this.outOfWaterTime < 10 && this.calculateOpenWater(blockPos);
|
| 220 | }
|
| 221 |
|
| 222 | if (isInWater) {
|
| 223 | this.outOfWaterTime = Math.max(0, this.outOfWaterTime - 1);
|
| 224 | if (this.biting) {
|
| 225 | this.setDeltaMovement(this.getDeltaMovement().add(0.0, -0.1 * this.syncronizedRandom.nextFloat() * this.syncronizedRandom.nextFloat(), 0.0));
|
| 226 | }
|
| 227 |
|
| 228 | if (!this.level().isClientSide()) {
|
| 229 | this.catchingFish(blockPos);
|
| 230 | }
|
| 231 | } else {
|
| 232 | this.outOfWaterTime = Math.min(10, this.outOfWaterTime + 1);
|
| 233 | }
|
| 234 | }
|
| 235 | }
|
| 236 |
|
| 237 | if (!fluidState.is(FluidTags.WATER) && !this.onGround() && this.hookedIn == null) {
|
| 238 | this.setDeltaMovement(this.getDeltaMovement().add(0.0, -this.getDefaultGravity(), 0.0));
|
| 239 | }
|
| 240 |
|
| 241 | this.move(MoverType.SELF, this.getDeltaMovement());
|
| 242 | this.applyEffectsFromBlocks();
|
| 243 | this.updateRotation();
|
| 244 | if (this.currentState == FishingHook.FishHookState.FLYING && (this.onGround() || this.horizontalCollision)) {
|
| 245 | this.setDeltaMovement(Vec3.ZERO);
|
| 246 | }
|
| 247 |
|
| 248 | double inertia = 0.92;
|
| 249 | this.setDeltaMovement(this.getDeltaMovement().scale(0.92));
|
| 250 | this.reapplyPosition();
|
| 251 | }
|
| 252 | }
|
| 253 |
|
| 254 | private boolean shouldStopFishing(final Player owner) {
|
| 255 | if (owner.canInteractWithLevel()) {
|
| 256 | ItemStack selectedItem = owner.getMainHandItem();
|
| 257 | ItemStack selectedItemOffHand = owner.getOffhandItem();
|
| 258 | boolean mainHandIsFishing = selectedItem.is(Items.FISHING_ROD);
|
| 259 | boolean offHandIsFishing = selectedItemOffHand.is(Items.FISHING_ROD);
|
| 260 | if ((mainHandIsFishing || offHandIsFishing) && this.distanceToSqr(owner) <= 1024.0) {
|
| 261 | return false;
|
| 262 | }
|
| 263 | }
|
| 264 |
|
| 265 | this.discard();
|
| 266 | return true;
|
| 267 | }
|
| 268 |
|
| 269 | private void checkCollision() {
|
| 270 | HitResult hitResult = ProjectileUtil.getHitResultOnMoveVector(this, this::canHitEntity);
|
| 271 | this.hitTargetOrDeflectSelf(hitResult);
|
| 272 | }
|
| 273 |
|
| 274 | @Override
|
| 275 | protected boolean canHitEntity(final Entity entity) {
|
| 276 | return super.canHitEntity(entity) || entity.isAlive() && entity instanceof ItemEntity;
|
| 277 | }
|
| 278 |
|
| 279 | @Override
|
| 280 | protected void onHitEntity(final EntityHitResult hitResult) {
|
| 281 | super.onHitEntity(hitResult);
|
| 282 | if (!this.level().isClientSide()) {
|
| 283 | this.setHookedEntity(hitResult.getEntity());
|
| 284 | }
|
| 285 | }
|
| 286 |
|
| 287 | @Override
|
| 288 | protected void onHitBlock(final BlockHitResult hitResult) {
|
| 289 | super.onHitBlock(hitResult);
|
| 290 | this.setDeltaMovement(this.getDeltaMovement().normalize().scale(hitResult.distanceTo(this)));
|
| 291 | }
|
| 292 |
|
| 293 | private void setHookedEntity(final @Nullable Entity hookedIn) {
|
| 294 | this.hookedIn = hookedIn;
|
| 295 | this.getEntityData().set(DATA_HOOKED_ENTITY, hookedIn == null ? 0 : hookedIn.getId() + 1);
|
| 296 | }
|
| 297 |
|
| 298 | private void catchingFish(final BlockPos blockPos) {
|
| 299 | ServerLevel serverLevel = (ServerLevel)this.level();
|
| 300 | int fishingSpeed = 1;
|
| 301 | BlockPos above = blockPos.above();
|
| 302 | if (this.random.nextFloat() < 0.25F && this.level().isRainingAt(above)) {
|
| 303 | fishingSpeed++;
|
| 304 | }
|
| 305 |
|
| 306 | if (this.random.nextFloat() < 0.5F && !this.level().canSeeSky(above)) {
|
| 307 | fishingSpeed--;
|
| 308 | }
|
| 309 |
|
| 310 | if (this.nibble > 0) {
|
| 311 | this.nibble--;
|
| 312 | if (this.nibble <= 0) {
|
| 313 | this.timeUntilLured = 0;
|
| 314 | this.timeUntilHooked = 0;
|
| 315 | this.getEntityData().set(DATA_BITING, false);
|
| 316 | }
|
| 317 | } else if (this.timeUntilHooked > 0) {
|
| 318 | this.timeUntilHooked -= fishingSpeed;
|
| 319 | if (this.timeUntilHooked > 0) {
|
| 320 | this.fishAngle = this.fishAngle + (float)this.random.triangle(0.0, 9.188);
|
| 321 | float angle = this.fishAngle * (float) (Math.PI / 180.0);
|
| 322 | float angleSin = Mth.sin(angle);
|
| 323 | float angleCos = Mth.cos(angle);
|
| 324 | double fishX = this.getX() + angleSin * this.timeUntilHooked * 0.1F;
|
| 325 | double fishY = Mth.floor(this.getY()) + 1.0F;
|
| 326 | double fishZ = this.getZ() + angleCos * this.timeUntilHooked * 0.1F;
|
| 327 | BlockState splashBlockState = serverLevel.getBlockState(BlockPos.containing(fishX, fishY - 1.0, fishZ));
|
| 328 | if (splashBlockState.is(Blocks.WATER)) {
|
| 329 | if (this.random.nextFloat() < 0.15F) {
|
| 330 | serverLevel.sendParticles(ParticleTypes.BUBBLE, fishX, fishY - 0.1F, fishZ, 1, angleSin, 0.1, angleCos, 0.0);
|
| 331 | }
|
| 332 |
|
| 333 | float particleXMovement = angleSin * 0.04F;
|
| 334 | float particleZMovement = angleCos * 0.04F;
|
| 335 | serverLevel.sendParticles(ParticleTypes.FISHING, fishX, fishY, fishZ, 0, particleZMovement, 0.01, -particleXMovement, 1.0);
|
| 336 | serverLevel.sendParticles(ParticleTypes.FISHING, fishX, fishY, fishZ, 0, -particleZMovement, 0.01, particleXMovement, 1.0);
|
| 337 | }
|
| 338 | } else {
|
| 339 | this.playSound(SoundEvents.FISHING_BOBBER_SPLASH, 0.25F, 1.0F + (this.random.nextFloat() - this.random.nextFloat()) * 0.4F);
|
| 340 | double y = this.getY() + 0.5;
|
| 341 | serverLevel.sendParticles(
|
| 342 | ParticleTypes.BUBBLE, this.getX(), y, this.getZ(), (int)(1.0F + this.getBbWidth() * 20.0F), this.getBbWidth(), 0.0, this.getBbWidth(), 0.2F
|
| 343 | );
|
| 344 | serverLevel.sendParticles(
|
| 345 | ParticleTypes.FISHING, this.getX(), y, this.getZ(), (int)(1.0F + this.getBbWidth() * 20.0F), this.getBbWidth(), 0.0, this.getBbWidth(), 0.2F
|
| 346 | );
|
| 347 | this.nibble = Mth.nextInt(this.random, 20, 40);
|
| 348 | this.getEntityData().set(DATA_BITING, true);
|
| 349 | }
|
| 350 | } else if (this.timeUntilLured > 0) {
|
| 351 | this.timeUntilLured -= fishingSpeed;
|
| 352 | float teaseChance = 0.15F;
|
| 353 | if (this.timeUntilLured < 20) {
|
| 354 | teaseChance += (20 - this.timeUntilLured) * 0.05F;
|
| 355 | } else if (this.timeUntilLured < 40) {
|
| 356 | teaseChance += (40 - this.timeUntilLured) * 0.02F;
|
| 357 | } else if (this.timeUntilLured < 60) {
|
| 358 | teaseChance += (60 - this.timeUntilLured) * 0.01F;
|
| 359 | }
|
| 360 |
|
| 361 | if (this.random.nextFloat() < teaseChance) {
|
| 362 | float angle = Mth.nextFloat(this.random, 0.0F, 360.0F) * (float) (Math.PI / 180.0);
|
| 363 | float dist = Mth.nextFloat(this.random, 25.0F, 60.0F);
|
| 364 | double fishX = this.getX() + Mth.sin(angle) * dist * 0.1;
|
| 365 | double fishY = Mth.floor(this.getY()) + 1.0F;
|
| 366 | double fishZ = this.getZ() + Mth.cos(angle) * dist * 0.1;
|
| 367 | BlockState splashBlockState = serverLevel.getBlockState(BlockPos.containing(fishX, fishY - 1.0, fishZ));
|
| 368 | if (splashBlockState.is(Blocks.WATER)) {
|
| 369 | serverLevel.sendParticles(ParticleTypes.SPLASH, fishX, fishY, fishZ, 2 + this.random.nextInt(2), 0.1F, 0.0, 0.1F, 0.0);
|
| 370 | }
|
| 371 | }
|
| 372 |
|
| 373 | if (this.timeUntilLured <= 0) {
|
| 374 | this.fishAngle = Mth.nextFloat(this.random, 0.0F, 360.0F);
|
| 375 | this.timeUntilHooked = Mth.nextInt(this.random, 20, 80);
|
| 376 | }
|
| 377 | } else {
|
| 378 | this.timeUntilLured = Mth.nextInt(this.random, 100, 600);
|
| 379 | this.timeUntilLured = this.timeUntilLured - this.lureSpeed;
|
| 380 | }
|
| 381 | }
|
| 382 |
|
| 383 | private boolean calculateOpenWater(final BlockPos blockPos) {
|
| 384 | FishingHook.OpenWaterType previousLayer = FishingHook.OpenWaterType.INVALID;
|
| 385 |
|
| 386 | for (int y = -1; y <= 2; y++) {
|
| 387 | FishingHook.OpenWaterType layer = this.getOpenWaterTypeForArea(blockPos.offset(-2, y, -2), blockPos.offset(2, y, 2));
|
| 388 | switch (layer) {
|
| 389 | case ABOVE_WATER:
|
| 390 | if (previousLayer == FishingHook.OpenWaterType.INVALID) {
|
| 391 | return false;
|
| 392 | }
|
| 393 | break;
|
| 394 | case INSIDE_WATER:
|
| 395 | if (previousLayer == FishingHook.OpenWaterType.ABOVE_WATER) {
|
| 396 | return false;
|
| 397 | }
|
| 398 | break;
|
| 399 | case INVALID:
|
| 400 | return false;
|
| 401 | }
|
| 402 |
|
| 403 | previousLayer = layer;
|
| 404 | }
|
| 405 |
|
| 406 | return true;
|
| 407 | }
|
| 408 |
|
| 409 | private FishingHook.OpenWaterType getOpenWaterTypeForArea(final BlockPos from, final BlockPos to) {
|
| 410 | return BlockPos.betweenClosedStream(from, to)
|
| 411 | .map(this::getOpenWaterTypeForBlock)
|
| 412 | .reduce((a, b) -> a == b ? a : FishingHook.OpenWaterType.INVALID)
|
| 413 | .orElse(FishingHook.OpenWaterType.INVALID);
|
| 414 | }
|
| 415 |
|
| 416 | private FishingHook.OpenWaterType getOpenWaterTypeForBlock(final BlockPos pos) {
|
| 417 | BlockState state = this.level().getBlockState(pos);
|
| 418 | if (!state.isAir() && !state.is(Blocks.LILY_PAD)) {
|
| 419 | FluidState fluidState = state.getFluidState();
|
| 420 | return fluidState.is(FluidTags.WATER) && fluidState.isSource() && state.getCollisionShape(this.level(), pos).isEmpty()
|
| 421 | ? FishingHook.OpenWaterType.INSIDE_WATER
|
| 422 | : FishingHook.OpenWaterType.INVALID;
|
| 423 | } else {
|
| 424 | return FishingHook.OpenWaterType.ABOVE_WATER;
|
| 425 | }
|
| 426 | }
|
| 427 |
|
| 428 | public boolean isOpenWaterFishing() {
|
| 429 | return this.openWater;
|
| 430 | }
|
| 431 |
|
| 432 | @Override
|
| 433 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 434 | }
|
| 435 |
|
| 436 | @Override
|
| 437 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 438 | }
|
| 439 |
|
| 440 | public int retrieve(final ItemStack rod) {
|
| 441 | Player owner = this.getPlayerOwner();
|
| 442 | if (!this.level().isClientSide() && owner != null && !this.shouldStopFishing(owner)) {
|
| 443 | int dmg = 0;
|
| 444 | if (this.hookedIn != null) {
|
| 445 | this.pullEntity(this.hookedIn);
|
| 446 | CriteriaTriggers.FISHING_ROD_HOOKED.trigger((ServerPlayer)owner, rod, this, Collections.emptyList());
|
| 447 | this.level().broadcastEntityEvent(this, (byte)31);
|
| 448 | dmg = this.hookedIn instanceof ItemEntity ? 3 : 5;
|
| 449 | } else if (this.nibble > 0) {
|
| 450 | LootParams params = new LootParams.Builder((ServerLevel)this.level())
|
| 451 | .withParameter(LootContextParams.ORIGIN, this.position())
|
| 452 | .withParameter(LootContextParams.TOOL, rod)
|
| 453 | .withParameter(LootContextParams.THIS_ENTITY, this)
|
| 454 | .withLuck(this.luck + owner.getLuck())
|
| 455 | .create(LootContextParamSets.FISHING);
|
| 456 | LootTable lootTable = this.level().getServer().reloadableRegistries().getLootTable(BuiltInLootTables.FISHING);
|
| 457 | List<ItemStack> items = lootTable.getRandomItems(params);
|
| 458 | CriteriaTriggers.FISHING_ROD_HOOKED.trigger((ServerPlayer)owner, rod, this, items);
|
| 459 |
|
| 460 | for (ItemStack itemStack : items) {
|
| 461 | ItemEntity entity = new ItemEntity(this.level(), this.getX(), this.getY(), this.getZ(), itemStack);
|
| 462 | double xa = owner.getX() - this.getX();
|
| 463 | double ya = owner.getY() - this.getY();
|
| 464 | double za = owner.getZ() - this.getZ();
|
| 465 | double speed = 0.1;
|
| 466 | entity.setDeltaMovement(xa * 0.1, ya * 0.1 + Math.sqrt(Math.sqrt(xa * xa + ya * ya + za * za)) * 0.08, za * 0.1);
|
| 467 | this.level().addFreshEntity(entity);
|
| 468 | owner.level().addFreshEntity(new ExperienceOrb(owner.level(), owner.getX(), owner.getY() + 0.5, owner.getZ() + 0.5, this.random.nextInt(6) + 1));
|
| 469 | if (itemStack.is(ItemTags.FISHES)) {
|
| 470 | owner.awardStat(Stats.FISH_CAUGHT, 1);
|
| 471 | }
|
| 472 | }
|
| 473 |
|
| 474 | dmg = 1;
|
| 475 | }
|
| 476 |
|
| 477 | if (this.onGround()) {
|
| 478 | dmg = 2;
|
| 479 | }
|
| 480 |
|
| 481 | this.discard();
|
| 482 | return dmg;
|
| 483 | } else {
|
| 484 | return 0;
|
| 485 | }
|
| 486 | }
|
| 487 |
|
| 488 | @Override
|
| 489 | public void handleEntityEvent(final @EntityEvent.Value byte id) {
|
| 490 | if (id == 31 && this.hookedIn != null) {
|
| 491 | this.pullEntity(this.hookedIn);
|
| 492 | }
|
| 493 |
|
| 494 | super.handleEntityEvent(id);
|
| 495 | }
|
| 496 |
|
| 497 | @Override
|
| 498 | protected double getDefaultGravity() {
|
| 499 | return 0.03F;
|
| 500 | }
|
| 501 |
|
| 502 | protected void pullEntity(final Entity entity) {
|
| 503 | Entity owner = this.getOwner();
|
| 504 | if (owner != null && entity.canSimulateMovement()) {
|
| 505 | Vec3 delta = new Vec3(owner.getX() - this.getX(), owner.getY() - this.getY(), owner.getZ() - this.getZ()).scale(0.1);
|
| 506 | entity.setDeltaMovement(entity.getDeltaMovement().add(delta));
|
| 507 | }
|
| 508 | }
|
| 509 |
|
| 510 | @Override
|
| 511 | protected Entity.MovementEmission getMovementEmission() {
|
| 512 | return Entity.MovementEmission.NONE;
|
| 513 | }
|
| 514 |
|
| 515 | @Override
|
| 516 | public void remove(final Entity.RemovalReason reason) {
|
| 517 | this.updateOwnerInfo(null);
|
| 518 | super.remove(reason);
|
| 519 | }
|
| 520 |
|
| 521 | @Override
|
| 522 | public void onClientRemoval() {
|
| 523 | this.updateOwnerInfo(null);
|
| 524 | }
|
| 525 |
|
| 526 | @Override
|
| 527 | public void setOwner(final @Nullable Entity owner) {
|
| 528 | super.setOwner(owner);
|
| 529 | this.updateOwnerInfo(this);
|
| 530 | }
|
| 531 |
|
| 532 | private void updateOwnerInfo(final @Nullable FishingHook hook) {
|
| 533 | Player owner = this.getPlayerOwner();
|
| 534 | if (owner != null) {
|
| 535 | owner.fishing = hook;
|
| 536 | }
|
| 537 | }
|
| 538 |
|
| 539 | public @Nullable Player getPlayerOwner() {
|
| 540 | return this.getOwner() instanceof Player player ? player : null;
|
| 541 | }
|
| 542 |
|
| 543 | public @Nullable Entity getHookedIn() {
|
| 544 | return this.hookedIn;
|
| 545 | }
|
| 546 |
|
| 547 | @Override
|
| 548 | public boolean canUsePortal(final boolean ignorePassenger) {
|
| 549 | return false;
|
| 550 | }
|
| 551 |
|
| 552 | @Override
|
| 553 | public Packet<ClientGamePacketListener> getAddEntityPacket(final ServerEntity serverEntity) {
|
| 554 | Entity owner = this.getOwner();
|
| 555 | return new ClientboundAddEntityPacket(this, serverEntity, owner == null ? this.getId() : owner.getId());
|
| 556 | }
|
| 557 |
|
| 558 | @Override
|
| 559 | public void recreateFromPacket(final ClientboundAddEntityPacket packet) {
|
| 560 | super.recreateFromPacket(packet);
|
| 561 | if (this.getPlayerOwner() == null) {
|
| 562 | int ownerId = packet.getData();
|
| 563 | LOGGER.error("Failed to recreate fishing hook on client. {} (id: {}) is not a valid owner.", this.level().getEntity(ownerId), ownerId);
|
| 564 | this.discard();
|
| 565 | }
|
| 566 | }
|
| 567 |
|
| 568 | private enum FishHookState {
|
| 569 | FLYING,
|
| 570 | HOOKED_IN_ENTITY,
|
| 571 | BOBBING;
|
| 572 | }
|
| 573 |
|
| 574 | private enum OpenWaterType {
|
| 575 | ABOVE_WATER,
|
| 576 | INSIDE_WATER,
|
| 577 | INVALID;
|
| 578 | }
|
| 579 | }
|
| 580 |
|