| 1 | package net.minecraft.world.entity.vehicle.minecart;
|
| 2 |
|
| 3 | import net.minecraft.server.level.ServerLevel;
|
| 4 | import net.minecraft.util.Mth;
|
| 5 | import net.minecraft.world.InteractionHand;
|
| 6 | import net.minecraft.world.InteractionResult;
|
| 7 | import net.minecraft.world.entity.Entity;
|
| 8 | import net.minecraft.world.entity.EntityType;
|
| 9 | import net.minecraft.world.entity.player.Player;
|
| 10 | import net.minecraft.world.item.Item;
|
| 11 | import net.minecraft.world.item.ItemStack;
|
| 12 | import net.minecraft.world.item.Items;
|
| 13 | import net.minecraft.world.level.Level;
|
| 14 | import net.minecraft.world.phys.Vec3;
|
| 15 |
|
| 16 | public class Minecart extends AbstractMinecart {
|
| 17 | private float rotationOffset;
|
| 18 | private float playerRotationOffset;
|
| 19 |
|
| 20 | public Minecart(final EntityType<?> type, final Level level) {
|
| 21 | super(type, level);
|
| 22 | }
|
| 23 |
|
| 24 | @Override
|
| 25 | public InteractionResult interact(final Player player, final InteractionHand hand, final Vec3 location) {
|
| 26 | if (!player.isSecondaryUseActive() && !this.isVehicle()) {
|
| 27 | this.playerRotationOffset = this.rotationOffset;
|
| 28 | return !this.level().isClientSide() && player.startRiding(this) ? InteractionResult.SUCCESS_SERVER : InteractionResult.CONSUME;
|
| 29 | } else {
|
| 30 | return InteractionResult.PASS;
|
| 31 | }
|
| 32 | }
|
| 33 |
|
| 34 | @Override
|
| 35 | protected Item getDropItem() {
|
| 36 | return Items.MINECART;
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | public ItemStack getPickResult() {
|
| 41 | return new ItemStack(Items.MINECART);
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | public void activateMinecart(final ServerLevel level, final int xt, final int yt, final int zt, final boolean state) {
|
| 46 | if (state) {
|
| 47 | if (this.isVehicle()) {
|
| 48 | this.ejectPassengers();
|
| 49 | }
|
| 50 |
|
| 51 | if (this.getHurtTime() == 0) {
|
| 52 | this.setHurtDir(-this.getHurtDir());
|
| 53 | this.setHurtTime(10);
|
| 54 | this.setDamage(50.0F);
|
| 55 | this.markHurt();
|
| 56 | }
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | public boolean isRideable() {
|
| 62 | return true;
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | public void tick() {
|
| 67 | double lastKnownYRot = this.getYRot();
|
| 68 | Vec3 lastKnownPos = this.position();
|
| 69 | super.tick();
|
| 70 | double tickDiff = (this.getYRot() - lastKnownYRot) % 360.0;
|
| 71 | if (this.level().isClientSide() && lastKnownPos.distanceTo(this.position()) > 0.01) {
|
| 72 | this.rotationOffset += (float)tickDiff;
|
| 73 | this.rotationOffset %= 360.0F;
|
| 74 | }
|
| 75 | }
|
| 76 |
|
| 77 | @Override
|
| 78 | protected void positionRider(final Entity passenger, final Entity.MoveFunction moveFunction) {
|
| 79 | super.positionRider(passenger, moveFunction);
|
| 80 | if (this.level().isClientSide() && passenger instanceof Player player && player.shouldRotateWithMinecart() && useExperimentalMovement(this.level())) {
|
| 81 | float yRot = (float)Mth.rotLerp(0.5, this.playerRotationOffset, this.rotationOffset);
|
| 82 | player.setYRot(player.getYRot() - (yRot - this.playerRotationOffset));
|
| 83 | this.playerRotationOffset = yRot;
|
| 84 | }
|
| 85 | }
|
| 86 | }
|
| 87 |
|