Package commands.admin

Source Code of commands.admin.AdminSkill

package commands.admin;

import l2p.extensions.scripts.ScriptFile;
import l2p.gameserver.cache.Msg;
import l2p.gameserver.handler.AdminCommandHandler;
import l2p.gameserver.handler.IAdminCommandHandler;
import l2p.gameserver.model.L2Character;
import l2p.gameserver.model.L2Effect;
import l2p.gameserver.model.L2Object;
import l2p.gameserver.model.L2Player;
import l2p.gameserver.model.L2Skill;
import l2p.gameserver.model.L2SkillLearn;
import l2p.gameserver.serverpackets.NpcHtmlMessage;
import l2p.gameserver.serverpackets.SkillList;
import l2p.gameserver.skills.Calculator;
import l2p.gameserver.skills.Env;
import l2p.gameserver.skills.funcs.Func;
import l2p.gameserver.tables.SkillTable;
import l2p.gameserver.tables.SkillTreeTable;
import l2p.util.GArray;
import l2p.util.Log;

import java.util.Collection;
import java.util.concurrent.ConcurrentLinkedQueue;

public class AdminSkill implements IAdminCommandHandler, ScriptFile
{
  private static enum Commands
  {
    admin_show_skills,
    admin_remove_skills,
    admin_skill_list,
    admin_skill_index,
    admin_add_skill,
    admin_remove_skill,
    admin_get_skills,
    admin_reset_skills,
    admin_give_all_skills,
    admin_show_effects,
    admin_debug_stats,
    admin_remove_cooldown,
    admin_buff,
    admin_callskill
  }

  private static L2Skill[] adminSkills;

  public boolean useAdminCommand(Enum comm, String[] wordList, String fullString, L2Player activeChar)
  {
    Commands command = (Commands) comm;
    if(!activeChar.getPlayerAccess().CanEditChar)
    {
      return false;
    }
    switch(command)
    {
      case admin_show_skills:
        showSkillsPage(activeChar);
        break;
      case admin_show_effects:
        showEffects(activeChar);
        break;
      case admin_remove_skills:
        removeSkillsPage(activeChar);
        break;
      case admin_skill_list:
        activeChar.sendPacket(new NpcHtmlMessage(5).setFile("data/html/admin/skills.htm"));
        break;
      case admin_skill_index:
        if(wordList.length > 1)
        {
          activeChar.sendPacket(new NpcHtmlMessage(5).setFile("data/html/admin/skills/" + wordList[1] + ".htm"));
        }
        break;
      case admin_add_skill:
        adminAddSkill(activeChar, wordList);
        break;
      case admin_remove_skill:
        adminRemoveSkill(activeChar, wordList);
        break;
      case admin_get_skills:
        adminGetSkills(activeChar);
        break;
      case admin_reset_skills:
        adminResetSkills(activeChar);
        break;
      case admin_give_all_skills:
        adminGiveAllSkills(activeChar);
        break;
      case admin_debug_stats:
        debug_stats(activeChar);
        break;
      case admin_remove_cooldown:
        activeChar.resetSkillsReuse();
        activeChar.sendMessage("Откат всех скилов обнулен.");
        break;
      case admin_buff:
        for(int i = 7041; i <= 7064; i++)
        {
          activeChar.addSkill(SkillTable.getInstance().getInfo(i, 1));
        }
        activeChar.sendPacket(new SkillList(activeChar));
        break;
      case admin_callskill:
        int skillid;
        int skilllevel;
        if(wordList.length < 3)
        {
          activeChar.sendMessage("USAGE: //callskill skillid skilllevel");
          return false;
        }
        try
        {
          skillid = Integer.parseInt(wordList[1]);
          skilllevel = Integer.parseInt(wordList[2]);
        }
        catch(Exception e)
        {
          activeChar.sendMessage("USAGE: //callskill skillid skilllevel");
          return false;
        }
        L2Skill skill = SkillTable.getInstance().getInfo(skillid, skilllevel);
        if(skill == null)
        {
          activeChar.sendMessage("USAGE: //callskill skillid skilllevel");
          return false;
        }
        L2Character target = null;
        L2Object obj = activeChar.getTarget();
        if(obj != null && obj.isCharacter())
        {
          target = (L2Character) obj;
        }
        if(target == null)
        {
          target = activeChar;
        }
        GArray<L2Character> targets = new GArray<L2Character>();
        targets.add(target);
        activeChar.callSkill(skill, targets, false);
        break;
    }
    return true;
  }

  private void debug_stats(L2Player activeChar)
  {
    L2Object target_obj = activeChar.getTarget();
    if(!target_obj.isCharacter())
    {
      activeChar.sendPacket(Msg.INVALID_TARGET);
      return;
    }
    L2Character target = (L2Character) target_obj;
    Calculator[] calculators = target.getCalculators();
    String log_str = "--- Debug for " + target.getName() + " ---\r\n";
    for(Calculator calculator : calculators)
    {
      if(calculator == null || calculator.getBase() == null)
      {
        continue;
      }
      Env env = new Env(target, activeChar, null);
      env.value = calculator.getBase();
      log_str += "Stat: " + calculator._stat.getValue() + ", limit: " + calculator._stat._max + ", prevValue: " + calculator.getLast() + "\r\n";
      Func[] funcs = calculator.getFunctions();
      for(int i = 0; i < funcs.length; i++)
      {
        String order = Integer.toHexString(funcs[i]._order).toUpperCase();
        if(order.length() == 1)
        {
          order = "0" + order;
        }
        log_str += "\tFunc #" + i + "@ [0x" + order + "]" + funcs[i].getClass().getSimpleName() + "\t" + env.value;
        if(funcs[i].getCondition() == null || funcs[i].getCondition().test(env))
        {
          funcs[i].calc(env);
        }
        log_str += " -> " + env.value + (funcs[i]._funcOwner != null ? "; owner: " + funcs[i]._funcOwner.toString() : "; no owner") + "\r\n";
      }
    }
    Log.add(log_str, "debug_stats");
  }

  /**
   * This function will give all the skills that the gm target can have at its
   * level to the traget
   *
   * @param activeChar: the gm char
   */
  private void adminGiveAllSkills(L2Player activeChar)
  {
    L2Object target = activeChar.getTarget();
    L2Player player;
    if(target != null && target.isPlayer() && (activeChar == target || activeChar.getPlayerAccess().CanEditCharAll))
    {
      player = (L2Player) target;
    }
    else
    {
      activeChar.sendPacket(Msg.INVALID_TARGET);
      return;
    }
    int unLearnable = 0;
    int skillCounter = 0;
    GArray<L2SkillLearn> skills = SkillTreeTable.getInstance().getAvailableSkills(player, player.getClassId());
    while(skills.size() > unLearnable)
    {
      unLearnable = 0;
      for(L2SkillLearn s : skills)
      {
        L2Skill sk = SkillTable.getInstance().getInfo(s.id, s.skillLevel);
        if(sk == null || !sk.getCanLearn(player.getClassId()))
        {
          unLearnable++;
          continue;
        }
        if(player.getSkillLevel(sk.getId()) == -1)
        {
          skillCounter++;
        }
        player.addSkill(sk, true);
      }
      skills = SkillTreeTable.getInstance().getAvailableSkills(player, player.getClassId());
    }
    player.sendMessage("Admin gave you " + skillCounter + " skills.");
    player.sendPacket(new SkillList(player));
    activeChar.sendMessage("You gave " + skillCounter + " skills to " + player.getName());
  }

  public Enum[] getAdminCommandEnum()
  {
    return Commands.values();
  }

  private void removeSkillsPage(L2Player activeChar)
  {
    L2Object target = activeChar.getTarget();
    L2Player player;
    if(target.isPlayer() && (activeChar == target || activeChar.getPlayerAccess().CanEditCharAll))
    {
      player = (L2Player) target;
    }
    else
    {
      activeChar.sendPacket(Msg.INVALID_TARGET);
      return;
    }
    Collection<L2Skill> skills = player.getAllSkills();
    NpcHtmlMessage adminReply = new NpcHtmlMessage(5);
    StringBuffer replyMSG = new StringBuffer("<html><body>");
    replyMSG.append("<table width=260><tr>");
    replyMSG.append("<td width=40><button value=\"Main\" action=\"bypass -h admin_admin\" width=40 height=15 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_CT1.Button_DF\"></td>");
    replyMSG.append("<td width=180><center>Character Selection Menu</center></td>");
    replyMSG.append("<td width=40><button value=\"Back\" action=\"bypass -h admin_show_skills\" width=40 height=15 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_CT1.Button_DF\"></td>");
    replyMSG.append("</tr></table>");
    replyMSG.append("<br><br>");
    replyMSG.append("<center>Editing character: " + player.getName() + "</center>");
    replyMSG.append("<br><table width=270><tr><td>Lv: " + player.getLevel() + " " + player.getTemplate().className + "</td></tr></table>");
    replyMSG.append("<br><center>Click on the skill you wish to remove:</center>");
    replyMSG.append("<br><table width=270>");
    replyMSG.append("<tr><td width=80>Name:</td><td width=60>Level:</td><td width=40>Id:</td></tr>");
    for(L2Skill element : skills)
    {
      replyMSG.append("<tr><td width=80><a action=\"bypass -h admin_remove_skill " + element.getId() + "\">" + element.getName() + "</a></td><td width=60>" + element.getLevel() + "</td><td width=40>" + element.getId() + "</td></tr>");
    }
    replyMSG.append("</table>");
    replyMSG.append("<br><center><table>");
    replyMSG.append("Remove custom skill:");
    replyMSG.append("<tr><td>Id: </td>");
    replyMSG.append("<td><edit var=\"id_to_remove\" width=110></td></tr>");
    replyMSG.append("</table></center>");
    replyMSG.append("<center><button value=\"Remove skill\" action=\"bypass -h admin_remove_skill $id_to_remove\" width=110 height=15 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_CT1.Button_DF\"></center>");
    replyMSG.append("<br><center><button value=\"Back\" action=\"bypass -h admin_current_player\" width=40 height=15></center>");
    replyMSG.append("</body></html>");
    adminReply.setHtml(replyMSG.toString());
    activeChar.sendPacket(adminReply);
  }

  private void showSkillsPage(L2Player activeChar)
  {
    L2Object target = activeChar.getTarget();
    L2Player player;
    if(target != null && target.isPlayer() && (activeChar == target || activeChar.getPlayerAccess().CanEditCharAll))
    {
      player = (L2Player) target;
    }
    else
    {
      activeChar.sendPacket(Msg.INVALID_TARGET);
      return;
    }
    NpcHtmlMessage adminReply = new NpcHtmlMessage(5);
    StringBuffer replyMSG = new StringBuffer("<html><body>");
    replyMSG.append("<table width=260><tr>");
    replyMSG.append("<td width=40><button value=\"Main\" action=\"bypass -h admin_admin\" width=40 height=15 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_CT1.Button_DF\"></td>");
    replyMSG.append("<td width=180><center>Character Selection Menu</center></td>");
    replyMSG.append("<td width=40><button value=\"Back\" action=\"bypass -h admin_current_player\" width=40 height=15 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_CT1.Button_DF\"></td>");
    replyMSG.append("</tr></table>");
    replyMSG.append("<br><br>");
    replyMSG.append("<center>Editing character: " + player.getName() + "</center>");
    replyMSG.append("<br><table width=270><tr><td>Lv: " + player.getLevel() + " " + player.getTemplate().className + "</td></tr></table>");
    replyMSG.append("<br><center><table>");
    replyMSG.append("<tr><td><button value=\"Add skills\" action=\"bypass -h admin_skill_list\" width=70 height=15 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_CT1.Button_DF\"></td>");
    replyMSG.append("<td><button value=\"Get skills\" action=\"bypass -h admin_get_skills\" width=70 height=15 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_CT1.Button_DF\"></td></tr>");
    replyMSG.append("<tr><td><button value=\"Delete skills\" action=\"bypass -h admin_remove_skills\" width=70 height=15 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_CT1.Button_DF\"></td>");
    replyMSG.append("<td><button value=\"Reset skills\" action=\"bypass -h admin_reset_skills\" width=70 height=15 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_CT1.Button_DF\"></td></tr>");
    replyMSG.append("<tr><td><button value=\"Give All Skills\" action=\"bypass -h admin_give_all_skills\" width=70 height=15 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_CT1.Button_DF\"></td></tr>");
    replyMSG.append("</table></center>");
    replyMSG.append("</body></html>");
    adminReply.setHtml(replyMSG.toString());
    activeChar.sendPacket(adminReply);
  }

  private void showEffects(L2Player activeChar)
  {
    L2Object target = activeChar.getTarget();
    L2Player player;
    if(target != null && target.isPlayer() && (activeChar == target || activeChar.getPlayerAccess().CanEditCharAll))
    {
      player = (L2Player) target;
    }
    else
    {
      activeChar.sendPacket(Msg.INVALID_TARGET);
      return;
    }
    NpcHtmlMessage adminReply = new NpcHtmlMessage(5);
    StringBuffer replyMSG = new StringBuffer("<html><body>");
    replyMSG.append("<table width=260><tr>");
    replyMSG.append("<td width=40><button value=\"Main\" action=\"bypass -h admin_admin\" width=40 height=15 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_CT1.Button_DF\"></td>");
    replyMSG.append("<td width=180><center>Character Selection Menu</center></td>");
    replyMSG.append("<td width=40><button value=\"Back\" action=\"bypass -h admin_current_player\" width=40 height=15 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_CT1.Button_DF\"></td>");
    replyMSG.append("</tr></table>");
    replyMSG.append("<br><br>");
    replyMSG.append("<center>Editing character: " + player.getName() + "</center>");
    replyMSG.append("<br><center><button value=\"");
    replyMSG.append(player.isLangRus() ? "Обновить" : "Refresh");
    replyMSG.append("\" action=\"bypass -h admin_show_effects\" width=100 height=15 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_CT1.Button_DF\" /></center>");
    replyMSG.append("<br>");
    ConcurrentLinkedQueue<L2Effect> list = player.getEffectList().getAllEffects();
    if(list != null && !list.isEmpty())
    {
      for(L2Effect e : list)
      {
        replyMSG.append(e.getSkill().getName()).append(" ").append(e.getSkill().getLevel()).append("<br1>");
      }
    }
    replyMSG.append("<br></body></html>");
    adminReply.setHtml(replyMSG.toString());
    activeChar.sendPacket(adminReply);
  }

  private void adminGetSkills(L2Player activeChar)
  {
    L2Object target = activeChar.getTarget();
    L2Player player;
    if(target.isPlayer() && (activeChar == target || activeChar.getPlayerAccess().CanEditCharAll))
    {
      player = (L2Player) target;
    }
    else
    {
      activeChar.sendPacket(Msg.INVALID_TARGET);
      return;
    }
    if(player.getName().equals(activeChar.getName()))
    {
      player.sendMessage("There is no point in doing it on your character.");
    }
    else
    {
      Collection<L2Skill> skills = player.getAllSkills();
      adminSkills = activeChar.getAllSkillsArray();
      for(L2Skill element : adminSkills)
      {
        activeChar.removeSkill(element, true);
      }
      for(L2Skill element : skills)
      {
        activeChar.addSkill(element, true);
      }
      activeChar.sendMessage("You now have all the skills of  " + player.getName() + ".");
    }
    showSkillsPage(activeChar);
  }

  private void adminResetSkills(L2Player activeChar)
  {
    L2Object target = activeChar.getTarget();
    L2Player player;
    if(target.isPlayer() && (activeChar == target || activeChar.getPlayerAccess().CanEditCharAll))
    {
      player = (L2Player) target;
    }
    else
    {
      activeChar.sendPacket(Msg.INVALID_TARGET);
      return;
    }
    L2Skill[] skills = player.getAllSkillsArray();
    int counter = 0;
    for(L2Skill element : skills)
    {
      if(!element.isCommon() && !SkillTreeTable.getInstance().isSkillPossible(player, element.getId(), element.getLevel()))
      {
        player.removeSkill(element, true);
        counter++;
      }
    }
    player.checkSkills();
    player.sendPacket(new SkillList(player));
    player.sendMessage("[GM]" + activeChar.getName() + " has updated your skills.");
    activeChar.sendMessage(counter + " skills removed.");
    showSkillsPage(activeChar);
  }

  private void adminAddSkill(L2Player activeChar, String[] wordList)
  {
    L2Object target = activeChar.getTarget();
    L2Player player;
    if(target != null && target.isPlayer() && (activeChar == target || activeChar.getPlayerAccess().CanEditCharAll))
    {
      player = (L2Player) target;
    }
    else
    {
      activeChar.sendPacket(Msg.INVALID_TARGET);
      return;
    }
    if(wordList.length == 3)
    {
      int id = Integer.parseInt(wordList[1]);
      int level = Integer.parseInt(wordList[2]);
      L2Skill skill = SkillTable.getInstance().getInfo(id, level);
      if(skill != null)
      {
        player.sendMessage("Admin gave you the skill " + skill.getName() + ".");
        player.addSkill(skill, true);
        player.sendPacket(new SkillList(player));
        activeChar.sendMessage("You gave the skill " + skill.getName() + " to " + player.getName() + ".");
      }
      else
      {
        activeChar.sendMessage("Error: there is no such skill.");
      }
    }
    showSkillsPage(activeChar);
  }

  private void adminRemoveSkill(L2Player activeChar, String[] wordList)
  {
    L2Object target = activeChar.getTarget();
    L2Player player;
    if(target.isPlayer() && (activeChar == target || activeChar.getPlayerAccess().CanEditCharAll))
    {
      player = (L2Player) target;
    }
    else
    {
      activeChar.sendPacket(Msg.INVALID_TARGET);
      return;
    }
    if(wordList.length == 2)
    {
      int id = Integer.parseInt(wordList[1]);
      int level = player.getSkillLevel(id);
      L2Skill skill = SkillTable.getInstance().getInfo(id, level);
      if(skill != null)
      {
        player.sendMessage("Admin removed the skill " + skill.getName() + ".");
        player.removeSkill(skill, true);
        player.sendPacket(new SkillList(player));
        activeChar.sendMessage("You removed the skill " + skill.getName() + " from " + player.getName() + ".");
      }
      else
      {
        activeChar.sendMessage("Error: there is no such skill.");
      }
    }
    removeSkillsPage(activeChar);
  }

  public void onLoad()
  {
    AdminCommandHandler.getInstance().registerAdminCommandHandler(this);
  }

  public void onReload()
  {
  }

  public void onShutdown()
  {
  }
}
TOP

Related Classes of commands.admin.AdminSkill

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.