Skip to content

Commit 7747f01

Browse files
committed
Merge pull request #213 from Gjum/fixes
Many fixes and improvements
2 parents 1f6fb0e + 351ded8 commit 7747f01

File tree

16 files changed

+213
-101
lines changed

16 files changed

+213
-101
lines changed

spockbot/mcdata/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from spockbot.mcdata.utils import find_by
44

55

6-
def get_item_or_block(find, meta=None, init=True):
6+
def get_item_or_block(find, meta=0, init=True):
77
ret = None
88
if isinstance(find, int): # by id
99
ret = find_by(find, items.items, blocks.blocks)

spockbot/mcdata/blocks.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
_block_exts = {}
1515

1616

17-
def get_block(block, meta=None, init=True):
17+
def get_block(block, meta=0, init=True):
1818
ret = None
1919
if isinstance(block, int): # by id
2020
ret = find_by(block, blocks)
@@ -51,6 +51,10 @@ def __init__(self, meta=None):
5151
if self.id in _block_exts:
5252
_block_exts[self.id](self)
5353

54+
def __str__(self):
55+
return '%s %i:%i' % (self.display_name, self.id,
56+
getattr(self, 'metadata', 0))
57+
5458

5559
def _convert_boundingbox(bb):
5660
if bb == 'block':

spockbot/mcdata/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
###########
99

1010
CLIENT_TICK_RATE = 0.05
11+
PLAYER_EYE_HEIGHT = 1.62
1112
PLAYER_HEIGHT = 1.80
1213
PLAYER_WIDTH = 0.6
1314

spockbot/mcdata/items.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
items_name = {}
99

1010

11-
def get_item(item, meta=None, init=True):
11+
def get_item(item, meta=0, init=True):
1212
ret = None
1313
if isinstance(item, int): # by id
1414
ret = find_by(item, items)
@@ -34,6 +34,10 @@ def __init__(self, meta=None):
3434
# TODO: apply other all possible variations
3535
self.display_name = self.variations[self.metadata]["display_name"]
3636

37+
def __str__(self):
38+
return '%s %i:%i' % (self.display_name, self.id,
39+
getattr(self, 'metadata', 0))
40+
3741

3842
def _make_item(item_dict):
3943
cls_name = '%sItem' % camel_case(str(item_dict['name']))

spockbot/mcdata/windows.py

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from minecraft_data.v1_8 import windows_list
66

77
from spockbot.mcdata import constants, get_item_or_block
8+
from spockbot.mcdata.blocks import Block
89
from spockbot.mcdata.items import Item
910
from spockbot.mcdata.utils import camel_case, snake_case
1011

@@ -23,10 +24,17 @@ def make_slot_check(wanted):
2324
if isinstance(wanted, int):
2425
item, meta = wanted, None
2526
elif isinstance(wanted, Slot):
26-
item, meta = wanted.item_id, wanted.damage
27-
# TODO compare NBT
28-
else: # wanted is list of (id, meta)
29-
item, meta = wanted
27+
item, meta = wanted.item_id, wanted.damage # TODO compare NBT
28+
elif isinstance(wanted, (Item, Block)):
29+
item, meta = wanted.id, wanted.metadata
30+
elif isinstance(wanted, str):
31+
item_or_block = get_item_or_block(wanted, init=True)
32+
item, meta = item_or_block.id, item_or_block.metadata
33+
else: # wanted is (id, meta)
34+
try:
35+
item, meta = wanted
36+
except TypeError:
37+
raise ValueError('Illegal args for make_slot_check(): %s' % wanted)
3038

3139
return lambda slot: item == slot.item_id and meta in (None, slot.damage)
3240

@@ -40,7 +48,6 @@ def __init__(self, window, slot_nr, id=constants.INV_ITEMID_EMPTY,
4048
self.damage = damage
4149
self.amount = amount
4250
self.nbt = enchants
43-
4451
self.item = get_item_or_block(self.item_id, self.damage) or Item()
4552

4653
def move_to_window(self, window, slot_nr):
@@ -83,20 +90,16 @@ def __bool__(self):
8390
return not self.is_empty
8491

8592
def __repr__(self):
86-
vals = {
87-
'name': self.item.display_name,
88-
'max': self.item.stack_size,
89-
}
90-
vals.update(self.__dict__)
9193
if self.is_empty:
9294
s = 'empty'
9395
else:
94-
s = '%(amount)i/%(max)i %(item_id)i:%(damage)i %(name)s' % vals
96+
item = self.item
97+
s = '%i/%i %s' % (self.amount, item.stack_size, str(item))
9598

9699
if self.slot_nr != -1:
97-
s += ' at %(slot_nr)i' % self.__dict__
100+
s += ' at %i' % self.slot_nr
98101
if self.window:
99-
s += ' in %(window)s' % self.__dict__
102+
s += ' in %s' % self.window
100103
return '<Slot: %s>' % s
101104

102105

@@ -196,8 +199,11 @@ def __init__(self, slot, button=constants.INV_BUTTON_LEFT):
196199
'Clicking with button %s not implemented' % button)
197200

198201
def get_packet(self, inv_plugin):
202+
slot_nr = self.slot.slot_nr
203+
if self.slot == inv_plugin.cursor_slot:
204+
slot_nr = constants.INV_OUTSIDE_WINDOW
199205
return {
200-
'slot': self.slot.slot_nr,
206+
'slot': slot_nr,
201207
'button': self.button,
202208
'mode': 0,
203209
'clicked_item': self.slot.get_dict(),
@@ -206,13 +212,19 @@ def get_packet(self, inv_plugin):
206212
def apply(self, inv_plugin):
207213
clicked = self.slot
208214
cursor = inv_plugin.cursor_slot
209-
if self.button == constants.INV_BUTTON_LEFT:
215+
if clicked == cursor:
216+
if self.button == constants.INV_BUTTON_LEFT:
217+
clicked.amount = 0
218+
elif self.button == constants.INV_BUTTON_RIGHT:
219+
clicked.amount -= 1
220+
self.cleanup_if_empty(clicked)
221+
elif self.button == constants.INV_BUTTON_LEFT:
210222
if clicked.stacks_with(cursor):
211223
self.transfer(cursor, clicked, cursor.amount)
212224
else:
213225
self.swap_slots(cursor, clicked)
214226
elif self.button == constants.INV_BUTTON_RIGHT:
215-
if cursor.item_id == constants.INV_ITEMID_EMPTY:
227+
if cursor.is_empty:
216228
# transfer half, round up
217229
self.transfer(clicked, cursor, (clicked.amount + 1) // 2)
218230
elif clicked.is_empty or clicked.stacks_with(cursor):
@@ -230,32 +242,33 @@ def __init__(self, slot, drop_stack=False):
230242
self.drop_stack = drop_stack
231243

232244
def get_packet(self, inv_plugin):
233-
if self.slot == inv_plugin.active_slot:
234-
slot_nr = constants.INV_OUTSIDE_WINDOW # drop cursor slot
235-
elif inv_plugin.cursor_slot.item_id != constants.INV_ITEMID_EMPTY:
236-
return None # can't drop while holding an item
237-
else: # default case
238-
slot_nr = self.slot.slot_nr
245+
if self.slot == inv_plugin.cursor_slot:
246+
raise ValueError("Can't drop cursor slot, use SingleClick")
247+
if not inv_plugin.cursor_slot.is_empty:
248+
raise ValueError("Can't drop other slots: cursor slot is occupied")
249+
239250
return {
240-
'slot': slot_nr,
251+
'slot': self.slot.slot_nr,
241252
'button': 1 if self.drop_stack else 0,
242253
'mode': 4,
243254
'clicked_item': inv_plugin.cursor_slot.get_dict(),
244255
}
245256

246257
def apply(self, inv_plugin):
247-
if inv_plugin.cursor_slot.is_empty:
248-
if self.drop_stack:
249-
self.slot.amount = 0
250-
else:
251-
self.slot.amount -= 1
252-
self.cleanup_if_empty(self.slot)
253-
# else: cursor not empty, can't drop while holding an item
258+
if self.drop_stack:
259+
self.slot.amount = 0
260+
else:
261+
self.slot.amount -= 1
262+
self.cleanup_if_empty(self.slot)
254263

255264

256265
class Window(object):
257266
""" Base class for all inventory types. """
258267

268+
name = None
269+
inv_type = None
270+
inv_data = {}
271+
259272
# the arguments must have the same names as the keys in the packet dict
260273
def __init__(self, window_id, title, slot_count,
261274
inv_type=None, persistent_slots=None, eid=None):
@@ -324,6 +337,7 @@ def _make_window(window_dict):
324337
'__module__': sys.modules[__name__],
325338
'name': str(window_dict['name']),
326339
'inv_type': str(window_dict['id']),
340+
'inv_data': window_dict,
327341
}
328342

329343
# creates function-local index and size variables

spockbot/plugins/core/auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def set_username(self, username):
4848
username = property(get_username, set_username)
4949

5050
def set_password(self, password):
51-
if not self.online_mode:
51+
if password and not self.online_mode:
5252
logger.warning("PASSWORD PROVIDED WITH ONLINE_MODE == FALSE")
5353
logger.warning("YOU PROBABLY DIDN'T WANT TO DO THAT")
5454
self.ygg.password = password

spockbot/plugins/core/taskmanager.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ def __init__(self, ploader, settings):
1010
super(TaskManager, self).__init__(ploader, settings)
1111
ploader.provides('TaskManager', self)
1212

13-
def run_task(self, task, parent=None):
13+
def run_task(self, task, parent=None, name=None):
1414
if not isinstance(task, Task):
15-
task = Task(task, parent)
15+
task = Task(task, parent, name)
16+
if parent:
17+
parent.last_child = task
1618
task.run(self)
1719
return task

spockbot/plugins/helpers/auxiliary.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from spockbot.mcp import proto
1414
from spockbot.plugins.base import PluginBase
1515

16+
1617
backend = default_backend()
1718

1819

@@ -43,7 +44,7 @@ def handshake_and_login_start(self, _, __):
4344
'protocol_version': proto.MC_PROTOCOL_VERSION,
4445
'host': self.net.host,
4546
'port': self.net.port,
46-
'next_state': proto.LOGIN_STATE
47+
'next_state': proto.LOGIN_STATE,
4748
})
4849
self.net.push_packet('LOGIN>Login Start', {'name': self.auth.username})
4950

@@ -54,13 +55,10 @@ def handle_encrypt_request(self, name, packet):
5455
self.auth.send_session_auth(pubkey_raw, packet.data['server_id'])
5556
pubkey = serialization.load_der_public_key(pubkey_raw, backend)
5657
encrypt = lambda data: pubkey.encrypt(data, padding.PKCS1v15()) # flake8: noqa
57-
self.net.push_packet(
58-
'LOGIN>Encryption Response',
59-
{
60-
'shared_secret': encrypt(self.auth.shared_secret),
61-
'verify_token': encrypt(packet.data['verify_token']),
62-
}
63-
)
58+
self.net.push_packet('LOGIN>Encryption Response', {
59+
'shared_secret': encrypt(self.auth.shared_secret),
60+
'verify_token': encrypt(packet.data['verify_token']),
61+
})
6462
self.net.enable_crypto(self.auth.shared_secret)
6563

6664
# Keep Alive - Reflects data back to server
@@ -70,6 +68,5 @@ def handle_keep_alive(self, name, packet):
7068

7169
# You be dead
7270
def handle_death(self, name, data):
73-
self.net.push_packet(
74-
'PLAY>Client Status', {'action': constants.CL_STATUS_RESPAWN}
75-
)
71+
self.net.push_packet('PLAY>Client Status',
72+
{'action': constants.CL_STATUS_RESPAWN})

spockbot/plugins/helpers/chat.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ def __init__(self, net):
2424
self.net = net
2525

2626
def chat(self, message):
27-
while message:
28-
msg_part, message = message[:100], message[100:]
29-
self.net.push_packet('PLAY>Chat Message', {'message': msg_part})
27+
for line in message.split('\n'):
28+
while line:
29+
part, line = line[:100], line[100:]
30+
self.net.push_packet('PLAY>Chat Message', {'message': part})
3031

3132
def whisper(self, player, message):
3233
self.chat('/tell %s %s' % (player, message))

spockbot/plugins/helpers/clientinfo.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ class ClientInfo(object):
7878
game_info (GameInfo): Information about the current world/server
7979
spawn_position (Position): Players initial position
8080
health (PlayerHealth): Player's health, food and saturation
81-
position (PlayerPosition): Player's Current position
81+
position (PlayerPosition): Player's current position
8282
player_list (dict): List of all players in the server
83+
eye_pos (PlayerPosition): Player's eye position
8384
8485
"""
8586
def __init__(self):
@@ -94,6 +95,10 @@ def __init__(self):
9495
self.position = PlayerPosition()
9596
self.player_list = {}
9697

98+
@property
99+
def eye_pos(self):
100+
return self.position + (0, const.PLAYER_EYE_HEIGHT, 0)
101+
97102
def reset(self):
98103
"""Resets the information in ClientInfo"""
99104
self.__init__()

0 commit comments

Comments
 (0)