Skip to content

Commit

Permalink
Limit eye of ender velocity
Browse files Browse the repository at this point in the history
  • Loading branch information
PureGero committed Aug 28, 2024
1 parent 6c398cc commit a0dd039
Showing 1 changed file with 47 additions and 8 deletions.
55 changes: 47 additions & 8 deletions patches/server/0055-Moving-into-another-region.patch
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,29 @@ Date: Fri, 23 Aug 2024 01:03:30 +0900
Subject: [PATCH] Moving into another region


diff --git a/src/main/java/io/multipaper/shreddedpaper/region/RegionPos.java b/src/main/java/io/multipaper/shreddedpaper/region/RegionPos.java
index c8caf9240e1aa5e434a47cbdf4b7bf68e4025586..c5fc229de6f086a0fe66d05380b612d2468d6079 100644
--- a/src/main/java/io/multipaper/shreddedpaper/region/RegionPos.java
+++ b/src/main/java/io/multipaper/shreddedpaper/region/RegionPos.java
@@ -16,6 +16,7 @@ public class RegionPos {
public static final int REGION_SIZE; // eg 8 (for an 8x8 region)
public static final int REGION_SHIFT; // eg 3 (1 << 3 == 8)
public static final int REGION_SIZE_MASK; // eg 7 (9 % 8 == 9 & 7 == 1)
+ public static final int MAX_DISTANCE_SQR;

static {
// desiredRegionSize = 7 -> shift = 3, size = 8, mask = 7
@@ -43,6 +44,8 @@ public class RegionPos {
}

LOGGER.info("Using region size: {}, shift={}, mask={}", REGION_SIZE, REGION_SHIFT, REGION_SIZE_MASK);
+
+ MAX_DISTANCE_SQR = RegionPos.REGION_SIZE * 16 * RegionPos.REGION_SIZE * 16;
}

public final int x;
diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
index 6b2c2e2f782630a3b7e25532260cc61e02c8049f..b015fe3b2a7ef9a13c6f91edb7fded542e2e8604 100644
index 6b2c2e2f782630a3b7e25532260cc61e02c8049f..f45ae96bdb13e8d697fc48a50f3e405a3b681a73 100644
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
@@ -1176,7 +1176,11 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
Expand All @@ -28,25 +49,43 @@ index 6b2c2e2f782630a3b7e25532260cc61e02c8049f..b015fe3b2a7ef9a13c6f91edb7fded54
- this.setDeltaMovement(this.getDeltaMovement().add(delta.getX(), delta.getY(), delta.getZ()));
+ // ShreddedPaper start - limit push velocity
+ Vec3 newDelta = this.getDeltaMovement().add(delta.getX(), delta.getY(), delta.getZ());
+ if (newDelta.lengthSqr() > (RegionPos.REGION_SIZE * 16 * RegionPos.REGION_SIZE * 16)) {
+ if (newDelta.lengthSqr() > RegionPos.MAX_DISTANCE_SQR) {
+ newDelta = newDelta.normalize().scale(RegionPos.REGION_SIZE * 16);
+ }
+ this.setDeltaMovement(newDelta);
+ // ShreddedPaper end - limit push velocity
// Paper end - Add EntityKnockbackByEntityEvent and EntityPushedByEntityAttackEvent
this.hasImpulse = true;
}
@@ -4779,7 +4789,13 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
return this.deltaMovement;
@@ -4780,6 +4790,11 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
}

+ // ShreddedPaper start - why is something setting the entity velocity to larger than one region...
+ private static final int REGION_SIZE_BLOCKS_SQR = (RegionPos.REGION_SIZE * 16) * (RegionPos.REGION_SIZE * 16) + 1;
public void setDeltaMovement(Vec3 velocity) {
+ if (velocity.horizontalDistanceSqr() > REGION_SIZE_BLOCKS_SQR && velocity.horizontalDistanceSqr() > this.deltaMovement.horizontalDistanceSqr()) {
+ // ShreddedPaper start - why is something setting the entity velocity to larger than one region...
+ if (velocity.horizontalDistanceSqr() > RegionPos.MAX_DISTANCE_SQR + 1 && velocity.horizontalDistanceSqr() > this.deltaMovement.horizontalDistanceSqr()) {
+ LOGGER.warn("Velocity is being set larger than the ShreddedPaper region size: {} for entity {}", velocity, this, new Exception("Velocity larger than region size"));
+ }
+ // ShreddedPaper end - why is something setting the entity velocity to larger than one region...
+ // ShreddedPaper end - why is something setting the entity velocity to larger than one region...
synchronized (this.posLock) { // Paper
this.deltaMovement = velocity;
} // Paper
diff --git a/src/main/java/net/minecraft/world/entity/projectile/EyeOfEnder.java b/src/main/java/net/minecraft/world/entity/projectile/EyeOfEnder.java
index fca3786d0a3f99a3e61e7a4b2251361276eff9d7..74f0577397c8665c9bea3f79775dc26c15543e62 100644
--- a/src/main/java/net/minecraft/world/entity/projectile/EyeOfEnder.java
+++ b/src/main/java/net/minecraft/world/entity/projectile/EyeOfEnder.java
@@ -1,5 +1,6 @@
package net.minecraft.world.entity.projectile;

+import io.multipaper.shreddedpaper.region.RegionPos;
import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.nbt.CompoundTag;
@@ -137,6 +138,8 @@ public class EyeOfEnder extends Entity implements ItemSupplier {

int i = this.getY() < this.ty ? 1 : -1;

+ if (d6 > RegionPos.MAX_DISTANCE_SQR) d6 = RegionPos.MAX_DISTANCE_SQR; // ShreddedPaper - keep within a region
+
vec3d = new Vec3(Math.cos((double) f1) * d6, d7 + ((double) i - d7) * 0.014999999664723873D, Math.sin((double) f1) * d6);
this.setDeltaMovement(vec3d);
}

0 comments on commit a0dd039

Please sign in to comment.