Skip to content

Commit e545fcc

Browse files
committed
Fix #38: Cap pushing velocities to region size
1 parent 5b57e9b commit e545fcc

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

shreddedpaper-server/minecraft-patches/sources/net/minecraft/world/entity/Entity.java.patch

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@
8484
BlockPos blockPos = this.mainSupportingBlockPos.get();
8585
if (!(yOffset > 1.0E-5F)) {
8686
return blockPos;
87+
@@ -2377,7 +_,7 @@
88+
}
89+
delta = event.getKnockback();
90+
}
91+
- this.setDeltaMovement(this.getDeltaMovement().add(delta.getX(), delta.getY(), delta.getZ()));
92+
+ this.setDeltaMovement(this.getDeltaMovement().add(delta.getX(), delta.getY(), delta.getZ()).shreddedPaperCapToRegionSize()); // ShreddedPaper - cap pushing delta to region size
93+
// Paper end - Add EntityKnockbackByEntityEvent and EntityPushedByEntityAttackEvent
94+
this.needsSync = true;
95+
}
8796
@@ -3546,7 +_,9 @@
8897
}
8998
}
@@ -127,14 +136,23 @@
127136
// Paper start - gateway-specific teleport event
128137
final org.bukkit.event.entity.EntityTeleportEvent teleEvent;
129138
if (this.portalProcess != null && this.portalProcess.isSamePortal(((net.minecraft.world.level.block.EndGatewayBlock) Blocks.END_GATEWAY)) && this.level.getBlockEntity(this.portalProcess.getEntryPosition()) instanceof net.minecraft.world.level.block.entity.TheEndGatewayBlockEntity theEndGatewayBlockEntity) {
139+
@@ -4920,7 +_,7 @@
140+
pushVector = pushVector.normalize().scale(0.0045000000000000005);
141+
}
142+
143+
- this.setDeltaMovement(currMovement.add(pushVector));
144+
+ this.setDeltaMovement(currMovement.add(pushVector).shreddedPaperCapToRegionSize()); // ShreddedPaper - cap pushing delta to region size
145+
146+
// note: inFluid = true here as pushVector != 0
147+
return true;
130148
@@ -4996,6 +_,12 @@
131149

132150
public void setDeltaMovement(Vec3 deltaMovement) {
133151
if (deltaMovement.isFinite()) {
134152
+ // ShreddedPaper start - why is something setting the entity velocity to larger than one region...
135153
+ if (deltaMovement.horizontalDistanceSqr() > RegionPos.MAX_DISTANCE_SQR + 1 && deltaMovement.horizontalDistanceSqr() > this.deltaMovement.horizontalDistanceSqr()) {
136154
+ LOGGER.warn("Velocity is being set larger than the ShreddedPaper region size: {} for entity {}", deltaMovement, this, new Exception("Velocity larger than region size, limiting velocity to region size"));
137-
+ deltaMovement = deltaMovement.normalize().scale(RegionPos.MAX_DISTANCE_SQR);
155+
+ deltaMovement = deltaMovement.shreddedPaperCapToRegionSize();
138156
+ }
139157
+ // ShreddedPaper end - why is something setting the entity velocity to larger than one region...
140158
synchronized (this.posLock) { // Paper - detailed watchdog information
Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
--- a/net/minecraft/world/phys/Vec3.java
22
+++ b/net/minecraft/world/phys/Vec3.java
3-
@@ -4,6 +_,7 @@
3+
@@ -1,9 +_,11 @@
4+
package net.minecraft.world.phys;
5+
6+
import com.mojang.serialization.Codec;
7+
+import io.multipaper.shreddedpaper.region.RegionPos;
48
import io.netty.buffer.ByteBuf;
59
import java.util.EnumSet;
610
import java.util.List;
711
+
812
import net.minecraft.core.Direction;
913
import net.minecraft.core.Position;
1014
import net.minecraft.core.Vec3i;
15+
@@ -335,4 +_,14 @@
16+
public boolean isFinite() {
17+
return Double.isFinite(this.x) && Double.isFinite(this.y) && Double.isFinite(this.z);
18+
}
19+
+
20+
+ // ShreddedPaper start - cap velocity to region size
21+
+ public Vec3 shreddedPaperCapToRegionSize() {
22+
+ double horizontalDistanceSqr = this.horizontalDistanceSqr();
23+
+ if (horizontalDistanceSqr > RegionPos.MAX_DISTANCE_SQR) {
24+
+ return this.scale((RegionPos.MAX_DISTANCE - 1) / Math.sqrt(horizontalDistanceSqr)); // Subtract 1 to ensure no double precision issues causes horizontalDistance > RegionPos.MAX_DISTANCE
25+
+ }
26+
+ return this;
27+
+ }
28+
+ // ShreddedPaper end - cap velocity to region size
29+
}

shreddedpaper-server/src/main/java/io/multipaper/shreddedpaper/region/RegionPos.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.multipaper.shreddedpaper.region;
22

33
import com.mojang.logging.LogUtils;
4+
import io.netty.util.internal.MathUtil;
45
import net.minecraft.core.BlockPos;
56
import net.minecraft.core.SectionPos;
67
import net.minecraft.world.level.ChunkPos;
@@ -16,7 +17,8 @@ public class RegionPos {
1617
public static final int REGION_SIZE; // eg 8 (for an 8x8 region)
1718
public static final int REGION_SHIFT; // eg 3 (1 << 3 == 8)
1819
public static final int REGION_SIZE_MASK; // eg 7 (9 % 8 == 9 & 7 == 1)
19-
public static final int MAX_DISTANCE_SQR;
20+
public static final double MAX_DISTANCE_SQR;
21+
public static final double MAX_DISTANCE;
2022

2123
static {
2224
// desiredRegionSize = 7 -> shift = 3, size = 8, mask = 7
@@ -45,7 +47,8 @@ public class RegionPos {
4547

4648
LOGGER.info("Using region size: {}, shift={}, mask={}", REGION_SIZE, REGION_SHIFT, REGION_SIZE_MASK);
4749

48-
MAX_DISTANCE_SQR = RegionPos.REGION_SIZE * 16 * RegionPos.REGION_SIZE * 16;
50+
MAX_DISTANCE_SQR = (double) RegionPos.REGION_SIZE * 16 * RegionPos.REGION_SIZE * 16;
51+
MAX_DISTANCE = Math.sqrt(MAX_DISTANCE_SQR);
4952
}
5053

5154
public final int x;

0 commit comments

Comments
 (0)