public static boolean checkSpace(
     final MapleClient c, final int Id, int quantity, final String owner) {
   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) {
         for (IItem eItem : existing) {
           final short oldQ = eItem.getQuantity();
           if (oldQ < slotMax && owner.equals(eItem.getOwner())) {
             final short newQ = (short) Math.min(oldQ + quantity, slotMax);
             quantity -= (newQ - oldQ);
           }
           if (quantity <= 0) {
             break;
           }
         }
       }
     }
     final int numSlotsNeeded;
     if (slotMax > 0) {
       numSlotsNeeded = (int) (Math.ceil(((double) quantity) / slotMax));
     } else if (InventoryConstants.isRechargable(Id)) {
       numSlotsNeeded = 1;
     } else {
       numSlotsNeeded = 1;
       System.out.println("checkSpace error");
     }
     return !c.getPlayer().getInventory(type).isFull(numSlotsNeeded - 1);
   } else {
     return !c.getPlayer().getInventory(type).isFull();
   }
 }
Exemple #2
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 void drop(MapleClient c, MapleInventoryType type, short src, short quantity) {
   if (src < 0) {
     type = MapleInventoryType.EQUIPPED;
   }
   IItem source = c.getPlayer().getInventory(type).getItem(src);
   int Id = source.getId();
   if (c.getPlayer().getItemEffect() == Id && source.getQuantity() == 1) {
     c.getPlayer().setItemEffect(0);
     c.getPlayer().getMap().broadcastMessage(EffectFactory.itemEffect(c.getPlayer().getId(), 0));
   } else if (Id == 5370000 || Id == 5370001) { // not actually possible
     if (c.getPlayer().getItemQuantity(Id, false) == 1) {
       c.getPlayer().setChalkboard(null);
     }
   } else if ((Id >= 5000000 && Id <= 5000100) || Id == 4031284) {
     c.getPlayer().dropMessage("This item may not be dropped.");
     return;
   }
   if (c.getPlayer().getItemQuantity(Id, true) < quantity
       || quantity < 0
       || source == null
       || source.getFlag() == InventoryConstants.LOCK
       || (quantity == 0 && !InventoryConstants.isRechargable(Id))) {
     return;
   }
   Point dropPos = new Point(c.getPlayer().getPosition());
   if (quantity < source.getQuantity() && !InventoryConstants.isRechargable(Id)) {
     IItem target = source.copy();
     target.setQuantity(quantity);
     source.setQuantity((short) (source.getQuantity() - quantity));
     c.announce(InventoryFactory.dropInventoryItemUpdate(type, source));
     dropItem(c, target, dropPos);
   } else {
     c.getPlayer().getInventory(type).removeSlot(src);
     c.announce(
         InventoryFactory.dropInventoryItem((src < 0 ? MapleInventoryType.EQUIP : type), src));
     if (src < 0) {
       c.getPlayer().equipChanged();
     }
     dropItem(c, source, dropPos);
   }
 }
 public static void removeFromSlot(
     final MapleClient c,
     final MapleInventoryType type,
     final short slot,
     final short quantity,
     final boolean fromDrop,
     final boolean consume) {
   final IItem item = c.getPlayer().getInventory(type).getItem(slot);
   final boolean allowZero = consume && InventoryConstants.isRechargable(item.getId());
   c.getPlayer().getInventory(type).removeItem(slot, quantity, allowZero);
   if (item.getQuantity() == 0 && !allowZero) {
     c.announce(InventoryFactory.clearInventoryItem(type, item.getPosition(), fromDrop));
   } else {
     c.announce(InventoryFactory.updateInventorySlot(type, (Item) item, fromDrop));
   }
 }
  public static void move(
      final MapleClient c, final MapleInventoryType type, final short src, final short dst) {
    if (src < 0 || dst < 0) {
      return;
    }
    final MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
    final IItem source = c.getPlayer().getInventory(type).getItem(src);
    final IItem initialTarget = c.getPlayer().getInventory(type).getItem(dst);
    if (source == null) {
      return;
    }

    short olddstQ = -1;
    if (initialTarget != null) {
      olddstQ = initialTarget.getQuantity();
    }
    final short oldsrcQ = source.getQuantity();
    final short slotMax = ii.getSlotMax(c, source.getId());

    c.getPlayer().getInventory(type).move(src, dst, slotMax);
    if (!type.equals(MapleInventoryType.EQUIP)
        && initialTarget != null
        && initialTarget.getId() == source.getId()
        && !InventoryConstants.isRechargable(source.getId())) {
      if ((olddstQ + oldsrcQ) > slotMax) {
        c.announce(
            InventoryFactory.moveAndMergeWithRestInventoryItem(
                type, src, dst, (short) ((olddstQ + oldsrcQ) - slotMax), slotMax));
      } else {
        c.announce(
            InventoryFactory.moveAndMergeInventoryItem(
                type,
                src,
                dst,
                ((Item) c.getPlayer().getInventory(type).getItem(dst)).getQuantity()));
      }
    } else {
      c.announce(InventoryFactory.moveInventoryItem(type, src, dst));
    }
  }
 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;
 }
 public final void handlePacket(SeekableLittleEndianAccessor slea, MapleClient c) {
   c.getPlayer().resetAfkTime();
   byte operation = slea.readByte();
   if (operation == Actions.TOSERVER_SEND_ITEM.getCode()) {
     final int fee = 5000;
     byte inventId = slea.readByte();
     short itemPos = slea.readShort();
     short amount = slea.readShort();
     int mesos = slea.readInt();
     String recipient = slea.readMapleAsciiString();
     if (amount < 0 || amount > 2000 || itemPos < 0 || inventId > 5 || inventId < 0) {
       return;
     }
     if (mesos < 0
         || (long) mesos > Integer.MAX_VALUE
         || ((long) mesos + fee + getFee(mesos)) > Integer.MAX_VALUE) {
       return;
     }
     int finalcost = mesos + fee + getFee(mesos);
     boolean send = false;
     if (c.getPlayer().getMeso() >= finalcost) {
       int accid = getAccIdFromCNAME(recipient, true);
       if (accid != -1) {
         if (accid != c.getAccID()) {
           c.getPlayer().gainMeso(-finalcost, false);
           c.getSession()
               .write(
                   MaplePacketCreator.sendDueyMSG(Actions.TOCLIENT_SUCCESSFULLY_SENT.getCode()));
           send = true;
         } else {
           c.getSession()
               .write(MaplePacketCreator.sendDueyMSG(Actions.TOCLIENT_SAMEACC_ERROR.getCode()));
         }
       } else {
         c.getSession()
             .write(
                 MaplePacketCreator.sendDueyMSG(Actions.TOCLIENT_NAME_DOES_NOT_EXIST.getCode()));
       }
     } else {
       c.getSession()
           .write(MaplePacketCreator.sendDueyMSG(Actions.TOCLIENT_NOT_ENOUGH_MESOS.getCode()));
     }
     boolean recipientOn = false;
     MapleClient rClient = null;
     try {
       int channel = c.getChannelServer().getWorldInterface().find(recipient);
       if (channel > -1) {
         recipientOn = true;
         ChannelServer rcserv = ChannelServer.getInstance(channel);
         rClient = rcserv.getPlayerStorage().getCharacterByName(recipient).getClient();
       }
     } catch (RemoteException re) {
       c.getChannelServer().reconnectWorld();
     }
     if (send) {
       if (inventId > 0) {
         MapleInventoryType inv = MapleInventoryType.getByType(inventId);
         IItem item = c.getPlayer().getInventory(inv).getItem((byte) itemPos);
         if (item != null && c.getPlayer().getItemQuantity(item.getItemId(), false) > amount) {
           if (InventoryConstants.isRechargable(item.getItemId())) {
             MapleInventoryManipulator.removeFromSlot(
                 c, inv, (byte) itemPos, item.getQuantity(), true);
           } else {
             MapleInventoryManipulator.removeFromSlot(c, inv, (byte) itemPos, amount, true, false);
           }
           addItemToDB(
               item, amount, mesos, c.getPlayer().getName(), getAccIdFromCNAME(recipient, false));
         } else {
           return;
         }
       } else {
         addMesoToDB(mesos, c.getPlayer().getName(), getAccIdFromCNAME(recipient, false));
       }
       if (recipientOn && rClient != null) {
         rClient
             .getSession()
             .write(MaplePacketCreator.sendDueyMSG(Actions.TOCLIENT_PACKAGE_MSG.getCode()));
       }
       c.getPlayer().gainMeso(-fee, false);
     }
   } else if (operation == Actions.TOSERVER_REMOVE_PACKAGE.getCode()) {
     int packageid = slea.readInt();
     removeItemFromDB(packageid);
     c.getSession().write(MaplePacketCreator.removeItemFromDuey(true, packageid));
   } else if (operation == Actions.TOSERVER_CLAIM_PACKAGE.getCode()) {
     int packageid = slea.readInt();
     List<DueyPackages> packages = new LinkedList<DueyPackages>();
     DueyPackages dp = null;
     Connection con = DatabaseConnection.getConnection();
     try {
       PreparedStatement ps =
           con.prepareStatement(
               "SELECT * FROM dueypackages LEFT JOIN dueyitems USING (PackageId) WHERE PackageId = ?"); // PLEASE WORK D:
       ps.setInt(1, packageid);
       ResultSet rs = ps.executeQuery();
       DueyPackages dueypack = null;
       if (rs.next()) {
         dueypack = getItemByPID(rs);
         dueypack.setSender(rs.getString("SenderName"));
         dueypack.setMesos(rs.getInt("Mesos"));
         dueypack.setSentTime(rs.getString("TimeStamp"));
         packages.add(dueypack);
       }
       rs.close();
       ps.close();
       dp = dueypack;
     } catch (SQLException e) {
     }
     if (dp.getItem() != null) {
       if (!MapleInventoryManipulator.checkSpace(
           c, dp.getItem().getItemId(), dp.getItem().getQuantity(), dp.getItem().getOwner())) {
         c.getPlayer().dropMessage(1, "Your inventory is full");
         c.getSession().write(MaplePacketCreator.enableActions());
         return;
       } else {
         MapleInventoryManipulator.addFromDrop(c, dp.getItem(), false);
       }
     }
     int gainmesos = 0;
     long totalmesos = (long) dp.getMesos() + (long) c.getPlayer().getMeso();
     if (totalmesos >= Integer.MAX_VALUE) {
       gainmesos = 2147383647 - c.getPlayer().getMeso();
     } else if (totalmesos < 0 || dp.getMesos() < 0) {
       // do nothing
     } else {
       c.getPlayer().gainMeso(gainmesos, false);
     }
     removeItemFromDB(packageid);
     c.getSession().write(MaplePacketCreator.removeItemFromDuey(false, packageid));
   }
 }