def make_slot_check(wanted): """ Creates and returns a function that takes a slot and checks if it matches the wanted item. Args: wanted: function(Slot) or Slot or itemID or (itemID, metadata) """ if isinstance(wanted, types.FunctionType): return wanted # just forward the slot check function if isinstance(wanted, int): item, meta = wanted, None elif isinstance(wanted, Slot): item, meta = wanted.item_id, wanted.damage # TODO compare NBT elif isinstance(wanted, (Item, Block)): item, meta = wanted.id, wanted.metadata elif isinstance(wanted, str): item_or_block = get_item_or_block(wanted, init=True) item, meta = item_or_block.id, item_or_block.metadata else: # wanted is (id, meta) try: item, meta = wanted except TypeError: raise ValueError('Illegal args for make_slot_check(): %s' % wanted) return lambda slot: item == slot.item_id and meta in (None, slot.damage)
def __init__(self, window, slot_nr, id=constants.INV_ITEMID_EMPTY, damage=0, amount=0, enchants=None): self.window = window self.slot_nr = slot_nr self.item_id = id self.damage = damage self.amount = amount self.nbt = enchants self.item = get_item_or_block(self.item_id, self.damage) or Item()
def drop_items(self, amount, item_id=None, meta=None, *excess_args): if excess_args: logger.warn('Too many args for drop <amount> [id/name] [meta]') amount = int(amount) try: item_id = int(item_id) meta = int(meta) except: # item_id is name string or None, or meta is None pass if item_id is None and meta is None: item = self.inv.active_slot.item else: item = get_item_or_block(item_id, meta or 0) stored = self.inv.total_stored(item) if stored < amount: logger.warn('Not enough %s stored (%i/%i)' % (item, stored, amount)) return def drop_items_task(amount_left): while amount_left > 0: found_slot = self.inv.find_slot(item) if found_slot is None: raise TaskFailed('No %s stored anymore' % item) if amount_left >= found_slot.amount: amount_left -= found_slot.amount yield self.inv. async .drop_slot(found_slot, drop_stack=True) else: logger.debug('Dropping %s single', amount_left) for i in range(amount_left): yield self.inv. async .drop_slot(found_slot) amount_left -= 1 self.taskmanager.run_task(drop_items_task(amount), TaskChatter(chat=self.chat), name='Drop %sx %s' % (amount, item))
def drop_items(self, amount, item_id=None, meta=None, *excess_args): if excess_args: logger.warn('Too many args for drop <amount> [id/name] [meta]') amount = int(amount) try: item_id = int(item_id) meta = int(meta) except: # item_id is name string or None, or meta is None pass if item_id is None and meta is None: item = self.inv.active_slot.item else: item = get_item_or_block(item_id, meta or 0) stored = self.inv.total_stored(item) if stored < amount: logger.warn('Not enough %s stored (%i/%i)' % (item, stored, amount)) return def drop_items_task(amount_left): while amount_left > 0: found_slot = self.inv.find_slot(item) if found_slot is None: raise TaskFailed('No %s stored anymore' % item) if amount_left >= found_slot.amount: amount_left -= found_slot.amount yield self.inv.async.drop_slot(found_slot, drop_stack=True) else: logger.debug('Dropping %s single', amount_left) for i in range(amount_left): yield self.inv.async.drop_slot(found_slot) amount_left -= 1 self.taskmanager.run_task(drop_items_task(amount), TaskChatter(chat=self.chat), name='Drop %sx %s' % (amount, item))