Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
Example "chat room" server
This server authenticates players, then spawns them in an empty world and does
the bare minimum to keep them in-game. Players can speak to eachother using
chat.
Supports Minecraft 1.15. Earlier versions will not work as the packet formats
differ.
"""
from twisted.internet import reactor
from quarry.net.server import ServerFactory, ServerProtocol
class ChatRoomProtocol(ServerProtocol):
def player_joined(self):
# Call super. This switches us to "play" mode, marks the player as
# in-game, and does some logging.
ServerProtocol.player_joined(self)
# Send "Join Game" packet
self.send_packet("join_game",
self.buff_type.pack("iBqiB",
0, # entity id
3, # game mode
0, # dimension
0, # hashed seed
0), # max players
self.buff_type.pack_string("flat"), # level type
self.buff_type.pack_varint(1), # view distance
self.buff_type.pack("??",
def real_kick(*a):
self.send_packet(
"disconnect",
self.buff_type.pack_chat(reason))
super(ServerProtocol, self).close(reason)
"""
Example "auth" server
This server authenticates players with the mojang session server, then kicks
them. Useful for server websites that ask users for a valid Minecraft account.
"""
from twisted.internet import reactor
from quarry.net.server import ServerFactory, ServerProtocol
class AuthProtocol(ServerProtocol):
def player_joined(self):
# This method gets called when a player successfully joins the server.
# If we're in online mode (the default), this means auth with the
# session server was successful and the user definitely owns the
# display name they claim to.
# Call super. This switches us to "play" mode, marks the player as
# in-game, and does some logging.
ServerProtocol.player_joined(self)
# Define your own logic here. It could be an HTTP request to an API,
# or perhaps an update to a database table.
display_name = self.display_name
ip_addr = self.remote_addr.host
self.logger.info("[%s authed with IP %s]" % (display_name, ip_addr))
"""
Example "downtime" server
This server kicks players with the MOTD when they try to connect. It can be
useful for when you want players to know that your usual server is down for
maintenance.
"""
from twisted.internet import reactor
from quarry.net.server import ServerFactory, ServerProtocol
class DowntimeProtocol(ServerProtocol):
def packet_login_start(self, buff):
buff.discard()
self.close(self.factory.motd)
class DowntimeFactory(ServerFactory):
protocol = DowntimeProtocol
def main(argv):
# Parse options
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-a", "--host", default="", help="address to listen on")
parser.add_argument("-p", "--port", default=25565, type=int, help="port to listen on")
parser.add_argument("-m", "--message", default="We're down for maintenance",
import random
players = {}
###PROTOCOL AND SERVER STUFF
from quarry.net.server import ServerFactory, ServerProtocol
import serverdata
from serverdata.values import Position
id_counter = 0 # We need to have unique ID for all entities in a server
__all__ = ["MineFactory", "Mineserver"]
class Mineserver(ServerProtocol):
def packet_login_start(self, buff):
ServerProtocol.packet_login_start(self, buff)
def get_free_id(self): # Get free ID for entity_id
global id_counter # Because TWISTED isn't threaded, we can safely call a global variable
id_counter += 1
return id_counter
# Plugin event method
def plugin_event(self, event_name, *args, **kwargs):
self.factory.plugin_system.call_event(event_name, self, *args, **kwargs)
def player_joined(self):
ServerProtocol.player_joined(self)
self.ip = self.remote_addr.host
self.position = Position(0, 66, 0)
def player_left(self):
ServerProtocol.player_left(self)
# Announce player left
self.factory.send_chat(u"\u00a7e%s has left." % self.display_name)
def packet_unhandled(self, buff, direction, name):
"""
Called when a packet is received that is not hooked. The default
implementation forwards the packet.
"""
if direction == "downstream":
self.downstream.send_packet(name, buff.read())
elif direction == "upstream":
self.upstream.send_packet(name, buff.read())
def packet_downstream_set_compression(self, buff):
self.upstream.set_compression(buff.unpack_varint())
class Downstream(ServerProtocol):
bridge = None
def setup(self):
self.bridge = self.factory.bridge_class(self.factory, self)
def player_joined(self):
ServerProtocol.player_joined(self)
self.bridge.downstream_ready()
def connection_lost(self, reason=None):
ServerProtocol.connection_lost(self, reason)
self.bridge.downstream_disconnected()
class DownstreamFactory(ServerFactory):
protocol = Downstream
def player_left(self):
self.plugin_event("player_leave")
del players[self.entity_id]
ServerProtocol.player_left(self)