Merge pull request #212 from yuzumika/fix-selection-underflow #minor

NPCMoreTalkHandler: don't underflow selection
This commit is contained in:
Ponk
2024-02-04 12:07:46 +01:00
committed by GitHub
4 changed files with 34 additions and 1 deletions

View File

@@ -22,6 +22,8 @@ public class ByteBufInPacket implements InPacket {
public byte readByte() {
return byteBuf.readByte();
}
@Override
public short readUnsignedByte() { return byteBuf.readUnsignedByte(); }
@Override
public short readShort() {

View File

@@ -4,6 +4,7 @@ import java.awt.*;
public interface InPacket extends Packet {
byte readByte();
short readUnsignedByte();
short readShort();
int readInt();
long readLong();

View File

@@ -59,7 +59,7 @@ public final class NPCMoreTalkHandler extends AbstractPacketHandler {
if (p.available() >= 4) {
selection = p.readInt();
} else if (p.available() > 0) {
selection = p.readByte();
selection = p.readUnsignedByte();
}
if (c.getQM() != null) {
if (c.getQM().isStart()) {

View File

@@ -38,6 +38,36 @@ class ByteBufInPacketTest {
assertEquals(writtenByte, readByte);
}
@Test
void readUnsignedByte() {
final byte writtenByte = Byte.MAX_VALUE;
byteBuf.writeByte(writtenByte);
short readUnsignedByte = inPacket.readUnsignedByte();
assertEquals(writtenByte, readUnsignedByte);
}
@Test
void readUnsignedByte_shouldBeNonnegative() {
final byte writtenByte = Byte.MIN_VALUE;
byteBuf.writeByte(writtenByte);
short readUnsignedByte = inPacket.readUnsignedByte();
assertEquals((short)writtenByte + 256, readUnsignedByte);
}
@Test
void readUnsignedByte_shouldBeNonnegative2() {
final byte writtenByte = -1;
byteBuf.writeByte(writtenByte);
short readUnsignedByte = inPacket.readUnsignedByte();
assertEquals((short)writtenByte + 256, readUnsignedByte);
}
@Test
void readShort() {
final short writtenShort = 12_345;