Skip to content

Commit ab20f50

Browse files
authored
Fix short overflow in old velocity handling (#1171)
1 parent 1f4f451 commit ab20f50

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/rewriter/EntityPacketRewriter1_21_2.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.viaversion.viabackwards.protocol.v1_21_2to1_21.Protocol1_21_2To1_21;
2929
import com.viaversion.viabackwards.protocol.v1_21_2to1_21.storage.PlayerStorage;
3030
import com.viaversion.viabackwards.protocol.v1_21_2to1_21.storage.SignStorage;
31+
import com.viaversion.viabackwards.utils.VelocityUtil;
3132
import com.viaversion.viaversion.api.data.entity.EntityTracker;
3233
import com.viaversion.viaversion.api.minecraft.Holder;
3334
import com.viaversion.viaversion.api.minecraft.Particle;
@@ -548,9 +549,9 @@ private void handleRelativeArguments(
548549
} else if (!relativeDeltaX && !relativeDeltaY && !relativeDeltaZ) {
549550
final PacketWrapper entityMotionPacket = wrapper.create(ClientboundPackets1_21.SET_ENTITY_MOTION);
550551
entityMotionPacket.write(Types.VAR_INT, entityId != null ? entityId : tracker(wrapper.user()).clientEntityId());
551-
entityMotionPacket.write(Types.SHORT, (short) (movementX * 8000));
552-
entityMotionPacket.write(Types.SHORT, (short) (movementY * 8000));
553-
entityMotionPacket.write(Types.SHORT, (short) (movementZ * 8000));
552+
entityMotionPacket.write(Types.SHORT, VelocityUtil.toLegacyVelocity(movementX));
553+
entityMotionPacket.write(Types.SHORT, VelocityUtil.toLegacyVelocity(movementY));
554+
entityMotionPacket.write(Types.SHORT, VelocityUtil.toLegacyVelocity(movementZ));
554555

555556
entityMotionPacket.send(Protocol1_21_2To1_21.class);
556557
} else if (!warned) {

common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_9to1_21_7/rewriter/EntityPacketRewriter1_21_9.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.viaversion.viabackwards.protocol.v1_21_9to1_21_7.storage.MannequinData;
2626
import com.viaversion.viabackwards.protocol.v1_21_9to1_21_7.storage.PlayerRotationStorage;
2727
import com.viaversion.viabackwards.protocol.v1_21_9to1_21_7.tracker.EntityTracker1_21_9;
28+
import com.viaversion.viabackwards.utils.VelocityUtil;
2829
import com.viaversion.viaversion.api.connection.UserConnection;
2930
import com.viaversion.viaversion.api.data.entity.TrackedEntity;
3031
import com.viaversion.viaversion.api.minecraft.BlockPosition;
@@ -329,9 +330,9 @@ private String randomHackyEmptyName() {
329330
}
330331

331332
private void writeMovementShorts(final PacketWrapper wrapper, final Vector3d movement) {
332-
wrapper.write(Types.SHORT, (short) (movement.x() * 8000));
333-
wrapper.write(Types.SHORT, (short) (movement.y() * 8000));
334-
wrapper.write(Types.SHORT, (short) (movement.z() * 8000));
333+
wrapper.write(Types.SHORT, VelocityUtil.toLegacyVelocity(movement.x()));
334+
wrapper.write(Types.SHORT, VelocityUtil.toLegacyVelocity(movement.y()));
335+
wrapper.write(Types.SHORT, VelocityUtil.toLegacyVelocity(movement.z()));
335336
}
336337

337338
private void storePlayerRotation(final PacketWrapper wrapper) {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards
3+
* Copyright (C) 2016-2026 ViaVersion and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package com.viaversion.viabackwards.utils;
19+
20+
public class VelocityUtil {
21+
22+
public static short toLegacyVelocity(double value) {
23+
return (short) Math.max(Short.MIN_VALUE, Math.min(Short.MAX_VALUE, (long) (value * 8000)));
24+
}
25+
26+
}

0 commit comments

Comments
 (0)