| 1 | package net.minecraft.world.level.portal;
|
| 2 |
|
| 3 | import java.util.Set;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.network.protocol.game.ClientboundLevelEventPacket;
|
| 6 | import net.minecraft.server.level.ServerLevel;
|
| 7 | import net.minecraft.server.level.ServerPlayer;
|
| 8 | import net.minecraft.world.entity.Entity;
|
| 9 | import net.minecraft.world.entity.Relative;
|
| 10 | import net.minecraft.world.level.storage.LevelData;
|
| 11 | import net.minecraft.world.phys.Vec3;
|
| 12 |
|
| 13 | public record TeleportTransition(
|
| 14 | ServerLevel newLevel,
|
| 15 | Vec3 position,
|
| 16 | Vec3 deltaMovement,
|
| 17 | float yRot,
|
| 18 | float xRot,
|
| 19 | boolean missingRespawnBlock,
|
| 20 | boolean asPassenger,
|
| 21 | Set<Relative> relatives,
|
| 22 | TeleportTransition.PostTeleportTransition postTeleportTransition
|
| 23 | ) {
|
| 24 | public static final TeleportTransition.PostTeleportTransition DO_NOTHING = entity -> {};
|
| 25 | public static final TeleportTransition.PostTeleportTransition PLAY_PORTAL_SOUND = TeleportTransition::playPortalSound;
|
| 26 | public static final TeleportTransition.PostTeleportTransition PLACE_PORTAL_TICKET = TeleportTransition::placePortalTicket;
|
| 27 |
|
| 28 | public TeleportTransition(
|
| 29 | final ServerLevel newLevel,
|
| 30 | final Vec3 pos,
|
| 31 | final Vec3 speed,
|
| 32 | final float yRot,
|
| 33 | final float xRot,
|
| 34 | final TeleportTransition.PostTeleportTransition postTeleportTransition
|
| 35 | ) {
|
| 36 | this(newLevel, pos, speed, yRot, xRot, Set.of(), postTeleportTransition);
|
| 37 | }
|
| 38 |
|
| 39 | public TeleportTransition(
|
| 40 | final ServerLevel newLevel,
|
| 41 | final Vec3 pos,
|
| 42 | final Vec3 speed,
|
| 43 | final float yRot,
|
| 44 | final float xRot,
|
| 45 | final Set<Relative> relatives,
|
| 46 | final TeleportTransition.PostTeleportTransition postTeleportTransition
|
| 47 | ) {
|
| 48 | this(newLevel, pos, speed, yRot, xRot, false, false, relatives, postTeleportTransition);
|
| 49 | }
|
| 50 |
|
| 51 | private static void playPortalSound(final Entity entity) {
|
| 52 | if (entity instanceof ServerPlayer player) {
|
| 53 | player.connection.send(new ClientboundLevelEventPacket(1032, BlockPos.ZERO, 0, false));
|
| 54 | }
|
| 55 | }
|
| 56 |
|
| 57 | private static void placePortalTicket(final Entity entity) {
|
| 58 | entity.placePortalTicket(BlockPos.containing(entity.position()));
|
| 59 | }
|
| 60 |
|
| 61 | public static TeleportTransition createDefault(final ServerPlayer player, final TeleportTransition.PostTeleportTransition postTeleportTransition) {
|
| 62 | ServerLevel newLevel = player.level().getServer().findRespawnDimension();
|
| 63 | LevelData.RespawnData respawnData = newLevel.getRespawnData();
|
| 64 | return new TeleportTransition(
|
| 65 | newLevel, findAdjustedSharedSpawnPos(newLevel, player), Vec3.ZERO, respawnData.yaw(), respawnData.pitch(), false, false, Set.of(), postTeleportTransition
|
| 66 | );
|
| 67 | }
|
| 68 |
|
| 69 | public static TeleportTransition missingRespawnBlock(final ServerPlayer player, final TeleportTransition.PostTeleportTransition postTeleportTransition) {
|
| 70 | ServerLevel newLevel = player.level().getServer().findRespawnDimension();
|
| 71 | LevelData.RespawnData respawnData = newLevel.getRespawnData();
|
| 72 | return new TeleportTransition(
|
| 73 | newLevel, findAdjustedSharedSpawnPos(newLevel, player), Vec3.ZERO, respawnData.yaw(), respawnData.pitch(), true, false, Set.of(), postTeleportTransition
|
| 74 | );
|
| 75 | }
|
| 76 |
|
| 77 | private static Vec3 findAdjustedSharedSpawnPos(final ServerLevel newLevel, final Entity entity) {
|
| 78 | return Vec3.atBottomCenterOf(entity.adjustSpawnLocation(newLevel, newLevel.getRespawnData().pos()));
|
| 79 | }
|
| 80 |
|
| 81 | public TeleportTransition withRotation(final float yRot, final float xRot) {
|
| 82 | return new TeleportTransition(
|
| 83 | this.newLevel(),
|
| 84 | this.position(),
|
| 85 | this.deltaMovement(),
|
| 86 | yRot,
|
| 87 | xRot,
|
| 88 | this.missingRespawnBlock(),
|
| 89 | this.asPassenger(),
|
| 90 | this.relatives(),
|
| 91 | this.postTeleportTransition()
|
| 92 | );
|
| 93 | }
|
| 94 |
|
| 95 | public TeleportTransition withPosition(final Vec3 position) {
|
| 96 | return new TeleportTransition(
|
| 97 | this.newLevel(),
|
| 98 | position,
|
| 99 | this.deltaMovement(),
|
| 100 | this.yRot(),
|
| 101 | this.xRot(),
|
| 102 | this.missingRespawnBlock(),
|
| 103 | this.asPassenger(),
|
| 104 | this.relatives(),
|
| 105 | this.postTeleportTransition()
|
| 106 | );
|
| 107 | }
|
| 108 |
|
| 109 | public TeleportTransition transitionAsPassenger() {
|
| 110 | return new TeleportTransition(
|
| 111 | this.newLevel(),
|
| 112 | this.position(),
|
| 113 | this.deltaMovement(),
|
| 114 | this.yRot(),
|
| 115 | this.xRot(),
|
| 116 | this.missingRespawnBlock(),
|
| 117 | true,
|
| 118 | this.relatives(),
|
| 119 | this.postTeleportTransition()
|
| 120 | );
|
| 121 | }
|
| 122 |
|
| 123 | @FunctionalInterface
|
| 124 | public interface PostTeleportTransition {
|
| 125 | void onTransition(final Entity entity);
|
| 126 |
|
| 127 | default TeleportTransition.PostTeleportTransition then(final TeleportTransition.PostTeleportTransition postTeleportTransition) {
|
| 128 | return entity -> {
|
| 129 | this.onTransition(entity);
|
| 130 | postTeleportTransition.onTransition(entity);
|
| 131 | };
|
| 132 | }
|
| 133 | }
|
| 134 | }
|
| 135 |
|