| 1 | package net.minecraft.world.entity.vehicle.boat;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import java.util.List;
|
| 5 | import java.util.function.Supplier;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.Direction;
|
| 8 | import net.minecraft.core.particles.ParticleTypes;
|
| 9 | import net.minecraft.network.protocol.game.ServerboundPaddleBoatPacket;
|
| 10 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 11 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 12 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 13 | import net.minecraft.server.level.ServerLevel;
|
| 14 | import net.minecraft.sounds.SoundEvent;
|
| 15 | import net.minecraft.sounds.SoundEvents;
|
| 16 | import net.minecraft.tags.EntityTypeTags;
|
| 17 | import net.minecraft.tags.FluidTags;
|
| 18 | import net.minecraft.util.BlockUtil;
|
| 19 | import net.minecraft.util.Mth;
|
| 20 | import net.minecraft.world.InteractionHand;
|
| 21 | import net.minecraft.world.InteractionResult;
|
| 22 | import net.minecraft.world.entity.Entity;
|
| 23 | import net.minecraft.world.entity.EntityDimensions;
|
| 24 | import net.minecraft.world.entity.EntityEvent;
|
| 25 | import net.minecraft.world.entity.EntitySelector;
|
| 26 | import net.minecraft.world.entity.EntityType;
|
| 27 | import net.minecraft.world.entity.InterpolationHandler;
|
| 28 | import net.minecraft.world.entity.Leashable;
|
| 29 | import net.minecraft.world.entity.LinearInterpolationHandler;
|
| 30 | import net.minecraft.world.entity.LivingEntity;
|
| 31 | import net.minecraft.world.entity.Mob;
|
| 32 | import net.minecraft.world.entity.MoverType;
|
| 33 | import net.minecraft.world.entity.Pose;
|
| 34 | import net.minecraft.world.entity.animal.Animal;
|
| 35 | import net.minecraft.world.entity.player.Player;
|
| 36 | import net.minecraft.world.entity.vehicle.DismountHelper;
|
| 37 | import net.minecraft.world.entity.vehicle.VehicleEntity;
|
| 38 | import net.minecraft.world.item.Item;
|
| 39 | import net.minecraft.world.item.ItemStack;
|
| 40 | import net.minecraft.world.level.Level;
|
| 41 | import net.minecraft.world.level.block.LilyPadBlock;
|
| 42 | import net.minecraft.world.level.block.state.BlockState;
|
| 43 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 44 | import net.minecraft.world.level.material.FluidState;
|
| 45 | import net.minecraft.world.level.storage.ValueInput;
|
| 46 | import net.minecraft.world.level.storage.ValueOutput;
|
| 47 | import net.minecraft.world.phys.AABB;
|
| 48 | import net.minecraft.world.phys.Vec3;
|
| 49 | import net.minecraft.world.phys.shapes.BooleanOp;
|
| 50 | import net.minecraft.world.phys.shapes.Shapes;
|
| 51 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 52 | import org.jspecify.annotations.Nullable;
|
| 53 |
|
| 54 | public abstract class AbstractBoat extends VehicleEntity implements Leashable {
|
| 55 | private static final EntityDataAccessor<Boolean> DATA_ID_PADDLE_LEFT = SynchedEntityData.defineId(AbstractBoat.class, EntityDataSerializers.BOOLEAN);
|
| 56 | private static final EntityDataAccessor<Boolean> DATA_ID_PADDLE_RIGHT = SynchedEntityData.defineId(AbstractBoat.class, EntityDataSerializers.BOOLEAN);
|
| 57 | private static final EntityDataAccessor<Integer> DATA_ID_BUBBLE_TIME = SynchedEntityData.defineId(AbstractBoat.class, EntityDataSerializers.INT);
|
| 58 | public static final int PADDLE_LEFT = 0;
|
| 59 | public static final int PADDLE_RIGHT = 1;
|
| 60 | private static final int TIME_TO_EJECT = 60;
|
| 61 | private static final float PADDLE_SPEED = (float) (Math.PI / 8);
|
| 62 | public static final double PADDLE_SOUND_TIME = (float) (Math.PI / 4);
|
| 63 | public static final int BUBBLE_TIME = 60;
|
| 64 | private final float[] paddlePositions = new float[2];
|
| 65 | private float outOfControlTicks;
|
| 66 | private float deltaRotation;
|
| 67 | private boolean inputLeft;
|
| 68 | private boolean inputRight;
|
| 69 | private boolean inputUp;
|
| 70 | private boolean inputDown;
|
| 71 | private double waterLevel;
|
| 72 | private float landFriction;
|
| 73 | private AbstractBoat.Status status;
|
| 74 | private AbstractBoat.Status oldStatus;
|
| 75 | private double lastYd;
|
| 76 | private boolean isAboveBubbleColumn;
|
| 77 | private boolean bubbleColumnDirectionIsDown;
|
| 78 | private float bubbleMultiplier;
|
| 79 | private float bubbleAngle;
|
| 80 | private float bubbleAngleO;
|
| 81 | private Leashable.@Nullable LeashData leashData;
|
| 82 | private final Supplier<Item> dropItem;
|
| 83 |
|
| 84 | public AbstractBoat(final EntityType<? extends AbstractBoat> type, final Level level, final Supplier<Item> dropItem) {
|
| 85 | super(type, level);
|
| 86 | this.dropItem = dropItem;
|
| 87 | this.blocksBuilding = true;
|
| 88 | }
|
| 89 |
|
| 90 | public void setInitialPos(final double x, final double y, final double z) {
|
| 91 | this.setPos(x, y, z);
|
| 92 | this.xo = x;
|
| 93 | this.yo = y;
|
| 94 | this.zo = z;
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | protected Entity.MovementEmission getMovementEmission() {
|
| 99 | return Entity.MovementEmission.EVENTS;
|
| 100 | }
|
| 101 |
|
| 102 | @Override
|
| 103 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 104 | super.defineSynchedData(entityData);
|
| 105 | entityData.define(DATA_ID_PADDLE_LEFT, false);
|
| 106 | entityData.define(DATA_ID_PADDLE_RIGHT, false);
|
| 107 | entityData.define(DATA_ID_BUBBLE_TIME, 0);
|
| 108 | }
|
| 109 |
|
| 110 | @Override
|
| 111 | public boolean canCollideWith(final Entity entity) {
|
| 112 | return canVehicleCollide(this, entity);
|
| 113 | }
|
| 114 |
|
| 115 | public static boolean canVehicleCollide(final Entity vehicle, final Entity entity) {
|
| 116 | return (entity.canBeCollidedWith(vehicle) || entity.isPushable()) && !vehicle.isPassengerOfSameVehicle(entity);
|
| 117 | }
|
| 118 |
|
| 119 | @Override
|
| 120 | public boolean canBeCollidedWith(final @Nullable Entity other) {
|
| 121 | return true;
|
| 122 | }
|
| 123 |
|
| 124 | @Override
|
| 125 | public boolean isPushable() {
|
| 126 | return true;
|
| 127 | }
|
| 128 |
|
| 129 | @Override
|
| 130 | public Vec3 getRelativePortalPosition(final Direction.Axis axis, final BlockUtil.FoundRectangle portalArea) {
|
| 131 | return LivingEntity.resetForwardDirectionOfRelativePortalPosition(super.getRelativePortalPosition(axis, portalArea));
|
| 132 | }
|
| 133 |
|
| 134 | protected abstract double rideHeight(final EntityDimensions dimensions);
|
| 135 |
|
| 136 | @Override
|
| 137 | protected Vec3 getPassengerAttachmentPoint(final Entity passenger, final EntityDimensions dimensions, final float scale) {
|
| 138 | float offset = this.getSinglePassengerXOffset();
|
| 139 | if (this.getPassengers().size() > 1) {
|
| 140 | int index = this.getPassengers().indexOf(passenger);
|
| 141 | if (index == 0) {
|
| 142 | offset = 0.2F;
|
| 143 | } else {
|
| 144 | offset = -0.6F;
|
| 145 | }
|
| 146 |
|
| 147 | if (passenger instanceof Animal) {
|
| 148 | offset += 0.2F;
|
| 149 | }
|
| 150 | }
|
| 151 |
|
| 152 | return new Vec3(0.0, this.rideHeight(dimensions), offset).yRot(-this.getYRot() * (float) (Math.PI / 180.0));
|
| 153 | }
|
| 154 |
|
| 155 | @Override
|
| 156 | public void onAboveBubbleColumn(final boolean dragDown, final BlockPos pos) {
|
| 157 | if (this.level() instanceof ServerLevel) {
|
| 158 | this.isAboveBubbleColumn = true;
|
| 159 | this.bubbleColumnDirectionIsDown = dragDown;
|
| 160 | if (this.getBubbleTime() == 0) {
|
| 161 | this.setBubbleTime(60);
|
| 162 | }
|
| 163 | }
|
| 164 |
|
| 165 | if (!this.isUnderWater() && this.random.nextInt(100) == 0) {
|
| 166 | this.level()
|
| 167 | .playLocalSound(this.getX(), this.getY(), this.getZ(), this.getSwimSplashSound(), this.getSoundSource(), 1.0F, 0.8F + 0.4F * this.random.nextFloat(), false);
|
| 168 | this.level()
|
| 169 | .addParticle(ParticleTypes.SPLASH, this.getX() + this.random.nextFloat(), this.getY() + 0.7, this.getZ() + this.random.nextFloat(), 0.0, 0.0, 0.0);
|
| 170 | this.gameEvent(GameEvent.SPLASH, this.getControllingPassenger());
|
| 171 | }
|
| 172 | }
|
| 173 |
|
| 174 | @Override
|
| 175 | public void push(final Entity entity) {
|
| 176 | if (entity instanceof AbstractBoat) {
|
| 177 | if (entity.getBoundingBox().minY < this.getBoundingBox().maxY) {
|
| 178 | super.push(entity);
|
| 179 | }
|
| 180 | } else if (entity.getBoundingBox().minY <= this.getBoundingBox().minY) {
|
| 181 | super.push(entity);
|
| 182 | }
|
| 183 | }
|
| 184 |
|
| 185 | @Override
|
| 186 | public void animateHurt(final float yaw) {
|
| 187 | this.setHurtDir(-this.getHurtDir());
|
| 188 | this.setHurtTime(10);
|
| 189 | this.setDamage(this.getDamage() * 11.0F);
|
| 190 | }
|
| 191 |
|
| 192 | @Override
|
| 193 | public boolean isPickable() {
|
| 194 | return !this.isRemoved();
|
| 195 | }
|
| 196 |
|
| 197 | @Override
|
| 198 | protected InterpolationHandler createInterpolationHandler() {
|
| 199 | return LinearInterpolationHandler.create(this, 3);
|
| 200 | }
|
| 201 |
|
| 202 | @Override
|
| 203 | public Direction getMotionDirection() {
|
| 204 | return this.getDirection().getClockWise();
|
| 205 | }
|
| 206 |
|
| 207 | @Override
|
| 208 | public void tick() {
|
| 209 | this.oldStatus = this.status;
|
| 210 | this.status = this.getStatus();
|
| 211 | if (this.status != AbstractBoat.Status.UNDER_WATER && this.status != AbstractBoat.Status.UNDER_FLOWING_WATER) {
|
| 212 | this.outOfControlTicks = 0.0F;
|
| 213 | } else {
|
| 214 | this.outOfControlTicks++;
|
| 215 | }
|
| 216 |
|
| 217 | if (!this.level().isClientSide() && this.outOfControlTicks >= 60.0F) {
|
| 218 | this.ejectPassengers();
|
| 219 | }
|
| 220 |
|
| 221 | if (this.getHurtTime() > 0) {
|
| 222 | this.setHurtTime(this.getHurtTime() - 1);
|
| 223 | }
|
| 224 |
|
| 225 | if (this.getDamage() > 0.0F) {
|
| 226 | this.setDamage(this.getDamage() - 1.0F);
|
| 227 | }
|
| 228 |
|
| 229 | super.tick();
|
| 230 | if (this.isLocalInstanceAuthoritative()) {
|
| 231 | if (!(this.getFirstPassenger() instanceof Player)) {
|
| 232 | this.setPaddleState(false, false);
|
| 233 | }
|
| 234 |
|
| 235 | this.floatBoat();
|
| 236 | if (this.level().isClientSide()) {
|
| 237 | this.controlBoat();
|
| 238 | this.level().sendPacketToServer(new ServerboundPaddleBoatPacket(this.getPaddleState(0), this.getPaddleState(1)));
|
| 239 | }
|
| 240 |
|
| 241 | this.move(MoverType.SELF, this.getDeltaMovement());
|
| 242 | } else {
|
| 243 | this.setDeltaMovement(Vec3.ZERO);
|
| 244 | this.deltaRotation = 0.0F;
|
| 245 | }
|
| 246 |
|
| 247 | this.applyEffectsFromBlocks();
|
| 248 | this.applyEffectsFromBlocks();
|
| 249 | this.tickBubbleColumn();
|
| 250 |
|
| 251 | for (int i = 0; i <= 1; i++) {
|
| 252 | if (this.getPaddleState(i)) {
|
| 253 | if (!this.isSilent()
|
| 254 | && this.paddlePositions[i] % (float) (Math.PI * 2) <= (float) (Math.PI / 4)
|
| 255 | && (this.paddlePositions[i] + (float) (Math.PI / 8)) % (float) (Math.PI * 2) >= (float) (Math.PI / 4)) {
|
| 256 | SoundEvent sound = this.getPaddleSound();
|
| 257 | if (sound != null) {
|
| 258 | Vec3 viewVector = this.getViewVector(1.0F);
|
| 259 | double dx = i == 1 ? -viewVector.z : viewVector.z;
|
| 260 | double dz = i == 1 ? viewVector.x : -viewVector.x;
|
| 261 | this.level().playSound(null, this.getX() + dx, this.getY(), this.getZ() + dz, sound, this.getSoundSource(), 1.0F, 0.8F + 0.4F * this.random.nextFloat());
|
| 262 | }
|
| 263 | }
|
| 264 |
|
| 265 | this.paddlePositions[i] = this.paddlePositions[i] + (float) (Math.PI / 8);
|
| 266 | } else {
|
| 267 | this.paddlePositions[i] = 0.0F;
|
| 268 | }
|
| 269 | }
|
| 270 |
|
| 271 | List<Entity> entities = this.level().getEntities(this, this.getBoundingBox().inflate(0.2F, -0.01F, 0.2F), EntitySelector.pushableBy(this));
|
| 272 | if (!entities.isEmpty()) {
|
| 273 | boolean addNewPassengers = !this.level().isClientSide() && !(this.getControllingPassenger() instanceof Player);
|
| 274 |
|
| 275 | for (Entity entity : entities) {
|
| 276 | if (!entity.hasPassenger(this)) {
|
| 277 | if (addNewPassengers
|
| 278 | && this.getPassengers().size() < this.getMaxPassengers()
|
| 279 | && !entity.isPassenger()
|
| 280 | && this.hasEnoughSpaceFor(entity)
|
| 281 | && entity instanceof LivingEntity
|
| 282 | && !entity.is(EntityTypeTags.CANNOT_BE_PUSHED_ONTO_BOATS)) {
|
| 283 | entity.startRiding(this);
|
| 284 | } else {
|
| 285 | this.push(entity);
|
| 286 | }
|
| 287 | }
|
| 288 | }
|
| 289 | }
|
| 290 | }
|
| 291 |
|
| 292 | private void tickBubbleColumn() {
|
| 293 | if (this.level().isClientSide()) {
|
| 294 | int clientBubbleTime = this.getBubbleTime();
|
| 295 | if (clientBubbleTime > 0) {
|
| 296 | this.bubbleMultiplier += 0.05F;
|
| 297 | } else {
|
| 298 | this.bubbleMultiplier -= 0.1F;
|
| 299 | }
|
| 300 |
|
| 301 | this.bubbleMultiplier = Mth.clamp(this.bubbleMultiplier, 0.0F, 1.0F);
|
| 302 | this.bubbleAngleO = this.bubbleAngle;
|
| 303 | this.bubbleAngle = 10.0F * (float)Math.sin(0.5 * this.tickCount) * this.bubbleMultiplier;
|
| 304 | } else {
|
| 305 | if (!this.isAboveBubbleColumn) {
|
| 306 | this.setBubbleTime(0);
|
| 307 | }
|
| 308 |
|
| 309 | int bubbleTime = this.getBubbleTime();
|
| 310 | if (bubbleTime > 0) {
|
| 311 | this.setBubbleTime(--bubbleTime);
|
| 312 | int diff = 60 - bubbleTime - 1;
|
| 313 | if (diff > 0 && bubbleTime == 0) {
|
| 314 | this.setBubbleTime(0);
|
| 315 | if (this.bubbleColumnDirectionIsDown) {
|
| 316 | this.ejectPassengers();
|
| 317 | this.level().broadcastEntityEvent(this, (byte)71);
|
| 318 | } else {
|
| 319 | this.level().broadcastEntityEvent(this, (byte)72);
|
| 320 | }
|
| 321 |
|
| 322 | this.handleBubbleColumnEffect(this.bubbleColumnDirectionIsDown);
|
| 323 | }
|
| 324 |
|
| 325 | this.isAboveBubbleColumn = false;
|
| 326 | }
|
| 327 | }
|
| 328 | }
|
| 329 |
|
| 330 | @Override
|
| 331 | public void handleEntityEvent(final @EntityEvent.Value byte id) {
|
| 332 | super.handleEntityEvent(id);
|
| 333 | if (id == 71) {
|
| 334 | this.handleBubbleColumnEffect(true);
|
| 335 | } else if (id == 72) {
|
| 336 | this.handleBubbleColumnEffect(false);
|
| 337 | }
|
| 338 | }
|
| 339 |
|
| 340 | protected void handleBubbleColumnEffect(final boolean dragDown) {
|
| 341 | if (this.canSimulateMovement()) {
|
| 342 | Vec3 movement = this.getDeltaMovement();
|
| 343 | if (dragDown) {
|
| 344 | this.setDeltaMovement(movement.add(0.0, -0.7, 0.0));
|
| 345 | } else {
|
| 346 | this.setDeltaMovement(movement.x, 0.6, movement.z);
|
| 347 | }
|
| 348 | }
|
| 349 | }
|
| 350 |
|
| 351 | protected @Nullable SoundEvent getPaddleSound() {
|
| 352 | return switch (this.getStatus()) {
|
| 353 | case IN_WATER, UNDER_WATER, UNDER_FLOWING_WATER -> SoundEvents.BOAT_PADDLE_WATER;
|
| 354 | case ON_LAND -> SoundEvents.BOAT_PADDLE_LAND;
|
| 355 | default -> null;
|
| 356 | };
|
| 357 | }
|
| 358 |
|
| 359 | public void setPaddleState(final boolean left, final boolean right) {
|
| 360 | this.entityData.set(DATA_ID_PADDLE_LEFT, left);
|
| 361 | this.entityData.set(DATA_ID_PADDLE_RIGHT, right);
|
| 362 | }
|
| 363 |
|
| 364 | public float getRowingTime(final int side, final float a) {
|
| 365 | return this.getPaddleState(side) ? Mth.clampedLerp(a, this.paddlePositions[side] - (float) (Math.PI / 8), this.paddlePositions[side]) : 0.0F;
|
| 366 | }
|
| 367 |
|
| 368 | @Override
|
| 369 | public Leashable.@Nullable LeashData getLeashData() {
|
| 370 | return this.leashData;
|
| 371 | }
|
| 372 |
|
| 373 | @Override
|
| 374 | public void setLeashData(final Leashable.@Nullable LeashData leashData) {
|
| 375 | this.leashData = leashData;
|
| 376 | }
|
| 377 |
|
| 378 | @Override
|
| 379 | public Vec3 getLeashOffset() {
|
| 380 | return new Vec3(0.0, 0.88F * this.getBbHeight(), 0.64F * this.getBbWidth());
|
| 381 | }
|
| 382 |
|
| 383 | @Override
|
| 384 | public boolean supportQuadLeash() {
|
| 385 | return true;
|
| 386 | }
|
| 387 |
|
| 388 | @Override
|
| 389 | public Vec3[] getQuadLeashOffsets() {
|
| 390 | return Leashable.createQuadLeashOffsets(this, 0.0, 0.64, 0.382, 0.88);
|
| 391 | }
|
| 392 |
|
| 393 | private AbstractBoat.Status getStatus() {
|
| 394 | AbstractBoat.Status waterStatus = this.isUnderwater();
|
| 395 | if (waterStatus != null) {
|
| 396 | this.waterLevel = this.getBoundingBox().maxY;
|
| 397 | return waterStatus;
|
| 398 | } else if (this.checkInWater()) {
|
| 399 | return AbstractBoat.Status.IN_WATER;
|
| 400 | } else {
|
| 401 | float friction = this.getGroundFriction();
|
| 402 | if (friction > 0.0F) {
|
| 403 | this.landFriction = friction;
|
| 404 | return AbstractBoat.Status.ON_LAND;
|
| 405 | } else {
|
| 406 | return AbstractBoat.Status.IN_AIR;
|
| 407 | }
|
| 408 | }
|
| 409 | }
|
| 410 |
|
| 411 | public float getWaterLevelAbove() {
|
| 412 | AABB aabb = this.getBoundingBox();
|
| 413 | int minX = Mth.floor(aabb.minX);
|
| 414 | int maxX = Mth.ceil(aabb.maxX);
|
| 415 | int minY = Mth.floor(aabb.maxY);
|
| 416 | int maxY = Mth.ceil(aabb.maxY - this.lastYd);
|
| 417 | int minZ = Mth.floor(aabb.minZ);
|
| 418 | int maxZ = Mth.ceil(aabb.maxZ);
|
| 419 | BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
|
| 420 |
|
| 421 | label39:
|
| 422 | for (int y = minY; y < maxY; y++) {
|
| 423 | float blockHeight = 0.0F;
|
| 424 |
|
| 425 | for (int x = minX; x < maxX; x++) {
|
| 426 | for (int z = minZ; z < maxZ; z++) {
|
| 427 | pos.set(x, y, z);
|
| 428 | FluidState fluidState = this.level().getFluidState(pos);
|
| 429 | if (fluidState.is(FluidTags.WATER)) {
|
| 430 | blockHeight = Math.max(blockHeight, fluidState.getHeight(this.level(), pos));
|
| 431 | }
|
| 432 |
|
| 433 | if (blockHeight >= 1.0F) {
|
| 434 | continue label39;
|
| 435 | }
|
| 436 | }
|
| 437 | }
|
| 438 |
|
| 439 | if (blockHeight < 1.0F) {
|
| 440 | return pos.getY() + blockHeight;
|
| 441 | }
|
| 442 | }
|
| 443 |
|
| 444 | return maxY + 1;
|
| 445 | }
|
| 446 |
|
| 447 | public float getGroundFriction() {
|
| 448 | AABB bb = this.getBoundingBox();
|
| 449 | AABB box = new AABB(bb.minX, bb.minY - 0.001, bb.minZ, bb.maxX, bb.minY, bb.maxZ);
|
| 450 | int x0 = Mth.floor(box.minX) - 1;
|
| 451 | int x1 = Mth.ceil(box.maxX) + 1;
|
| 452 | int y0 = Mth.floor(box.minY) - 1;
|
| 453 | int y1 = Mth.ceil(box.maxY) + 1;
|
| 454 | int z0 = Mth.floor(box.minZ) - 1;
|
| 455 | int z1 = Mth.ceil(box.maxZ) + 1;
|
| 456 | VoxelShape boatShape = Shapes.create(box);
|
| 457 | float friction = 0.0F;
|
| 458 | int count = 0;
|
| 459 | BlockPos.MutableBlockPos blockPos = new BlockPos.MutableBlockPos();
|
| 460 |
|
| 461 | for (int x = x0; x < x1; x++) {
|
| 462 | for (int z = z0; z < z1; z++) {
|
| 463 | int edges = (x != x0 && x != x1 - 1 ? 0 : 1) + (z != z0 && z != z1 - 1 ? 0 : 1);
|
| 464 | if (edges != 2) {
|
| 465 | for (int y = y0; y < y1; y++) {
|
| 466 | if (edges <= 0 || y != y0 && y != y1 - 1) {
|
| 467 | blockPos.set(x, y, z);
|
| 468 | BlockState blockState = this.level().getBlockState(blockPos);
|
| 469 | if (!(blockState.getBlock() instanceof LilyPadBlock)
|
| 470 | && Shapes.joinIsNotEmpty(blockState.getCollisionShape(this.level(), blockPos).move(blockPos), boatShape, BooleanOp.AND)) {
|
| 471 | friction += blockState.getBlock().getFriction();
|
| 472 | count++;
|
| 473 | }
|
| 474 | }
|
| 475 | }
|
| 476 | }
|
| 477 | }
|
| 478 | }
|
| 479 |
|
| 480 | return friction / count;
|
| 481 | }
|
| 482 |
|
| 483 | private boolean checkInWater() {
|
| 484 | AABB bb = this.getBoundingBox();
|
| 485 | int minX = Mth.floor(bb.minX);
|
| 486 | int maxX = Mth.ceil(bb.maxX);
|
| 487 | int minY = Mth.floor(bb.minY);
|
| 488 | int maxY = Mth.ceil(bb.minY + 0.001);
|
| 489 | int minZ = Mth.floor(bb.minZ);
|
| 490 | int maxZ = Mth.ceil(bb.maxZ);
|
| 491 | boolean inWater = false;
|
| 492 | this.waterLevel = -Double.MAX_VALUE;
|
| 493 | BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
|
| 494 |
|
| 495 | for (int x = minX; x < maxX; x++) {
|
| 496 | for (int y = minY; y < maxY; y++) {
|
| 497 | for (int z = minZ; z < maxZ; z++) {
|
| 498 | pos.set(x, y, z);
|
| 499 | FluidState fluidState = this.level().getFluidState(pos);
|
| 500 | if (fluidState.is(FluidTags.WATER)) {
|
| 501 | float height = y + fluidState.getHeight(this.level(), pos);
|
| 502 | this.waterLevel = Math.max(height, this.waterLevel);
|
| 503 | inWater |= bb.minY < height;
|
| 504 | }
|
| 505 | }
|
| 506 | }
|
| 507 | }
|
| 508 |
|
| 509 | return inWater;
|
| 510 | }
|
| 511 |
|
| 512 | private AbstractBoat.@Nullable Status isUnderwater() {
|
| 513 | AABB aabb = this.getBoundingBox();
|
| 514 | double maxY = aabb.maxY + 0.001;
|
| 515 | int x0 = Mth.floor(aabb.minX);
|
| 516 | int x1 = Mth.ceil(aabb.maxX);
|
| 517 | int y0 = Mth.floor(aabb.maxY);
|
| 518 | int y1 = Mth.ceil(maxY);
|
| 519 | int z0 = Mth.floor(aabb.minZ);
|
| 520 | int z1 = Mth.ceil(aabb.maxZ);
|
| 521 | boolean underWater = false;
|
| 522 | BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
|
| 523 |
|
| 524 | for (int x = x0; x < x1; x++) {
|
| 525 | for (int y = y0; y < y1; y++) {
|
| 526 | for (int z = z0; z < z1; z++) {
|
| 527 | pos.set(x, y, z);
|
| 528 | FluidState fluidState = this.level().getFluidState(pos);
|
| 529 | if (fluidState.is(FluidTags.WATER) && maxY < pos.getY() + fluidState.getHeight(this.level(), pos)) {
|
| 530 | if (!fluidState.isSource()) {
|
| 531 | return AbstractBoat.Status.UNDER_FLOWING_WATER;
|
| 532 | }
|
| 533 |
|
| 534 | underWater = true;
|
| 535 | }
|
| 536 | }
|
| 537 | }
|
| 538 | }
|
| 539 |
|
| 540 | return underWater ? AbstractBoat.Status.UNDER_WATER : null;
|
| 541 | }
|
| 542 |
|
| 543 | @Override
|
| 544 | protected double getDefaultGravity() {
|
| 545 | return 0.04;
|
| 546 | }
|
| 547 |
|
| 548 | private void floatBoat() {
|
| 549 | double vspeed = -this.getGravity();
|
| 550 | double buoyancy = 0.0;
|
| 551 | float invFriction = 0.05F;
|
| 552 | if (this.oldStatus == AbstractBoat.Status.IN_AIR && this.status != AbstractBoat.Status.IN_AIR && this.status != AbstractBoat.Status.ON_LAND) {
|
| 553 | this.waterLevel = this.getY(1.0);
|
| 554 | double targetY = this.getWaterLevelAbove() - this.getBbHeight() + 0.101;
|
| 555 | if (this.level().noCollision(this, this.getBoundingBox().move(0.0, targetY - this.getY(), 0.0))) {
|
| 556 | this.setPos(this.getX(), targetY, this.getZ());
|
| 557 | this.setDeltaMovement(this.getDeltaMovement().multiply(1.0, 0.0, 1.0));
|
| 558 | this.lastYd = 0.0;
|
| 559 | }
|
| 560 |
|
| 561 | this.status = AbstractBoat.Status.IN_WATER;
|
| 562 | } else {
|
| 563 | if (this.status == AbstractBoat.Status.IN_WATER) {
|
| 564 | buoyancy = (this.waterLevel - this.getY()) / this.getBbHeight();
|
| 565 | invFriction = 0.9F;
|
| 566 | } else if (this.status == AbstractBoat.Status.UNDER_FLOWING_WATER) {
|
| 567 | vspeed = -7.0E-4;
|
| 568 | invFriction = 0.9F;
|
| 569 | } else if (this.status == AbstractBoat.Status.UNDER_WATER) {
|
| 570 | buoyancy = 0.01F;
|
| 571 | invFriction = 0.45F;
|
| 572 | } else if (this.status == AbstractBoat.Status.IN_AIR) {
|
| 573 | invFriction = 0.9F;
|
| 574 | } else if (this.status == AbstractBoat.Status.ON_LAND) {
|
| 575 | invFriction = this.landFriction;
|
| 576 | if (this.getControllingPassenger() instanceof Player) {
|
| 577 | this.landFriction /= 2.0F;
|
| 578 | }
|
| 579 | }
|
| 580 |
|
| 581 | Vec3 movement = this.getDeltaMovement();
|
| 582 | this.setDeltaMovement(movement.x * invFriction, movement.y + vspeed, movement.z * invFriction);
|
| 583 | this.deltaRotation *= invFriction;
|
| 584 | if (buoyancy > 0.0) {
|
| 585 | Vec3 deltaMovement = this.getDeltaMovement();
|
| 586 | this.setDeltaMovement(deltaMovement.x, (deltaMovement.y + buoyancy * (this.getDefaultGravity() / 0.65)) * 0.75, deltaMovement.z);
|
| 587 | }
|
| 588 | }
|
| 589 | }
|
| 590 |
|
| 591 | @Override
|
| 592 | protected float getAirDrag() {
|
| 593 | return 1.0F;
|
| 594 | }
|
| 595 |
|
| 596 | private void controlBoat() {
|
| 597 | if (this.isVehicle()) {
|
| 598 | float acceleration = 0.0F;
|
| 599 | if (this.inputLeft) {
|
| 600 | this.deltaRotation--;
|
| 601 | }
|
| 602 |
|
| 603 | if (this.inputRight) {
|
| 604 | this.deltaRotation++;
|
| 605 | }
|
| 606 |
|
| 607 | if (this.inputRight != this.inputLeft && !this.inputUp && !this.inputDown) {
|
| 608 | acceleration += 0.005F;
|
| 609 | }
|
| 610 |
|
| 611 | this.setYRot(this.getYRot() + this.deltaRotation);
|
| 612 | if (this.inputUp) {
|
| 613 | acceleration += 0.04F;
|
| 614 | }
|
| 615 |
|
| 616 | if (this.inputDown) {
|
| 617 | acceleration -= 0.005F;
|
| 618 | }
|
| 619 |
|
| 620 | this.setDeltaMovement(
|
| 621 | this.getDeltaMovement()
|
| 622 | .add(Mth.sin(-this.getYRot() * (float) (Math.PI / 180.0)) * acceleration, 0.0, Mth.cos(this.getYRot() * (float) (Math.PI / 180.0)) * acceleration)
|
| 623 | );
|
| 624 | this.setPaddleState(this.inputRight && !this.inputLeft || this.inputUp, this.inputLeft && !this.inputRight || this.inputUp);
|
| 625 | }
|
| 626 | }
|
| 627 |
|
| 628 | protected float getSinglePassengerXOffset() {
|
| 629 | return 0.0F;
|
| 630 | }
|
| 631 |
|
| 632 | public boolean hasEnoughSpaceFor(final Entity entity) {
|
| 633 | return entity.getBbWidth() < this.getBbWidth();
|
| 634 | }
|
| 635 |
|
| 636 | @Override
|
| 637 | protected void positionRider(final Entity passenger, final Entity.MoveFunction moveFunction) {
|
| 638 | super.positionRider(passenger, moveFunction);
|
| 639 | if (!passenger.is(EntityTypeTags.CAN_TURN_IN_BOATS)) {
|
| 640 | if (passenger.isLocalInstanceAuthoritative()) {
|
| 641 | passenger.setYRot(passenger.getYRot() + this.deltaRotation);
|
| 642 | passenger.setYHeadRot(passenger.getYHeadRot() + this.deltaRotation);
|
| 643 | }
|
| 644 |
|
| 645 | this.clampRotation(passenger);
|
| 646 | }
|
| 647 | }
|
| 648 |
|
| 649 | @Override
|
| 650 | public Vec3 getDismountLocationForPassenger(final LivingEntity passenger) {
|
| 651 | Vec3 direction = getCollisionHorizontalEscapeVector(this.getBbWidth() * Mth.SQRT_OF_TWO, passenger.getBbWidth(), passenger.getYRot());
|
| 652 | double targetX = this.getX() + direction.x;
|
| 653 | double targetZ = this.getZ() + direction.z;
|
| 654 | BlockPos targetBlockPos = BlockPos.containing(targetX, this.getBoundingBox().maxY, targetZ);
|
| 655 | BlockPos belowBlockPos = targetBlockPos.below();
|
| 656 | if (!this.level().isWaterAt(belowBlockPos)) {
|
| 657 | List<Vec3> targets = Lists.newArrayList();
|
| 658 | double targetFloor = this.level().getBlockFloorHeight(targetBlockPos);
|
| 659 | if (DismountHelper.isBlockFloorValid(targetFloor)) {
|
| 660 | targets.add(new Vec3(targetX, targetBlockPos.getY() + targetFloor, targetZ));
|
| 661 | }
|
| 662 |
|
| 663 | double belowFloor = this.level().getBlockFloorHeight(belowBlockPos);
|
| 664 | if (DismountHelper.isBlockFloorValid(belowFloor)) {
|
| 665 | targets.add(new Vec3(targetX, belowBlockPos.getY() + belowFloor, targetZ));
|
| 666 | }
|
| 667 |
|
| 668 | for (Pose dismountPose : passenger.getDismountPoses()) {
|
| 669 | for (Vec3 target : targets) {
|
| 670 | if (DismountHelper.canDismountTo(this.level(), target, passenger, dismountPose)) {
|
| 671 | passenger.setPose(dismountPose);
|
| 672 | return target;
|
| 673 | }
|
| 674 | }
|
| 675 | }
|
| 676 | }
|
| 677 |
|
| 678 | return super.getDismountLocationForPassenger(passenger);
|
| 679 | }
|
| 680 |
|
| 681 | protected float clampRotation(final Entity passenger) {
|
| 682 | float passengerBodyYRot = this.calculatePassengerBodyYRot(passenger);
|
| 683 | passenger.setYBodyRot(passengerBodyYRot);
|
| 684 | if (passenger instanceof Mob mob) {
|
| 685 | passenger.setYRot(passengerBodyYRot);
|
| 686 | mob.clampHeadRotationToBody();
|
| 687 | return Mth.wrapDegrees(passengerBodyYRot - passenger.getYRot());
|
| 688 | } else {
|
| 689 | float delta = Mth.wrapDegrees(passenger.getYRot() - passengerBodyYRot);
|
| 690 | float maxHeadRotation = 105.0F;
|
| 691 | float targetDelta = Mth.clamp(delta, -105.0F, 105.0F);
|
| 692 | passenger.setYRot(passenger.getYRot() + targetDelta - delta);
|
| 693 | return targetDelta - delta;
|
| 694 | }
|
| 695 | }
|
| 696 |
|
| 697 | private float calculatePassengerBodyYRot(final Entity passenger) {
|
| 698 | return passenger instanceof Animal && this.getPassengers().size() == this.getMaxPassengers()
|
| 699 | ? this.getYRot() + (passenger.getId() % 2 == 0 ? 90.0F : 270.0F)
|
| 700 | : this.getYRot();
|
| 701 | }
|
| 702 |
|
| 703 | @Override
|
| 704 | public void onPassengerTurned(final Entity passenger) {
|
| 705 | float yDelta = this.clampRotation(passenger);
|
| 706 | passenger.yRotO += yDelta;
|
| 707 | }
|
| 708 |
|
| 709 | @Override
|
| 710 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 711 | this.writeLeashData(output, this.leashData);
|
| 712 | }
|
| 713 |
|
| 714 | @Override
|
| 715 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 716 | this.readLeashData(input);
|
| 717 | }
|
| 718 |
|
| 719 | @Override
|
| 720 | public InteractionResult interact(final Player player, final InteractionHand hand, final Vec3 location) {
|
| 721 | InteractionResult superInteraction = super.interact(player, hand, location);
|
| 722 | if (superInteraction != InteractionResult.PASS) {
|
| 723 | return superInteraction;
|
| 724 | } else {
|
| 725 | return player.isSecondaryUseActive() || !(this.outOfControlTicks < 60.0F) || !this.level().isClientSide() && !player.startRiding(this)
|
| 726 | ? InteractionResult.PASS
|
| 727 | : InteractionResult.SUCCESS;
|
| 728 | }
|
| 729 | }
|
| 730 |
|
| 731 | @Override
|
| 732 | public void remove(final Entity.RemovalReason reason) {
|
| 733 | if (!this.level().isClientSide() && reason.shouldDestroy() && this.isLeashed()) {
|
| 734 | this.dropLeash();
|
| 735 | }
|
| 736 |
|
| 737 | super.remove(reason);
|
| 738 | }
|
| 739 |
|
| 740 | @Override
|
| 741 | protected void checkFallDamage(final double ya, final boolean onGround, final BlockState onState, final BlockPos pos) {
|
| 742 | this.lastYd = this.getDeltaMovement().y;
|
| 743 | if (!this.isPassenger()) {
|
| 744 | if (onGround) {
|
| 745 | this.resetFallDistance();
|
| 746 | } else if (!this.level().getFluidState(this.blockPosition().below()).is(FluidTags.WATER) && ya < 0.0) {
|
| 747 | this.fallDistance -= (float)ya;
|
| 748 | }
|
| 749 | }
|
| 750 | }
|
| 751 |
|
| 752 | public boolean getPaddleState(final int side) {
|
| 753 | return this.entityData.get(side == 0 ? DATA_ID_PADDLE_LEFT : DATA_ID_PADDLE_RIGHT) && this.getControllingPassenger() != null;
|
| 754 | }
|
| 755 |
|
| 756 | private void setBubbleTime(final int val) {
|
| 757 | this.entityData.set(DATA_ID_BUBBLE_TIME, val);
|
| 758 | }
|
| 759 |
|
| 760 | private int getBubbleTime() {
|
| 761 | return this.entityData.get(DATA_ID_BUBBLE_TIME);
|
| 762 | }
|
| 763 |
|
| 764 | public float getBubbleAngle(final float a) {
|
| 765 | return Mth.lerp(a, this.bubbleAngleO, this.bubbleAngle);
|
| 766 | }
|
| 767 |
|
| 768 | @Override
|
| 769 | protected boolean canAddPassenger(final Entity passenger) {
|
| 770 | return this.getPassengers().size() < this.getMaxPassengers() && !this.isEyeInFluid(FluidTags.WATER);
|
| 771 | }
|
| 772 |
|
| 773 | protected int getMaxPassengers() {
|
| 774 | return 2;
|
| 775 | }
|
| 776 |
|
| 777 | @Override
|
| 778 | public @Nullable LivingEntity getControllingPassenger() {
|
| 779 | return this.getFirstPassenger() instanceof Player passenger ? passenger : super.getControllingPassenger();
|
| 780 | }
|
| 781 |
|
| 782 | public void setInput(final boolean left, final boolean right, final boolean up, final boolean down) {
|
| 783 | this.inputLeft = left;
|
| 784 | this.inputRight = right;
|
| 785 | this.inputUp = up;
|
| 786 | this.inputDown = down;
|
| 787 | }
|
| 788 |
|
| 789 | @Override
|
| 790 | public boolean isUnderWater() {
|
| 791 | return this.status == AbstractBoat.Status.UNDER_WATER || this.status == AbstractBoat.Status.UNDER_FLOWING_WATER;
|
| 792 | }
|
| 793 |
|
| 794 | @Override
|
| 795 | protected final Item getDropItem() {
|
| 796 | return this.dropItem.get();
|
| 797 | }
|
| 798 |
|
| 799 | @Override
|
| 800 | public final ItemStack getPickResult() {
|
| 801 | return new ItemStack(this.dropItem.get());
|
| 802 | }
|
| 803 |
|
| 804 | @Override
|
| 805 | protected @Nullable AABB modifyPassengerFluidInteractionBox(final AABB passengerBox) {
|
| 806 | if (this.isUnderWater()) {
|
| 807 | return passengerBox;
|
| 808 | }
|
| 809 |
|
| 810 | AABB boatBox = this.getBoundingBox();
|
| 811 | if (boatBox.maxY >= passengerBox.maxY) {
|
| 812 | return null;
|
| 813 | }
|
| 814 |
|
| 815 | double minY = Math.max(passengerBox.minY, boatBox.maxY);
|
| 816 | return new AABB(passengerBox.minX, minY, passengerBox.minZ, passengerBox.maxX, passengerBox.maxY, passengerBox.maxZ);
|
| 817 | }
|
| 818 |
|
| 819 | public enum Status {
|
| 820 | IN_WATER,
|
| 821 | UNDER_WATER,
|
| 822 | UNDER_FLOWING_WATER,
|
| 823 | ON_LAND,
|
| 824 | IN_AIR;
|
| 825 | }
|
| 826 | }
|
| 827 |
|