Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Movement #62

Closed
BloodySenpai opened this issue Jul 2, 2017 · 18 comments
Closed

Movement #62

BloodySenpai opened this issue Jul 2, 2017 · 18 comments

Comments

@BloodySenpai
Copy link

I don't understand how to send the packet to move character

@joodicator
Copy link
Collaborator

from minecraft.networking.connection import Connection
from minecraft.networking.packets import PositionAndLookPacket

conn = Connection(...)
conn.connect()
...
packet = PositionAndLookPacket(x=..., feet_y=..., z=..., yaw=..., pitch=..., on_ground=...)
conn.write_packet(packet)

Is this what you mean?

See also: http://wiki.vg/Protocol#Player_Position_And_Look_.28serverbound.29

@BloodySenpai
Copy link
Author

yes that's what I mean thannks m8

@BloodySenpai
Copy link
Author

I tried to write packets passes but my character does not move

@Gjum
Copy link

Gjum commented Jul 2, 2017

do you send the teleport response? http://wiki.vg/Protocol#Teleport_Confirm

also this might still help even though a lot of it is outdated

@joodicator
Copy link
Collaborator

@BloodySenpai It would be better to keep discussions on this repository to issues with pyCraft itself, rather than Minecraft's protocol in general. For the latter, #mcdevs on irc.freenode.net is a better forum.

However, insofar as your problem relates to pyCraft's API, post your code or a minimal test case, and I'll have a look at it.

@BloodySenpai
Copy link
Author

I'm sorry I'm new to this so I wouldn't know. I just did what you sent as code

@joodicator
Copy link
Collaborator

  1. I noticed just now that the packet ID for PlayerPositionAndLookPacket (the clientbound packet) was incorrect in pyCraft, which would have made this a bit difficult to do properly. That has been fixed by 2cf1d3c.

  2. I would note that pyCraft automatically sends Teleport Confirm packets when requested, so it is not necessary to do that manually.

  3. @BloodySenpai In case the previous example was unclear, here is a full working example of movement using pyCraft. The player moves around continuously in a circle, unless stopped by an obstacle. It assumes a server running in offline mode, and works best if the player is on a flat surface, or in the air in creative mode.

import threading
import time
import math

from minecraft.networking.connection import Connection
from minecraft.networking.packets import PlayerPositionAndLookPacket
from minecraft.networking.packets import PositionAndLookPacket

# Protect objects accessed by multiple threads with a lock.
lock = threading.Lock()

# The player's current position and look direction.
pos_look = PlayerPositionAndLookPacket.PositionAndLook()

# This conditions occurs when the server sets (or resets) pos_look.
pos_look_set = threading.Condition(lock)

conn = Connection('localhost', username='User')
def h_position_and_look(packet):
    with lock:
        packet.apply(pos_look)
        pos_look_set.notify_all()
conn.register_packet_listener(h_position_and_look, PlayerPositionAndLookPacket)
conn.connect()

# Wait until our initial position is set by the server.
with lock:
    pos_look_set.wait()

while True:
    # Every 1 tick (50ms):
    time.sleep(0.05)

    with lock:
        # Rotate 1 degree to the right.
        pos_look.yaw = (pos_look.yaw + 1.0) % 360.0
    
        # Move forward 0.1m.
        pos_look.x -= 0.1 * math.sin(pos_look.yaw * math.pi / 180.0)
        pos_look.z += 0.1 * math.cos(pos_look.yaw * math.pi / 180.0)
    
        conn.write_packet(PositionAndLookPacket(
            x         = pos_look.x,
            feet_y    = pos_look.y,
            z         = pos_look.z,
            yaw       = pos_look.yaw,
            pitch     = pos_look.pitch,
            on_ground = True))

@BloodySenpai
Copy link
Author

I just don't know how to use both chat and position at the same time.
I get this error TypeError: unsupported operand type(s) for +: 'NoneType' and 'float'

@joodicator
Copy link
Collaborator

@BloodySenpai You will have to be more specific than that. It is possible to use chat and position packets at the same time, of course, but there are many ways that could be done.

Did you get that type error from the code I posted? If so, please post a full stack trace. If not, you should post the code that is causing it.

@BloodySenpai
Copy link
Author

Is there any other way I can contact you? Like email or stuff like that?

@BloodySenpai
Copy link
Author

I'm use the example that they gave with library

@BloodySenpai
Copy link
Author

connection.register_packet_listener(print_chat, ChatMessagePacket,PlayerPositionAndLookPacket)
elif "Position" in text:
print True
packet = PlayerPositionAndLookPacket()
print "true1"
pos_look = PlayerPositionAndLookPacket.PositionAndLook()
print pos_look
print "true2"
packet = ChatPacket()
packet.message = "Status:1 Passed Movement Packet"
connection.write_packet(packet)
pos_look.yaw = (pos_look.yaw + 1.0) % 360.0
pos_look.x -= 0.1 * math.sin(pos_look.yaw * math.pi / 180.0)
pos_look.z += 0.1 * math.cos(pos_look.yaw * math.pi / 180.0)
connection.write_packet(PositionAndLookPacket(
x = pos_look.x,
feet_y = pos_look.y,
z = pos_look.z,
yaw = pos_look.yaw,
pitch = pos_look.pitch,
on_ground = True))
print "Passed that shit"
packet.apply(pos_look)
print True

@joodicator
Copy link
Collaborator

@BloodySenpai The code you have posted there appears to be incomplete (and not properly indented or formatted), so I can't say much about it. You should post the full code you are using so that I can run it myself if I need to.

However, the error you are getting is probably from the fact that you're using the uninitialised value of pos_look.yaw in an arithmethic expression. PositionAndLook instances need to receive an initial value, such as from the first PlayerPositionAndLookPacket the server sends. That is why my example waits for this to happen before continuing.

We can continue this conversation by email. Email me at ''.join(reversed('moc.liamg@eornicam.nhoj')).

@yunfan
Copy link

yunfan commented Jun 4, 2018

hi , i'd check this code and added to my bot. it looks like work
but unfortunately it will caused my bot kicked for flying status. i'd already change the 1 stick to 1.5s which i think it cant be send too fast. so is it because i should wait the server confirm before i send the next packet?

@joodicator
Copy link
Collaborator

@yunfan If you're referring to the code from this comment, the reason you are being kicked for "flying" is most likely because this example assumes the player is initially standing on a flat surface large enough for the player to walk around in a circle, but in your world it ends up walking off the edge of a block and into the air. This example (and pyCraft in general) does not implement the physics required to remain on the ground while walking around.

@yunfan
Copy link

yunfan commented Jun 6, 2018

@joodicator thanks for the replying, after some trying, i got the same conclution, i'll check that later.

also i havnt saw any event emiting examples, would you recommand some sample code for it? i intent to make a bot which could move and dig

@joodicator
Copy link
Collaborator

@yunfan start.py contains some basic examples of sending packets, which is what I interpret you to mean by "event emitting", but as far as I know nobody has used pyCraft to implement anything as advanced as a bot that walks around and digs. If you do choose to do this yourself, your experience might be valuable for others, though.

@yunfan
Copy link

yunfan commented Jun 8, 2018

@joodicator thanks, i already read the code again and again, and learnt a lot, but i do remember there was a python library which is event based and could emit your own event just like the js implementation by rom1514. but that library has stopped since 1.7.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants