Implement New Type. FixedPointInteger. (#93)

Fix: SpawnPlayerPacket coordinates read wrongly before protocol 100. Add types.FixedPointInteger.
This commit is contained in:
Zachy authored and joo committed 2018-06-20 05:32:35 +01:00
1 parent 0a1776f97a
commit d3a8cc8dfb
3 files changed
+27 -9

No files matched your search

@@ -3,8 +3,8 @@ from minecraft.networking.packets import (
) )
from minecraft.networking.types import ( from minecraft.networking.types import (
Integer, UnsignedByte, Byte, Boolean, UUID, Short, VarInt, Double, Float, Integer, FixedPointInteger, UnsignedByte, Byte, Boolean, UUID, Short,
String, Enum, VarInt, Double, Float, String, Enum,
) )
from .combat_event_packet import CombatEventPacket from .combat_event_packet import CombatEventPacket
@@ -126,9 +126,12 @@ class SpawnPlayerPacket(Packet):
get_definition = staticmethod(lambda context: [ get_definition = staticmethod(lambda context: [
{'entity_id': VarInt}, {'entity_id': VarInt},
{'player_UUID': UUID}, {'player_UUID': UUID},
{'x': Double} if context.protocol_version >= 100 else {'x': Integer}, {'x': Double} if context.protocol_version >= 100
{'y': Double} if context.protocol_version >= 100 else {'y': Integer}, else {'x': FixedPointInteger},
{'z': Double} if context.protocol_version >= 100 else {'z': Integer}, {'y': Double} if context.protocol_version >= 100
else {'y': FixedPointInteger},
{'z': Double} if context.protocol_version >= 100
else {'z': FixedPointInteger},
{'yaw': Float}, {'yaw': Float},
{'pitch': Float}, {'pitch': Float},
# TODO: read entity metadata # TODO: read entity metadata
+11
View File
@@ -2,6 +2,7 @@
Each type has a method which is used to read and write it. Each type has a method which is used to read and write it.
These definitions and methods are used by the packet definitions These definitions and methods are used by the packet definitions
""" """
from __future__ import division
import struct import struct
import uuid import uuid
from collections import namedtuple from collections import namedtuple
@@ -120,6 +121,16 @@ class Integer(Type):
socket.send(struct.pack('>i', value)) socket.send(struct.pack('>i', value))
class FixedPointInteger(Type):
@staticmethod
def read(file_object):
return Integer.read(file_object) / 32
@staticmethod
def send(value, socket):
Integer.send(int(value * 32), socket)
class VarInt(Type): class VarInt(Type):
@staticmethod @staticmethod
def read(file_object): def read(file_object):
+8 -4
View File
@@ -3,9 +3,9 @@
import unittest import unittest
from minecraft.networking.types import ( from minecraft.networking.types import (
Type, Boolean, UnsignedByte, Byte, Short, UnsignedShort, Type, Boolean, UnsignedByte, Byte, Short, UnsignedShort,
Integer, VarInt, Long, Float, Double, ShortPrefixedByteArray, Integer, FixedPointInteger, VarInt, Long, Float, Double,
VarIntPrefixedByteArray, UUID, String as StringType, Position, ShortPrefixedByteArray, VarIntPrefixedByteArray, UUID,
TrailingByteArray, UnsignedLong, String as StringType, Position, TrailingByteArray, UnsignedLong,
) )
from minecraft.networking.packets import PacketBuffer from minecraft.networking.packets import PacketBuffer
@@ -18,6 +18,7 @@ TEST_DATA = {
UnsignedShort: [0, 400], UnsignedShort: [0, 400],
UnsignedLong: [0, 400], UnsignedLong: [0, 400],
Integer: [-1000, 1000], Integer: [-1000, 1000],
FixedPointInteger: [float(-13098.3435), float(-0.83), float(1000)],
VarInt: [1, 250, 50000, 10000000], VarInt: [1, 250, 50000, 10000000],
Long: [50000000], Long: [50000000],
Float: [21.000301], Float: [21.000301],
@@ -44,7 +45,10 @@ class SerializationTest(unittest.TestCase):
packet_buffer.reset_cursor() packet_buffer.reset_cursor()
deserialized = data_type.read(packet_buffer) deserialized = data_type.read(packet_buffer)
if data_type is Float or data_type is Double: if data_type is FixedPointInteger:
self.assertAlmostEqual(
test_data, deserialized, delta=1.0/32.0)
elif data_type is Float or data_type is Double:
self.assertAlmostEquals(test_data, deserialized, 3) self.assertAlmostEquals(test_data, deserialized, 3)
else: else:
self.assertEqual(test_data, deserialized) self.assertEqual(test_data, deserialized)