| 1 | package net.minecraft.world.entity.vehicle.minecart;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Pair;
|
| 4 | import java.util.List;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.Direction;
|
| 7 | import net.minecraft.core.Vec3i;
|
| 8 | import net.minecraft.server.level.ServerLevel;
|
| 9 | import net.minecraft.server.level.ServerPlayer;
|
| 10 | import net.minecraft.tags.BlockTags;
|
| 11 | import net.minecraft.util.Mth;
|
| 12 | import net.minecraft.world.entity.Entity;
|
| 13 | import net.minecraft.world.entity.EntitySelector;
|
| 14 | import net.minecraft.world.entity.InterpolationHandler;
|
| 15 | import net.minecraft.world.entity.MoverType;
|
| 16 | import net.minecraft.world.entity.animal.golem.IronGolem;
|
| 17 | import net.minecraft.world.entity.player.Player;
|
| 18 | import net.minecraft.world.level.block.BaseRailBlock;
|
| 19 | import net.minecraft.world.level.block.Blocks;
|
| 20 | import net.minecraft.world.level.block.PoweredRailBlock;
|
| 21 | import net.minecraft.world.level.block.state.BlockState;
|
| 22 | import net.minecraft.world.level.block.state.properties.RailShape;
|
| 23 | import net.minecraft.world.phys.AABB;
|
| 24 | import net.minecraft.world.phys.Vec3;
|
| 25 | import org.jspecify.annotations.Nullable;
|
| 26 |
|
| 27 | public class OldMinecartBehavior extends MinecartBehavior {
|
| 28 | private static final double MINECART_RIDABLE_THRESHOLD = 0.01;
|
| 29 | private static final double MAX_SPEED_IN_WATER = 0.2;
|
| 30 | private static final double MAX_SPEED_ON_LAND = 0.4;
|
| 31 | private static final double ABSOLUTE_MAX_SPEED = 0.4;
|
| 32 | private Vec3 targetDeltaMovement = Vec3.ZERO;
|
| 33 |
|
| 34 | public OldMinecartBehavior(final AbstractMinecart minecart) {
|
| 35 | super(minecart);
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | public void onInterpolationStart(final InterpolationHandler interpolation) {
|
| 40 | this.setDeltaMovement(this.targetDeltaMovement);
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | public void lerpMotion(final Vec3 movement) {
|
| 45 | this.targetDeltaMovement = movement;
|
| 46 | this.setDeltaMovement(this.targetDeltaMovement);
|
| 47 | }
|
| 48 |
|
| 49 | @Override
|
| 50 | public void tick() {
|
| 51 | if (this.level() instanceof ServerLevel level) {
|
| 52 | this.minecart.applyGravity();
|
| 53 | BlockPos var11 = this.minecart.getCurrentBlockPosOrRailBelow();
|
| 54 | BlockState state = this.level().getBlockState(var11);
|
| 55 | boolean onRails = BaseRailBlock.isRail(state);
|
| 56 | this.minecart.setOnRails(onRails);
|
| 57 | if (onRails) {
|
| 58 | this.moveAlongTrack(level);
|
| 59 | if (state.is(Blocks.ACTIVATOR_RAIL)) {
|
| 60 | this.minecart.activateMinecart(level, var11.getX(), var11.getY(), var11.getZ(), state.getValue(PoweredRailBlock.POWERED));
|
| 61 | }
|
| 62 | } else {
|
| 63 | this.minecart.comeOffTrack(level);
|
| 64 | }
|
| 65 |
|
| 66 | this.minecart.applyEffectsFromBlocks();
|
| 67 | this.setXRot(0.0F);
|
| 68 | double xDiff = this.minecart.xo - this.getX();
|
| 69 | double zDiff = this.minecart.zo - this.getZ();
|
| 70 | if (xDiff * xDiff + zDiff * zDiff > 0.001) {
|
| 71 | this.setYRot((float)(Mth.atan2(zDiff, xDiff) * 180.0 / Math.PI));
|
| 72 | if (this.minecart.isFlipped()) {
|
| 73 | this.setYRot(this.getYRot() + 180.0F);
|
| 74 | }
|
| 75 | }
|
| 76 |
|
| 77 | double rotDiff = Mth.wrapDegrees(this.getYRot() - this.minecart.yRotO);
|
| 78 | if (rotDiff < -170.0 || rotDiff >= 170.0) {
|
| 79 | this.setYRot(this.getYRot() + 180.0F);
|
| 80 | this.minecart.setFlipped(!this.minecart.isFlipped());
|
| 81 | }
|
| 82 |
|
| 83 | this.setXRot(this.getXRot() % 360.0F);
|
| 84 | this.setYRot(this.getYRot() % 360.0F);
|
| 85 | this.pushAndPickupEntities();
|
| 86 | } else {
|
| 87 | if (!this.minecart.getInterpolation().hasActiveInterpolation()) {
|
| 88 | this.minecart.reapplyPosition();
|
| 89 | this.setXRot(this.getXRot() % 360.0F);
|
| 90 | this.setYRot(this.getYRot() % 360.0F);
|
| 91 | }
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | @Override
|
| 96 | public void moveAlongTrack(final ServerLevel level) {
|
| 97 | BlockPos pos = this.minecart.getCurrentBlockPosOrRailBelow();
|
| 98 | BlockState state = this.level().getBlockState(pos);
|
| 99 | this.minecart.resetFallDistance();
|
| 100 | double x = this.minecart.getX();
|
| 101 | double y = this.minecart.getY();
|
| 102 | double z = this.minecart.getZ();
|
| 103 | Vec3 oldPos = this.getPos(x, y, z);
|
| 104 | y = pos.getY();
|
| 105 | boolean powerTrack = false;
|
| 106 | boolean haltTrack = false;
|
| 107 | if (state.is(Blocks.POWERED_RAIL)) {
|
| 108 | powerTrack = state.getValue(PoweredRailBlock.POWERED);
|
| 109 | haltTrack = !powerTrack;
|
| 110 | }
|
| 111 |
|
| 112 | double slideSpeed = 0.0078125;
|
| 113 | if (this.minecart.isInWater()) {
|
| 114 | slideSpeed *= 0.2;
|
| 115 | }
|
| 116 |
|
| 117 | Vec3 movement = this.getDeltaMovement();
|
| 118 | RailShape shape = state.getValue(((BaseRailBlock)state.getBlock()).getShapeProperty());
|
| 119 | switch (shape) {
|
| 120 | case ASCENDING_EAST:
|
| 121 | this.setDeltaMovement(movement.add(-slideSpeed, 0.0, 0.0));
|
| 122 | y++;
|
| 123 | break;
|
| 124 | case ASCENDING_WEST:
|
| 125 | this.setDeltaMovement(movement.add(slideSpeed, 0.0, 0.0));
|
| 126 | y++;
|
| 127 | break;
|
| 128 | case ASCENDING_NORTH:
|
| 129 | this.setDeltaMovement(movement.add(0.0, 0.0, slideSpeed));
|
| 130 | y++;
|
| 131 | break;
|
| 132 | case ASCENDING_SOUTH:
|
| 133 | this.setDeltaMovement(movement.add(0.0, 0.0, -slideSpeed));
|
| 134 | y++;
|
| 135 | }
|
| 136 |
|
| 137 | movement = this.getDeltaMovement();
|
| 138 | Pair<Vec3i, Vec3i> exits = AbstractMinecart.exits(shape);
|
| 139 | Vec3i exit0 = exits.getFirst();
|
| 140 | Vec3i exit1 = exits.getSecond();
|
| 141 | double xD = exit1.getX() - exit0.getX();
|
| 142 | double zD = exit1.getZ() - exit0.getZ();
|
| 143 | double length = Math.sqrt(xD * xD + zD * zD);
|
| 144 | double flip = movement.x * xD + movement.z * zD;
|
| 145 | if (flip < 0.0) {
|
| 146 | xD = -xD;
|
| 147 | zD = -zD;
|
| 148 | }
|
| 149 |
|
| 150 | double pow = Math.min(2.0, movement.horizontalDistance());
|
| 151 | movement = new Vec3(pow * xD / length, movement.y, pow * zD / length);
|
| 152 | this.setDeltaMovement(movement);
|
| 153 | Entity controllingPassenger = this.minecart.getFirstPassenger();
|
| 154 | Vec3 moveIntent;
|
| 155 | if (this.minecart.getFirstPassenger() instanceof ServerPlayer player) {
|
| 156 | moveIntent = player.getLastClientMoveIntent();
|
| 157 | } else {
|
| 158 | moveIntent = Vec3.ZERO;
|
| 159 | }
|
| 160 |
|
| 161 | if (controllingPassenger instanceof Player && moveIntent.lengthSqr() > 0.0) {
|
| 162 | Vec3 riderMovement = moveIntent.normalize();
|
| 163 | double ownDist = this.getDeltaMovement().horizontalDistanceSqr();
|
| 164 | if (riderMovement.lengthSqr() > 0.0 && ownDist < 0.01) {
|
| 165 | this.setDeltaMovement(this.getDeltaMovement().add(moveIntent.x * 0.001, 0.0, moveIntent.z * 0.001));
|
| 166 | haltTrack = false;
|
| 167 | }
|
| 168 | }
|
| 169 |
|
| 170 | if (haltTrack) {
|
| 171 | double speedLength = this.getDeltaMovement().horizontalDistance();
|
| 172 | if (speedLength < 0.03) {
|
| 173 | this.setDeltaMovement(Vec3.ZERO);
|
| 174 | } else {
|
| 175 | this.setDeltaMovement(this.getDeltaMovement().multiply(0.5, 0.0, 0.5));
|
| 176 | }
|
| 177 | }
|
| 178 |
|
| 179 | double x0 = pos.getX() + 0.5 + exit0.getX() * 0.5;
|
| 180 | double z0 = pos.getZ() + 0.5 + exit0.getZ() * 0.5;
|
| 181 | double x1 = pos.getX() + 0.5 + exit1.getX() * 0.5;
|
| 182 | double z1 = pos.getZ() + 0.5 + exit1.getZ() * 0.5;
|
| 183 | xD = x1 - x0;
|
| 184 | zD = z1 - z0;
|
| 185 | double progress;
|
| 186 | if (xD == 0.0) {
|
| 187 | progress = z - pos.getZ();
|
| 188 | } else if (zD == 0.0) {
|
| 189 | progress = x - pos.getX();
|
| 190 | } else {
|
| 191 | double xx = x - x0;
|
| 192 | double zz = z - z0;
|
| 193 | progress = (xx * xD + zz * zD) * 2.0;
|
| 194 | }
|
| 195 |
|
| 196 | x = x0 + xD * progress;
|
| 197 | z = z0 + zD * progress;
|
| 198 | this.setPos(x, y, z);
|
| 199 | double scale = this.minecart.isVehicle() ? 0.75 : 1.0;
|
| 200 | double maxSpeed = this.minecart.getMaxSpeed(level);
|
| 201 | movement = this.getDeltaMovement();
|
| 202 | this.minecart.move(MoverType.SELF, new Vec3(Mth.clamp(scale * movement.x, -maxSpeed, maxSpeed), 0.0, Mth.clamp(scale * movement.z, -maxSpeed, maxSpeed)));
|
| 203 | if (exit0.getY() != 0 && Mth.floor(this.minecart.getX()) - pos.getX() == exit0.getX() && Mth.floor(this.minecart.getZ()) - pos.getZ() == exit0.getZ()) {
|
| 204 | this.setPos(this.minecart.getX(), this.minecart.getY() + exit0.getY(), this.minecart.getZ());
|
| 205 | } else if (exit1.getY() != 0 && Mth.floor(this.minecart.getX()) - pos.getX() == exit1.getX() && Mth.floor(this.minecart.getZ()) - pos.getZ() == exit1.getZ()) {
|
| 206 | this.setPos(this.minecart.getX(), this.minecart.getY() + exit1.getY(), this.minecart.getZ());
|
| 207 | }
|
| 208 |
|
| 209 | this.setDeltaMovement(this.minecart.applyNaturalSlowdown(this.getDeltaMovement()));
|
| 210 | Vec3 newPos = this.getPos(this.minecart.getX(), this.minecart.getY(), this.minecart.getZ());
|
| 211 | if (newPos != null && oldPos != null) {
|
| 212 | double speed = (oldPos.y - newPos.y) * 0.05;
|
| 213 | Vec3 vec3 = this.getDeltaMovement();
|
| 214 | double otherPow = vec3.horizontalDistance();
|
| 215 | if (otherPow > 0.0) {
|
| 216 | this.setDeltaMovement(vec3.multiply((otherPow + speed) / otherPow, 1.0, (otherPow + speed) / otherPow));
|
| 217 | }
|
| 218 |
|
| 219 | this.setPos(this.minecart.getX(), newPos.y, this.minecart.getZ());
|
| 220 | }
|
| 221 |
|
| 222 | int xn = Mth.floor(this.minecart.getX());
|
| 223 | int zn = Mth.floor(this.minecart.getZ());
|
| 224 | if (xn != pos.getX() || zn != pos.getZ()) {
|
| 225 | Vec3 vec3 = this.getDeltaMovement();
|
| 226 | double otherPow = vec3.horizontalDistance();
|
| 227 | this.setDeltaMovement(otherPow * (xn - pos.getX()), vec3.y, otherPow * (zn - pos.getZ()));
|
| 228 | }
|
| 229 |
|
| 230 | if (powerTrack) {
|
| 231 | Vec3 vec3 = this.getDeltaMovement();
|
| 232 | double speedLength = vec3.horizontalDistance();
|
| 233 | if (speedLength > 0.01) {
|
| 234 | double speed = 0.06;
|
| 235 | this.setDeltaMovement(vec3.add(vec3.x / speedLength * 0.06, 0.0, vec3.z / speedLength * 0.06));
|
| 236 | } else {
|
| 237 | Vec3 deltaMovement = this.getDeltaMovement();
|
| 238 | double dx = deltaMovement.x;
|
| 239 | double dz = deltaMovement.z;
|
| 240 | if (shape == RailShape.EAST_WEST) {
|
| 241 | if (this.minecart.isRedstoneConductor(pos.west())) {
|
| 242 | dx = 0.02;
|
| 243 | } else if (this.minecart.isRedstoneConductor(pos.east())) {
|
| 244 | dx = -0.02;
|
| 245 | }
|
| 246 | } else {
|
| 247 | if (shape != RailShape.NORTH_SOUTH) {
|
| 248 | return;
|
| 249 | }
|
| 250 |
|
| 251 | if (this.minecart.isRedstoneConductor(pos.north())) {
|
| 252 | dz = 0.02;
|
| 253 | } else if (this.minecart.isRedstoneConductor(pos.south())) {
|
| 254 | dz = -0.02;
|
| 255 | }
|
| 256 | }
|
| 257 |
|
| 258 | this.setDeltaMovement(dx, deltaMovement.y, dz);
|
| 259 | }
|
| 260 | }
|
| 261 | }
|
| 262 |
|
| 263 | public @Nullable Vec3 getPosOffs(double x, double y, double z, final double offs) {
|
| 264 | int xt = Mth.floor(x);
|
| 265 | int yt = Mth.floor(y);
|
| 266 | int zt = Mth.floor(z);
|
| 267 | if (this.level().getBlockState(new BlockPos(xt, yt - 1, zt)).is(BlockTags.RAILS)) {
|
| 268 | yt--;
|
| 269 | }
|
| 270 |
|
| 271 | BlockState state = this.level().getBlockState(new BlockPos(xt, yt, zt));
|
| 272 | if (BaseRailBlock.isRail(state)) {
|
| 273 | RailShape shape = state.getValue(((BaseRailBlock)state.getBlock()).getShapeProperty());
|
| 274 | y = yt;
|
| 275 | if (shape.isSlope()) {
|
| 276 | y = yt + 1;
|
| 277 | }
|
| 278 |
|
| 279 | Pair<Vec3i, Vec3i> exits = AbstractMinecart.exits(shape);
|
| 280 | Vec3i exit0 = exits.getFirst();
|
| 281 | Vec3i exit1 = exits.getSecond();
|
| 282 | double xD = exit1.getX() - exit0.getX();
|
| 283 | double zD = exit1.getZ() - exit0.getZ();
|
| 284 | double dd = Math.sqrt(xD * xD + zD * zD);
|
| 285 | xD /= dd;
|
| 286 | zD /= dd;
|
| 287 | x += xD * offs;
|
| 288 | z += zD * offs;
|
| 289 | if (exit0.getY() != 0 && Mth.floor(x) - xt == exit0.getX() && Mth.floor(z) - zt == exit0.getZ()) {
|
| 290 | y += exit0.getY();
|
| 291 | } else if (exit1.getY() != 0 && Mth.floor(x) - xt == exit1.getX() && Mth.floor(z) - zt == exit1.getZ()) {
|
| 292 | y += exit1.getY();
|
| 293 | }
|
| 294 |
|
| 295 | return this.getPos(x, y, z);
|
| 296 | } else {
|
| 297 | return null;
|
| 298 | }
|
| 299 | }
|
| 300 |
|
| 301 | public @Nullable Vec3 getPos(double x, double y, double z) {
|
| 302 | int xt = Mth.floor(x);
|
| 303 | int yt = Mth.floor(y);
|
| 304 | int zt = Mth.floor(z);
|
| 305 | if (this.level().getBlockState(new BlockPos(xt, yt - 1, zt)).is(BlockTags.RAILS)) {
|
| 306 | yt--;
|
| 307 | }
|
| 308 |
|
| 309 | BlockState state = this.level().getBlockState(new BlockPos(xt, yt, zt));
|
| 310 | if (BaseRailBlock.isRail(state)) {
|
| 311 | RailShape shape = state.getValue(((BaseRailBlock)state.getBlock()).getShapeProperty());
|
| 312 | Pair<Vec3i, Vec3i> exits = AbstractMinecart.exits(shape);
|
| 313 | Vec3i exit0 = exits.getFirst();
|
| 314 | Vec3i exit1 = exits.getSecond();
|
| 315 | double x0 = xt + 0.5 + exit0.getX() * 0.5;
|
| 316 | double y0 = yt + 0.0625 + exit0.getY() * 0.5;
|
| 317 | double z0 = zt + 0.5 + exit0.getZ() * 0.5;
|
| 318 | double x1 = xt + 0.5 + exit1.getX() * 0.5;
|
| 319 | double y1 = yt + 0.0625 + exit1.getY() * 0.5;
|
| 320 | double z1 = zt + 0.5 + exit1.getZ() * 0.5;
|
| 321 | double xD = x1 - x0;
|
| 322 | double yD = (y1 - y0) * 2.0;
|
| 323 | double zD = z1 - z0;
|
| 324 | double progress;
|
| 325 | if (xD == 0.0) {
|
| 326 | progress = z - zt;
|
| 327 | } else if (zD == 0.0) {
|
| 328 | progress = x - xt;
|
| 329 | } else {
|
| 330 | double xx = x - x0;
|
| 331 | double zz = z - z0;
|
| 332 | progress = (xx * xD + zz * zD) * 2.0;
|
| 333 | }
|
| 334 |
|
| 335 | x = x0 + xD * progress;
|
| 336 | y = y0 + yD * progress;
|
| 337 | z = z0 + zD * progress;
|
| 338 | if (yD < 0.0) {
|
| 339 | y++;
|
| 340 | } else if (yD > 0.0) {
|
| 341 | y += 0.5;
|
| 342 | }
|
| 343 |
|
| 344 | return new Vec3(x, y, z);
|
| 345 | } else {
|
| 346 | return null;
|
| 347 | }
|
| 348 | }
|
| 349 |
|
| 350 | @Override
|
| 351 | public double stepAlongTrack(final BlockPos pos, final RailShape shape, final double movementLeft) {
|
| 352 | return 0.0;
|
| 353 | }
|
| 354 |
|
| 355 | @Override
|
| 356 | public boolean pushAndPickupEntities() {
|
| 357 | AABB hitbox = this.minecart.getBoundingBox().inflate(0.2F, 0.0, 0.2F);
|
| 358 | if (this.minecart.isRideable() && this.getDeltaMovement().horizontalDistanceSqr() >= 0.01) {
|
| 359 | List<Entity> entities = this.level().getEntities(this.minecart, hitbox, EntitySelector.pushableBy(this.minecart));
|
| 360 | if (!entities.isEmpty()) {
|
| 361 | for (Entity entity : entities) {
|
| 362 | if (!(entity instanceof Player)
|
| 363 | && !(entity instanceof IronGolem)
|
| 364 | && !(entity instanceof AbstractMinecart)
|
| 365 | && !this.minecart.isVehicle()
|
| 366 | && !entity.isPassenger()) {
|
| 367 | entity.startRiding(this.minecart);
|
| 368 | } else {
|
| 369 | entity.push(this.minecart);
|
| 370 | }
|
| 371 | }
|
| 372 | }
|
| 373 | } else {
|
| 374 | for (Entity entity : this.level().getEntities(this.minecart, hitbox)) {
|
| 375 | if (!this.minecart.hasPassenger(entity) && entity.isPushable() && entity instanceof AbstractMinecart) {
|
| 376 | entity.push(this.minecart);
|
| 377 | }
|
| 378 | }
|
| 379 | }
|
| 380 |
|
| 381 | return false;
|
| 382 | }
|
| 383 |
|
| 384 | @Override
|
| 385 | public Direction getMotionDirection() {
|
| 386 | return this.minecart.isFlipped() ? this.minecart.getDirection().getOpposite().getClockWise() : this.minecart.getDirection().getClockWise();
|
| 387 | }
|
| 388 |
|
| 389 | @Override
|
| 390 | public Vec3 getKnownMovement(final Vec3 knownMovement) {
|
| 391 | return !Double.isNaN(knownMovement.x) && !Double.isNaN(knownMovement.y) && !Double.isNaN(knownMovement.z)
|
| 392 | ? new Vec3(Mth.clamp(knownMovement.x, -0.4, 0.4), knownMovement.y, Mth.clamp(knownMovement.z, -0.4, 0.4))
|
| 393 | : Vec3.ZERO;
|
| 394 | }
|
| 395 |
|
| 396 | @Override
|
| 397 | public double getMaxSpeed(final ServerLevel level) {
|
| 398 | return this.minecart.isInWater() ? 0.2 : 0.4;
|
| 399 | }
|
| 400 |
|
| 401 | @Override
|
| 402 | public double getSlowdownFactor() {
|
| 403 | return this.minecart.isVehicle() ? 0.997 : 0.96;
|
| 404 | }
|
| 405 | }
|
| 406 |
|