Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing ServerVersion.V_1_9_1, Update ByteBufHelper.writeVarInt #1064

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public enum ServerVersion {
//TODO Rename to MinecraftVersion?
V_1_7_10(5),
V_1_8(47), V_1_8_3(47), V_1_8_8(47),
V_1_9(107), V_1_9_2(109), V_1_9_4(110),
V_1_9(107), V_1_9_1(108), V_1_9_2(109), V_1_9_4(110),
//1.10 and 1.10.1 are redundant
V_1_10(210), V_1_10_1(210), V_1_10_2(210),
V_1_11(315), V_1_11_2(316),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package com.github.retrooper.packetevents.netty.buffer;

import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.protocol.player.ClientVersion;

import java.nio.charset.Charset;

Expand Down Expand Up @@ -254,14 +255,28 @@ public static int readVarInt(Object buffer) {
return value;
}

public static void writeVarInt(Object buffer, int value) {
while (true) {
if ((value & ~0x7F) == 0) {
writeByte(buffer, value);
return;
}
writeByte(buffer, (value & 0x7F) | 0x80);
value >>>= 7;
public static void writeVarInt(Object buf, int i) {
if ((i & (0xFFFFFFFF << 7)) == 0) {
// 1 byte case
writeByte(buf, i);
} else if ((i & (0xFFFFFFFF << 14)) == 0) {
// 2 byte case
int w = (i & 0x7F | 0x80) << 8 | (i >>> 7);
writeShort(buf, w);
} else if ((i & (0xFFFFFFFF << 21)) == 0) {
// 3 byte case
int w = ((i & 0x7F | 0x80) << 16) | ((i >>> 7 & 0x7F | 0x80) << 8) | (i >>> 14);
writeMedium(buf, w);
} else if ((i & (0xFFFFFFFF << 28)) == 0) {
// 4 byte case
int w = ((i & 0x7F | 0x80) << 24) | ((i >>> 7 & 0x7F | 0x80) << 16) | ((i >>> 14 & 0x7F | 0x80) << 8) | (i >>> 21);
writeInt(buf, w);
} else {
// 5 byte case (the max size for a VarInt)
// Write the first 4 bytes as an int
int w = ((i & 0x7F | 0x80) << 24) | ((i >>> 7 & 0x7F | 0x80) << 16) | ((i >>> 14 & 0x7F | 0x80) << 8) | (i >>> 21 & 0x7F | 0x80);
writeInt(buf, w); // Write the first 4 bytes
writeByte(buf, i >>> 28); // Write the remaining 5th byte
}
}

Expand Down