| 1 | package net.minecraft.world.entity;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.mojang.datafixers.util.Either;
|
| 5 | import com.mojang.serialization.Codec;
|
| 6 | import java.util.ArrayList;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Objects;
|
| 9 | import java.util.Optional;
|
| 10 | import java.util.UUID;
|
| 11 | import java.util.function.Predicate;
|
| 12 | import net.minecraft.core.BlockPos;
|
| 13 | import net.minecraft.core.UUIDUtil;
|
| 14 | import net.minecraft.network.protocol.game.ClientboundSetEntityLinkPacket;
|
| 15 | import net.minecraft.server.level.ServerLevel;
|
| 16 | import net.minecraft.sounds.SoundEvents;
|
| 17 | import net.minecraft.sounds.SoundSource;
|
| 18 | import net.minecraft.world.entity.decoration.LeashFenceKnotEntity;
|
| 19 | import net.minecraft.world.item.Items;
|
| 20 | import net.minecraft.world.level.Level;
|
| 21 | import net.minecraft.world.level.gamerules.GameRules;
|
| 22 | import net.minecraft.world.level.storage.ValueInput;
|
| 23 | import net.minecraft.world.level.storage.ValueOutput;
|
| 24 | import net.minecraft.world.phys.AABB;
|
| 25 | import net.minecraft.world.phys.Vec3;
|
| 26 | import org.jspecify.annotations.Nullable;
|
| 27 |
|
| 28 | public interface Leashable {
|
| 29 | String LEASH_TAG = "leash";
|
| 30 | double LEASH_TOO_FAR_DIST = 12.0;
|
| 31 | double LEASH_ELASTIC_DIST = 6.0;
|
| 32 | double MAXIMUM_ALLOWED_LEASHED_DIST = 16.0;
|
| 33 | Vec3 AXIS_SPECIFIC_ELASTICITY = new Vec3(0.8, 0.2, 0.8);
|
| 34 | float SPRING_DAMPENING = 0.7F;
|
| 35 | double TORSIONAL_ELASTICITY = 10.0;
|
| 36 | double STIFFNESS = 0.11;
|
| 37 | List<Vec3> ENTITY_ATTACHMENT_POINT = ImmutableList.of(new Vec3(0.0, 0.5, 0.5));
|
| 38 | List<Vec3> LEASHER_ATTACHMENT_POINT = ImmutableList.of(new Vec3(0.0, 0.5, 0.0));
|
| 39 | List<Vec3> SHARED_QUAD_ATTACHMENT_POINTS = ImmutableList.of(
|
| 40 | new Vec3(-0.5, 0.5, 0.5), new Vec3(-0.5, 0.5, -0.5), new Vec3(0.5, 0.5, -0.5), new Vec3(0.5, 0.5, 0.5)
|
| 41 | );
|
| 42 |
|
| 43 | Leashable.@Nullable LeashData getLeashData();
|
| 44 |
|
| 45 | void setLeashData(Leashable.@Nullable LeashData leashData);
|
| 46 |
|
| 47 | default boolean isLeashed() {
|
| 48 | return this.getLeashData() != null && this.getLeashData().leashHolder != null;
|
| 49 | }
|
| 50 |
|
| 51 | default boolean mayBeLeashed() {
|
| 52 | return this.getLeashData() != null;
|
| 53 | }
|
| 54 |
|
| 55 | default boolean canHaveALeashAttachedTo(final Entity entity) {
|
| 56 | if (this == entity) {
|
| 57 | return false;
|
| 58 | } else {
|
| 59 | return this.leashDistanceTo(entity) > this.leashSnapDistance() ? false : this.canBeLeashed();
|
| 60 | }
|
| 61 | }
|
| 62 |
|
| 63 | default double leashDistanceTo(final Entity entity) {
|
| 64 | return entity.getBoundingBox().getCenter().distanceTo(((Entity)this).getBoundingBox().getCenter());
|
| 65 | }
|
| 66 |
|
| 67 | default boolean canBeLeashed() {
|
| 68 | return true;
|
| 69 | }
|
| 70 |
|
| 71 | default void setDelayedLeashHolderId(final int entityId) {
|
| 72 | this.setLeashData(new Leashable.LeashData(entityId));
|
| 73 | dropLeash((Entity & Leashable)this, false, false);
|
| 74 | }
|
| 75 |
|
| 76 | default void readLeashData(final ValueInput input) {
|
| 77 | Leashable.LeashData newLeashData = input.read("leash", Leashable.LeashData.CODEC).orElse(null);
|
| 78 | if (this.getLeashData() != null && newLeashData == null) {
|
| 79 | this.removeLeash();
|
| 80 | }
|
| 81 |
|
| 82 | this.setLeashData(newLeashData);
|
| 83 | }
|
| 84 |
|
| 85 | default void writeLeashData(final ValueOutput output, final Leashable.@Nullable LeashData leashData) {
|
| 86 | output.storeNullable("leash", Leashable.LeashData.CODEC, leashData);
|
| 87 | }
|
| 88 |
|
| 89 | private static <E extends Entity & Leashable> void restoreLeashFromSave(final E entity, final Leashable.LeashData leashData) {
|
| 90 | if (leashData.delayedLeashInfo != null && entity.level() instanceof ServerLevel serverLevel) {
|
| 91 | Optional<UUID> leashUuid = leashData.delayedLeashInfo.left();
|
| 92 | Optional<BlockPos> pos = leashData.delayedLeashInfo.right();
|
| 93 | if (leashUuid.isPresent()) {
|
| 94 | Entity leasher = serverLevel.getEntity(leashUuid.get());
|
| 95 | if (leasher != null) {
|
| 96 | setLeashedTo(entity, leasher, true);
|
| 97 | return;
|
| 98 | }
|
| 99 | } else if (pos.isPresent()) {
|
| 100 | setLeashedTo(entity, LeashFenceKnotEntity.getOrCreateKnot(serverLevel, pos.get()), true);
|
| 101 | return;
|
| 102 | }
|
| 103 |
|
| 104 | if (entity.tickCount > 100) {
|
| 105 | entity.spawnAtLocation(serverLevel, Items.LEAD);
|
| 106 | entity.setLeashData(null);
|
| 107 | }
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | default void dropLeash() {
|
| 112 | dropLeash((Entity & Leashable)this, true, true);
|
| 113 | }
|
| 114 |
|
| 115 | default void removeLeash() {
|
| 116 | dropLeash((Entity & Leashable)this, true, false);
|
| 117 | }
|
| 118 |
|
| 119 | default void onLeashRemoved() {
|
| 120 | }
|
| 121 |
|
| 122 | private static <E extends Entity & Leashable> void dropLeash(final E entity, final boolean sendPacket, final boolean dropLead) {
|
| 123 | Leashable.LeashData leashData = entity.getLeashData();
|
| 124 | if (leashData != null && leashData.leashHolder != null) {
|
| 125 | entity.setLeashData(null);
|
| 126 | entity.onLeashRemoved();
|
| 127 | if (entity.level() instanceof ServerLevel level) {
|
| 128 | if (dropLead) {
|
| 129 | entity.spawnAtLocation(level, Items.LEAD);
|
| 130 | }
|
| 131 |
|
| 132 | if (sendPacket) {
|
| 133 | level.getChunkSource().sendToTrackingPlayers(entity, new ClientboundSetEntityLinkPacket(entity, null));
|
| 134 | }
|
| 135 |
|
| 136 | leashData.leashHolder.notifyLeasheeRemoved(entity);
|
| 137 | }
|
| 138 | }
|
| 139 | }
|
| 140 |
|
| 141 | static <E extends Entity & Leashable> void tickLeash(final ServerLevel level, final E entity) {
|
| 142 | Leashable.LeashData leashData = entity.getLeashData();
|
| 143 | if (leashData != null && leashData.delayedLeashInfo != null) {
|
| 144 | restoreLeashFromSave(entity, leashData);
|
| 145 | }
|
| 146 |
|
| 147 | if (leashData != null && leashData.leashHolder != null) {
|
| 148 | if (!entity.canInteractWithLevel() || !leashData.leashHolder.canInteractWithLevel()) {
|
| 149 | if (level.getGameRules().get(GameRules.ENTITY_DROPS)) {
|
| 150 | entity.dropLeash();
|
| 151 | } else {
|
| 152 | entity.removeLeash();
|
| 153 | }
|
| 154 | }
|
| 155 |
|
| 156 | Entity leashHolder = entity.getLeashHolder();
|
| 157 | if (leashHolder != null && leashHolder.level() == entity.level()) {
|
| 158 | double distanceTo = entity.leashDistanceTo(leashHolder);
|
| 159 | entity.whenLeashedTo(leashHolder);
|
| 160 | if (distanceTo > entity.leashSnapDistance()) {
|
| 161 | level.playSound(null, leashHolder.getX(), leashHolder.getY(), leashHolder.getZ(), SoundEvents.LEAD_BREAK, SoundSource.NEUTRAL, 1.0F, 1.0F);
|
| 162 | entity.leashTooFarBehaviour();
|
| 163 | } else if (distanceTo > entity.leashElasticDistance() - leashHolder.getBbWidth() - entity.getBbWidth()
|
| 164 | && entity.checkElasticInteractions(leashHolder, leashData)) {
|
| 165 | entity.onElasticLeashPull();
|
| 166 | } else {
|
| 167 | entity.closeRangeLeashBehaviour(leashHolder);
|
| 168 | }
|
| 169 |
|
| 170 | entity.setYRot((float)(entity.getYRot() - leashData.angularMomentum));
|
| 171 | leashData.angularMomentum = leashData.angularMomentum * angularFriction(entity);
|
| 172 | }
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | default void onElasticLeashPull() {
|
| 177 | Entity entity = (Entity)this;
|
| 178 | entity.checkFallDistanceAccumulation();
|
| 179 | }
|
| 180 |
|
| 181 | default double leashSnapDistance() {
|
| 182 | return 12.0;
|
| 183 | }
|
| 184 |
|
| 185 | default double leashElasticDistance() {
|
| 186 | return 6.0;
|
| 187 | }
|
| 188 |
|
| 189 | static <E extends Entity & Leashable> float angularFriction(final E entity) {
|
| 190 | if (entity.onGround()) {
|
| 191 | return entity.level().getBlockState(entity.getBlockPosBelowThatAffectsMyMovement()).getBlock().getFriction() * 0.91F;
|
| 192 | } else {
|
| 193 | return entity.isInLiquid() ? 0.8F : 0.91F;
|
| 194 | }
|
| 195 | }
|
| 196 |
|
| 197 | default void whenLeashedTo(final Entity leashHolder) {
|
| 198 | leashHolder.notifyLeashHolder(this);
|
| 199 | }
|
| 200 |
|
| 201 | default void leashTooFarBehaviour() {
|
| 202 | this.dropLeash();
|
| 203 | }
|
| 204 |
|
| 205 | default void closeRangeLeashBehaviour(final Entity leashHolder) {
|
| 206 | }
|
| 207 |
|
| 208 | default boolean checkElasticInteractions(final Entity leashHolder, final Leashable.LeashData leashData) {
|
| 209 | boolean quadConnection = leashHolder.supportQuadLeashAsHolder() && this.supportQuadLeash();
|
| 210 | List<Leashable.Wrench> wrenches = computeElasticInteraction(
|
| 211 | (Entity & Leashable)this,
|
| 212 | leashHolder,
|
| 213 | quadConnection ? SHARED_QUAD_ATTACHMENT_POINTS : ENTITY_ATTACHMENT_POINT,
|
| 214 | quadConnection ? SHARED_QUAD_ATTACHMENT_POINTS : LEASHER_ATTACHMENT_POINT
|
| 215 | );
|
| 216 | if (wrenches.isEmpty()) {
|
| 217 | return false;
|
| 218 | }
|
| 219 |
|
| 220 | Leashable.Wrench result = Leashable.Wrench.accumulate(wrenches).scale(quadConnection ? 0.25 : 1.0);
|
| 221 | leashData.angularMomentum = leashData.angularMomentum + 10.0 * result.torque();
|
| 222 | Vec3 relativeVelocityToLeasher = getHolderMovement(leashHolder).subtract(((Entity)this).getKnownMovement());
|
| 223 | ((Entity)this).addDeltaMovement(result.force().multiply(AXIS_SPECIFIC_ELASTICITY).add(relativeVelocityToLeasher.scale(0.11)));
|
| 224 | return true;
|
| 225 | }
|
| 226 |
|
| 227 | private static Vec3 getHolderMovement(final Entity leashHolder) {
|
| 228 | return leashHolder instanceof Mob mob && mob.isNoAi() ? Vec3.ZERO : leashHolder.getKnownMovement();
|
| 229 | }
|
| 230 |
|
| 231 | private static <E extends Entity & Leashable> List<Leashable.Wrench> computeElasticInteraction(
|
| 232 | final E entity, final Entity leashHolder, final List<Vec3> entityAttachmentPoints, final List<Vec3> leasherAttachmentPoints
|
| 233 | ) {
|
| 234 | double slackDistance = entity.leashElasticDistance();
|
| 235 | Vec3 currentMovement = getHolderMovement(entity);
|
| 236 | float entityYRot = entity.getYRot() * (float) (Math.PI / 180.0);
|
| 237 | Vec3 entityDimensions = new Vec3(entity.getBbWidth(), entity.getBbHeight(), entity.getBbWidth());
|
| 238 | float leashHolderYRot = leashHolder.getYRot() * (float) (Math.PI / 180.0);
|
| 239 | Vec3 leasherDimensions = new Vec3(leashHolder.getBbWidth(), leashHolder.getBbHeight(), leashHolder.getBbWidth());
|
| 240 | List<Leashable.Wrench> wrenches = new ArrayList<>();
|
| 241 |
|
| 242 | for (int i = 0; i < entityAttachmentPoints.size(); i++) {
|
| 243 | Vec3 entityAttachVector = entityAttachmentPoints.get(i).multiply(entityDimensions).yRot(-entityYRot);
|
| 244 | Vec3 entityAttachPos = entity.position().add(entityAttachVector);
|
| 245 | Vec3 leasherAttachVector = leasherAttachmentPoints.get(i).multiply(leasherDimensions).yRot(-leashHolderYRot);
|
| 246 | Vec3 leasherAttachPos = leashHolder.position().add(leasherAttachVector);
|
| 247 | computeDampenedSpringInteraction(leasherAttachPos, entityAttachPos, slackDistance, currentMovement, entityAttachVector).ifPresent(wrenches::add);
|
| 248 | }
|
| 249 |
|
| 250 | return wrenches;
|
| 251 | }
|
| 252 |
|
| 253 | private static Optional<Leashable.Wrench> computeDampenedSpringInteraction(
|
| 254 | final Vec3 pivotPoint, final Vec3 objectPosition, final double springSlack, final Vec3 objectMotion, final Vec3 leverArm
|
| 255 | ) {
|
| 256 | double distance = objectPosition.distanceTo(pivotPoint);
|
| 257 | if (distance < springSlack) {
|
| 258 | return Optional.empty();
|
| 259 | }
|
| 260 |
|
| 261 | Vec3 displacement = pivotPoint.subtract(objectPosition).normalize().scale(distance - springSlack);
|
| 262 | double torque = Leashable.Wrench.torqueFromForce(leverArm, displacement);
|
| 263 | boolean sameDirectionToMovement = objectMotion.dot(displacement) >= 0.0;
|
| 264 | if (sameDirectionToMovement) {
|
| 265 | displacement = displacement.scale(0.3F);
|
| 266 | }
|
| 267 |
|
| 268 | return Optional.of(new Leashable.Wrench(displacement, torque));
|
| 269 | }
|
| 270 |
|
| 271 | default boolean supportQuadLeash() {
|
| 272 | return false;
|
| 273 | }
|
| 274 |
|
| 275 | default Vec3[] getQuadLeashOffsets() {
|
| 276 | return createQuadLeashOffsets((Entity)this, 0.0, 0.5, 0.5, 0.5);
|
| 277 | }
|
| 278 |
|
| 279 | static Vec3[] createQuadLeashOffsets(final Entity entity, final double frontOffset, final double frontBack, final double leftRight, final double height) {
|
| 280 | float width = entity.getBbWidth();
|
| 281 | double frontOffsetScaled = frontOffset * width;
|
| 282 | double frontBackScaled = frontBack * width;
|
| 283 | double leftRightScaled = leftRight * width;
|
| 284 | double heightScaled = height * entity.getBbHeight();
|
| 285 | return new Vec3[]{
|
| 286 | new Vec3(-leftRightScaled, heightScaled, frontBackScaled + frontOffsetScaled),
|
| 287 | new Vec3(-leftRightScaled, heightScaled, -frontBackScaled + frontOffsetScaled),
|
| 288 | new Vec3(leftRightScaled, heightScaled, -frontBackScaled + frontOffsetScaled),
|
| 289 | new Vec3(leftRightScaled, heightScaled, frontBackScaled + frontOffsetScaled)
|
| 290 | };
|
| 291 | }
|
| 292 |
|
| 293 | default Vec3 getLeashOffset(final float partialTicks) {
|
| 294 | return this.getLeashOffset();
|
| 295 | }
|
| 296 |
|
| 297 | default Vec3 getLeashOffset() {
|
| 298 | Entity entity = (Entity)this;
|
| 299 | return new Vec3(0.0, entity.getEyeHeight(), entity.getBbWidth() * 0.4F);
|
| 300 | }
|
| 301 |
|
| 302 | default void setLeashedTo(final Entity holder, final boolean synch) {
|
| 303 | if (this != holder) {
|
| 304 | setLeashedTo((Entity & Leashable)this, holder, synch);
|
| 305 | }
|
| 306 | }
|
| 307 |
|
| 308 | private static <E extends Entity & Leashable> void setLeashedTo(final E entity, final Entity holder, final boolean synch) {
|
| 309 | Leashable.LeashData leashData = entity.getLeashData();
|
| 310 | if (leashData == null) {
|
| 311 | leashData = new Leashable.LeashData(holder);
|
| 312 | entity.setLeashData(leashData);
|
| 313 | } else {
|
| 314 | Entity oldHolder = leashData.leashHolder;
|
| 315 | leashData.setLeashHolder(holder);
|
| 316 | if (oldHolder != null && oldHolder != holder) {
|
| 317 | oldHolder.notifyLeasheeRemoved(entity);
|
| 318 | }
|
| 319 | }
|
| 320 |
|
| 321 | if (synch && entity.level() instanceof ServerLevel level) {
|
| 322 | level.getChunkSource().sendToTrackingPlayers(entity, new ClientboundSetEntityLinkPacket(entity, holder));
|
| 323 | }
|
| 324 |
|
| 325 | if (entity.isPassenger()) {
|
| 326 | entity.stopRiding();
|
| 327 | }
|
| 328 | }
|
| 329 |
|
| 330 | default @Nullable Entity getLeashHolder() {
|
| 331 | return getLeashHolder((Entity & Leashable)this);
|
| 332 | }
|
| 333 |
|
| 334 | private static <E extends Entity & Leashable> @Nullable Entity getLeashHolder(final E entity) {
|
| 335 | Leashable.LeashData leashData = entity.getLeashData();
|
| 336 | if (leashData == null) {
|
| 337 | return null;
|
| 338 | }
|
| 339 |
|
| 340 | Entity ntt = entity.level().getEntity(leashData.delayedLeashHolderId);
|
| 341 | if (leashData.delayedLeashHolderId != 0 && entity.level().isClientSide() && ntt != null) {
|
| 342 | leashData.setLeashHolder(ntt);
|
| 343 | }
|
| 344 |
|
| 345 | return leashData.leashHolder;
|
| 346 | }
|
| 347 |
|
| 348 | static List<Leashable> leashableLeashedTo(final Entity entity) {
|
| 349 | return leashableInArea(entity, l -> l.getLeashHolder() == entity);
|
| 350 | }
|
| 351 |
|
| 352 | static List<Leashable> leashableInArea(final Entity entity, final Predicate<Leashable> test) {
|
| 353 | return leashableInArea(entity.level(), entity.getBoundingBox().getCenter(), test);
|
| 354 | }
|
| 355 |
|
| 356 | static List<Leashable> leashableInArea(final Level level, final Vec3 pos, final Predicate<Leashable> test) {
|
| 357 | double size = 32.0;
|
| 358 | AABB scanArea = AABB.ofSize(pos, 32.0, 32.0, 32.0);
|
| 359 | return level.getEntitiesOfClass(Entity.class, scanArea, e -> e instanceof Leashable leashable && test.test(leashable))
|
| 360 | .stream()
|
| 361 | .map(Leashable.class::cast)
|
| 362 | .toList();
|
| 363 | }
|
| 364 |
|
| 365 | final class LeashData {
|
| 366 | public static final Codec<Leashable.LeashData> CODEC = Codec.xor(UUIDUtil.CODEC.fieldOf("UUID").codec(), BlockPos.CODEC)
|
| 367 | .xmap(
|
| 368 | Leashable.LeashData::new,
|
| 369 | data -> {
|
| 370 | if (data.leashHolder instanceof LeashFenceKnotEntity leashKnot) {
|
| 371 | return Either.right(leashKnot.getPos());
|
| 372 | } else {
|
| 373 | return data.leashHolder != null
|
| 374 | ? Either.left(data.leashHolder.getUUID())
|
| 375 | : Objects.requireNonNull(data.delayedLeashInfo, "Invalid LeashData had no attachment");
|
| 376 | }
|
| 377 | }
|
| 378 | );
|
| 379 | private int delayedLeashHolderId;
|
| 380 | public @Nullable Entity leashHolder;
|
| 381 | public @Nullable Either<UUID, BlockPos> delayedLeashInfo;
|
| 382 | public double angularMomentum;
|
| 383 |
|
| 384 | private LeashData(final Either<UUID, BlockPos> delayedLeashInfo) {
|
| 385 | this.delayedLeashInfo = delayedLeashInfo;
|
| 386 | }
|
| 387 |
|
| 388 | private LeashData(final Entity entity) {
|
| 389 | this.leashHolder = entity;
|
| 390 | }
|
| 391 |
|
| 392 | private LeashData(final int entityId) {
|
| 393 | this.delayedLeashHolderId = entityId;
|
| 394 | }
|
| 395 |
|
| 396 | public void setLeashHolder(final Entity leashHolder) {
|
| 397 | this.leashHolder = leashHolder;
|
| 398 | this.delayedLeashInfo = null;
|
| 399 | this.delayedLeashHolderId = 0;
|
| 400 | }
|
| 401 | }
|
| 402 |
|
| 403 | record Wrench(Vec3 force, double torque) {
|
| 404 | public static final Leashable.Wrench ZERO = new Leashable.Wrench(Vec3.ZERO, 0.0);
|
| 405 |
|
| 406 | public static double torqueFromForce(final Vec3 leverArm, final Vec3 force) {
|
| 407 | return leverArm.z * force.x - leverArm.x * force.z;
|
| 408 | }
|
| 409 |
|
| 410 | public static Leashable.Wrench accumulate(final List<Leashable.Wrench> wrenches) {
|
| 411 | if (wrenches.isEmpty()) {
|
| 412 | return ZERO;
|
| 413 | }
|
| 414 |
|
| 415 | double x = 0.0;
|
| 416 | double y = 0.0;
|
| 417 | double z = 0.0;
|
| 418 | double t = 0.0;
|
| 419 |
|
| 420 | for (Leashable.Wrench wrench : wrenches) {
|
| 421 | Vec3 force = wrench.force;
|
| 422 | x += force.x;
|
| 423 | y += force.y;
|
| 424 | z += force.z;
|
| 425 | t += wrench.torque;
|
| 426 | }
|
| 427 |
|
| 428 | return new Leashable.Wrench(new Vec3(x, y, z), t);
|
| 429 | }
|
| 430 |
|
| 431 | public Leashable.Wrench scale(final double scale) {
|
| 432 | return new Leashable.Wrench(this.force.scale(scale), this.torque * scale);
|
| 433 | }
|
| 434 | }
|
| 435 | }
|
| 436 |
|