private void destroy(final int var, QuestEnv env) {
    final int targetObjectId = env.getVisibleObject().getObjectId();

    final Player player = env.getPlayer();
    PacketSendUtility.sendPacket(
        player, new SM_USE_OBJECT(player.getObjectId(), targetObjectId, 3000, 1));
    PacketSendUtility.broadcastPacket(
        player, new SM_EMOTION(player, EmotionType.NEUTRALMODE2, 0, targetObjectId), true);
    ThreadPoolManager.getInstance()
        .schedule(
            new Runnable() {
              @Override
              public void run() {
                if (player.getTarget().getObjectId() != targetObjectId) return;
                PacketSendUtility.sendPacket(
                    player, new SM_USE_OBJECT(player.getObjectId(), targetObjectId, 3000, 0));
                PacketSendUtility.broadcastPacket(
                    player,
                    new SM_EMOTION(player, EmotionType.START_LOOT, 0, targetObjectId),
                    true);
                PacketSendUtility.broadcastPacket(
                    player.getTarget(),
                    new SM_EMOTION((Creature) player.getTarget(), EmotionType.EMOTE, 128, 0));
                QuestState qs = player.getQuestStateList().getQuestState(questId);
                if (var == -1) PacketSendUtility.sendPacket(player, new SM_PLAY_MOVIE(0, 56));
                qs.setQuestVarById(0, qs.getQuestVarById(0) + 1);
                updateQuestStatus(player, qs);
              }
            },
            3000);
  }
  @Override
  public HandlerResult onItemUseEvent(final QuestEnv env, Item item) {
    final Player player = env.getPlayer();
    final int itemObjId = item.getObjectId();
    final int id = item.getItemTemplate().getTemplateId();
    QuestState qs = player.getQuestStateList().getQuestState(questId);

    if (id != 182201765) {
      return HandlerResult.UNKNOWN;
    }

    if (qs != null) {
      if (qs.getStatus() == QuestStatus.COMPLETE) {
        removeQuestItem(env, 182201765, 1);
        return HandlerResult.FAILED;
      }
    }

    PacketSendUtility.broadcastPacket(
        player, new SM_ITEM_USAGE_ANIMATION(player.getObjectId(), itemObjId, id, 3000, 0, 0), true);
    ThreadPoolManager.getInstance()
        .schedule(
            new Runnable() {
              @Override
              public void run() {
                PacketSendUtility.broadcastPacket(
                    player,
                    new SM_ITEM_USAGE_ANIMATION(player.getObjectId(), itemObjId, id, 0, 1, 0),
                    true);
                sendQuestDialog(env, 4);
              }
            },
            3000);
    return HandlerResult.SUCCESS;
  }
  private void destroy(final int var, final QuestEnv env) {
    final int targetObjectId = env.getVisibleObject().getObjectId();

    final Player player = env.getPlayer();
    PacketSendUtility.sendPacket(
        player, new SM_USE_OBJECT(player.getObjectId(), targetObjectId, 3000, 1));
    PacketSendUtility.broadcastPacket(
        player, new SM_EMOTION(player, EmotionType.NEUTRALMODE2, 0, targetObjectId), true);
    ThreadPoolManager.getInstance()
        .schedule(
            new Runnable() {

              @Override
              public void run() {
                if (player.getTarget() == null
                    || player.getTarget().getObjectId() != targetObjectId) return;
                PacketSendUtility.sendPacket(
                    player, new SM_USE_OBJECT(player.getObjectId(), targetObjectId, 3000, 0));
                PacketSendUtility.broadcastPacket(
                    player,
                    new SM_EMOTION(player, EmotionType.START_LOOT, 0, targetObjectId),
                    true);
                sendEmotion(env, player, EmotionId.STAND, true);
                QuestState qs = player.getQuestStateList().getQuestState(questId);
                if (var != -1) qs.setQuestVarById(0, var);
                else {
                  playQuestMovie(env, 21);
                  qs.setStatus(QuestStatus.REWARD);
                }
                updateQuestStatus(env);
              }
            },
            3000);
  }
  @Override
  public void loadCraftCooldowns(final Player player) {
    Connection con = null;
    FastMap<Integer, Long> craftCoolDowns = new FastMap<Integer, Long>();
    try {
      con = DatabaseFactory.getConnection();
      PreparedStatement stmt = con.prepareStatement(SELECT_QUERY);

      stmt.setInt(1, player.getObjectId());
      ResultSet rset = stmt.executeQuery();

      while (rset.next()) {
        int delayId = rset.getInt("delay_id");
        long reuseTime = rset.getLong("reuse_time");
        int delay = (int) ((reuseTime - System.currentTimeMillis()) / 1000);

        if (delay > 0) {
          craftCoolDowns.put(delayId, reuseTime);
        }
      }
      player.getCraftCooldownList().setCraftCoolDowns(craftCoolDowns);
      rset.close();
      stmt.close();
    } catch (SQLException e) {
      log.error("LoadcraftCoolDowns", e);
    } finally {
      DatabaseFactory.close(con);
    }
  }
  @Override
  protected void startUsage(Creature creature) {
    Player player = (Player) creature;
    // TODO extract method
    Creature target = creature.getTarget();
    if (target == null) {
      return;
    }

    final int unk = 0;

    PacketSendUtility.broadcastPacket(
        player,
        new SM_CASTSPELL(
            player.getObjectId(),
            getSkillId(),
            getSkillTemplate().getLevel(),
            unk,
            target.getObjectId(),
            getSkillTemplate().getDuration()),
        true);

    // TODO magic skill >1 lvl can be instant - check from template "type"
    schedulePerformAction(player, getSkillTemplate().getDuration());
  }
  @Override
  public void storeCraftCooldowns(final Player player) {
    deleteCraftCoolDowns(player);
    Map<Integer, Long> craftCoolDowns = player.getCraftCooldownList().getCraftCoolDowns();

    if (craftCoolDowns == null) {
      return;
    }

    for (Map.Entry<Integer, Long> entry : craftCoolDowns.entrySet()) {
      final int delayId = entry.getKey();
      final long reuseTime = entry.getValue();

      if (reuseTime < System.currentTimeMillis()) {
        continue;
      }

      Connection con = null;

      try {
        con = DatabaseFactory.getConnection();
        PreparedStatement stmt = con.prepareStatement(INSERT_QUERY);

        stmt.setInt(1, player.getObjectId());
        stmt.setInt(2, delayId);
        stmt.setLong(3, reuseTime);
        stmt.execute();
      } catch (SQLException e) {
        log.error("storecraftCoolDowns", e);
      } finally {
        DatabaseFactory.close(con);
      }
    }
  }
Exemple #7
0
  @Override
  public boolean onItemUseEvent(QuestEnv env, Item item) {
    final Player player = env.getPlayer();
    final int id = item.getItemTemplate().getTemplateId();
    final int itemObjId = item.getObjectId();

    if (id != 182200007) return false;
    if (!zoneService.isInsideZone(player, ZoneName.ITEMUSE_Q1006)) return false;
    final QuestState qs = player.getQuestStateList().getQuestState(questId);
    if (qs == null) return false;
    PacketSendUtility.broadcastPacket(
        player, new SM_ITEM_USAGE_ANIMATION(player.getObjectId(), itemObjId, id, 3000, 0, 0), true);
    ThreadPoolManager.getInstance()
        .schedule(
            new Runnable() {
              @Override
              public void run() {
                PacketSendUtility.broadcastPacket(
                    player,
                    new SM_ITEM_USAGE_ANIMATION(player.getObjectId(), itemObjId, id, 0, 1, 0),
                    true);
                player.getInventory().removeFromBagByObjectId(itemObjId, 1);
                itemService.addItems(
                    player, Collections.singletonList(new QuestItems(182200008, 1)));
                qs.setQuestVarById(0, 2);
                updateQuestStatus(player, qs);
              }
            },
            3000);
    return true;
  }
  @Override
  public HandlerResult onItemUseEvent(final QuestEnv env, Item item) {
    final Player player = env.getPlayer();
    final int id = item.getItemTemplate().getTemplateId();
    final int itemObjId = item.getObjectId();

    if (id != 182204123) {
      return HandlerResult.UNKNOWN;
    }
    PacketSendUtility.broadcastPacket(
        player, new SM_ITEM_USAGE_ANIMATION(player.getObjectId(), itemObjId, id, 3000, 0, 0), true);
    ThreadPoolManager.getInstance()
        .schedule(
            new Runnable() {
              @Override
              public void run() {
                PacketSendUtility.broadcastPacket(
                    player,
                    new SM_ITEM_USAGE_ANIMATION(player.getObjectId(), itemObjId, id, 0, 1, 0),
                    true);
                sendQuestDialog(env, 4);
              }
            },
            3000);
    return HandlerResult.SUCCESS;
  }
  @Override
  public void act(Player player, Item parentItem, Item targetItem) {
    if (!player.getInventory().decreaseByObjectId(parentItem.getObjectId(), 1)) {
      return;
    }
    if (targetItem.getItemSkinTemplate().isItemDyePermitted()) {
      if (getColorBGRA() == 0) {
        targetItem.setItemColor(0);
        targetItem.setColorExpireTime(0);
      } else {
        targetItem.setItemColor(parentItem.getItemTemplate().getTemplateId());
        if (minutes != null) {
          targetItem.setColorExpireTime((int) (System.currentTimeMillis() / 1000 + minutes * 60));
        }
      }

      // item is equipped, so need broadcast packet
      if (player.getEquipment().getEquippedItemByObjId(targetItem.getObjectId()) != null) {
        PacketSendUtility.broadcastPacket(
            player,
            new SM_UPDATE_PLAYER_APPEARANCE(
                player.getObjectId(), player.getEquipment().getEquippedForApparence()),
            true);
        player.getEquipment().setPersistentState(PersistentState.UPDATE_REQUIRED);
      } // item is not equipped
      else {
        player.getInventory().setPersistentState(PersistentState.UPDATE_REQUIRED);
      }

      ItemPacketService.updateItemAfterInfoChange(player, targetItem);
    }
  }
  @Override
  public HandlerResult onItemUseEvent(final QuestEnv env, Item item) {
    final Player player = env.getPlayer();
    final QuestState qs = player.getQuestStateList().getQuestState(questId);
    final int id = item.getItemTemplate().getTemplateId();
    final int itemObjId = item.getObjectId();

    if (id != 182206726) {
      return HandlerResult.UNKNOWN;
    }
    PacketSendUtility.broadcastPacket(
        player, new SM_ITEM_USAGE_ANIMATION(player.getObjectId(), itemObjId, id, 1000, 0, 0), true);
    ThreadPoolManager.getInstance()
        .schedule(
            new Runnable() {
              @Override
              public void run() {
                PacketSendUtility.broadcastPacket(
                    player,
                    new SM_ITEM_USAGE_ANIMATION(player.getObjectId(), itemObjId, id, 0, 1, 0),
                    true);
                removeQuestItem(env, 182206726, 1);
                qs.setStatus(QuestStatus.REWARD);
                updateQuestStatus(env);
              }
            },
            1000);
    return HandlerResult.SUCCESS;
  }
 @Override
 public boolean onDialogEvent(QuestEnv env) {
   final Player player = env.getPlayer();
   int targetId = 0;
   if (env.getVisibleObject() instanceof Npc) targetId = ((Npc) env.getVisibleObject()).getNpcId();
   final QuestState qs = player.getQuestStateList().getQuestState(questId);
   if (qs == null) {
     if (targetId == 205130) {
       if (env.getDialogId() == 25)
         return sendQuestDialog(player, env.getVisibleObject().getObjectId(), 1011);
       else return defaultQuestStartDialog(env);
     }
   } else if (qs.getStatus() == QuestStatus.START) {
     switch (targetId) {
       case 730107:
         {
           if (qs.getQuestVarById(0) == 0 && env.getDialogId() == -1) {
             final int targetObjectId = env.getVisibleObject().getObjectId();
             PacketSendUtility.sendPacket(
                 player, new SM_USE_OBJECT(player.getObjectId(), targetObjectId, 3000, 1));
             PacketSendUtility.broadcastPacket(
                 player, new SM_EMOTION(player, 37, 0, targetObjectId), true);
             ThreadPoolManager.getInstance()
                 .schedule(
                     new Runnable() {
                       @Override
                       public void run() {
                         if (!player.isTargeting(targetObjectId)) return;
                         PacketSendUtility.sendPacket(
                             player,
                             new SM_USE_OBJECT(player.getObjectId(), targetObjectId, 3000, 0));
                         PacketSendUtility.broadcastPacket(
                             player, new SM_EMOTION(player, 38, 0, targetObjectId), true);
                         qs.setQuestVarById(0, 1);
                         updateQuestStatus(player, qs);
                       }
                     },
                     3000);
           }
         }
       case 205130:
         {
           if (qs.getQuestVarById(0) == 1) {
             if (env.getDialogId() == 25)
               return sendQuestDialog(player, env.getVisibleObject().getObjectId(), 2375);
             else if (env.getDialogId() == 1009) {
               qs.setStatus(QuestStatus.REWARD);
               updateQuestStatus(player, qs);
               return defaultQuestEndDialog(env);
             } else return defaultQuestEndDialog(env);
           }
         }
     }
   } else if (qs.getStatus() == QuestStatus.REWARD) {
     if (targetId == 205130) return defaultQuestEndDialog(env);
   }
   return false;
 }
 /**
  * Called by client when some action is performed or on finish gathering Called by move observer
  * on player move
  *
  * @param player
  */
 public void finishGathering(Player player) {
   if (currentGatherer == player.getObjectId()) {
     if (state == GatherState.GATHERING) {
       task.abort();
     }
     currentGatherer = 0;
     state = GatherState.IDLE;
   }
 }
  // necessary overloading to not change AbstractItemAction
  public void act(
      final Player player,
      final Item parentItem,
      final Item targetItem,
      final Item supplementItem) {
    PacketSendUtility.sendPacket(
        player,
        new SM_ITEM_USAGE_ANIMATION(
            player.getObjectId(),
            parentItem.getObjectId(),
            parentItem.getItemTemplate().getTemplateId(),
            5000,
            0,
            0));
    player.cancelTask(TaskId.ITEM_USE);
    player.addNewTask(
        TaskId.ITEM_USE,
        ThreadPoolManager.getInstance()
            .schedule(
                new Runnable() {
                  @Override
                  public void run() {

                    int itemId = parentItem.getItemTemplate().getTemplateId();
                    if (itemId > 166000000 && itemId < 167000000) {
                      boolean result =
                          EnchantService.enchantItem(
                              player, parentItem, targetItem, supplementItem);
                      PacketSendUtility.sendPacket(
                          player,
                          new SM_ITEM_USAGE_ANIMATION(
                              player.getObjectId(),
                              parentItem.getObjectId(),
                              parentItem.getItemTemplate().getTemplateId(),
                              0,
                              result ? 1 : 2,
                              0));
                    } else {
                      boolean result =
                          EnchantService.socketManastone(
                              player, parentItem, targetItem, supplementItem);
                      PacketSendUtility.sendPacket(
                          player,
                          new SM_ITEM_USAGE_ANIMATION(
                              player.getObjectId(),
                              parentItem.getObjectId(),
                              parentItem.getItemTemplate().getTemplateId(),
                              0,
                              result ? 1 : 2,
                              0));
                    }
                  }
                },
                5000));
  }
 /**
  * Constructs new <tt>SM_MESSAGE </tt> packet
  *
  * @param player who sent message
  * @param message actual message
  * @param chatType what chat type should be used
  */
 public SM_MESSAGE(Player player, String message, ChatType chatType) {
   this.player = player;
   this.senderObjectId = player.getObjectId();
   this.senderName = player.getName();
   this.message = message;
   this.race = player.getCommonData().getRace();
   this.chatType = chatType;
   this.x = player.getX();
   this.y = player.getY();
   this.z = player.getZ();
 }
  /**
   * This method is used for creating new players
   *
   * @param playerCommonData
   * @param playerAppearance
   * @return Player
   */
  public static Player newPlayer(
      PlayerCommonData playerCommonData, PlayerAppearance playerAppearance) {
    PlayerInitialData playerInitialData = DataManager.PLAYER_INITIAL_DATA;
    LocationData ld = playerInitialData.getSpawnLocation(playerCommonData.getRace());

    WorldPosition position =
        World.getInstance()
            .createPosition(ld.getMapId(), ld.getX(), ld.getY(), ld.getZ(), ld.getHeading());
    playerCommonData.setPosition(position);

    Player newPlayer = new Player(new PlayerController(), playerCommonData, playerAppearance);

    // Starting skills
    SkillLearnService.addNewSkills(newPlayer, true);

    // Starting items
    PlayerCreationData playerCreationData =
        playerInitialData.getPlayerCreationData(playerCommonData.getPlayerClass());

    List<ItemType> items = playerCreationData.getItems();

    Storage playerInventory = new Storage(StorageType.CUBE);
    Storage regularWarehouse = new Storage(StorageType.REGULAR_WAREHOUSE);
    Storage accountWarehouse = new Storage(StorageType.ACCOUNT_WAREHOUSE);

    Equipment equipment = new Equipment(newPlayer);
    newPlayer.setStorage(playerInventory, StorageType.CUBE);
    newPlayer.setStorage(regularWarehouse, StorageType.REGULAR_WAREHOUSE);
    newPlayer.setStorage(accountWarehouse, StorageType.ACCOUNT_WAREHOUSE);
    newPlayer.setEquipment(equipment);
    newPlayer.setMailbox(new Mailbox());

    for (ItemType itemType : items) {
      int itemId = itemType.getTemplate().getTemplateId();
      Item item = ItemService.newItem(itemId, itemType.getCount());
      if (item == null) continue;

      // When creating new player - all equipment that has slot values will be equipped
      // Make sure you will not put into xml file more items than possible to equip.
      ItemTemplate itemTemplate = item.getItemTemplate();
      item.setOwnerId(newPlayer.getObjectId());
      if (itemTemplate.isArmor() || itemTemplate.isWeapon()) {
        item.setEquipped(true);
        List<ItemSlot> itemSlots = ItemSlot.getSlotsFor(itemTemplate.getItemSlot());
        item.setEquipmentSlot(itemSlots.get(0).getSlotIdMask());
        equipment.onLoadHandler(item);
      } else ItemService.onLoadHandler(newPlayer, newPlayer.getInventory(), item);
    }
    equipment.onLoadApplyEquipmentStats();
    /** Mark inventory and equipment as UPDATE_REQUIRED to be saved during character creation */
    playerInventory.setPersistentState(PersistentState.UPDATE_REQUIRED);
    equipment.setPersistentState(PersistentState.UPDATE_REQUIRED);
    return newPlayer;
  }
  /**
   * This method is called when player leaves the game, which includes just two cases: either player
   * goes back to char selection screen or it's leaving the game [closing client].<br>
   * <br>
   * <b><font color='red'>NOTICE: </font> This method is called only from {@link AionConnection} and
   * {@link CM_QUIT} and must not be called from anywhere else</b>
   *
   * @param player
   */
  public static void playerLoggedOut(final Player player) {
    log.info(
        "Player logged out: "
            + player.getName()
            + " Account: "
            + player.getClientConnection().getAccount().getName());
    player.onLoggedOut();

    // Update prison timer
    if (player.isInPrison()) {
      long prisonTimer = System.currentTimeMillis() - player.getStartPrison();
      prisonTimer = player.getPrisonTimer() - prisonTimer;

      player.setPrisonTimer(prisonTimer);

      log.debug("Update prison timer to " + prisonTimer / 1000 + " seconds !");
    }

    // store current effects
    DAOManager.getDAO(PlayerEffectsDAO.class).storePlayerEffects(player);
    DAOManager.getDAO(ItemCooldownsDAO.class).storeItemCooldowns(player);
    DAOManager.getDAO(PlayerLifeStatsDAO.class).updatePlayerLifeStat(player);
    player.getEffectController().removeAllEffects();

    player.getLifeStats().cancelAllTasks();

    if (player.getLifeStats().isAlreadyDead()) TeleportService.moveToBindLocation(player, false);

    if (DuelService.getInstance().isDueling(player.getObjectId()))
      DuelService.getInstance().loseDuel(player);

    if (player.getSummon() != null) player.getSummon().getController().release(UnsummonType.LOGOUT);

    PunishmentService.stopPrisonTask(player, true);

    player.getCommonData().setOnline(false);
    player.getCommonData().setLastOnline(new Timestamp(System.currentTimeMillis()));

    player.setClientConnection(null);

    if (player.isLegionMember()) LegionService.getInstance().onLogout(player);

    if (player.isInGroup()) GroupService.getInstance().scheduleRemove(player);

    if (player.isInAlliance()) AllianceService.getInstance().onLogout(player);

    player.getController().delete();
    DAOManager.getDAO(PlayerDAO.class).onlinePlayer(player, false);

    if (!GSConfig.DISABLE_CHAT_SERVER) ChatService.onPlayerLogout(player);

    storePlayer(player);
    player.getEquipment().setOwner(null);
  }
 @Override
 public void insertPlayerGameStat(final Player player) {
   Connection con = null;
   try {
     con = DatabaseFactory.getConnection();
     PreparedStatement stmt = con.prepareStatement(INSERT_QUERY);
     stmt.setInt(1, player.getObjectId());
     stmt.setInt(2, player.getGameStats().getPDef().getCurrent());
     stmt.setInt(3, player.getGameStats().getBlock().getCurrent());
     stmt.setInt(4, player.getGameStats().getParry().getCurrent());
     stmt.setInt(5, player.getGameStats().getMCritical().getCurrent());
     stmt.setInt(6, player.getGameStats().getWill().getCurrent());
     stmt.setInt(7, player.getGameStats().getAccuracy().getCurrent());
     stmt.setInt(8, player.getGameStats().getPower().getCurrent());
     stmt.setInt(9, player.getGameStats().getMainHandMAccuracy().getCurrent());
     stmt.setInt(10, player.getGameStats().getAttackSpeed().getCurrent());
     stmt.setInt(11, player.getGameStats().getMResist().getCurrent());
     stmt.setInt(12, player.getGameStats().getMainHandMAttack().getCurrent());
     stmt.setInt(13, player.getGameStats().getMainHandPCritical().getCurrent());
     stmt.setInt(14, player.getGameStats().getAttackRange().getCurrent());
     stmt.setInt(15, player.getGameStats().getMDef().getCurrent());
     stmt.setInt(16, player.getGameStats().getAgility().getCurrent());
     stmt.setInt(17, player.getGameStats().getKnowledge().getCurrent());
     stmt.setInt(18, player.getGameStats().getWill().getCurrent());
     stmt.setInt(19, player.getGameStats().getMBoost().getCurrent());
     stmt.setInt(20, player.getGameStats().getMBResist().getCurrent());
     stmt.setInt(21, player.getGameStats().getPCR().getCurrent());
     stmt.setInt(22, player.getGameStats().getMCR().getCurrent());
     stmt.execute();
     stmt.close();
   } catch (Exception e) {
     log.error(
         "Could not store PlayerGameStat data for player "
             + player.getObjectId()
             + " from DB: "
             + e.getMessage(),
         e);
   } finally {
     DatabaseFactory.close(con);
   }
 }
  private void deleteCraftCoolDowns(final Player player) {
    Connection con = null;

    try {
      con = DatabaseFactory.getConnection();
      PreparedStatement stmt = con.prepareStatement(DELETE_QUERY);

      stmt.setInt(1, player.getObjectId());
      stmt.execute();
    } catch (SQLException e) {
      log.error("deletecraftCoolDowns", e);
    } finally {
      DatabaseFactory.close(con);
    }
  }
  @Override
  public boolean onItemUseEvent(QuestEnv env, Item item) {
    final Player player = env.getPlayer();
    final int id = item.getItemTemplate().getTemplateId();
    final int itemObjId = item.getObjectId();

    final QuestState qs = player.getQuestStateList().getQuestState(questId);
    if (qs != null && qs.getStatus() != QuestStatus.NONE) return false;
    if (id != 182200214) return false;

    PacketSendUtility.broadcastPacket(
        player, new SM_ITEM_USAGE_ANIMATION(player.getObjectId(), itemObjId, id, 20, 1, 0), true);
    sendQuestDialog(player, 0, 4);

    return true;
  }
 @Override
 public void removePlayerPet(Player player, int petId) {
   Connection con = null;
   try {
     con = DatabaseFactory.getConnection();
     PreparedStatement stmt =
         con.prepareStatement("DELETE FROM player_pets WHERE player_id = ? AND pet_id = ?");
     stmt.setInt(1, player.getObjectId());
     stmt.setInt(2, petId);
     stmt.execute();
     stmt.close();
   } catch (Exception e) {
     log.error("Error removing pet #" + petId, e);
   } finally {
     DatabaseFactory.close(con);
   }
 }
  @Override
  protected void changeLeaderTo(final Player player) {
    team.changeLeader(team.getMember(player.getObjectId()));
    team.applyOnMembers(
        new Predicate<Player>() {

          @Override
          public boolean apply(Player member) {
            PacketSendUtility.sendPacket(member, new SM_GROUP_INFO(team));
            if (!player.equals(member)) {
              PacketSendUtility.sendPacket(
                  member, SM_SYSTEM_MESSAGE.STR_PARTY_HE_IS_NEW_LEADER(player.getName()));
            } else {
              PacketSendUtility.sendPacket(
                  member, SM_SYSTEM_MESSAGE.STR_PARTY_YOU_BECOME_NEW_LEADER);
            }
            return true;
          }
        });
  }
 @Override
 public void insertPlayerPet(Player player, int petId, int decoration, String name) {
   Connection con = null;
   try {
     con = DatabaseFactory.getConnection();
     PreparedStatement stmt =
         con.prepareStatement(
             "INSERT INTO player_pets(player_id, pet_id, decoration, name) VALUES(?, ?, ?, ?)");
     stmt.setInt(1, player.getObjectId());
     stmt.setInt(2, petId);
     stmt.setInt(3, decoration);
     stmt.setString(4, name);
     stmt.execute();
     stmt.close();
   } catch (Exception e) {
     log.error("Error inserting new pet #" + petId + "[" + name + "]", e);
   } finally {
     DatabaseFactory.close(con);
   }
 }
  @Override
  public void act(Player player, Item parentItem, Item targetItem) {
    if (!player.getInventory().decreaseByObjectId(parentItem.getObjectId(), 1)) {
      return;
    }
    ItemTemplate itemTemplate = parentItem.getItemTemplate();
    PacketSendUtility.broadcastPacket(
        player,
        new SM_ITEM_USAGE_ANIMATION(
            player.getObjectId(), parentItem.getObjectId(), itemTemplate.getTemplateId()),
        true);

    switch (storage) {
      case CUBE:
        CubeExpandService.expand(player, false);
        break;
      case WAREHOUSE:
        WarehouseService.expand(player);
        break;
    }
  }
  @Override
  protected void performAction(Creature creature) {
    Player player = (Player) creature;
    // TODO extract method
    Creature target = creature.getTarget();
    if (target == null) {
      return;
    }

    // TODO investigate unk
    int unk = 0;

    Map<String, Integer> incluenceResult = new HashMap<String, Integer>();

    SkillEffectData skillEffectData = getSkillTemplate().getSkillEffectData();
    if (skillEffectData != null) {
      for (SkillEffectTemplate effectTemplate : skillEffectData.getSkillEffects()) {
        AbstractEffect effect = SkillEffectType.getEffectByName(effectTemplate);
        // TODO player.onEffectInfluence(AbstractEffect)
        int result = effect.influence(player, target);
        log.error("putting into map: " + result + " " + effectTemplate.getName());
        incluenceResult.put(effectTemplate.getName(), result);
      }
    }

    target.getController().onAttack(player);

    // TODO now spell ends after damage is done.
    PacketSendUtility.broadcastPacket(
        player,
        new SM_CASTSPELL_END(
            player.getObjectId(),
            getSkillId(),
            getSkillTemplate().getLevel(),
            unk,
            target.getObjectId(),
            incluenceResult.get(SkillEffectType.DAMAGE.getName())),
        true);
  }
  /** @param player */
  public static void onPlayerLogin(Player player) {
    int worldId = player.getWorldId();

    WorldMapTemplate worldTemplate = DataManager.WORLD_MAPS_DATA.getTemplate(worldId);
    if (worldTemplate.isInstance()) {
      PortalTemplate portalTemplate =
          DataManager.PORTAL_DATA.getInstancePortalTemplate(
              worldId, player.getCommonData().getRace());

      if (portalTemplate == null) {
        log.error("No portal template found for " + worldId);
        return;
      }

      int lookupId = player.getObjectId();
      if (portalTemplate.isGroup() && player.getPlayerGroup() != null) {
        lookupId = player.getPlayerGroup().getGroupId();
      }

      WorldMapInstance registeredInstance = getRegisteredInstance(worldId, lookupId);
      if (registeredInstance != null) {
        World.getInstance()
            .setPosition(
                player,
                worldId,
                registeredInstance.getInstanceId(),
                player.getX(),
                player.getY(),
                player.getZ(),
                player.getHeading());
        return;
      }

      moveToEntryPoint(player, portalTemplate, false);
    }
  }
  @Override
  public boolean onItemUseEvent(QuestEnv env, Item item) {
    final Player player = env.getPlayer();
    final int id = item.getItemTemplate().getTemplateId();
    final int itemObjId = item.getObjectId();

    if (id != 182200501) return false;
    PacketSendUtility.broadcastPacket(
        player, new SM_ITEM_USAGE_ANIMATION(player.getObjectId(), itemObjId, id, 3000, 0, 0), true);
    ThreadPoolManager.getInstance()
        .schedule(
            new Runnable() {
              @Override
              public void run() {
                PacketSendUtility.broadcastPacket(
                    player,
                    new SM_ITEM_USAGE_ANIMATION(player.getObjectId(), itemObjId, id, 0, 1, 0),
                    true);
                sendQuestDialog(player, 0, 4);
              }
            },
            3000);
    return true;
  }
  @Override
  public boolean onDialogEvent(QuestEnv env) {
    final Player player = env.getPlayer();
    int targetId = 0;
    final QuestState qs = player.getQuestStateList().getQuestState(questId);
    if (env.getVisibleObject() instanceof Npc) targetId = ((Npc) env.getVisibleObject()).getNpcId();

    if (targetId == 0) {
      if (qs == null || qs.getStatus() == QuestStatus.NONE) {
        if (env.getDialogId() == 1002) {
          questService.startQuest(env, QuestStatus.START);
          if (!itemService.addItems(
              player, Collections.singletonList(new QuestItems(182200226, 1)))) ;
          player.getInventory().removeFromBagByItemId(182200214, 1);
          PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(0, 0));
          return true;
        } else PacketSendUtility.sendPacket(player, new SM_DIALOG_WINDOW(0, 0));
      }
    }

    if (qs == null) return false;

    int var = qs.getQuestVarById(0);

    if (qs.getStatus() == QuestStatus.REWARD) {
      if (targetId == 203075 && var == 4) {
        if (env.getDialogId() == -1)
          return sendQuestDialog(player, env.getVisibleObject().getObjectId(), 2375);
        else if (env.getDialogId() == 1009)
          return sendQuestDialog(player, env.getVisibleObject().getObjectId(), 6);
        else return defaultQuestEndDialog(env);
      } else if (targetId == 203058 && var == 3) return defaultQuestEndDialog(env);
    } else if (qs.getStatus() != QuestStatus.START) return false;

    if (targetId == 203075) {
      switch (env.getDialogId()) {
        case 25:
          if (var == 0) return sendQuestDialog(player, env.getVisibleObject().getObjectId(), 1011);
          else if (var == 2)
            return sendQuestDialog(player, env.getVisibleObject().getObjectId(), 1693);
          else if (var == 3)
            return sendQuestDialog(player, env.getVisibleObject().getObjectId(), 2375);

        case 1009:
          if (var == 2) {
            qs.setQuestVarById(0, var + 2);
            qs.setStatus(QuestStatus.REWARD);
            updateQuestStatus(player, qs);
            player.getInventory().removeFromBagByItemId(182200217, 1);
            return sendQuestDialog(player, env.getVisibleObject().getObjectId(), 6);
          }
          if (var == 3) {
            qs.setQuestVarById(0, var + 1);
            qs.setStatus(QuestStatus.REWARD);
            updateQuestStatus(player, qs);
            player.getInventory().removeFromBagByItemId(182200217, 1);
            return sendQuestDialog(player, env.getVisibleObject().getObjectId(), 6);
          }
        case 10000:
          if (var == 0) {
            qs.setQuestVarById(0, var + 1);
            updateQuestStatus(player, qs);
            player.getInventory().removeFromBagByItemId(182200226, 1);
            PacketSendUtility.sendPacket(
                player, new SM_DIALOG_WINDOW(env.getVisibleObject().getObjectId(), 10));
            return true;
          }
        case 10001:
          if (var == 2) {
            qs.setQuestVarById(0, var + 1);
            updateQuestStatus(player, qs);
            PacketSendUtility.sendPacket(
                player, new SM_DIALOG_WINDOW(env.getVisibleObject().getObjectId(), 10));
            return true;
          }
      }
    } else if (targetId == 700008) {
      switch (env.getDialogId()) {
        case -1:
          if (var == 1) {
            // final Npc npc = (Npc)env.getVisibleObject();
            final int targetObjectId = env.getVisibleObject().getObjectId();
            PacketSendUtility.sendPacket(
                player, new SM_USE_OBJECT(player.getObjectId(), targetObjectId, 3000, 1));
            PacketSendUtility.broadcastPacket(
                player, new SM_EMOTION(player, 37, 0, targetObjectId), true);
            ThreadPoolManager.getInstance()
                .schedule(
                    new Runnable() {
                      @Override
                      public void run() {
                        PacketSendUtility.sendPacket(
                            player,
                            new SM_USE_OBJECT(player.getObjectId(), targetObjectId, 3000, 0));
                        PacketSendUtility.broadcastPacket(
                            player, new SM_EMOTION(player, 38, 0, targetObjectId), true);
                        for (VisibleObject obj : player.getKnownList()) {
                          if (!(obj instanceof Npc)) continue;
                          if (((Npc) obj).getNpcId() != 203175) continue;
                          if (((Npc) obj).getNpcId() != 203175) return;
                          ((Npc) obj).getAggroList().addDamage(player, 50);
                        }
                        if (!itemService.addItems(
                            player, Collections.singletonList(new QuestItems(182200217, 1)))) ;
                        qs.setQuestVarById(0, 2);
                        updateQuestStatus(player, qs);
                      }
                    },
                    3000);
          }
          return true;
      }
    }
    if (targetId == 203058) {
      switch (env.getDialogId()) {
        case 25:
          if (var == 3) return sendQuestDialog(player, env.getVisibleObject().getObjectId(), 2034);
        case 10002:
          if (var == 3) {
            qs.setStatus(QuestStatus.REWARD);
            updateQuestStatus(player, qs);
            player.getInventory().removeFromBagByItemId(182200217, 1);
            return sendQuestDialog(player, env.getVisibleObject().getObjectId(), 5);
          }
        case 10001:
          if (var == 3) {
            PacketSendUtility.sendPacket(
                player, new SM_DIALOG_WINDOW(env.getVisibleObject().getObjectId(), 10));
            return true;
          }
      }
    }
    return false;
  }
 public void showWindow(Player player, byte instanceMaskId) {
   if (!playersWithCooldown.contains(player.getObjectId())) {
     PacketSendUtility.sendPacket(player, new SM_AUTO_GROUP(instanceMaskId));
   }
 }
 public boolean hasCoolDown(Player player) {
   return playersWithCooldown.contains(player.getObjectId());
 }
 public void addCoolDown(Player player) {
   playersWithCooldown.add(player.getObjectId());
 }