Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions spockbot/mcdata/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,10 @@
INV_SLOTS_HOTBAR = 9
# always accessible
INV_SLOTS_PERSISTENT = INV_SLOTS_INVENTORY + INV_SLOTS_HOTBAR

#########
# Books #
#########

BOOK_MAXPAGES = 50
BOOK_CHARS_PER_PAGE = 266
2 changes: 1 addition & 1 deletion spockbot/mcp/mcpacket.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def encode(self, proto_comp_state, proto_comp_threshold, comp_level=6):
o = zlib.compress(o, comp_level)
ulen_varint = datautils.pack(MC_VARINT, uncompressed_len)
header = datautils.pack(MC_VARINT,
uncompressed_len + len(ulen_varint))
len(o) + len(ulen_varint))
header += ulen_varint
return header + o
elif proto_comp_state == mcdata.PROTO_COMP_OFF:
Expand Down
38 changes: 21 additions & 17 deletions spockbot/plugins/helpers/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,6 @@
logger = logging.getLogger('spockbot')


translations = {}
try:
with open('en_US.lang', 'r') as lang_file:
# the chat data comes in as strings, so we need to
# replace all %d, %i, %3$d etc. with %s
import re
pcts_only = re.compile('%([0-9]\$)?[a-z]')
for line in lang_file:
if '=' in line:
# cut off newline, split some.translation.id=format %s string
translation_id, format_str = line[:-1].split('=', 1)
translations[translation_id] = pcts_only.sub('%s', format_str)
except:
logger.warn('en_US.lang not loaded, cannot translate chat messages')


class ChatParseError(Exception):
pass

Expand Down Expand Up @@ -69,6 +53,26 @@ def __init__(self, ploader, settings):
super(ChatPlugin, self).__init__(ploader, settings)
self.chatcore = ChatCore(self.net)
ploader.provides('Chat', self.chatcore)
self.translations = {}
self._load_translations()

def _load_translations(self):
try:
with open('en_US.lang', 'r') as lang_file:
# the chat data comes in as strings, so we need to
# replace all %d, %i, %3$d etc. with %s
import re
pcts_only = re.compile('%([0-9]\$)?[a-z]')
for line in lang_file:
if '=' in line:
# cut off newline, split some.
# translation.id=format %s string
trans_id, format_str = line[:-1].split('=', 1)
self.translations[trans_id] = pcts_only.sub('%s',
format_str)
except:
logger.warn('CHAT: en_US.lang not loaded, '
'cannot translate chat messages')

def handle_chat(self, evt, packet):
position = packet.data['position'] # where is the text displayed?
Expand Down Expand Up @@ -133,7 +137,7 @@ def render_chat(self, chat_data):
translate_id = chat_data['translate']
args = tuple(map(self.render_chat, chat_data.get('with', [])))
try:
translate_fmt = translations[translate_id]
translate_fmt = self.translations[translate_id]
message = translate_fmt % args
except KeyError: # could not find translate_id
message = '<"%s" %s>' % (translate_id, args)
Expand Down
49 changes: 34 additions & 15 deletions spockbot/plugins/helpers/interact.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,32 +228,51 @@ def unmount_vehicle(self):
def jump_vehicle(self):
self.steer_vehicle(jump=True)

def write_book(self, text, author="", title="", sign=False):
"""Write text to the current book in hand, optionally sign the book"""
book = self._setup_book()
if book is None:
return False
pages = (text[0+i:constants.BOOK_CHARS_PER_PAGE+i]
for i in range(0, len(text), constants.BOOK_CHARS_PER_PAGE))
pages = pages[constants.BOOK_MAXPAGES:]
self.edit_book(pages)
if sign:
self.sign_book(author, title)

def edit_book(self, pages):
book = self.inventory.active_slot
# TODO: don't use hard coded id
if book.item_id != 386: # book and quill
"""Set the pages of current book in hand"""
book = self._setup_book()
if book is None:
return False
if book.nbt is None:
book.nbt = nbt.TagCompound()
nbtpages = nbt.TagList(nbt.TagString)
for i, page in enumerate(pages):
if i >= constants.BOOK_MAXPAGES:
break
nbtpages.insert(i, nbt.TagString(page))
book.nbt["pages"] = nbtpages
data = self.channels.encode(((MC_SLOT, "slot"),),
{"slot": book.get_dict()})
self.channels.send("MC|BEdit", data)
self.channels.send("MC|BEdit", self._pack_book(book))

def sign_book(self, author, title):
book = self.inventory.active_slot
# TODO: don't use hard coded id
if book.item_id != 386: # book and quill
"""Sign the book in hand"""
book = self._setup_book()
if book is None:
return False
if book.nbt is None:
book.nbt = nbt.TagCompound()
book.nbt["author"] = nbt.TagString(author)
book.nbt["title"] = nbt.TagString(title)
# TODO: don't use hard coded id
book.item_id = 387 # written book
data = self.channels.encode(((MC_SLOT, "slot"),),
self.channels.send("MC|BSign", self._pack_book(book))

def _setup_book(self):
book = self.inventory.active_slot
# TODO: Dont use hard coded ID
if book.item_id != 386: # book and quill
return None
if book.nbt is None:
book.nbt = nbt.TagCompound()
return book

def _pack_book(self, book):
return self.channels.encode(((MC_SLOT, "slot"),),
{"slot": book.get_dict()})
self.channels.send("MC|BSign", data)