Exemplo n.º 1
0
 public void move(byte sSlot, byte dSlot, short slotMax) {
   Item source = (Item) inventory.get(sSlot);
   Item target = (Item) inventory.get(dSlot);
   if (source == null) {
     throw new RuntimeException("Trying to move empty slot");
   }
   if (target == null) {
     source.setPosition(dSlot);
     inventory.put(dSlot, source);
     inventory.remove(sSlot);
   } else if (target.getItemId() == source.getItemId()
       && !InventoryConstants.isRechargable(source.getItemId())) {
     if (type.getType() == MapleInventoryType.EQUIP.getType()) {
       swap(target, source);
     }
     if (source.getQuantity() + target.getQuantity() > slotMax) {
       short rest = (short) ((source.getQuantity() + target.getQuantity()) - slotMax);
       source.setQuantity(rest);
       target.setQuantity(slotMax);
     } else {
       target.setQuantity((short) (source.getQuantity() + target.getQuantity()));
       inventory.remove(sSlot);
     }
   } else {
     swap(target, source);
   }
 }
 public static boolean addById(MapleClient c, int Id, short quantity, String owner, int petid) {
   final MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
   final MapleInventoryType type = ii.getInventoryType(Id);
   if (!type.equals(MapleInventoryType.EQUIP)) {
     final short slotMax = ii.getSlotMax(c, Id);
     final List<IItem> existing = c.getPlayer().getInventory(type).listById(Id);
     if (!InventoryConstants.isRechargable(Id)) {
       if (existing.size() > 0) { // first update all existing slots to slotMax
         Iterator<IItem> i = existing.iterator();
         while (quantity > 0) {
           if (i.hasNext()) {
             Item eItem = (Item) i.next();
             short oldQ = eItem.getQuantity();
             if (oldQ < slotMax && (eItem.getOwner().equals(owner) || owner == null)) {
               short newQ = (short) Math.min(oldQ + quantity, slotMax);
               quantity -= (newQ - oldQ);
               eItem.setQuantity(newQ);
               c.announce(InventoryFactory.updateInventorySlot(type, eItem));
             }
           } else {
             break;
           }
         }
       }
       while (quantity > 0 || InventoryConstants.isRechargable(Id)) {
         short newQ = (short) Math.min(quantity, slotMax);
         if (newQ != 0) {
           quantity -= newQ;
           Item nItem = new Item(Id, (short) 0, newQ, petid);
           short newSlot = c.getPlayer().getInventory(type).addItem(nItem);
           if (newSlot == -1) {
             c.announce(InventoryFactory.getInventoryFull());
             c.announce(InventoryFactory.getShowInventoryFull());
             return false;
           }
           if (owner != null) {
             nItem.setOwner(owner);
           }
           c.announce(InventoryFactory.addInventorySlot(type, nItem));
           if ((InventoryConstants.isRechargable(Id)) && quantity == 0) {
             break;
           }
         } else {
           c.announce(IntraPersonalFactory.enableActions());
           return false;
         }
       }
     } else {
       final Item nItem = new Item(Id, (short) 0, quantity);
       final short newSlot = c.getPlayer().getInventory(type).addItem(nItem);
       if (newSlot == -1) {
         c.announce(InventoryFactory.getInventoryFull());
         c.announce(InventoryFactory.getShowInventoryFull());
         return false;
       }
       c.announce(InventoryFactory.addInventorySlot(type, nItem));
       c.announce(IntraPersonalFactory.enableActions());
     }
   } else if (quantity == 1) {
     IItem nEquip = ii.getEquipById(Id);
     if (owner != null) {
       nEquip.setOwner(owner);
     }
     short newSlot = c.getPlayer().getInventory(type).addItem(nEquip);
     if (newSlot == -1) {
       c.announce(InventoryFactory.getInventoryFull());
       c.announce(InventoryFactory.getShowInventoryFull());
       return false;
     }
     c.announce(InventoryFactory.addInventorySlot(type, nEquip));
   } else {
     throw new RuntimeException("Trying to create equip with non-one quantity");
   }
   return true;
 }
 public static boolean addFromDrop(
     final MapleClient c, final IItem item, final boolean show, final boolean showAnyMessage) {
   final MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
   if (ii.isPickupRestricted(item.getId())
       && c.getPlayer().getItemQuantity(item.getId(), true) > 0
       && showAnyMessage) {
     c.announce(InventoryFactory.getInventoryFull());
     c.announce(InventoryFactory.showItemUnavailable());
     return false;
   }
   final MapleInventoryType type = ii.getInventoryType(item.getId());
   short quantity = item.getQuantity();
   if (!type.equals(MapleInventoryType.EQUIP)) {
     final short slotMax = ii.getSlotMax(c, item.getId());
     final List<IItem> existing = c.getPlayer().getInventory(type).listById(item.getId());
     if (!InventoryConstants.isRechargable(item.getId())) {
       if (existing.size() > 0) { // first update all existing slots to slotMax
         Iterator<IItem> i = existing.iterator();
         while (quantity > 0) {
           if (i.hasNext()) {
             final Item eItem = (Item) i.next();
             final short oldQ = eItem.getQuantity();
             if (oldQ < slotMax && item.getOwner().equals(eItem.getOwner())) {
               short newQ = (short) Math.min(oldQ + quantity, slotMax);
               quantity -= (newQ - oldQ);
               eItem.setQuantity(newQ);
               short newQi = (short) Math.min(quantity, slotMax);
               quantity -= newQi;
               Item nItem = new Item(item.getId(), (short) 0, newQi);
               nItem.setOwner(item.getOwner());
               if (c.getPlayer().getInventory(type).fakeAddItem(nItem) == -1) {
                 eItem.setQuantity(oldQ);
                 return false;
               }
               quantity += newQi;
               c.announce(InventoryFactory.updateInventorySlot(type, eItem, showAnyMessage));
             }
           } else {
             break;
           }
         }
       }
       while (quantity > 0 || InventoryConstants.isRechargable(item.getId())) {
         final short newQ = (short) Math.min(quantity, slotMax);
         quantity -= newQ;
         final Item nItem = new Item(item.getId(), (short) 0, newQ);
         nItem.setOwner(item.getOwner());
         final short newSlot = c.getPlayer().getInventory(type).addItem(nItem);
         if (newSlot == -1) {
           if (showAnyMessage) {
             c.announce(InventoryFactory.getInventoryFull());
             c.announce(InventoryFactory.getShowInventoryFull());
           }
           item.setQuantity((short) (quantity + newQ));
           return false;
         }
         c.announce(InventoryFactory.addInventorySlot(type, nItem, true));
         if ((InventoryConstants.isRechargable(item.getId())) && quantity == 0) {
           break;
         }
       }
     } else {
       final Item nItem = new Item(item.getId(), (short) 0, quantity);
       final short newSlot = c.getPlayer().getInventory(type).addItem(nItem);
       if (newSlot == -1) {
         if (showAnyMessage) {
           c.announce(InventoryFactory.getInventoryFull());
           c.announce(InventoryFactory.getShowInventoryFull());
         }
         return false;
       }
       c.announce(InventoryFactory.addInventorySlot(type, nItem));
       c.announce(IntraPersonalFactory.enableActions());
     }
   } else if (quantity == 1) {
     final short newSlot = c.getPlayer().getInventory(type).addItem(item);
     if (newSlot == -1) {
       if (showAnyMessage) {
         c.announce(InventoryFactory.getInventoryFull());
         c.announce(InventoryFactory.getShowInventoryFull());
       }
       return false;
     }
     c.announce(InventoryFactory.addInventorySlot(type, item, true));
   } else {
     return false;
   }
   if (show) {
     c.announce(EffectFactory.getShowItemGain(item.getId(), item.getQuantity()));
   }
   return true;
 }