pyCraft
pyCraft copied to clipboard
How do i get current player position
I have been looking at other people code and they seem to get the player position through seemingly complex methods. They do something with threading Condition is confuses me can someone explain it to me sorry i am still new to all of this :< so far i made a player class to make it simpler to use:
class player:
kname = None
auth_token = None
def __init__(self, uname, password=None):
if password == None:
self.kname = uname
else:
self.auth_token = authentication.AuthenticationToken()
self.auth_token.authenticate(uname, password)
def chat(self,msg):
msg = str(msg)
if len(msg) > 0:
packet = serverbound.play.ChatPacket()
packet.message = msg
self.connection.write_packet(packet)
def keepaliv(self):
packet = serverbound.play.KeepAlivePacket()
packet.keep_alive_id = random.randint(0, 5000)
self.connection.write_packet(packet)
def server(self,ip,port=25565, exhnd = None):
self.ip = ip
self.port = port
self.exhnd = exhnd
if self.auth_token == None:
self.connection = Connection(
ip, port, username=self.kname, handle_exception=exhnd)
else:
self.connection = Connection(
ip, port, auth_token=self.auth_token, handle_exception=exhnd)
self.connection.exception_handler(print)
self.connection.connect()
def reconnect(self):
self.server(self.ip,self.port, self.exhnd)
def ison(self):
try:
self.connection.status()
except InvalidState as e:
if e == 'There is an existing connection.':
return True
return False
def regchat(self, func):
self.connection.register_packet_listener(func, ChatMessagePacket)
def crot(self, x, y, z):
packet = PositionAndLookPacket()
packet.position = x, y, z
packet.look = 0, 0
packet.on_ground = True
self.connection.write_packet(packet)
def fling(self, x, y, z,xrang=0,zrang=0, timeout=0.1):
xchk = 0
zchk = 0
while (xchk > xrang) or (zchk > zrang):
if (xchk > xrang):
if (xchk > 0): x+=1
else: x-=1
if (xchk > 0): xchk+=1
else: xchk-=1
self.crot(x,y,z)
if (zchk > zrang):
if (zchk > 0): z+=1
else: z-=1
if (zchk > 0): zchk+=1
else: zchk-=1
self.crot(x,y,z)
time.sleep(timeout)
I'm also new, sorry if I say something false.. I've done it like that :
def handle_movement(packet):
print(packet.x)
connection.register_packet_listener(handle_movement,clientbound.play.PlayerPositionAndLookPacket)