add Float datatype

This commit is contained in:
sirjonasxx 2021-02-12 00:20:10 +01:00
parent c1188ab228
commit ba371c4d5b
2 changed files with 34 additions and 12 deletions

View File

@ -208,6 +208,7 @@ public class PacketStringUtils {
.replace("\r", "\\r") // CR -> \r
).append("\"}");
else if (c == 'd') builder.append("{d:").append(p.readDouble()).append('}');
else if (c == 'f') builder.append("{f:").append(p.readFloat()).append('}');
else if (c == 'b') builder.append("{b:").append(p.readByte()).append('}');
else if (c == 'B') builder.append("{b:").append(p.readBoolean()).append('}');
else if (c == 'l') builder.append("{l:").append(p.readLong()).append('}');

View File

@ -149,6 +149,15 @@ public class HPacket implements StringifyAble {
return java.nio.ByteBuffer.wrap(packetInBytes).getDouble(index);
}
public float readFloat(){
float result = readFloat(readIndex);
readIndex += 4;
return result;
}
public float readFloat(int index) {
return java.nio.ByteBuffer.wrap(packetInBytes).getFloat(index);
}
public int length() {
return readInteger(0);
}
@ -263,6 +272,14 @@ public class HPacket implements StringifyAble {
}
return this;
}
public HPacket replaceFloat(int index, float f) {
isEdited = true;
ByteBuffer b = ByteBuffer.allocate(4).putFloat(f);
for (int j = 0; j < 4; j++) {
packetInBytes[index + j] = b.array()[j];
}
return this;
}
public HPacket replaceByte(int index, byte b) {
isEdited = true;
packetInBytes[index] = b;
@ -443,6 +460,16 @@ public class HPacket implements StringifyAble {
fixLength();
return this;
}
public HPacket appendFloat(float f) {
isEdited = true;
packetInBytes = Arrays.copyOf(packetInBytes, packetInBytes.length + 4);
ByteBuffer byteBuffer = ByteBuffer.allocate(4).putFloat(f);
for (int j = 0; j < 4; j++) {
packetInBytes[packetInBytes.length - 4 + j] = byteBuffer.array()[j];
}
fixLength();
return this;
}
public HPacket appendByte(byte b) {
isEdited = true;
packetInBytes = Arrays.copyOf(packetInBytes, packetInBytes.length + 1);
@ -526,6 +553,12 @@ public class HPacket implements StringifyAble {
else if (o instanceof Long) {
appendLong((Long) o);
}
else if (o instanceof Float) {
appendFloat((Float) o);
}
else if (o instanceof Double) {
appendDouble((Double) o);
}
else {
throw new InvalidParameterException();
}
@ -608,16 +641,4 @@ public class HPacket implements StringifyAble {
HPacket packet2 = (HPacket) object;
return Arrays.equals(packetInBytes, packet2.packetInBytes) && (isEdited == packet2.isEdited);
}
public static void main(String[] args) {
HPacket packet = new HPacket("{l}{h:4564}{i:3}{i:0}{s:\"hi\"}{i:0}{i:1}{s:\"how\"}{i:3}{b:1}{b:2}{b:3}{i:2}{s:\"r u\"}{i:1}{b:120}{i:2}{b:true}{d:1.4}");
String str = PacketStringUtils.toExpressionFromGivenStructure(packet, "i(isi(b))iBd");
HPacket packetverify = new HPacket(str);
System.out.println(str);
System.out.println(packetverify.toString().equals(packet.toString()));
}
}