Skip to content

Commit

Permalink
Merge 02b6881 into 5525f0d
Browse files Browse the repository at this point in the history
  • Loading branch information
Gjum committed Sep 19, 2015
2 parents 5525f0d + 02b6881 commit 635207e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
26 changes: 14 additions & 12 deletions spock/plugins/helpers/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,28 +149,29 @@ def __init__(self, ploader, settings):
self.is_synchronized = False

# emit inv_open_window when the inventory is loaded
self.event.reg_event_handler('inv_ready', self.emit_open_window)
self.event.reg_event_handler('inventory_synced', self.emit_open_window)

def emit_open_window(self, *_):
self.event.emit('inv_open_window', {'window': self.inventory.window})
self.event.emit('inventory_open_window',
{'window': self.inventory.window})
return True # unregister this handler

def handle_held_item_change(self, event, packet):
self.inventory.active_slot_nr = packet.data['slot']
self.event.emit('inv_held_item_change', packet.data)
self.event.emit('inventory_held_item_change', packet.data)

def handle_open_window(self, event, packet):
inv_type = windows.inv_types[packet.data['inv_type']]
self.inventory.window = inv_type(
persistent_slots=self.inventory.window.slots, **packet.data)
self.is_synchronized = False
self.event.reg_event_handler('inv_ready', self.emit_open_window)
self.event.reg_event_handler('inventory_synced', self.emit_open_window)

def handle_close_window(self, event, packet):
closed_window = self.inventory.window
self.inventory.window = windows.PlayerWindow(
persistent_slots=closed_window.slots)
self.event.emit('inv_close_window', {'window': closed_window})
self.event.emit('inventory_close_window', {'window': closed_window})

def handle_set_slot(self, event, packet):
data = packet.data
Expand All @@ -181,7 +182,7 @@ def handle_set_slot(self, event, packet):
and data['window_id'] == constants.INV_WINID_CURSOR:
# all slots received, inventory state synchronized with server
self.is_synchronized = True
self.event.emit('inv_ready', {})
self.event.emit('inventory_synced', {})

def handle_window_items(self, event, packet):
window_id = packet.data['window_id']
Expand Down Expand Up @@ -213,21 +214,21 @@ def set_slot(self, window_id, slot_nr, slot_data):
self.emit_set_slot(slot)

def emit_set_slot(self, slot):
self.event.emit('inv_set_slot', {'slot': slot})
self.event.emit('inventory_set_slot', {'slot': slot})

def handle_window_prop(self, event, packet):
self.inventory.window.properties[packet.data['property']] = \
packet.data['value']
self.event.emit('inv_win_prop', packet.data)
self.event.emit('inventory_win_prop', packet.data)

def handle_confirm_transaction(self, event, packet):
click = self.last_click
self.last_click = None
action_id = packet.data['action']
accepted = packet.data['accepted']

def emit_response_event(*_):
self.event.emit('inv_click_response', {
def emit_click_response(*_):
self.event.emit('inventory_click_response', {
'action_id': action_id,
'accepted': accepted,
'click': click,
Expand All @@ -239,14 +240,15 @@ def emit_response_event(*_):
# never occured during testing update inventory, because 1.8
# server does not send slot updates after successful clicks
click.on_success(self.inventory, self.emit_set_slot)
emit_response_event()
emit_click_response()
else: # click not accepted
self.is_synchronized = False
# confirm that we received this packet
packet.new_ident('PLAY>Confirm Transaction')
self.net.push(packet)
# 1.8 server will re-send all slots now
self.event.reg_event_handler('inv_ready', emit_response_event)
self.event.reg_event_handler('inventory_synced',
emit_click_response)

def send_click(self, click):
"""
Expand Down
6 changes: 3 additions & 3 deletions spock/plugins/tools/inventory_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def click_slot(self, slot, *args, **kwargs):
action_id = self.inventory.click_slot(slot, *args, **kwargs)
if not action_id:
raise TaskFailed('Click slot failed: not clicked')
yield 'inv_click_response', check_action_id(action_id)
yield 'inventory_click_response', check_action_id(action_id)
# TODO make sure window is not closed while clicking

empty_cursor = old_cursor.is_empty
Expand All @@ -49,7 +49,7 @@ def drop_slot(self, slot=None, *args, **kwargs):
action_id = self.inventory.drop_slot(slot, *args, **kwargs)
if not action_id:
raise TaskFailed('Drop slot failed: not clicked')
yield 'inv_click_response', check_action_id(action_id)
yield 'inventory_click_response', check_action_id(action_id)

new_slot = self.inventory.window.slots[old_slot]
if old_slot is not None and new_slot.amount > 0:
Expand All @@ -58,7 +58,7 @@ def drop_slot(self, slot=None, *args, **kwargs):
def creative_set_slot(self, slot_nr=None, slot_dict=None, slot=None):
self.inventory.creative_set_slot(slot_nr, slot_dict, slot)
slot_nr = slot_nr if slot is None else slot.slot_nr
e, data = yield ('inv_set_slot',
e, data = yield ('inventory_set_slot',
lambda e, data: data['slot'].slot_nr == slot_nr)
if False: # TODO implement check, for now just assume it worked
raise TaskFailed('Creative set slot failed: not set')
Expand Down

0 comments on commit 635207e

Please sign in to comment.