AdminHtml.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (C) 2004-2015 L2J DataPack
  3. *
  4. * This file is part of L2J DataPack.
  5. *
  6. * L2J DataPack is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J DataPack is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package handlers.admincommandhandlers;
  20. import java.io.File;
  21. import java.util.StringTokenizer;
  22. import com.l2jserver.Config;
  23. import com.l2jserver.gameserver.cache.HtmCache;
  24. import com.l2jserver.gameserver.handler.IAdminCommandHandler;
  25. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  26. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  27. /**
  28. * @author NosBit
  29. */
  30. public class AdminHtml implements IAdminCommandHandler
  31. {
  32. private static final String[] ADMIN_COMMANDS =
  33. {
  34. "admin_html",
  35. "admin_loadhtml"
  36. };
  37. @Override
  38. public boolean useAdminCommand(String command, L2PcInstance activeChar)
  39. {
  40. final StringTokenizer st = new StringTokenizer(command, " ");
  41. final String actualCommand = st.nextToken();
  42. switch (actualCommand.toLowerCase())
  43. {
  44. case "admin_html":
  45. {
  46. if (!st.hasMoreTokens())
  47. {
  48. activeChar.sendMessage("Usage: //html path");
  49. return false;
  50. }
  51. final String path = st.nextToken();
  52. showAdminHtml(activeChar, path);
  53. break;
  54. }
  55. case "admin_loadhtml":
  56. {
  57. if (!st.hasMoreTokens())
  58. {
  59. activeChar.sendMessage("Usage: //loadhtml path");
  60. return false;
  61. }
  62. final String path = st.nextToken();
  63. showHtml(activeChar, path, true);
  64. break;
  65. }
  66. }
  67. return true;
  68. }
  69. /**
  70. * Shows a html message to activeChar
  71. * @param activeChar activeChar where html is shown
  72. * @param path relative path from directory data/html/admin/ to html
  73. */
  74. public static void showAdminHtml(L2PcInstance activeChar, String path)
  75. {
  76. showHtml(activeChar, "data/html/admin/" + path, false);
  77. }
  78. /**
  79. * Shows a html message to activeChar.
  80. * @param activeChar activeChar where html message is shown.
  81. * @param path relative path from Config.DATAPACK_ROOT to html.
  82. * @param reload {@code true} will reload html and show it {@code false} will show it from cache.
  83. */
  84. public static void showHtml(L2PcInstance activeChar, String path, boolean reload)
  85. {
  86. String content = null;
  87. if (!reload)
  88. {
  89. content = HtmCache.getInstance().getHtm(activeChar.getHtmlPrefix(), path);
  90. }
  91. else
  92. {
  93. File file = new File(Config.DATAPACK_ROOT, path);
  94. content = HtmCache.getInstance().loadFile(file);
  95. }
  96. final NpcHtmlMessage html = new NpcHtmlMessage();
  97. if (content != null)
  98. {
  99. html.setHtml(content);
  100. }
  101. else
  102. {
  103. html.setHtml("<html><body>My text is missing:<br>" + path + "</body></html>");
  104. }
  105. activeChar.sendPacket(html);
  106. }
  107. @Override
  108. public String[] getAdminCommandList()
  109. {
  110. return ADMIN_COMMANDS;
  111. }
  112. }