public static boolean checkSpace(MapleClient c, int itemid, int quantity, String owner) {
    MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
    MapleInventoryType type = ii.getInventoryType(itemid);

    if (!type.equals(MapleInventoryType.EQUIP)) {
      short slotMax = ii.getSlotMax(c, itemid);
      List<IItem> existing = c.getPlayer().getInventory(type).listById(itemid);
      if (!ii.isThrowingStar(itemid) && !ii.isBullet(itemid)) {
        if (existing.size() > 0) { // first update all existing slots to slotMax

          for (IItem eItem : existing) {
            short oldQ = eItem.getQuantity();
            if (oldQ < slotMax && owner.equals(eItem.getOwner())) {
              short newQ = (short) Math.min(oldQ + quantity, slotMax);
              quantity -= (newQ - oldQ);
            }
            if (quantity <= 0) {
              break;
            }
          }
        }
      }
      // add new slots if there is still something left
      final int numSlotsNeeded;
      if (slotMax > 0) {
        numSlotsNeeded = (int) (Math.ceil(((double) quantity) / slotMax));
      } else if (ii.isThrowingStar(itemid) || ii.isBullet(itemid)) {
        numSlotsNeeded = 1;
      } else {
        numSlotsNeeded = 1;
        log.error("SUCK ERROR - FIX ME! - 0 slotMax");
      }
      return !c.getPlayer().getInventory(type).isFull(numSlotsNeeded - 1);
    } else {
      return !c.getPlayer().getInventory(type).isFull();
    }
  }
 public static void move(MapleClient c, MapleInventoryType type, byte src, byte dst) {
   if (src < 0 || dst < 0) {
     return;
   }
   MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
   IItem source = c.getPlayer().getInventory(type).getItem(src);
   IItem initialTarget = c.getPlayer().getInventory(type).getItem(dst);
   if (source == null) {
     return;
   }
   short olddstQ = -1;
   if (initialTarget != null) {
     olddstQ = initialTarget.getQuantity();
   }
   short oldsrcQ = source.getQuantity();
   short slotMax = ii.getSlotMax(c, source.getItemId());
   c.getPlayer().getInventory(type).move(src, dst, slotMax);
   if (!type.equals(MapleInventoryType.EQUIP)
       && initialTarget != null
       && initialTarget.getItemId() == source.getItemId()
       && !ii.isThrowingStar(source.getItemId())
       && !ii.isBullet(source.getItemId())) {
     if ((olddstQ + oldsrcQ) > slotMax) {
       c.getSession()
           .write(
               MaplePacketCreator.moveAndMergeWithRestInventoryItem(
                   type, src, dst, (short) ((olddstQ + oldsrcQ) - slotMax), slotMax));
     } else {
       c.getSession()
           .write(
               MaplePacketCreator.moveAndMergeInventoryItem(
                   type,
                   src,
                   dst,
                   ((Item) c.getPlayer().getInventory(type).getItem(dst)).getQuantity()));
     }
   } else {
     c.getSession().write(MaplePacketCreator.moveInventoryItem(type, src, dst));
   }
 }
  public static boolean addById(
      MapleClient c, int itemId, short quantity, String owner, int petid) {
    if (quantity >= 4000 || quantity < 0) {
      AutobanManager.getInstance()
          .autoban(c.getPlayer().getClient(), "PE Item: " + quantity + "x " + itemId);
      return false;
    }
    MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
    MapleInventoryType type = ii.getInventoryType(itemId);
    if (!type.equals(MapleInventoryType.EQUIP)) {
      short slotMax = ii.getSlotMax(c, itemId);
      List<IItem> existing = c.getPlayer().getInventory(type).listById(itemId);
      if (!ii.isThrowingStar(itemId) && !ii.isBullet(itemId)) {
        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.getSession().write(MaplePacketCreator.updateInventorySlot(type, eItem));
              }
            } else {
              break;
            }
          }
        }
        // add new slots if there is still something left
        while (quantity > 0 || ii.isThrowingStar(itemId) || ii.isBullet(itemId)) {
          short newQ = (short) Math.min(quantity, slotMax);
          if (newQ != 0) {
            quantity -= newQ;
            Item nItem = new Item(itemId, (byte) 0, newQ, petid);
            byte newSlot = c.getPlayer().getInventory(type).addItem(nItem);
            if (newSlot == -1) {
              c.getSession().write(MaplePacketCreator.getInventoryFull());
              c.getSession().write(MaplePacketCreator.getShowInventoryFull());
              return false;
            }
            if (owner != null) {
              nItem.setOwner(owner);
            }
            c.getSession().write(MaplePacketCreator.addInventorySlot(type, nItem));
            if ((ii.isThrowingStar(itemId) || ii.isBullet(itemId)) && quantity == 0) {
              break;
            }
          } else {
            c.getSession().write(MaplePacketCreator.enableActions());
            return false;
          }
        }
      } else {
        // Throwing Stars and Bullets - Add all into one slot regardless of quantity.
        Item nItem = new Item(itemId, (byte) 0, quantity);
        byte newSlot = c.getPlayer().getInventory(type).addItem(nItem);

        if (newSlot == -1) {
          c.getSession().write(MaplePacketCreator.getInventoryFull());
          c.getSession().write(MaplePacketCreator.getShowInventoryFull());
          return false;
        }
        c.getSession().write(MaplePacketCreator.addInventorySlot(type, nItem));
        c.getSession().write(MaplePacketCreator.enableActions());
      }
    } else {
      if (quantity == 1) {
        IItem nEquip = ii.getEquipById(itemId);
        if (owner != null) {
          nEquip.setOwner(owner);
        }

        byte newSlot = c.getPlayer().getInventory(type).addItem(nEquip);
        if (newSlot == -1) {
          c.getSession().write(MaplePacketCreator.getInventoryFull());
          c.getSession().write(MaplePacketCreator.getShowInventoryFull());
          return false;
        }
        c.getSession().write(MaplePacketCreator.addInventorySlot(type, nEquip));
      } else {
        throw new InventoryException("Trying to create equip with non-one quantity");
      }
    }
    return true;
  }
  public static void drop(MapleClient c, MapleInventoryType type, byte src, short quantity) {
    MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
    if (src < 0) {
      type = MapleInventoryType.EQUIPPED;
    }
    IItem source = c.getPlayer().getInventory(type).getItem(src);
    int itemId = source.getItemId();
    if (itemId == 1302065 || itemId == 1302033) {
      c.getPlayer().hasflag = false;
      c.getPlayer().setCanPickup(true);
    }

    if (itemId == 1112000) {
      c.getPlayer().dropMessage("Super Rebirth rings cannot be dropped or traded.");
      return;
    }

    if (itemId == 1012076) {
      c.getPlayer().dropMessage("The Beta Mask cannot be traded or dropped.");
      return;
    }

    if (c.getPlayer().getItemEffect() == itemId && source.getQuantity() == 1) {
      c.getPlayer().setItemEffect(0);
      c.getPlayer()
          .getMap()
          .broadcastMessage(MaplePacketCreator.itemEffect(c.getPlayer().getId(), 0));
    } else if (itemId == 5370000 || itemId == 5370001) {
      if (c.getPlayer().getItemQuantity(itemId, false) == 1) {
        c.getPlayer().setChalkboard(null);
      }
    }

    if (itemId == 4000076) {
      c.getPlayer().mapCheck();
    }

    if (quantity < 0
        || source == null
        || quantity == 0 && !ii.isThrowingStar(itemId) && !ii.isBullet(itemId)) {
      String message =
          "Dropping "
              + quantity
              + " "
              + (source == null ? "?" : itemId)
              + " ("
              + type.name()
              + "/"
              + src
              + ")";
      // AutobanManager.getInstance().addPoints(c, 1000, 0, message);
      log.info(MapleClient.getLogMessage(c, message));
      c.getSession()
          .close(); // disconnect the client as is inventory is inconsistent with the serverside
                    // inventory -> f**k

      return;
    }
    Point dropPos = new Point(c.getPlayer().getPosition());
    // dropPos.y -= 99;
    if (quantity < source.getQuantity() && !ii.isThrowingStar(itemId) && !ii.isBullet(itemId)) {
      IItem target = source.copy();
      target.setQuantity(quantity);
      source.setQuantity((short) (source.getQuantity() - quantity));
      c.getSession().write(MaplePacketCreator.dropInventoryItemUpdate(type, source));
      boolean weddingRing =
          source.getItemId() == 1112803
              || source.getItemId() == 1112806
              || source.getItemId() == 1112807
              || source.getItemId() == 1112809;
      if (weddingRing) {
        c.getPlayer().getMap().disappearingItemDrop(c.getPlayer(), c.getPlayer(), target, dropPos);
      } else if (c.getPlayer().getMap().getEverlast()) {
        if (!c.getChannelServer().allowUndroppablesDrop()
            && ii.isDropRestricted(target.getItemId())) {
          c.getPlayer()
              .getMap()
              .disappearingItemDrop(c.getPlayer(), c.getPlayer(), target, dropPos);
        } else {
          c.getPlayer()
              .getMap()
              .spawnItemDrop(c.getPlayer(), c.getPlayer(), target, dropPos, true, false);
        }
      } else {
        if (!c.getChannelServer().allowUndroppablesDrop()
            && ii.isDropRestricted(target.getItemId())) {
          c.getPlayer()
              .getMap()
              .disappearingItemDrop(c.getPlayer(), c.getPlayer(), target, dropPos);
        } else {
          c.getPlayer()
              .getMap()
              .spawnItemDrop(c.getPlayer(), c.getPlayer(), target, dropPos, true, true);
        }
      }
    } else {
      c.getPlayer().getInventory(type).removeSlot(src);
      c.getSession()
          .write(
              MaplePacketCreator.dropInventoryItem(
                  (src < 0 ? MapleInventoryType.EQUIP : type), src));
      if (src < 0) {
        c.getPlayer().equipChanged();
      }
      if (c.getPlayer().getMap().getEverlast()) {
        if (!c.getChannelServer().allowUndroppablesDrop() && ii.isDropRestricted(itemId)) {
          c.getPlayer()
              .getMap()
              .disappearingItemDrop(c.getPlayer(), c.getPlayer(), source, dropPos);
        } else {
          c.getPlayer()
              .getMap()
              .spawnItemDrop(c.getPlayer(), c.getPlayer(), source, dropPos, true, false);
        }
      } else {
        if (!c.getChannelServer().allowUndroppablesDrop() && ii.isDropRestricted(itemId)) {
          c.getPlayer()
              .getMap()
              .disappearingItemDrop(c.getPlayer(), c.getPlayer(), source, dropPos);
        } else {
          c.getPlayer()
              .getMap()
              .spawnItemDrop(c.getPlayer(), c.getPlayer(), source, dropPos, true, true);
        }
      }
    }
  }
  public static boolean addFromDrop(MapleClient c, IItem item, boolean show, String owner) {
    MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
    MapleInventoryType type = ii.getInventoryType(item.getItemId());

    if (!c.getChannelServer().allowMoreThanOne()
        && ii.isPickupRestricted(item.getItemId())
        && c.getPlayer().haveItem(item.getItemId(), 1, true, false)) {
      c.getSession().write(MaplePacketCreator.getInventoryFull());
      c.getSession().write(MaplePacketCreator.showItemUnavailable());
      return false;
    }
    short quantity = item.getQuantity();
    if (quantity >= 4000 || quantity < 0) {
      AutobanManager.getInstance()
          .autoban(c.getPlayer().getClient(), "PE Item: " + quantity + "x " + item.getItemId());
      return false;
    }

    if (!type.equals(MapleInventoryType.EQUIP)) {
      short slotMax = ii.getSlotMax(c, item.getItemId());
      List<IItem> existing = c.getPlayer().getInventory(type).listById(item.getItemId());
      if (!ii.isThrowingStar(item.getItemId()) && !ii.isBullet(item.getItemId())) {
        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 && item.getOwner().equals(eItem.getOwner())) {
                short newQ = (short) Math.min(oldQ + quantity, slotMax);
                quantity -= (newQ - oldQ);
                eItem.setQuantity(newQ);
                c.getSession().write(MaplePacketCreator.updateInventorySlot(type, eItem, true));
              }
            } else {
              break;
            }
          }
        }
        // add new slots if there is still something left
        while (quantity > 0
            || ii.isThrowingStar(item.getItemId())
            || ii.isBullet(item.getItemId())) {
          short newQ = (short) Math.min(quantity, slotMax);
          quantity -= newQ;
          Item nItem = new Item(item.getItemId(), (byte) 0, newQ);
          nItem.setOwner(item.getOwner());
          byte newSlot = c.getPlayer().getInventory(type).addItem(nItem);
          if (newSlot == -1) {
            c.getSession().write(MaplePacketCreator.getInventoryFull());
            c.getSession().write(MaplePacketCreator.getShowInventoryFull());
            item.setQuantity((short) (quantity + newQ));
            return false;
          }
          c.getSession().write(MaplePacketCreator.addInventorySlot(type, nItem, true));
        }
      } else {
        // Throwing Stars and Bullets - Add all into one slot regardless of quantity.
        Item nItem = new Item(item.getItemId(), (byte) 0, quantity);
        byte newSlot = c.getPlayer().getInventory(type).addItem(nItem);
        if (newSlot == -1) {
          c.getSession().write(MaplePacketCreator.getInventoryFull());
          c.getSession().write(MaplePacketCreator.getShowInventoryFull());
          return false;
        }
        c.getSession().write(MaplePacketCreator.addInventorySlot(type, nItem));
        c.getSession().write(MaplePacketCreator.enableActions());
      }
    } else {
      if (quantity == 1) {
        byte newSlot = c.getPlayer().getInventory(type).addItem(item);

        if (newSlot == -1) {
          c.getSession().write(MaplePacketCreator.getInventoryFull());
          c.getSession().write(MaplePacketCreator.getShowInventoryFull());
          return false;
        }
        c.getSession().write(MaplePacketCreator.addInventorySlot(type, item, true));
      } else {
        throw new RuntimeException("Trying to create equip with non-one quantity");
      }
    }
    if (owner != null) {
      item.setOwner(owner);
    }
    if (show) {
      c.getSession()
          .write(MaplePacketCreator.getShowItemGain(item.getItemId(), item.getQuantity()));
    }
    return true;
  }