55from minecraft_data .v1_8 import windows_list
66
77from spockbot .mcdata import constants , get_item_or_block
8+ from spockbot .mcdata .blocks import Block
89from spockbot .mcdata .items import Item
910from 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
256265class 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
0 commit comments