| 1 | package net.minecraft.world.waypoints;
|
| 2 |
|
| 3 | import java.util.Optional;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.network.protocol.game.ClientboundTrackedWaypointPacket;
|
| 6 | import net.minecraft.server.level.ServerPlayer;
|
| 7 | import net.minecraft.util.Mth;
|
| 8 | import net.minecraft.world.entity.LivingEntity;
|
| 9 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 10 | import net.minecraft.world.level.ChunkPos;
|
| 11 | import net.minecraft.world.phys.Vec3;
|
| 12 |
|
| 13 | public interface WaypointTransmitter extends Waypoint {
|
| 14 | int REALLY_FAR_DISTANCE = 332;
|
| 15 |
|
| 16 | boolean isTransmittingWaypoint();
|
| 17 |
|
| 18 | Optional<WaypointTransmitter.Connection> makeWaypointConnectionWith(ServerPlayer player);
|
| 19 |
|
| 20 | Waypoint.Icon waypointIcon();
|
| 21 |
|
| 22 | static boolean doesSourceIgnoreReceiver(final LivingEntity source, final ServerPlayer receiver) {
|
| 23 | if (receiver.isSpectator()) {
|
| 24 | return false;
|
| 25 | } else if (!source.isSpectator() && !source.hasIndirectPassenger(receiver)) {
|
| 26 | double broadcastRange = Math.min(source.getAttributeValue(Attributes.WAYPOINT_TRANSMIT_RANGE), receiver.getAttributeValue(Attributes.WAYPOINT_RECEIVE_RANGE));
|
| 27 | return source.distanceTo(receiver) >= broadcastRange;
|
| 28 | } else {
|
| 29 | return true;
|
| 30 | }
|
| 31 | }
|
| 32 |
|
| 33 | static boolean isChunkVisible(final ChunkPos chunkPos, final ServerPlayer receiver) {
|
| 34 | return receiver.getChunkTrackingView().isInViewDistance(chunkPos.x(), chunkPos.z());
|
| 35 | }
|
| 36 |
|
| 37 | static boolean isReallyFar(final LivingEntity source, final ServerPlayer receiver) {
|
| 38 | return source.distanceTo(receiver) > 332.0F;
|
| 39 | }
|
| 40 |
|
| 41 | interface BlockConnection extends WaypointTransmitter.Connection {
|
| 42 | int distanceManhattan();
|
| 43 |
|
| 44 | @Override
|
| 45 | default boolean isBroken() {
|
| 46 | return this.distanceManhattan() > 1;
|
| 47 | }
|
| 48 | }
|
| 49 |
|
| 50 | interface ChunkConnection extends WaypointTransmitter.Connection {
|
| 51 | int distanceChessboard();
|
| 52 |
|
| 53 | @Override
|
| 54 | default boolean isBroken() {
|
| 55 | return this.distanceChessboard() > 1;
|
| 56 | }
|
| 57 | }
|
| 58 |
|
| 59 | interface Connection {
|
| 60 | void connect();
|
| 61 |
|
| 62 | void disconnect();
|
| 63 |
|
| 64 | void update();
|
| 65 |
|
| 66 | boolean isBroken();
|
| 67 | }
|
| 68 |
|
| 69 | class EntityAzimuthConnection implements WaypointTransmitter.Connection {
|
| 70 | private final LivingEntity source;
|
| 71 | private final Waypoint.Icon icon;
|
| 72 | private final ServerPlayer receiver;
|
| 73 | private float lastAngle;
|
| 74 |
|
| 75 | public EntityAzimuthConnection(final LivingEntity source, final Waypoint.Icon icon, final ServerPlayer receiver) {
|
| 76 | this.source = source;
|
| 77 | this.icon = icon;
|
| 78 | this.receiver = receiver;
|
| 79 | Vec3 direction = receiver.position().subtract(source.position()).rotateClockwise90();
|
| 80 | this.lastAngle = (float)Mth.atan2(direction.z(), direction.x());
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | public boolean isBroken() {
|
| 85 | return WaypointTransmitter.doesSourceIgnoreReceiver(this.source, this.receiver)
|
| 86 | || WaypointTransmitter.isChunkVisible(this.source.chunkPosition(), this.receiver)
|
| 87 | || !WaypointTransmitter.isReallyFar(this.source, this.receiver);
|
| 88 | }
|
| 89 |
|
| 90 | @Override
|
| 91 | public void connect() {
|
| 92 | this.receiver.connection.send(ClientboundTrackedWaypointPacket.addWaypointAzimuth(this.source.getUUID(), this.icon, this.lastAngle));
|
| 93 | }
|
| 94 |
|
| 95 | @Override
|
| 96 | public void disconnect() {
|
| 97 | this.receiver.connection.send(ClientboundTrackedWaypointPacket.removeWaypoint(this.source.getUUID()));
|
| 98 | }
|
| 99 |
|
| 100 | @Override
|
| 101 | public void update() {
|
| 102 | Vec3 direction = this.receiver.position().subtract(this.source.position()).rotateClockwise90();
|
| 103 | float currentAngle = (float)Mth.atan2(direction.z(), direction.x());
|
| 104 | if (Mth.abs(currentAngle - this.lastAngle) > 0.008726646F) {
|
| 105 | this.receiver.connection.send(ClientboundTrackedWaypointPacket.updateWaypointAzimuth(this.source.getUUID(), this.icon, currentAngle));
|
| 106 | this.lastAngle = currentAngle;
|
| 107 | }
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | class EntityBlockConnection implements WaypointTransmitter.BlockConnection {
|
| 112 | private final LivingEntity source;
|
| 113 | private final Waypoint.Icon icon;
|
| 114 | private final ServerPlayer receiver;
|
| 115 | private BlockPos lastPosition;
|
| 116 |
|
| 117 | public EntityBlockConnection(final LivingEntity source, final Waypoint.Icon icon, final ServerPlayer receiver) {
|
| 118 | this.source = source;
|
| 119 | this.receiver = receiver;
|
| 120 | this.icon = icon;
|
| 121 | this.lastPosition = source.blockPosition();
|
| 122 | }
|
| 123 |
|
| 124 | @Override
|
| 125 | public void connect() {
|
| 126 | this.receiver.connection.send(ClientboundTrackedWaypointPacket.addWaypointPosition(this.source.getUUID(), this.icon, this.lastPosition));
|
| 127 | }
|
| 128 |
|
| 129 | @Override
|
| 130 | public void disconnect() {
|
| 131 | this.receiver.connection.send(ClientboundTrackedWaypointPacket.removeWaypoint(this.source.getUUID()));
|
| 132 | }
|
| 133 |
|
| 134 | @Override
|
| 135 | public void update() {
|
| 136 | BlockPos currentPosition = this.source.blockPosition();
|
| 137 | if (currentPosition.distManhattan(this.lastPosition) > 0) {
|
| 138 | this.receiver.connection.send(ClientboundTrackedWaypointPacket.updateWaypointPosition(this.source.getUUID(), this.icon, currentPosition));
|
| 139 | this.lastPosition = currentPosition;
|
| 140 | }
|
| 141 | }
|
| 142 |
|
| 143 | @Override
|
| 144 | public int distanceManhattan() {
|
| 145 | return this.lastPosition.distManhattan(this.source.blockPosition());
|
| 146 | }
|
| 147 |
|
| 148 | @Override
|
| 149 | public boolean isBroken() {
|
| 150 | return WaypointTransmitter.BlockConnection.super.isBroken() || WaypointTransmitter.doesSourceIgnoreReceiver(this.source, this.receiver);
|
| 151 | }
|
| 152 | }
|
| 153 |
|
| 154 | class EntityChunkConnection implements WaypointTransmitter.ChunkConnection {
|
| 155 | private final LivingEntity source;
|
| 156 | private final Waypoint.Icon icon;
|
| 157 | private final ServerPlayer receiver;
|
| 158 | private ChunkPos lastPosition;
|
| 159 |
|
| 160 | public EntityChunkConnection(final LivingEntity source, final Waypoint.Icon icon, final ServerPlayer receiver) {
|
| 161 | this.source = source;
|
| 162 | this.icon = icon;
|
| 163 | this.receiver = receiver;
|
| 164 | this.lastPosition = source.chunkPosition();
|
| 165 | }
|
| 166 |
|
| 167 | @Override
|
| 168 | public int distanceChessboard() {
|
| 169 | return this.lastPosition.getChessboardDistance(this.source.chunkPosition());
|
| 170 | }
|
| 171 |
|
| 172 | @Override
|
| 173 | public void connect() {
|
| 174 | this.receiver.connection.send(ClientboundTrackedWaypointPacket.addWaypointChunk(this.source.getUUID(), this.icon, this.lastPosition));
|
| 175 | }
|
| 176 |
|
| 177 | @Override
|
| 178 | public void disconnect() {
|
| 179 | this.receiver.connection.send(ClientboundTrackedWaypointPacket.removeWaypoint(this.source.getUUID()));
|
| 180 | }
|
| 181 |
|
| 182 | @Override
|
| 183 | public void update() {
|
| 184 | ChunkPos currentPosition = this.source.chunkPosition();
|
| 185 | if (currentPosition.getChessboardDistance(this.lastPosition) > 0) {
|
| 186 | this.receiver.connection.send(ClientboundTrackedWaypointPacket.updateWaypointChunk(this.source.getUUID(), this.icon, currentPosition));
|
| 187 | this.lastPosition = currentPosition;
|
| 188 | }
|
| 189 | }
|
| 190 |
|
| 191 | @Override
|
| 192 | public boolean isBroken() {
|
| 193 | return !WaypointTransmitter.ChunkConnection.super.isBroken() && !WaypointTransmitter.doesSourceIgnoreReceiver(this.source, this.receiver)
|
| 194 | ? WaypointTransmitter.isChunkVisible(this.lastPosition, this.receiver)
|
| 195 | : true;
|
| 196 | }
|
| 197 | }
|
| 198 | }
|
| 199 |
|