исправлена сериализация пакета PlayerAbilities (+тест)
This commit is contained in:
@@ -18,6 +18,11 @@ import mc.core.network.SCPacket;
|
||||
@Setter
|
||||
@ToString
|
||||
public class PlayerAbilitiesPacket implements SCPacket, CSPacket {
|
||||
private static final byte $GOD_MODE_MASK = 0x01,
|
||||
$FLYING_MASK = 0x02,
|
||||
$CAN_FLY_MASK = 0x04,
|
||||
$IDB_MASK = 0x08;
|
||||
|
||||
private boolean godMode = false;
|
||||
private boolean flying = false;
|
||||
private boolean canFly = false;
|
||||
@@ -29,10 +34,10 @@ public class PlayerAbilitiesPacket implements SCPacket, CSPacket {
|
||||
@Override
|
||||
public void writeSelf(NetOutputStream netStream) {
|
||||
byte flag = 0;
|
||||
if (godMode) flag = (byte)(flag | 0x01);
|
||||
if (flying) flag = (byte)(flag | 0x02);
|
||||
if (canFly) flag = (byte)(flag | 0x04);
|
||||
if (instantDestroyBlocks) flag = (byte)(flag | 0x08);
|
||||
if (godMode) flag = (byte)(flag | $GOD_MODE_MASK);
|
||||
if (flying) flag = (byte)(flag | $FLYING_MASK);
|
||||
if (canFly) flag = (byte)(flag | $CAN_FLY_MASK);
|
||||
if (instantDestroyBlocks) flag = (byte)(flag | $IDB_MASK);
|
||||
|
||||
netStream.writeByte(flag);
|
||||
netStream.writeFloat(flyingSpeed);
|
||||
@@ -42,11 +47,10 @@ public class PlayerAbilitiesPacket implements SCPacket, CSPacket {
|
||||
@Override
|
||||
public void readSelf(NetInputStream netStream) {
|
||||
byte flag = netStream.readByte();
|
||||
//FIXME треубет проверки
|
||||
godMode = (flag == 0x08);
|
||||
canFly = (flag == 0x04);
|
||||
flying = (flag == 0x02);
|
||||
instantDestroyBlocks = (flag == 0x01);
|
||||
godMode = (flag & $GOD_MODE_MASK) > 0;
|
||||
canFly = (flag & $CAN_FLY_MASK) > 0;
|
||||
flying = (flag & $FLYING_MASK) > 0;
|
||||
instantDestroyBlocks = (flag & $IDB_MASK) > 0;
|
||||
|
||||
flyingSpeed = netStream.readFloat();
|
||||
walkingSpeed = netStream.readFloat();
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
package mc.core.network.proto_1_12_2.packets;
|
||||
|
||||
import mc.core.network.proto_1_12_2.NetInputStream_p340;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.EOFException;
|
||||
|
||||
public class ByteArrayInputNetStream extends NetInputStream_p340 {
|
||||
private ByteArrayInputStream bais;
|
||||
|
||||
public ByteArrayInputNetStream(byte[] buff) {
|
||||
bais = new ByteArrayInputStream(buff);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean readBoolean() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte readByte() {
|
||||
return (byte) bais.read();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readBytes(byte[] buffer) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int readUnsignedByte() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int readUnsignedShort() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public short readShort() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int readInt() {
|
||||
int ch1 = bais.read();
|
||||
int ch2 = bais.read();
|
||||
int ch3 = bais.read();
|
||||
int ch4 = bais.read();
|
||||
if ((ch1 | ch2 | ch3 | ch4) < 0) return 0;
|
||||
return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
|
||||
}
|
||||
|
||||
@Override
|
||||
public long readLong() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float readFloat() {
|
||||
return Float.intBitsToFloat(readInt());
|
||||
}
|
||||
|
||||
@Override
|
||||
public double readDouble() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void skipBytes(int count) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package mc.core.network.proto_1_12_2.packets;
|
||||
|
||||
import mc.core.network.proto_1_12_2.ByteArrayOutputNetStream;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.util.Random;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class TestByteArrayInputNetStream {
|
||||
private Random rnd = new Random();
|
||||
|
||||
@Test
|
||||
public void testReadByte() {
|
||||
final byte b0 = (byte) rnd.nextInt();
|
||||
ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream();
|
||||
netStream.writeByte(b0);
|
||||
byte[] buffer = netStream.toByteArray();
|
||||
|
||||
ByteArrayInputNetStream netInputStream = new ByteArrayInputNetStream(buffer);
|
||||
byte b1 = netInputStream.readByte();
|
||||
assertEquals(b0, b1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadInt() {
|
||||
final int i0 = rnd.nextInt();
|
||||
ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream();
|
||||
netStream.writeInt(i0);
|
||||
byte[] buffer = netStream.toByteArray();
|
||||
|
||||
ByteArrayInputNetStream netInputStream = new ByteArrayInputNetStream(buffer);
|
||||
int i1 = netInputStream.readInt();
|
||||
assertEquals(i0, i1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReadFloat() {
|
||||
final float f0 = rnd.nextFloat();
|
||||
ByteArrayOutputNetStream netStream = new ByteArrayOutputNetStream();
|
||||
netStream.writeFloat(f0);
|
||||
byte[] buffer = netStream.toByteArray();
|
||||
|
||||
ByteArrayInputNetStream netInputStream = new ByteArrayInputNetStream(buffer);
|
||||
float f1 = netInputStream.readFloat();
|
||||
assertEquals(f0, f1, 0.0f);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package mc.core.network.proto_1_12_2.packets;
|
||||
|
||||
import mc.core.network.proto_1_12_2.ByteArrayOutputNetStream;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class TestPlayerAbilitiesPacket {
|
||||
private Random rnd = new Random();
|
||||
private PlayerAbilitiesPacket packet;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
packet = new PlayerAbilitiesPacket();
|
||||
packet.setGodMode(rnd.nextBoolean());
|
||||
packet.setFlying(rnd.nextBoolean());
|
||||
packet.setCanFly(rnd.nextBoolean());
|
||||
packet.setInstantDestroyBlocks(rnd.nextBoolean());
|
||||
packet.setFlyingSpeed(rnd.nextFloat());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
ByteArrayOutputNetStream netOutputStream = new ByteArrayOutputNetStream();
|
||||
packet.writeSelf(netOutputStream);
|
||||
|
||||
ByteArrayInputNetStream netInputStream = new ByteArrayInputNetStream(netOutputStream.toByteArray());
|
||||
PlayerAbilitiesPacket outPkt = new PlayerAbilitiesPacket();
|
||||
outPkt.readSelf(netInputStream);
|
||||
|
||||
assertEquals("god mode", packet.isGodMode(), outPkt.isGodMode());
|
||||
assertEquals("flying", packet.isFlying(), outPkt.isFlying());
|
||||
assertEquals("can fly", packet.isCanFly(), outPkt.isCanFly());
|
||||
assertEquals("instant destroy block", packet.isInstantDestroyBlocks(), outPkt.isInstantDestroyBlocks());
|
||||
assertEquals("flying speed", packet.getFlyingSpeed(), outPkt.getFlyingSpeed(), 0.0f);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user