pyclassic package¶
Submodules¶
pyclassic.auth module¶
This module contains some classes useful in authentication.
- class pyclassic.auth.ClassiCubeAuth(username, password)¶
Bases:
pyclassic.auth.SimpleAuth
Authentication class to use with a ClassiCube account. It makes use of the API to log in and retrieve the sever list.
Warning
Don’t do like me, if you want to upload your bot somewhere, make sure to not expose your creds lol
- Parameters
- check_auth()¶
Function mostly used internally to check if the user is authenticated to ClassiCube.
- connect(**kargs)¶
Provides information for
PyClassic.client.Client
in order to connect to a server.- Parameters
kargs – Server list query, can be the server name, the IP or even the port. Can be multiple at once. Log in to your ClassiCube account and visit http://www.classicube.net/api/servers to know available parameters you can use.
- Raises
pyclassic.PyClassicError – Server has not been found or it failed to retrieve the mppass (server salt).
- Returns
ip, port, username and mppass (see
pyclassic.auth.SimpleAuth.connect()
)- Return type
- get_session(username, password)¶
Also mostly used internally, creates the session cookie and log in to ClassiCube servers using the API.
- Parameters
- Raises
AssertionError – The requests to /api/login did not returned 200 OK as status code.
pyclassic.PyClassicError – Authentication has failed.
- Returns
Session
- Return type
requests.Session
- class pyclassic.auth.SimpleAuth(username, mppass)¶
Bases:
object
Simple authentication by providing manually the username and the server salt. This is also the base class if other authentication classes has to be implemented.
- connect(**kargs)¶
Provides information for
PyClassic.client.Client
in order to connect to a server.
pyclassic.batch module¶
Queue batch system.
This is a very simple batch system to generate queues based on specific commands. Positions must be relative. documentation soon:tm:
- class pyclassic.batch.Batch(fn: dict, filterfn: dict)¶
Bases:
object
The Batch class can generate queues on a given script.
- current_queue = []¶
- execute_line(line)¶
- generate_queue(script, ox=0, oy=0, oz=0)¶
Generates a queue from a script and a given offset.
- Parameters
script (str) – The script
- Returns
The generated queue
- Return type
list[
pyclassic.queue.Block
]
- class pyclassic.batch.DefaultBatchFunctions¶
Bases:
object
This is a static class with all the default batch functions.
- batch = Batch(fn={'cuboid': <function DefaultBatchFunctions.cuboid>, 'wall': <function DefaultBatchFunctions.wall>}, filterfn={'fblock': <function DefaultBatchFunctions.filter_block>})¶
- cuboid(ay, az, bx, by, bz, bid)¶
- filter_block()¶
- filterfn_dict = {'fblock': <function DefaultBatchFunctions.filter_block>}¶
- fn_dict = {'cuboid': <function DefaultBatchFunctions.cuboid>, 'wall': <function DefaultBatchFunctions.wall>}¶
- wall(ay, az, bx, by, bz, bid)¶
pyclassic.client module¶
The client module contains… well… the Client class that is used to handle the protocol itself such as packet receiving and sending.
It can be used independently from pyclassic.PyClassic
if
you want to. However you won’t have the event system obviously.
This class allows the connection and packet handling as well as some additional methods to make programming easier.
- class pyclassic.client.Client(auth: pyclassic.auth.SimpleAuth, client_name=None)¶
Bases:
object
Client class
- Parameters
auth (pyclassic.auth.SimpleAuth) – Authentication class to connect the client to a server.
client_name (str or None, optional) – Client name, can be changed as you wish but defaults to “pyclassic <VERSION>”. Can be useful sometimes :troll:
- connect(**kargs)¶
Connects to a server. Will use the specified
pyclassic.auth.SimpleAuth
-based class to retrieve the IP, port, username and server salt. Seepyclassic.auth.SimpleAuth.connect()
If a socket is already active, it will disconnect it first then reconnect.
- Parameters
kargs – Arguments to pass to the auth object to retrieve the server and the credentials to connect.
- Raises
pyclassic.utils.PyClassicError – Failed to connect.
pyclassic.utils.PyClassicError – Kicked from server on connect.
- Returns
Server information or None
- Return type
- disconnect()¶
Disconnects the client if it’s connected to a server, otherwise does a great amount of nothing.
- move(x, y, z, pitch=0, yaw=0)¶
Teleports the player at the specified position and direction. This functions relies on block position. See
pyclassic.client.Client.move_precise()
for more precise teleportation.Note
Pitch and yaw angles are between 0 and 255 and not 0 and 359 since these fits in a byte.
- move_precise(x, y, z, pitch=0, yaw=0)¶
Teleports the player at the specified position and direction.
Note
Pitch and yaw angles are between 0 and 255 and not 0 and 359 since these fits in a byte.
- recv()¶
Receives a packet and decodes it appropriately.
- Raises
pyclassic.utils.PyClassicError – no more data is received.
- Returns
packet information and the decoded packet (without the packet ID as there is packet info)
- Return type
(
pyclassic.utils.PacketFormat
, list)
- send(pid, *args)¶
Sends a packet to the server.
- Parameters
pid (int) – Packet ID
args – arguments
- Raises
pyclassic.utils.PyClassicError – Invalid packet.
- set_block(x, y, z, block_id)¶
Make the client place a block at a specified position.
Note
Abuse this and you’ll get kicked lmao.
pyclassic.extra module¶
- pyclassic.extra.cuboid(x1, y1, z1, x2, y2, z2, blockid)¶
- pyclassic.extra.download_map(bot)¶
Hacky function to download the map from a server. I would not recommend using it unless you’re doing hacky automated shit like I do.
- Parameters
bot (
pyclassic.client.Client
) – The client to receive data with- Returns
The downloaded, decompressed and parsed map.
- Return type
- pyclassic.extra.hollow(y, x1, z1, x2, z2, bid)¶
- pyclassic.extra.pyramid(size, ox, oy, oz, bid)¶
- pyclassic.extra.throwaway(username, clientname=None, **kargs)¶
Makes a “throw-away” account. This can only be used on servers that does not check user authentication. It uses
pyclassic.auth.SimpleAuth
as auth object.- Parameters
- Returns
The freshly new client that has been created just for you
- Return type
pyclassic.map module¶
This module take care of doing all the map stuff for you. It features map data decoding (including gzip decompression), and can also be edited which has been useful for the event system.
We are also considering supporting other formats such as schematic or ClassicWorld (.cw) files
Note
Concerning map loading and saving, the format it uses is essentially just the same as the data downloaded on a regular Classic server with 6 shorts (12 bytes) prepended. The 3 first being the offset of the map and the 3 last being the size of the map. All of it gzipped. It is quite trivial to parse.
- class pyclassic.map.ClassicMap(data: bytes, width, height, length, compressed=True)¶
Bases:
object
This ClassicMap class is used to store map data which can also be modified and saved in a file with the format mentionned at the beginning of the module documentation. It can also be sliced into a region.
Getting and setting blocks can be done like on an array. For example, map[3, 1, 2] = 1 sets a stone block at the position (3, 1, 2) of the stored map.
- Parameters
- get_queue(ox=0, oy=0, oz=0)¶
Turns a map into a queue, a list of
pyclassic.queue.Block
to be used withpyclassic.queue.ThreadedQueue
- Parameters
- Returns
The queue converted from the map.
- Return type
- get_queue_from_region(x1, y1, z1, x2, y2, z2, ox=0, oy=0, oz=0)¶
- getpos(idx)¶
Calculate the x, y, z position of a specified index.
- save(filename, compresslevel=9, ox=0, oy=0, oz=0)¶
Saves the map in a file.
- slice_down(vectors)¶
- pyclassic.map.load(filename)¶
Loads a map.
- Parameters
filename (str) – File name
- Returns
The offset and the generate map object.
- Return type
(int, int, int),
pyclassic.map.ClassicMap
pyclassic.queue module¶
This is a queue system which can be used to build stuff. It uses a job queue and threading to optimize the building speed as it is slow using the asynchronous system of the event system.
It is designed to be used by an array of clients but can also be used with only one client. If you want to build fast on a server with anti-grief policies, this module is for you.
Warning
Some servers may IP-ban you :) Make sure to only use it on unmoderated servers. Or maybe you can do a little trolling while no one is connected :troll:
- class pyclassic.queue.ThreadedQueue(player: pyclassic.PyClassic, map=None, delay=0.03)¶
Bases:
object
The class that does all the queue job.
Warning
To prevent Python from eventually doing a funny because threading while editing the job queue, please use the functions as they ensure the queue is not locked before modifying it.
- Parameters
player (
pyclassic.PyClassic
or a list ofpyclassic.client.Client
instances.) – The array of bots to use, can also be apyclassic.PyClassic
instance, the class will take the bot array from it.delay (float, optional) – The delay, defaults to 0.03s (30ms) as it is the usual delay to bypass the anti-grief system.
- Raises
pyclassic.queue.QueueError – The list is empty or the PyClassic instance has no bot array.
- add_queue(queue)¶
Adds a queue to the job queue.
- Parameters
queue (list[
pyclassic.queue.Block
]) – Block queue
- check_lock()¶
Checks if the block queue has been locked (it locks when the thread is running). It will raise an exception if it is. This is an helper function used in functions that edits the queue.
- Raises
pyclassic.queue.QueueError – The queue is locked.
- clear_current_queue()¶
Clears only the current queue.
- clear_queues()¶
Clear the whole job queue along with the current queue.
- do_all_blockqueues()¶
Do all queues from the job queue until there is nothing left.
Warning
I do not recommend manually running as it breaks the whole point of this class but do as you wish. See
pyclassic.queue.ThreadedQueue.start_all()
.
- do_blockqueue(threaded=True)¶
Pops a queue from the job queue and make the multibot run it. If there is already an unfinished current queue, continue it. Otherwise if it is empty, do nothing.
Warning
I do not recommend manually running as it breaks the whole point of this class but do as you wish. See
pyclassic.queue.ThreadedQueue.start()
.- Parameters
threaded (bool, optional) – is it running in a thread?
- make_thread(**kargs)¶
Helper function used internally to make and start a thread.
- start()¶
Starts a thread to execute one job in the queue. Does nothing if the thread is already running.
- start_all()¶
Starts a thread to execute all jobs in the queue. Does nothing if the thread is already running.
- stop()¶
Send a termination signal to the running thread if there is one and wait for it to stop.
pyclassic.utils module¶
This module contains a bunch of useful functions. I am not going to document this because those are mostly helpers for other modules. I will only document the functions that can be useful for the developer.
- pyclassic.utils.api_url(n)¶
- pyclassic.utils.contains_all(needle, haystack)¶
- pyclassic.utils.decint(n, s=True)¶
- pyclassic.utils.decstr(n)¶
- pyclassic.utils.enc(n)¶
- pyclassic.utils.encint(n, l=2)¶
- pyclassic.utils.encode_packet(fmt, *args)¶
- pyclassic.utils.encstr(n)¶
- pyclassic.utils.find_packet_id(t)¶
Module contents¶
PyClassic - Minecraft Classic Protocol implementation in Python.
This is the main classes of the module.
- class pyclassic.Player(name: str, x: int, y: int, z: int, pitch: int, yaw: int)¶
Bases:
object
This is a simple class to store player data such as their position, their username and their head direction.
- class pyclassic.PyClassic(client, multibot=[], client_name=None, build_delay=0.03)¶
Bases:
object
The PyClassic class is the “main” class. It handles the event system which the core design of this library. However you do not have to use this class. See
pyclassic.client.Client
.It supports event handling of course but also multibot which can be useful for building stuff using
pyclassic.queue.ThreadedQueue
.- Parameters
client (
pyclassic.auth.SimpleAuth
orpyclassic.client.Client
) – The main client that will be used for event handlingmultibot (list[
pyclassic.auth.SimpleAuth
orpyclassic.client.Client
], optional) – array of clients or auth objects for multibotclient_name (str or None, optional) – name of the client, defaults to “pyclassic <VERSION>” if None
build_delay (float, optional) – Block placing delay for multibot building
- Raises
pyclassic.PyClassicError – if the client parameter is invalid.
- async asend(pid, *args)¶
Actually not very useful, sends stuff but asynchronously.
- connect(**kargs)¶
Connects the main client to a server.
- connect_multibot(delay=4, **kargs)¶
Connects the multibot army to a server.
- die(*msg)¶
Logs stuff but more dramatically
- disconnect()¶
Disconnects the main client.
- disconnect_multibot()¶
Disconnects the whole bot army.
- event(fn)¶
Decorator to define an event.
- Parameters
fn (function) – Event function. The function must be named as the wanted event.
- async event_loop()¶
Runs the asynchronous event loop.
Warning
This function SHOULD NOT be run manually unless you are doing cursed shit. See
pyclassic.PyClassic.run()
- get_block(x, y, z)¶
Retrieve a block from self.map
- log(*msg)¶
Logs stuff
- map: pyclassic.map.ClassicMap¶
Map object that stores the downloaded map.
- async message(*args)¶
Send a message from the main client.
- async move(*args)¶
Moves the main client. see
pyclassic.client.Client.move()
- queue: pyclassic.queue.ThreadedQueue¶
Queue object for multibot, None if there are no multibot.
- recv()¶
- run(delay=4, **kargs)¶
Connects all clients and run the event loop.
- Parameters
delay – Delay of connection between each multibot connection.
kargs – Arguments such as the IP address, port, etc. It depends of the
pyclassic.auth
class.
- send(pid, *args)¶
- async set_block(x, y, z, bid)¶
Makes the main client place a block and updates the map if there is one.
See
pyclassic.client.Client.set_block()
- Parameters
bid (int) – Block ID to place
- update_player(playerid, name=None, x=None, y=None, z=None, pitch=None, yaw=None, relative=False)¶