From 4a508f935b7da90daba750556a00c2b0c72b0637 Mon Sep 17 00:00:00 2001 From: TheSnoozer Date: Tue, 8 Aug 2017 15:26:04 +0200 Subject: [PATCH] use namedtuple for position type and use it as subclass for ClientExplosion.Record --- .../packets/clientbound/play/__init__.py | 16 ++-------------- minecraft/networking/types.py | 7 +++++-- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/minecraft/networking/packets/clientbound/play/__init__.py b/minecraft/networking/packets/clientbound/play/__init__.py index 8346f74..a422f7b 100644 --- a/minecraft/networking/packets/clientbound/play/__init__.py +++ b/minecraft/networking/packets/clientbound/play/__init__.py @@ -172,20 +172,8 @@ class ExplosionPacket(Packet): packet_name = 'explosion' - class Record(object): - __slots__ = 'x', 'y', 'z' - - def __init__(self, x, y, z): - self.x = x - self.y = y - self.z = z - - def __repr__(self): - return ('Record(x=%s, y=%s, z=%s)' - % (self.x, self.y, self.z)) - - def __str__(self): - return self.__repr__() + class Record(Position): + pass def read(self, file_object): self.x = Float.read(file_object) diff --git a/minecraft/networking/types.py b/minecraft/networking/types.py index c9f6843..949be9f 100644 --- a/minecraft/networking/types.py +++ b/minecraft/networking/types.py @@ -4,6 +4,7 @@ These definitions and methods are used by the packet definitions """ import struct import uuid +from collections import namedtuple class Type(object): @@ -225,7 +226,9 @@ class UUID(Type): socket.send(uuid.UUID(value).bytes) -class Position(Type): +class Position(Type, namedtuple('Position', ('x', 'y', 'z'))): + __slots__ = () + @staticmethod def read(file_object): location = UnsignedLong.read(file_object) @@ -242,7 +245,7 @@ class Position(Type): if z >= pow(2, 25): z -= pow(2, 26) - return {'x': x, 'y': y, 'z': z} + return Position(x=x, y=y, z=z) @staticmethod def send(x, y, z, socket):