Improve test coverage wrt protocol versions; other fixes/improvements
Improvements to the test suite: * List release version names and numbers in minecraft/__init__.py. * Make some tests, which previously ran for *all* protocol versions, run only for release protocol versions (to improve test performance). * Make some tests, which previously ran only for the latest protocol version, run for all release protocol versions (to improve coverage). * Print each protocol version being tested to the debug log, to help identify sources of errors. * Use the `nose-timer' plugin to show the run time of each test. Fix errors revealed by increased test coverage: * Fix: SoundEffectPacket.Pitch is not serialised correctly for early protocol versions. * Fix: handleExceptionTest finishes later than necessary because the test overrode an exception handler used internally by `_FakeServerTest', causing the server thread to time out after 4s. * Add support for multiple exception handlers in `Connection' (required for the above). Improvements to data descriptors: * Make syntax of property declarations more consistent/Pythonic. * Factor the definition of several aliasing properties into the utility methods `attribute_alias' and `multi_attribute_alias', which construct suitable data descriptors. * Define and use the named tuple `Direction' for (pitch, yaw) values.
This commit is contained in:
1 parent
6ef868bc5b
commit
7b1567c352
12 files changed
+318
-157
No files matched your search
@@ -1,6 +1,7 @@
|
||||
from minecraft.networking.packets import Packet
|
||||
from minecraft.networking.types import (
|
||||
VarInt, Integer, UnsignedByte, Position, Vector, MutableRecord
|
||||
VarInt, Integer, UnsignedByte, Position, Vector, MutableRecord,
|
||||
attribute_alias, multi_attribute_alias,
|
||||
)
|
||||
|
||||
|
||||
@@ -20,19 +21,25 @@ class BlockChangePacket(Packet):
|
||||
block_state_id = 0
|
||||
|
||||
# For protocols < 347: an accessor for (block_state_id >> 4).
|
||||
@property
|
||||
def blockId(self):
|
||||
return self.block_state_id >> 4
|
||||
|
||||
@blockId.setter
|
||||
def blockId(self, block_id):
|
||||
self.block_state_id = (self.block_state_id & 0xF) | (block_id << 4)
|
||||
blockId = property(lambda self: self.block_state_id >> 4, blockId)
|
||||
|
||||
# For protocols < 347: an accessor for (block_state_id & 0xF).
|
||||
@property
|
||||
def blockMeta(self):
|
||||
return self.block_state_id & 0xF
|
||||
|
||||
@blockMeta.setter
|
||||
def blockMeta(self, meta):
|
||||
self.block_state_id = (self.block_state_id & ~0xF) | (meta & 0xF)
|
||||
blockMeta = property(lambda self: self.block_state_id & 0xF, blockMeta)
|
||||
|
||||
# This alias is retained for backward compatibility.
|
||||
def blockStateId(self, block_state_id):
|
||||
self.block_state_id = block_state_id
|
||||
blockStateId = property(lambda self: self.block_state_id, blockStateId)
|
||||
blockStateId = attribute_alias('block_state_id')
|
||||
|
||||
|
||||
class MultiBlockChangePacket(Packet):
|
||||
@@ -54,24 +61,28 @@ class MultiBlockChangePacket(Packet):
|
||||
super(MultiBlockChangePacket.Record, self).__init__(**kwds)
|
||||
|
||||
# Access the 'x', 'y', 'z' fields as a Vector of ints.
|
||||
def position(self, position):
|
||||
self.x, self.y, self.z = position
|
||||
position = property(lambda r: Vector(r.x, r.y, r.z), position)
|
||||
position = multi_attribute_alias(Vector, 'x', 'y', 'z')
|
||||
|
||||
# For protocols < 347: an accessor for (block_state_id >> 4).
|
||||
@property
|
||||
def blockId(self):
|
||||
return self.block_state_id >> 4
|
||||
|
||||
@blockId.setter
|
||||
def blockId(self, block_id):
|
||||
self.block_state_id = self.block_state_id & 0xF | block_id << 4
|
||||
blockId = property(lambda r: r.block_state_id >> 4, blockId)
|
||||
|
||||
# For protocols < 347: an accessor for (block_state_id & 0xF).
|
||||
@property
|
||||
def blockMeta(self):
|
||||
return self.block_state_id & 0xF
|
||||
|
||||
@blockMeta.setter
|
||||
def blockMeta(self, meta):
|
||||
self.block_state_id = self.block_state_id & ~0xF | meta & 0xF
|
||||
blockMeta = property(lambda r: r.block_state_id & 0xF, blockMeta)
|
||||
|
||||
# This alias is retained for backward compatibility.
|
||||
def blockStateId(self, block_state_id):
|
||||
self.block_state_id = block_state_id
|
||||
blockStateId = property(lambda r: r.block_state_id, blockStateId)
|
||||
blockStateId = attribute_alias('block_state_id')
|
||||
|
||||
def read(self, file_object):
|
||||
h_position = UnsignedByte.read(file_object)
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
from minecraft.networking.types import Vector, Float, Byte, Integer
|
||||
from minecraft.networking.types import (
|
||||
Vector, Float, Byte, Integer, multi_attribute_alias,
|
||||
)
|
||||
from minecraft.networking.packets import Packet
|
||||
|
||||
|
||||
@@ -19,23 +21,10 @@ class ExplosionPacket(Packet):
|
||||
class Record(Vector):
|
||||
__slots__ = ()
|
||||
|
||||
@property
|
||||
def position(self):
|
||||
return Vector(self.x, self.y, self.x)
|
||||
position = multi_attribute_alias(Vector, 'x', 'y', 'z')
|
||||
|
||||
@position.setter
|
||||
def position(self, new_position):
|
||||
self.x, self.y, self.z = new_position
|
||||
|
||||
@property
|
||||
def player_motion(self):
|
||||
return Vector(self.player_motion_x, self.player_motion_y,
|
||||
self.player_motion_z)
|
||||
|
||||
@player_motion.setter
|
||||
def player_motion(self, new_player_motion):
|
||||
self.player_motion_x, self.player_motion_y, self.player_motion_z \
|
||||
= new_player_motion
|
||||
player_motion = multi_attribute_alias(
|
||||
Vector, 'player_motion_x', 'player_motion_y', 'player_motion_z')
|
||||
|
||||
def read(self, file_object):
|
||||
self.x = Float.read(file_object)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from minecraft.networking.packets import Packet
|
||||
|
||||
from minecraft.networking.types import (
|
||||
String, Boolean, UUID, VarInt
|
||||
String, Boolean, UUID, VarInt,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -68,7 +68,7 @@ class SoundEffectPacket(Packet):
|
||||
if context.protocol_version >= 201:
|
||||
value = Float.read(file_object)
|
||||
else:
|
||||
value = Byte.read(file_object) / 63.5
|
||||
value = Byte.read(file_object)
|
||||
if context.protocol_version < 204:
|
||||
value /= 63.5
|
||||
return value
|
||||
|
||||
@@ -3,7 +3,7 @@ from minecraft.networking.types.utility import descriptor
|
||||
|
||||
from minecraft.networking.types import (
|
||||
VarInt, UUID, Byte, Double, Integer, UnsignedByte, Short, Enum, Vector,
|
||||
PositionAndLook
|
||||
PositionAndLook, attribute_alias, multi_attribute_alias,
|
||||
)
|
||||
|
||||
|
||||
@@ -120,6 +120,7 @@ class SpawnObjectPacket(Packet):
|
||||
else:
|
||||
Byte.send(self.type_id, packet_buffer)
|
||||
|
||||
# pylint: disable=no-member
|
||||
xyz_type = Double if self.context.protocol_version >= 100 else Integer
|
||||
for coord in self.x, self.y, self.z:
|
||||
xyz_type.send(coord, packet_buffer)
|
||||
@@ -147,28 +148,19 @@ class SpawnObjectPacket(Packet):
|
||||
'in order to set the "type" property.')
|
||||
self.type_id = getattr(self.EntityType, type_name)
|
||||
|
||||
# Access the fields 'x', 'y', 'z' as a Vector.
|
||||
def position(self, position):
|
||||
self.x, self.y, self.z = position
|
||||
position = property(lambda p: Vector(p.x, p.y, p.z), position)
|
||||
@type.deleter
|
||||
def type(self):
|
||||
del self.type_id
|
||||
|
||||
position = multi_attribute_alias(Vector, 'x', 'y', 'z')
|
||||
|
||||
# Access the fields 'x', 'y', 'z', 'yaw', 'pitch' as a PositionAndLook.
|
||||
# NOTE: modifying the object retrieved from this property will not change
|
||||
# the packet; it can only be changed by attribute or property assignment.
|
||||
def position_and_look(self, position_and_look):
|
||||
self.x, self.y, self.z = position_and_look.position
|
||||
self.yaw, self.pitch = position_and_look.look
|
||||
position_and_look = property(lambda p: PositionAndLook(
|
||||
x=p.x, y=p.y, z=p.z, yaw=p.yaw, pitch=p.pitch),
|
||||
position_and_look)
|
||||
position_and_look = multi_attribute_alias(
|
||||
PositionAndLook, x='x', y='y', z='z', yaw='yaw', pitch='pitch')
|
||||
|
||||
# Access the fields 'velocity_x', 'velocity_y', 'velocity_z' as a Vector.
|
||||
def velocity(self, velocity):
|
||||
self.velocity_x, self.velocity_y, self.velocity_z = velocity
|
||||
velocity = property(lambda p: Vector(p.velocity_x, p.velocity_y,
|
||||
p.velocity_z), velocity)
|
||||
velocity = multi_attribute_alias(
|
||||
Vector, 'velocity_x', 'velocity_y', 'velocity_z')
|
||||
|
||||
# This alias is retained for backward compatibility.
|
||||
def objectUUID(self, object_uuid):
|
||||
self.object_uuid = object_uuid
|
||||
objectUUID = property(lambda self: self.object_uuid, objectUUID)
|
||||
objectUUID = attribute_alias('object_uuid')
|
||||
Reference in new issue
Block a user