/*	TODO:
   *	1. Move the equpping into a function.
   */
  @Override
  public void handlePacket(SeekableLittleEndianAccessor slea, MapleClient c) {
    c.getPlayer().resetAfkTime();
    slea.readInt();
    byte slot = slea.readByte();
    slea.readByte();
    boolean lead = slea.readByte() == 1;
    MapleCharacter player = c.getPlayer();
    IItem item = player.getInventory(MapleInventoryType.CASH).getItem(slot);

    if (item.getItemId() == 5000028 || item.getItemId() == 5000047) {
      boolean done = false;
      int petno;
      int[] pet;
      int[] dragon = {5000029, 5000030, 5000031, 5000032, 5000033};
      int[] robot = {5000048, 5000049, 5000050, 5000051, 5000052, 5000053};

      pet = item.getItemId() == 5000028 ? dragon : robot;
      Random egg = new Random();
      for (int i = 0; i < pet.length && !done; i++) {
        petno = egg.nextInt(pet.length);
        if (!player.haveItem(pet[petno], 1, true, true)) {
          MapleInventoryManipulator.removeFromSlot(
              c, MapleInventoryType.CASH, item.getPosition(), (short) 1, true, false);
          MapleInventoryManipulator.addById(
              c, pet[petno], (short) 1, null, MaplePet.createPet(pet[petno]));
          done = true;
        }
      }
      if (!done) {
        player.dropMessage(1, "You currently have all the dragons or robots.");
        return;
      }
    }
    // New instance of MaplePet - using the item ID and unique pet ID
    MaplePet pet =
        MaplePet.loadFromDb(
            player.getInventory(MapleInventoryType.CASH).getItem(slot).getItemId(),
            slot,
            player.getInventory(MapleInventoryType.CASH).getItem(slot).getPetId());

    if (pet == null) {
      MapleInventoryManipulator.removeById(
          c, MapleInventoryType.CASH, item.getItemId(), (short) item.getQuantity(), false, false);
      c.getSession().write(MaplePacketCreator.enableActions());
      return;
    }
    // Assign the pet to the player, set stats
    if (player.getPetIndex(pet) != -1) {
      player.unequipPet(pet, true);
    } else {
      if (player.getSkillLevel(SkillFactory.getSkill(8)) == 0 && player.getPet(0) != null) {
        player.unequipPet(player.getPet(0), false);
      }
      if (lead) {
        player.shiftPetsRight();
      }
      Point pos = player.getPosition();
      pos.y -= 12;
      pet.setPos(pos);
      pet.setFh(player.getMap().getFootholds().findBelow(pet.getPos()).getId());
      pet.setStance(0);

      player.addPet(pet);
      // Broadcast packet to the map...
      player
          .getMap()
          .broadcastMessage(player, MaplePacketCreator.showPet(player, pet, false), true);
      // Find the pet's unique ID
      int uniqueid = pet.getUniqueId();
      // Make a new List for the stat update
      List<Pair<MapleStat, Integer>> stats = new ArrayList<Pair<MapleStat, Integer>>();
      stats.add(new Pair<MapleStat, Integer>(MapleStat.PET, Integer.valueOf(uniqueid)));
      // Write the stat update to the player...
      c.getSession().write(MaplePacketCreator.petStatUpdate(player));
      c.getSession().write(MaplePacketCreator.enableActions());
      // Get the data
      int hunger = PetDataFactory.getHunger(pet.getItemId());
      // Start the fullness schedule
      player.startFullnessSchedule(hunger, pet, player.getPetIndex(pet));
    }
  }
 @Override
 public void handlePacket(SeekableLittleEndianAccessor slea, MapleClient c) {
   c.getPlayer().resetAfkTime();
   if (c.getPlayer().getNoPets() == 0) {
     return;
   }
   int slot = 0;
   MaplePet[] pets = c.getPlayer().getPets();
   for (int i = 0; i < 3; i++) {
     if (pets[i] != null) {
       if (pets[i].getFullness() < 100) {
         slot = i;
       }
     } else {
       break;
     }
   }
   MaplePet pet = c.getPlayer().getPet(slot);
   slea.readInt();
   slea.readShort();
   int itemId = slea.readInt();
   if (c.getPlayer().haveItem(itemId, 1, false, true)) {
     boolean gainCloseness = new Random().nextInt(101) <= 50;
     int newFullness = pet.getFullness() + 30;
     if (pet.getFullness() < 100) {
       if (newFullness > 100) {
         newFullness = 100;
       }
       pet.setFullness(newFullness);
       if (gainCloseness && pet.getCloseness() < 30000) {
         int newCloseness = pet.getCloseness() + (1 * c.getChannelServer().getPetExpRate());
         if (newCloseness > 30000) {
           newCloseness = 30000;
         }
         pet.setCloseness(newCloseness);
         if (newCloseness >= ExpTable.getClosenessNeededForLevel(pet.getLevel() + 1)) {
           pet.setLevel(pet.getLevel() + 1);
           c.getSession()
               .write(MaplePacketCreator.showOwnPetLevelUp(c.getPlayer().getPetIndex(pet)));
           c.getPlayer()
               .getMap()
               .broadcastMessage(
                   MaplePacketCreator.showPetLevelUp(
                       c.getPlayer(), c.getPlayer().getPetIndex(pet)));
         }
       }
     } else {
       if (gainCloseness) {
         int newCloseness = pet.getCloseness() - (1 * c.getChannelServer().getPetExpRate());
         if (newCloseness < 0) {
           newCloseness = 0;
         }
         pet.setCloseness(newCloseness);
         if (newCloseness < ExpTable.getClosenessNeededForLevel(pet.getLevel())) {
           pet.setLevel(pet.getLevel() - 1);
         }
       }
     }
     c.getSession().write(MaplePacketCreator.updatePet(pet, true));
     c.getPlayer()
         .getMap()
         .broadcastMessage(
             c.getPlayer(),
             MaplePacketCreator.commandResponse(c.getPlayer().getId(), (byte) 1, slot, true, true),
             true);
     MapleInventoryManipulator.removeById(c, MapleInventoryType.USE, itemId, 1, true, false);
   }
 }
Exemplo n.º 3
0
  public void handlePacket(SeekableLittleEndianAccessor slea, MapleClient c) {
    slea.readByte();
    slea.readInt(); // ?
    slea.readInt(); // position, but we dont need it o.o
    int oid = slea.readInt();
    MapleMapObject ob = c.getPlayer().getMap().getMapObject(oid);
    if (ob == null) {
      c.getSession().write(MaplePacketCreator.getInventoryFull());
      c.getSession().write(MaplePacketCreator.getShowInventoryFull());
      return;
    }
    if (ob instanceof MapleMapItem) {
      MapleMapItem mapitem = (MapleMapItem) ob;
      synchronized (mapitem) {
        if (mapitem.isPickedUp()) {
          c.getSession().write(MaplePacketCreator.getInventoryFull());
          c.getSession().write(MaplePacketCreator.getShowInventoryFull());
          return;
        }
        double distance = c.getPlayer().getPosition().distanceSq(mapitem.getPosition());
        c.getPlayer().getCheatTracker().checkPickupAgain();
        if (distance > 90000.0) { // 300^2, 550 is approximatly the range of ultis
          // AutobanManager.getInstance().addPoints(c, 100, 300000, "Itemvac");
          c.getPlayer().getCheatTracker().registerOffense(CheatingOffense.ITEMVAC);
          // Double.valueOf(Math.sqrt(distance))
        } else if (distance > 30000.0) {
          // log.warn("[h4x] Player {} is picking up an item that's fairly far away: {}",
          // c.getPlayer().getName(), Double.valueOf(Math.sqrt(distance)));
          c.getPlayer().getCheatTracker().registerOffense(CheatingOffense.SHORT_ITEMVAC);
        }

        if (mapitem.getMeso() > 0) {
          if (c.getPlayer().getParty() != null) {
            ChannelServer cserv = c.getChannelServer();
            int mesoAmm = mapitem.getMeso();
            int partyNum = 0;
            Collection<MaplePartyCharacter> partyMembers = c.getPlayer().getParty().getMembers();
            for (MaplePartyCharacter partyMember : partyMembers) {
              if (partyMember.getMapid() == c.getPlayer().getMapId()) {
                partyNum++;
              }
            }
            int mesoGain = mesoAmm / partyNum;
            for (MaplePartyCharacter partyMember : partyMembers) {
              if (partyMember.getMapid() == c.getPlayer().getMapId()) {
                cserv
                    .getPlayerStorage()
                    .getCharacterById(partyMember.getId())
                    .gainMeso(mesoGain, true, true);
              }
            }
          } else {
            c.getPlayer().gainMeso(mapitem.getMeso(), true, true);
          }
          c.getPlayer()
              .getMap()
              .broadcastMessage(
                  MaplePacketCreator.removeItemFromMap(
                      mapitem.getObjectId(), 2, c.getPlayer().getId()),
                  mapitem.getPosition());
          c.getPlayer().getCheatTracker().pickupComplete();
          c.getPlayer().getMap().removeMapObject(ob);
        } else {
          StringBuilder logInfo = new StringBuilder("Picked up by ");
          logInfo.append(c.getPlayer().getName());
          if (mapitem.getItem().getItemId() >= 5000000
              && mapitem.getItem().getItemId() <= 5000100) {
            int petId = MaplePet.createPet(mapitem.getItem().getItemId());
            if (petId == -1) {
              return;
            }
            MapleInventoryManipulator.addById(
                c,
                mapitem.getItem().getItemId(),
                mapitem.getItem().getQuantity(),
                "Cash Item was purchased.",
                null,
                petId);
            c.getPlayer()
                .getMap()
                .broadcastMessage(
                    MaplePacketCreator.removeItemFromMap(
                        mapitem.getObjectId(), 2, c.getPlayer().getId()),
                    mapitem.getPosition());
            c.getPlayer().getCheatTracker().pickupComplete();
            c.getPlayer().getMap().removeMapObject(ob);
          } else {
            if (MapleInventoryManipulator.addFromDrop(c, mapitem.getItem(), logInfo.toString())) {
              c.getPlayer()
                  .getMap()
                  .broadcastMessage(
                      MaplePacketCreator.removeItemFromMap(
                          mapitem.getObjectId(), 2, c.getPlayer().getId()),
                      mapitem.getPosition());
              c.getPlayer().getCheatTracker().pickupComplete();
              c.getPlayer().getMap().removeMapObject(ob);
            } else {
              c.getPlayer().getCheatTracker().pickupComplete();
              return;
            }
            if (mapitem.getItem().getItemId() == 1002357) {
              c.getPlayer().finishAchievement(13);
            }
          }
        }
        mapitem.setPickedUp(true);
      }
    }
    c.getSession().write(MaplePacketCreator.enableActions());
  }