| 1 | package net.minecraft.world.entity.vehicle.minecart;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Pair;
|
| 4 | import io.netty.buffer.ByteBuf;
|
| 5 | import java.util.LinkedList;
|
| 6 | import java.util.List;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.core.Vec3i;
|
| 9 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 10 | import net.minecraft.network.codec.StreamCodec;
|
| 11 | import net.minecraft.server.level.ServerLevel;
|
| 12 | import net.minecraft.server.level.ServerPlayer;
|
| 13 | import net.minecraft.util.Mth;
|
| 14 | import net.minecraft.world.entity.Entity;
|
| 15 | import net.minecraft.world.entity.EntitySelector;
|
| 16 | import net.minecraft.world.entity.MoverType;
|
| 17 | import net.minecraft.world.entity.animal.golem.IronGolem;
|
| 18 | import net.minecraft.world.entity.player.Player;
|
| 19 | import net.minecraft.world.level.block.BaseRailBlock;
|
| 20 | import net.minecraft.world.level.block.Blocks;
|
| 21 | import net.minecraft.world.level.block.PoweredRailBlock;
|
| 22 | import net.minecraft.world.level.block.state.BlockState;
|
| 23 | import net.minecraft.world.level.block.state.properties.RailShape;
|
| 24 | import net.minecraft.world.level.gamerules.GameRules;
|
| 25 | import net.minecraft.world.phys.AABB;
|
| 26 | import net.minecraft.world.phys.Vec3;
|
| 27 | import org.jspecify.annotations.Nullable;
|
| 28 |
|
| 29 | public class NewMinecartBehavior extends MinecartBehavior {
|
| 30 | public static final int POS_ROT_LERP_TICKS = 3;
|
| 31 | public static final double ON_RAIL_Y_OFFSET = 0.1;
|
| 32 | public static final double OPPOSING_SLOPES_REST_AT_SPEED_THRESHOLD = 0.005;
|
| 33 | private NewMinecartBehavior.@Nullable StepPartialTicks cacheIndexAlpha;
|
| 34 | private int cachedLerpDelay;
|
| 35 | private float cachedPartialTick;
|
| 36 | private int lerpDelay = 0;
|
| 37 | public final List<NewMinecartBehavior.MinecartStep> lerpSteps = new LinkedList<>();
|
| 38 | public final List<NewMinecartBehavior.MinecartStep> currentLerpSteps = new LinkedList<>();
|
| 39 | public double currentLerpStepsTotalWeight = 0.0;
|
| 40 | public NewMinecartBehavior.MinecartStep oldLerp = NewMinecartBehavior.MinecartStep.ZERO;
|
| 41 |
|
| 42 | public NewMinecartBehavior(final AbstractMinecart minecart) {
|
| 43 | super(minecart);
|
| 44 | }
|
| 45 |
|
| 46 | @Override
|
| 47 | public void tick() {
|
| 48 | if (this.level() instanceof ServerLevel serverLevel) {
|
| 49 | BlockPos var5 = this.minecart.getCurrentBlockPosOrRailBelow();
|
| 50 | BlockState state = this.level().getBlockState(var5);
|
| 51 | if (this.minecart.isFirstTick()) {
|
| 52 | this.minecart.setOnRails(BaseRailBlock.isRail(state));
|
| 53 | this.adjustToRails(var5, state, true);
|
| 54 | }
|
| 55 |
|
| 56 | this.minecart.applyGravity();
|
| 57 | this.minecart.moveAlongTrack(serverLevel);
|
| 58 | } else {
|
| 59 | this.lerpClientPositionAndRotation();
|
| 60 | boolean onRails = BaseRailBlock.isRail(this.level().getBlockState(this.minecart.getCurrentBlockPosOrRailBelow()));
|
| 61 | this.minecart.setOnRails(onRails);
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | private void lerpClientPositionAndRotation() {
|
| 66 | if (--this.lerpDelay <= 0) {
|
| 67 | this.setOldLerpValues();
|
| 68 | this.currentLerpSteps.clear();
|
| 69 | if (!this.lerpSteps.isEmpty()) {
|
| 70 | this.currentLerpSteps.addAll(this.lerpSteps);
|
| 71 | this.lerpSteps.clear();
|
| 72 | this.currentLerpStepsTotalWeight = 0.0;
|
| 73 |
|
| 74 | for (NewMinecartBehavior.MinecartStep minecartStep : this.currentLerpSteps) {
|
| 75 | this.currentLerpStepsTotalWeight = this.currentLerpStepsTotalWeight + minecartStep.weight;
|
| 76 | }
|
| 77 |
|
| 78 | this.lerpDelay = this.currentLerpStepsTotalWeight == 0.0 ? 0 : 3;
|
| 79 | }
|
| 80 | }
|
| 81 |
|
| 82 | if (this.cartHasPosRotLerp()) {
|
| 83 | this.setPos(this.getCartLerpPosition(1.0F));
|
| 84 | this.setDeltaMovement(this.getCartLerpMovements(1.0F));
|
| 85 | this.setXRot(this.getCartLerpXRot(1.0F));
|
| 86 | this.setYRot(this.getCartLerpYRot(1.0F));
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | public void setOldLerpValues() {
|
| 91 | this.oldLerp = new NewMinecartBehavior.MinecartStep(this.position(), this.getDeltaMovement(), this.getYRot(), this.getXRot(), 0.0F);
|
| 92 | }
|
| 93 |
|
| 94 | public boolean cartHasPosRotLerp() {
|
| 95 | return !this.currentLerpSteps.isEmpty();
|
| 96 | }
|
| 97 |
|
| 98 | public float getCartLerpXRot(final float partialTicks) {
|
| 99 | NewMinecartBehavior.StepPartialTicks currentStepPartialTicks = this.getCurrentLerpStep(partialTicks);
|
| 100 | return Mth.rotLerp(currentStepPartialTicks.partialTicksInStep, currentStepPartialTicks.previousStep.xRot, currentStepPartialTicks.currentStep.xRot);
|
| 101 | }
|
| 102 |
|
| 103 | public float getCartLerpYRot(final float partialTicks) {
|
| 104 | NewMinecartBehavior.StepPartialTicks currentStepPartialTicks = this.getCurrentLerpStep(partialTicks);
|
| 105 | return Mth.rotLerp(currentStepPartialTicks.partialTicksInStep, currentStepPartialTicks.previousStep.yRot, currentStepPartialTicks.currentStep.yRot);
|
| 106 | }
|
| 107 |
|
| 108 | public Vec3 getCartLerpPosition(final float partialTicks) {
|
| 109 | NewMinecartBehavior.StepPartialTicks currentStepPartialTicks = this.getCurrentLerpStep(partialTicks);
|
| 110 | return Mth.lerp(currentStepPartialTicks.partialTicksInStep, currentStepPartialTicks.previousStep.position, currentStepPartialTicks.currentStep.position);
|
| 111 | }
|
| 112 |
|
| 113 | public Vec3 getCartLerpMovements(final float partialTicks) {
|
| 114 | NewMinecartBehavior.StepPartialTicks currentStepPartialTicks = this.getCurrentLerpStep(partialTicks);
|
| 115 | return Mth.lerp(currentStepPartialTicks.partialTicksInStep, currentStepPartialTicks.previousStep.movement, currentStepPartialTicks.currentStep.movement);
|
| 116 | }
|
| 117 |
|
| 118 | private NewMinecartBehavior.StepPartialTicks getCurrentLerpStep(final float partialTick) {
|
| 119 | if (partialTick == this.cachedPartialTick && this.lerpDelay == this.cachedLerpDelay && this.cacheIndexAlpha != null) {
|
| 120 | return this.cacheIndexAlpha;
|
| 121 | }
|
| 122 |
|
| 123 | float alpha = (3 - this.lerpDelay + partialTick) / 3.0F;
|
| 124 | float countUp = 0.0F;
|
| 125 | float indexedPartialTick = 1.0F;
|
| 126 | boolean foundIndex = false;
|
| 127 |
|
| 128 | int index;
|
| 129 | for (index = 0; index < this.currentLerpSteps.size(); index++) {
|
| 130 | float weight = this.currentLerpSteps.get(index).weight;
|
| 131 | if (!(weight <= 0.0F)) {
|
| 132 | countUp += weight;
|
| 133 | if (countUp >= this.currentLerpStepsTotalWeight * alpha) {
|
| 134 | float current = countUp - weight;
|
| 135 | indexedPartialTick = (float)((alpha * this.currentLerpStepsTotalWeight - current) / weight);
|
| 136 | foundIndex = true;
|
| 137 | break;
|
| 138 | }
|
| 139 | }
|
| 140 | }
|
| 141 |
|
| 142 | if (!foundIndex) {
|
| 143 | index = this.currentLerpSteps.size() - 1;
|
| 144 | }
|
| 145 |
|
| 146 | NewMinecartBehavior.MinecartStep currentStep = this.currentLerpSteps.get(index);
|
| 147 | NewMinecartBehavior.MinecartStep previousStep = index > 0 ? this.currentLerpSteps.get(index - 1) : this.oldLerp;
|
| 148 | this.cacheIndexAlpha = new NewMinecartBehavior.StepPartialTicks(indexedPartialTick, currentStep, previousStep);
|
| 149 | this.cachedLerpDelay = this.lerpDelay;
|
| 150 | this.cachedPartialTick = partialTick;
|
| 151 | return this.cacheIndexAlpha;
|
| 152 | }
|
| 153 |
|
| 154 | public void adjustToRails(final BlockPos targetBlockPos, final BlockState currentState, final boolean instant) {
|
| 155 | if (BaseRailBlock.isRail(currentState)) {
|
| 156 | RailShape shape = currentState.getValue(((BaseRailBlock)currentState.getBlock()).getShapeProperty());
|
| 157 | Pair<Vec3i, Vec3i> exits = AbstractMinecart.exits(shape);
|
| 158 | Vec3 exit0 = new Vec3(exits.getFirst()).scale(0.5);
|
| 159 | Vec3 exit1 = new Vec3(exits.getSecond()).scale(0.5);
|
| 160 | Vec3 horizontalOutDirection = exit0.horizontal();
|
| 161 | Vec3 horizontalInDirection = exit1.horizontal();
|
| 162 | if (this.getDeltaMovement().length() > 1.0E-5F && this.getDeltaMovement().dot(horizontalOutDirection) < this.getDeltaMovement().dot(horizontalInDirection)
|
| 163 | || this.isDecending(horizontalInDirection, shape)) {
|
| 164 | Vec3 swap = horizontalOutDirection;
|
| 165 | horizontalOutDirection = horizontalInDirection;
|
| 166 | horizontalInDirection = swap;
|
| 167 | }
|
| 168 |
|
| 169 | float yRot = 180.0F - (float)(Math.atan2(horizontalOutDirection.z, horizontalOutDirection.x) * 180.0 / Math.PI);
|
| 170 | yRot += this.minecart.isFlipped() ? 180.0F : 0.0F;
|
| 171 | Vec3 previousPosition = this.position();
|
| 172 | boolean inCorner = exit0.x() != exit1.x() && exit0.z() != exit1.z();
|
| 173 | Vec3 targetPosition;
|
| 174 | if (inCorner) {
|
| 175 | Vec3 from0to1 = exit1.subtract(exit0);
|
| 176 | Vec3 from0toPos = previousPosition.subtract(Vec3.atBottomCenterOf(targetBlockPos)).subtract(exit0);
|
| 177 | Vec3 travelVectorFrom0 = from0to1.scale(from0to1.dot(from0toPos) / from0to1.dot(from0to1));
|
| 178 | targetPosition = Vec3.atBottomCenterOf(targetBlockPos).add(exit0).add(travelVectorFrom0);
|
| 179 | yRot = 180.0F - (float)(Math.atan2(travelVectorFrom0.z, travelVectorFrom0.x) * 180.0 / Math.PI);
|
| 180 | yRot += this.minecart.isFlipped() ? 180.0F : 0.0F;
|
| 181 | } else {
|
| 182 | boolean zSnap = exit0.subtract(exit1).x != 0.0;
|
| 183 | boolean xSnap = exit0.subtract(exit1).z != 0.0;
|
| 184 | targetPosition = new Vec3(
|
| 185 | xSnap ? Vec3.atCenterOf(targetBlockPos).x : previousPosition.x, targetBlockPos.getY(), zSnap ? Vec3.atCenterOf(targetBlockPos).z : previousPosition.z
|
| 186 | );
|
| 187 | }
|
| 188 |
|
| 189 | Vec3 diffFromBlock = targetPosition.subtract(previousPosition);
|
| 190 | this.setPos(previousPosition.add(diffFromBlock));
|
| 191 | float xRot = 0.0F;
|
| 192 | boolean inHill = exit0.y() != exit1.y();
|
| 193 | if (inHill) {
|
| 194 | Vec3 inPosition = Vec3.atBottomCenterOf(targetBlockPos).add(horizontalInDirection);
|
| 195 | double horizontalDistanceFromIn = inPosition.distanceTo(this.position());
|
| 196 | this.setPos(this.position().add(0.0, horizontalDistanceFromIn + 0.1, 0.0));
|
| 197 | xRot = this.minecart.isFlipped() ? 45.0F : -45.0F;
|
| 198 | } else {
|
| 199 | this.setPos(this.position().add(0.0, 0.1, 0.0));
|
| 200 | }
|
| 201 |
|
| 202 | this.setRotation(yRot, xRot);
|
| 203 | double adjustDistance = previousPosition.distanceTo(this.position());
|
| 204 | if (adjustDistance > 0.0) {
|
| 205 | this.lerpSteps
|
| 206 | .add(
|
| 207 | new NewMinecartBehavior.MinecartStep(this.position(), this.getDeltaMovement(), this.getYRot(), this.getXRot(), instant ? 0.0F : (float)adjustDistance)
|
| 208 | );
|
| 209 | }
|
| 210 | }
|
| 211 | }
|
| 212 |
|
| 213 | private void setRotation(float yRot, float xRot) {
|
| 214 | double yRotDiff = Math.abs(yRot - this.getYRot());
|
| 215 | if (yRotDiff >= 175.0 && yRotDiff <= 185.0) {
|
| 216 | this.minecart.setFlipped(!this.minecart.isFlipped());
|
| 217 | yRot -= 180.0F;
|
| 218 | xRot *= -1.0F;
|
| 219 | }
|
| 220 |
|
| 221 | xRot = Math.clamp(xRot, -45.0F, 45.0F);
|
| 222 | this.setXRot(xRot % 360.0F);
|
| 223 | this.setYRot(yRot % 360.0F);
|
| 224 | }
|
| 225 |
|
| 226 | @Override
|
| 227 | public void moveAlongTrack(final ServerLevel level) {
|
| 228 | for (NewMinecartBehavior.TrackIteration trackIteration = new NewMinecartBehavior.TrackIteration();
|
| 229 | trackIteration.shouldIterate() && this.minecart.isAlive();
|
| 230 | trackIteration.firstIteration = false
|
| 231 | ) {
|
| 232 | Vec3 initialStepDeltaMovement = this.getDeltaMovement();
|
| 233 | BlockPos currentPos = this.minecart.getCurrentBlockPosOrRailBelow();
|
| 234 | BlockState currentState = this.level().getBlockState(currentPos);
|
| 235 | boolean onRails = BaseRailBlock.isRail(currentState);
|
| 236 | if (this.minecart.isOnRails() != onRails) {
|
| 237 | this.minecart.setOnRails(onRails);
|
| 238 | this.adjustToRails(currentPos, currentState, false);
|
| 239 | }
|
| 240 |
|
| 241 | if (onRails) {
|
| 242 | this.minecart.resetFallDistance();
|
| 243 | this.minecart.setOldPosAndRot();
|
| 244 | if (currentState.is(Blocks.ACTIVATOR_RAIL)) {
|
| 245 | this.minecart.activateMinecart(level, currentPos.getX(), currentPos.getY(), currentPos.getZ(), currentState.getValue(PoweredRailBlock.POWERED));
|
| 246 | }
|
| 247 |
|
| 248 | RailShape shape = currentState.getValue(((BaseRailBlock)currentState.getBlock()).getShapeProperty());
|
| 249 | Vec3 newDeltaMovement = this.calculateTrackSpeed(level, initialStepDeltaMovement.horizontal(), trackIteration, currentPos, currentState, shape);
|
| 250 | if (trackIteration.firstIteration) {
|
| 251 | trackIteration.movementLeft = newDeltaMovement.horizontalDistance();
|
| 252 | } else {
|
| 253 | trackIteration.movementLeft = trackIteration.movementLeft + (newDeltaMovement.horizontalDistance() - initialStepDeltaMovement.horizontalDistance());
|
| 254 | }
|
| 255 |
|
| 256 | this.setDeltaMovement(newDeltaMovement);
|
| 257 | trackIteration.movementLeft = this.minecart.makeStepAlongTrack(currentPos, shape, trackIteration.movementLeft);
|
| 258 | } else {
|
| 259 | this.minecart.comeOffTrack(level);
|
| 260 | trackIteration.movementLeft = 0.0;
|
| 261 | }
|
| 262 |
|
| 263 | Vec3 stepPosition = this.position();
|
| 264 | Vec3 stepDelta = stepPosition.subtract(this.minecart.oldPosition());
|
| 265 | double stepLength = stepDelta.length();
|
| 266 | if (stepLength > 1.0E-5F) {
|
| 267 | if (!(stepDelta.horizontalDistanceSqr() > 1.0E-5F)) {
|
| 268 | if (!this.minecart.isOnRails()) {
|
| 269 | this.setXRot(this.minecart.onGround() ? 0.0F : Mth.rotLerp(0.2F, this.getXRot(), 0.0F));
|
| 270 | }
|
| 271 | } else {
|
| 272 | float yRot = 180.0F - (float)(Math.atan2(stepDelta.z, stepDelta.x) * 180.0 / Math.PI);
|
| 273 | float xRot = this.minecart.onGround() && !this.minecart.isOnRails()
|
| 274 | ? 0.0F
|
| 275 | : 90.0F - (float)(Math.atan2(stepDelta.horizontalDistance(), stepDelta.y) * 180.0 / Math.PI);
|
| 276 | yRot += this.minecart.isFlipped() ? 180.0F : 0.0F;
|
| 277 | xRot *= this.minecart.isFlipped() ? -1.0F : 1.0F;
|
| 278 | this.setRotation(yRot, xRot);
|
| 279 | }
|
| 280 |
|
| 281 | this.lerpSteps
|
| 282 | .add(
|
| 283 | new NewMinecartBehavior.MinecartStep(
|
| 284 | stepPosition, this.getDeltaMovement(), this.getYRot(), this.getXRot(), (float)Math.min(stepLength, this.getMaxSpeed(level))
|
| 285 | )
|
| 286 | );
|
| 287 | } else if (initialStepDeltaMovement.horizontalDistanceSqr() > 0.0) {
|
| 288 | this.lerpSteps.add(new NewMinecartBehavior.MinecartStep(stepPosition, this.getDeltaMovement(), this.getYRot(), this.getXRot(), 1.0F));
|
| 289 | }
|
| 290 |
|
| 291 | if (stepLength > 1.0E-5F || trackIteration.firstIteration) {
|
| 292 | this.minecart.applyEffectsFromBlocks();
|
| 293 | this.minecart.applyEffectsFromBlocks();
|
| 294 | }
|
| 295 | }
|
| 296 | }
|
| 297 |
|
| 298 | private Vec3 calculateTrackSpeed(
|
| 299 | final ServerLevel level,
|
| 300 | final Vec3 deltaMovement,
|
| 301 | final NewMinecartBehavior.TrackIteration trackIteration,
|
| 302 | final BlockPos currentPos,
|
| 303 | final BlockState currentState,
|
| 304 | final RailShape shape
|
| 305 | ) {
|
| 306 | Vec3 newDeltaMovement = deltaMovement;
|
| 307 | if (!trackIteration.hasGainedSlopeSpeed) {
|
| 308 | Vec3 slopedDeltaMovement = this.calculateSlopeSpeed(newDeltaMovement, shape);
|
| 309 | if (slopedDeltaMovement.horizontalDistanceSqr() != newDeltaMovement.horizontalDistanceSqr()) {
|
| 310 | trackIteration.hasGainedSlopeSpeed = true;
|
| 311 | newDeltaMovement = slopedDeltaMovement;
|
| 312 | }
|
| 313 | }
|
| 314 |
|
| 315 | if (trackIteration.firstIteration) {
|
| 316 | Vec3 playerInputMovement = this.calculatePlayerInputSpeed(newDeltaMovement);
|
| 317 | if (playerInputMovement.horizontalDistanceSqr() != newDeltaMovement.horizontalDistanceSqr()) {
|
| 318 | trackIteration.hasHalted = true;
|
| 319 | newDeltaMovement = playerInputMovement;
|
| 320 | }
|
| 321 | }
|
| 322 |
|
| 323 | if (!trackIteration.hasHalted) {
|
| 324 | Vec3 haltedDeltaMovement = this.calculateHaltTrackSpeed(newDeltaMovement, currentState);
|
| 325 | if (haltedDeltaMovement.horizontalDistanceSqr() != newDeltaMovement.horizontalDistanceSqr()) {
|
| 326 | trackIteration.hasHalted = true;
|
| 327 | newDeltaMovement = haltedDeltaMovement;
|
| 328 | }
|
| 329 | }
|
| 330 |
|
| 331 | if (trackIteration.firstIteration) {
|
| 332 | newDeltaMovement = this.minecart.applyNaturalSlowdown(newDeltaMovement);
|
| 333 | if (newDeltaMovement.lengthSqr() > 0.0) {
|
| 334 | double speed = Math.min(newDeltaMovement.length(), this.minecart.getMaxSpeed(level));
|
| 335 | newDeltaMovement = newDeltaMovement.normalize().scale(speed);
|
| 336 | }
|
| 337 | }
|
| 338 |
|
| 339 | if (!trackIteration.hasBoosted) {
|
| 340 | Vec3 boostedDeltaMovement = this.calculateBoostTrackSpeed(newDeltaMovement, currentPos, currentState);
|
| 341 | if (boostedDeltaMovement.horizontalDistanceSqr() != newDeltaMovement.horizontalDistanceSqr()) {
|
| 342 | trackIteration.hasBoosted = true;
|
| 343 | newDeltaMovement = boostedDeltaMovement;
|
| 344 | }
|
| 345 | }
|
| 346 |
|
| 347 | return newDeltaMovement;
|
| 348 | }
|
| 349 |
|
| 350 | private Vec3 calculateSlopeSpeed(final Vec3 deltaMovement, final RailShape shape) {
|
| 351 | double slideSpeed = Math.max(0.0078125, deltaMovement.horizontalDistance() * 0.02);
|
| 352 | if (this.minecart.isInWater()) {
|
| 353 | slideSpeed *= 0.2;
|
| 354 | }
|
| 355 | return switch (shape) {
|
| 356 | case ASCENDING_EAST -> deltaMovement.add(-slideSpeed, 0.0, 0.0);
|
| 357 | case ASCENDING_WEST -> deltaMovement.add(slideSpeed, 0.0, 0.0);
|
| 358 | case ASCENDING_NORTH -> deltaMovement.add(0.0, 0.0, slideSpeed);
|
| 359 | case ASCENDING_SOUTH -> deltaMovement.add(0.0, 0.0, -slideSpeed);
|
| 360 | default -> deltaMovement;
|
| 361 | };
|
| 362 | }
|
| 363 |
|
| 364 | private Vec3 calculatePlayerInputSpeed(final Vec3 deltaMovement) {
|
| 365 | if (this.minecart.getFirstPassenger() instanceof ServerPlayer player) {
|
| 366 | Vec3 moveIntent = player.getLastClientMoveIntent();
|
| 367 | if (moveIntent.lengthSqr() > 0.0) {
|
| 368 | Vec3 riderMovement = moveIntent.normalize();
|
| 369 | double ownDist = deltaMovement.horizontalDistanceSqr();
|
| 370 | if (riderMovement.lengthSqr() > 0.0 && ownDist < 0.01) {
|
| 371 | return deltaMovement.add(new Vec3(riderMovement.x, 0.0, riderMovement.z).normalize().scale(0.001));
|
| 372 | }
|
| 373 | }
|
| 374 |
|
| 375 | return deltaMovement;
|
| 376 | } else {
|
| 377 | return deltaMovement;
|
| 378 | }
|
| 379 | }
|
| 380 |
|
| 381 | private Vec3 calculateHaltTrackSpeed(final Vec3 deltaMovement, final BlockState state) {
|
| 382 | if (state.is(Blocks.POWERED_RAIL) && !state.getValue(PoweredRailBlock.POWERED)) {
|
| 383 | return deltaMovement.length() < 0.03 ? Vec3.ZERO : deltaMovement.scale(0.5);
|
| 384 | } else {
|
| 385 | return deltaMovement;
|
| 386 | }
|
| 387 | }
|
| 388 |
|
| 389 | private Vec3 calculateBoostTrackSpeed(final Vec3 deltaMovement, final BlockPos pos, final BlockState state) {
|
| 390 | if (state.is(Blocks.POWERED_RAIL) && state.getValue(PoweredRailBlock.POWERED)) {
|
| 391 | if (deltaMovement.length() > 0.01) {
|
| 392 | return deltaMovement.normalize().scale(deltaMovement.length() + 0.06);
|
| 393 | }
|
| 394 |
|
| 395 | Vec3 powerDirection = this.minecart.getRedstoneDirection(pos);
|
| 396 | return powerDirection.lengthSqr() <= 0.0 ? deltaMovement : powerDirection.scale(deltaMovement.length() + 0.2);
|
| 397 | } else {
|
| 398 | return deltaMovement;
|
| 399 | }
|
| 400 | }
|
| 401 |
|
| 402 | @Override
|
| 403 | public double stepAlongTrack(final BlockPos pos, final RailShape shape, double movementLeft) {
|
| 404 | if (movementLeft < 1.0E-5F) {
|
| 405 | return 0.0;
|
| 406 | }
|
| 407 |
|
| 408 | Vec3 oldPosition = this.position();
|
| 409 | Pair<Vec3i, Vec3i> exits = AbstractMinecart.exits(shape);
|
| 410 | Vec3i exit0 = exits.getFirst();
|
| 411 | Vec3i exit1 = exits.getSecond();
|
| 412 | Vec3 movement = this.getDeltaMovement().horizontal();
|
| 413 | if (movement.length() < 1.0E-5F) {
|
| 414 | this.setDeltaMovement(Vec3.ZERO);
|
| 415 | return 0.0;
|
| 416 | }
|
| 417 |
|
| 418 | boolean inHill = exit0.getY() != exit1.getY();
|
| 419 | Vec3 horizontalInDirection = new Vec3(exit1).scale(0.5).horizontal();
|
| 420 | Vec3 horizontalOutDirection = new Vec3(exit0).scale(0.5).horizontal();
|
| 421 | if (movement.dot(horizontalOutDirection) < movement.dot(horizontalInDirection)) {
|
| 422 | horizontalOutDirection = horizontalInDirection;
|
| 423 | }
|
| 424 |
|
| 425 | Vec3 outPosition = Vec3.atBottomCenterOf(pos).add(horizontalOutDirection).add(0.0, 0.1, 0.0).add(horizontalOutDirection.normalize().scale(1.0E-5F));
|
| 426 | if (inHill && !this.isDecending(movement, shape)) {
|
| 427 | outPosition = outPosition.add(0.0, 1.0, 0.0);
|
| 428 | }
|
| 429 |
|
| 430 | Vec3 towardsOut = outPosition.subtract(this.position()).normalize();
|
| 431 | movement = towardsOut.scale(movement.length() / towardsOut.horizontalDistance());
|
| 432 | Vec3 newPosition = oldPosition.add(movement.normalize().scale(movementLeft * (inHill ? Mth.SQRT_OF_TWO : 1.0F)));
|
| 433 | if (oldPosition.distanceToSqr(outPosition) <= oldPosition.distanceToSqr(newPosition)) {
|
| 434 | movementLeft = outPosition.subtract(newPosition).horizontalDistance();
|
| 435 | newPosition = outPosition;
|
| 436 | } else {
|
| 437 | movementLeft = 0.0;
|
| 438 | }
|
| 439 |
|
| 440 | this.minecart.move(MoverType.SELF, newPosition.subtract(oldPosition));
|
| 441 | BlockState newBlockState = this.level().getBlockState(BlockPos.containing(newPosition));
|
| 442 | if (inHill) {
|
| 443 | if (BaseRailBlock.isRail(newBlockState)) {
|
| 444 | RailShape newRailShape = newBlockState.getValue(((BaseRailBlock)newBlockState.getBlock()).getShapeProperty());
|
| 445 | if (this.restAtVShape(shape, newRailShape)) {
|
| 446 | return 0.0;
|
| 447 | }
|
| 448 | }
|
| 449 |
|
| 450 | double horizontalDistanceFromOut = outPosition.horizontal().distanceTo(this.position().horizontal());
|
| 451 | double projectYPos = outPosition.y + (this.isDecending(movement, shape) ? horizontalDistanceFromOut : -horizontalDistanceFromOut);
|
| 452 | if (this.position().y < projectYPos) {
|
| 453 | this.setPos(this.position().x, projectYPos, this.position().z);
|
| 454 | }
|
| 455 | }
|
| 456 |
|
| 457 | if (this.position().distanceTo(oldPosition) < 1.0E-5F && newPosition.distanceTo(oldPosition) > 1.0E-5F) {
|
| 458 | this.setDeltaMovement(Vec3.ZERO);
|
| 459 | return 0.0;
|
| 460 | } else {
|
| 461 | this.setDeltaMovement(movement);
|
| 462 | return movementLeft;
|
| 463 | }
|
| 464 | }
|
| 465 |
|
| 466 | private boolean restAtVShape(final RailShape currentRailShape, final RailShape newRailShape) {
|
| 467 | if (this.getDeltaMovement().lengthSqr() < 0.005
|
| 468 | && newRailShape.isSlope()
|
| 469 | && this.isDecending(this.getDeltaMovement(), currentRailShape)
|
| 470 | && !this.isDecending(this.getDeltaMovement(), newRailShape)) {
|
| 471 | this.setDeltaMovement(Vec3.ZERO);
|
| 472 | return true;
|
| 473 | } else {
|
| 474 | return false;
|
| 475 | }
|
| 476 | }
|
| 477 |
|
| 478 | @Override
|
| 479 | public double getMaxSpeed(final ServerLevel level) {
|
| 480 | return level.getGameRules().get(GameRules.MAX_MINECART_SPEED).intValue() * (this.minecart.isInWater() ? 0.5 : 1.0) / 20.0;
|
| 481 | }
|
| 482 |
|
| 483 | private boolean isDecending(final Vec3 movement, final RailShape shape) {
|
| 484 | return switch (shape) {
|
| 485 | case ASCENDING_EAST -> movement.x < 0.0;
|
| 486 | case ASCENDING_WEST -> movement.x > 0.0;
|
| 487 | case ASCENDING_NORTH -> movement.z > 0.0;
|
| 488 | case ASCENDING_SOUTH -> movement.z < 0.0;
|
| 489 | default -> false;
|
| 490 | };
|
| 491 | }
|
| 492 |
|
| 493 | @Override
|
| 494 | public double getSlowdownFactor() {
|
| 495 | return this.minecart.isVehicle() ? 0.997 : 0.975;
|
| 496 | }
|
| 497 |
|
| 498 | @Override
|
| 499 | public boolean pushAndPickupEntities() {
|
| 500 | boolean pickedUp = this.pickupEntities(this.minecart.getBoundingBox().inflate(0.2, 0.0, 0.2));
|
| 501 | if (!this.minecart.horizontalCollision && !this.minecart.verticalCollision) {
|
| 502 | return false;
|
| 503 | }
|
| 504 |
|
| 505 | boolean pushed = this.pushEntities(this.minecart.getBoundingBox().inflate(1.0E-7));
|
| 506 | return pickedUp && !pushed;
|
| 507 | }
|
| 508 |
|
| 509 | public boolean pickupEntities(final AABB hitbox) {
|
| 510 | if (this.minecart.isRideable() && !this.minecart.isVehicle()) {
|
| 511 | List<Entity> entities = this.level().getEntities(this.minecart, hitbox, EntitySelector.pushableBy(this.minecart));
|
| 512 | if (!entities.isEmpty()) {
|
| 513 | for (Entity entity : entities) {
|
| 514 | if (!(entity instanceof Player)
|
| 515 | && !(entity instanceof IronGolem)
|
| 516 | && !(entity instanceof AbstractMinecart)
|
| 517 | && !this.minecart.isVehicle()
|
| 518 | && !entity.isPassenger()) {
|
| 519 | boolean pickedUp = entity.startRiding(this.minecart);
|
| 520 | if (pickedUp) {
|
| 521 | return true;
|
| 522 | }
|
| 523 | }
|
| 524 | }
|
| 525 | }
|
| 526 | }
|
| 527 |
|
| 528 | return false;
|
| 529 | }
|
| 530 |
|
| 531 | public boolean pushEntities(final AABB hitbox) {
|
| 532 | boolean pushed = false;
|
| 533 | if (this.minecart.isRideable()) {
|
| 534 | List<Entity> entities = this.level().getEntities(this.minecart, hitbox, EntitySelector.pushableBy(this.minecart));
|
| 535 | if (!entities.isEmpty()) {
|
| 536 | for (Entity entity : entities) {
|
| 537 | if (entity instanceof Player || entity instanceof IronGolem || entity instanceof AbstractMinecart || this.minecart.isVehicle() || entity.isPassenger()) {
|
| 538 | entity.push(this.minecart);
|
| 539 | pushed = true;
|
| 540 | }
|
| 541 | }
|
| 542 | }
|
| 543 | } else {
|
| 544 | for (Entity entity : this.level().getEntities(this.minecart, hitbox)) {
|
| 545 | if (!this.minecart.hasPassenger(entity) && entity.isPushable() && entity instanceof AbstractMinecart) {
|
| 546 | entity.push(this.minecart);
|
| 547 | pushed = true;
|
| 548 | }
|
| 549 | }
|
| 550 | }
|
| 551 |
|
| 552 | return pushed;
|
| 553 | }
|
| 554 |
|
| 555 | public record MinecartStep(Vec3 position, Vec3 movement, float yRot, float xRot, float weight) {
|
| 556 | public static final StreamCodec<ByteBuf, NewMinecartBehavior.MinecartStep> STREAM_CODEC = StreamCodec.composite(
|
| 557 | Vec3.STREAM_CODEC,
|
| 558 | NewMinecartBehavior.MinecartStep::position,
|
| 559 | Vec3.STREAM_CODEC,
|
| 560 | NewMinecartBehavior.MinecartStep::movement,
|
| 561 | ByteBufCodecs.ROTATION_BYTE,
|
| 562 | NewMinecartBehavior.MinecartStep::yRot,
|
| 563 | ByteBufCodecs.ROTATION_BYTE,
|
| 564 | NewMinecartBehavior.MinecartStep::xRot,
|
| 565 | ByteBufCodecs.FLOAT,
|
| 566 | NewMinecartBehavior.MinecartStep::weight,
|
| 567 | NewMinecartBehavior.MinecartStep::new
|
| 568 | );
|
| 569 | public static final NewMinecartBehavior.MinecartStep ZERO = new NewMinecartBehavior.MinecartStep(Vec3.ZERO, Vec3.ZERO, 0.0F, 0.0F, 0.0F);
|
| 570 | }
|
| 571 |
|
| 572 | private record StepPartialTicks(float partialTicksInStep, NewMinecartBehavior.MinecartStep currentStep, NewMinecartBehavior.MinecartStep previousStep) {
|
| 573 | }
|
| 574 |
|
| 575 | private static class TrackIteration {
|
| 576 | public double movementLeft = 0.0;
|
| 577 | public boolean firstIteration = true;
|
| 578 | public boolean hasGainedSlopeSpeed = false;
|
| 579 | public boolean hasHalted = false;
|
| 580 | public boolean hasBoosted = false;
|
| 581 |
|
| 582 | public boolean shouldIterate() {
|
| 583 | return this.firstIteration || this.movementLeft > 1.0E-5F;
|
| 584 | }
|
| 585 | }
|
| 586 | }
|
| 587 |
|