Skip to content

Commit dfdc324

Browse files
committed
Fix dropping items
1 parent 80baa2e commit dfdc324

File tree

2 files changed

+48
-20
lines changed

2 files changed

+48
-20
lines changed

spockbot/mcdata/windows.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,11 @@ def __init__(self, slot, button=constants.INV_BUTTON_LEFT):
199199
'Clicking with button %s not implemented' % button)
200200

201201
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
202205
return {
203-
'slot': self.slot.slot_nr,
206+
'slot': slot_nr,
204207
'button': self.button,
205208
'mode': 0,
206209
'clicked_item': self.slot.get_dict(),
@@ -209,13 +212,19 @@ def get_packet(self, inv_plugin):
209212
def apply(self, inv_plugin):
210213
clicked = self.slot
211214
cursor = inv_plugin.cursor_slot
212-
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:
213222
if clicked.stacks_with(cursor):
214223
self.transfer(cursor, clicked, cursor.amount)
215224
else:
216225
self.swap_slots(cursor, clicked)
217226
elif self.button == constants.INV_BUTTON_RIGHT:
218-
if cursor.item_id == constants.INV_ITEMID_EMPTY:
227+
if cursor.is_empty:
219228
# transfer half, round up
220229
self.transfer(clicked, cursor, (clicked.amount + 1) // 2)
221230
elif clicked.is_empty or clicked.stacks_with(cursor):
@@ -233,27 +242,24 @@ def __init__(self, slot, drop_stack=False):
233242
self.drop_stack = drop_stack
234243

235244
def get_packet(self, inv_plugin):
236-
if self.slot == inv_plugin.active_slot:
237-
slot_nr = constants.INV_OUTSIDE_WINDOW # drop cursor slot
238-
elif inv_plugin.cursor_slot.item_id != constants.INV_ITEMID_EMPTY:
239-
return None # can't drop while holding an item
240-
else: # default case
241-
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+
242250
return {
243-
'slot': slot_nr,
251+
'slot': self.slot.slot_nr,
244252
'button': 1 if self.drop_stack else 0,
245253
'mode': 4,
246254
'clicked_item': inv_plugin.cursor_slot.get_dict(),
247255
}
248256

249257
def apply(self, inv_plugin):
250-
if inv_plugin.cursor_slot.is_empty:
251-
if self.drop_stack:
252-
self.slot.amount = 0
253-
else:
254-
self.slot.amount -= 1
255-
self.cleanup_if_empty(self.slot)
256-
# 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)
257263

258264

259265
class Window(object):

spockbot/plugins/helpers/inventory.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def click_slot(self, slot, right=False):
8686
8787
Args:
8888
slot (Slot): The clicked slot. Can be ``Slot`` instance or integer.
89-
Set to ``inventory.cursor_slot`` or -999
89+
Set to ``inventory.cursor_slot``
9090
for clicking outside the window.
9191
"""
9292
if isinstance(slot, int):
@@ -96,10 +96,32 @@ def click_slot(self, slot, right=False):
9696
return self.send_click(windows.SingleClick(slot, button))
9797

9898
def drop_slot(self, slot=None, drop_stack=False):
99-
if slot is None: # drop held item
100-
slot = self.active_slot
99+
"""
100+
Drop one or all items of the slot.
101+
102+
Does not wait for confirmation from the server. If you want that,
103+
use a ``Task`` and ``yield inventory.async.drop_slot()`` instead.
104+
105+
If ``slot`` is None, drops the ``cursor_slot`` or, if that's empty,
106+
the currently held item (``active_slot``).
107+
108+
Args:
109+
slot (Optional[Slot]): The dropped slot. Can be None, integer,
110+
or ``Slot`` instance.
111+
112+
Returns:
113+
int: The action ID of the click
114+
"""
115+
if slot is None:
116+
if self.cursor_slot.is_empty:
117+
slot = self.active_slot
118+
else:
119+
slot = self.cursor_slot
101120
elif isinstance(slot, int): # also allow slot nr
102121
slot = self.window.slots[slot]
122+
if slot == self.cursor_slot:
123+
# dropping items from cursor is done via normal click
124+
return self.click_slot(self.cursor_slot, not drop_stack)
103125
return self.send_click(windows.DropClick(slot, drop_stack))
104126

105127
def close_window(self):

0 commit comments

Comments
 (0)