AdminShutdown.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package handlers.admincommandhandlers;
  16. import java.text.SimpleDateFormat;
  17. import java.util.Calendar;
  18. import com.l2jserver.Config;
  19. import com.l2jserver.gameserver.GameTimeController;
  20. import com.l2jserver.gameserver.Shutdown;
  21. import com.l2jserver.gameserver.handler.IAdminCommandHandler;
  22. import com.l2jserver.gameserver.model.L2World;
  23. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  24. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  25. import com.l2jserver.gameserver.util.Util;
  26. /**
  27. * This class handles following admin commands:
  28. * - server_shutdown [sec] = shows menu or shuts down server in sec seconds
  29. *
  30. * @version $Revision: 1.5.2.1.2.4 $ $Date: 2005/04/11 10:06:06 $
  31. */
  32. public class AdminShutdown implements IAdminCommandHandler
  33. {
  34. //private static Logger _log = Logger.getLogger(AdminShutdown.class.getName());
  35. private static final String[] ADMIN_COMMANDS =
  36. {
  37. "admin_server_shutdown",
  38. "admin_server_restart",
  39. "admin_server_abort"
  40. };
  41. @Override
  42. public boolean useAdminCommand(String command, L2PcInstance activeChar)
  43. {
  44. if (command.startsWith("admin_server_shutdown"))
  45. {
  46. try
  47. {
  48. final String val = command.substring(22);
  49. if (Util.isDigit(val))
  50. {
  51. serverShutdown(activeChar, Integer.valueOf(val), false);
  52. }
  53. else
  54. {
  55. activeChar.sendMessage("Usage: //server_shutdown <seconds>");
  56. sendHtmlForm(activeChar);
  57. }
  58. }
  59. catch (StringIndexOutOfBoundsException e)
  60. {
  61. sendHtmlForm(activeChar);
  62. }
  63. }
  64. else if (command.startsWith("admin_server_restart"))
  65. {
  66. try
  67. {
  68. final String val = command.substring(21);
  69. if (Util.isDigit(val))
  70. {
  71. serverShutdown(activeChar, Integer.parseInt(val), true);
  72. }
  73. else
  74. {
  75. activeChar.sendMessage("Usage: //server_restart <seconds>");
  76. sendHtmlForm(activeChar);
  77. }
  78. }
  79. catch (StringIndexOutOfBoundsException e)
  80. {
  81. sendHtmlForm(activeChar);
  82. }
  83. }
  84. else if (command.startsWith("admin_server_abort"))
  85. {
  86. serverAbort(activeChar);
  87. }
  88. return true;
  89. }
  90. @Override
  91. public String[] getAdminCommandList()
  92. {
  93. return ADMIN_COMMANDS;
  94. }
  95. private void sendHtmlForm(L2PcInstance activeChar)
  96. {
  97. NpcHtmlMessage adminReply = new NpcHtmlMessage(5);
  98. int t = GameTimeController.getInstance().getGameTime();
  99. int h = t / 60;
  100. int m = t % 60;
  101. SimpleDateFormat format = new SimpleDateFormat("h:mm a");
  102. Calendar cal = Calendar.getInstance();
  103. cal.set(Calendar.HOUR_OF_DAY, h);
  104. cal.set(Calendar.MINUTE, m);
  105. adminReply.setFile(activeChar.getHtmlPrefix(), "data/html/admin/shutdown.htm");
  106. adminReply.replace("%count%", String.valueOf(L2World.getInstance().getAllPlayersCount()));
  107. adminReply.replace("%used%", String.valueOf(Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()));
  108. adminReply.replace("%xp%", String.valueOf(Config.RATE_XP));
  109. adminReply.replace("%sp%", String.valueOf(Config.RATE_SP));
  110. adminReply.replace("%adena%", String.valueOf(Config.RATE_DROP_ITEMS_ID.get(57)));
  111. adminReply.replace("%drop%", String.valueOf(Config.RATE_DROP_ITEMS));
  112. adminReply.replace("%time%", String.valueOf(format.format(cal.getTime())));
  113. activeChar.sendPacket(adminReply);
  114. }
  115. private void serverShutdown(L2PcInstance activeChar, int seconds, boolean restart)
  116. {
  117. Shutdown.getInstance().startShutdown(activeChar, seconds, restart);
  118. }
  119. private void serverAbort(L2PcInstance activeChar)
  120. {
  121. Shutdown.getInstance().abort(activeChar);
  122. }
  123. }