Skip to content

Instantly share code, notes, and snippets.

@joodicator
Created June 8, 2019 06:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joodicator/e2066ba7337ec1826807426dcd236fc8 to your computer and use it in GitHub Desktop.
Save joodicator/e2066ba7337ec1826807426dcd236fc8 to your computer and use it in GitHub Desktop.
from threading import Condition
import traceback
import time
from minecraft.networking.connection import Connection
from minecraft.networking.packets import clientbound, serverbound
from minecraft.networking.types import (
Position, PositionAndLook, RelativeHand, BlockFace,
)
TICK_S = 0.05 # 1 tick = 50ms
SPEED_BPS = 1 # 1 block per second
if __name__ == '__main__':
conn = Connection('localhost', username='CakeBot')
# Keep track of the player's position and look direction.
pos_look = PositionAndLook()
pos_look_cond = Condition()
# Keep track of the coordinates of the last block placed.
last_block = None
# Allow the networking thread to terminate the main thread.
interrupt = False
@conn.listener(clientbound.play.PlayerPositionAndLookPacket)
def on_player_position_and_look(packet):
with pos_look_cond:
packet.apply(pos_look)
# Notify the main thread that pos_look has been updated.
pos_look_cond.notify_all()
@conn.exception_handler()
def on_exception(exc, exc_info):
global interrupt
traceback.print_exception(*exc_info)
interrupt = True
with pos_look_cond:
# Connect and wait until pos_look is initialised.
conn.connect()
pos_look_cond.wait()
pos_look.look = 0, 0 # Face straight North.
while not interrupt:
with pos_look_cond:
# Walk backwards at SPEED blocks per second.
pos_look.z -= SPEED_BPS * TICK_S
conn.write_packet(serverbound.play.PositionAndLookPacket(
position_and_look=pos_look, on_ground=True,
))
# Place a block in front of the player at every unit position.
if last_block is None or int(pos_look.z) + 1 < last_block.z:
last_block = Position(
int(pos_look.x), int(pos_look.y), int(pos_look.z) + 1)
conn.write_packet(serverbound.play.PlayerBlockPlacementPacket(
location=last_block,
hand=RelativeHand.MAIN,
face=BlockFace.TOP,
x=0.5, y=0.5, z=0.5,
inside_block=False,
))
time.sleep(TICK_S)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment