AdminShutdown.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.text.SimpleDateFormat;
  21. import java.util.Calendar;
  22. import com.l2jserver.gameserver.GameTimeController;
  23. import com.l2jserver.gameserver.Shutdown;
  24. import com.l2jserver.gameserver.handler.IAdminCommandHandler;
  25. import com.l2jserver.gameserver.model.L2World;
  26. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  27. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  28. import com.l2jserver.gameserver.util.Util;
  29. /**
  30. * This class handles following admin commands: - server_shutdown [sec] = shows menu or shuts down server in sec seconds
  31. * @version $Revision: 1.5.2.1.2.4 $ $Date: 2005/04/11 10:06:06 $
  32. */
  33. public class AdminShutdown implements IAdminCommandHandler
  34. {
  35. // private static Logger _log = Logger.getLogger(AdminShutdown.class.getName());
  36. private static final String[] ADMIN_COMMANDS =
  37. {
  38. "admin_server_shutdown",
  39. "admin_server_restart",
  40. "admin_server_abort"
  41. };
  42. @Override
  43. public boolean useAdminCommand(String command, L2PcInstance activeChar)
  44. {
  45. if (command.startsWith("admin_server_shutdown"))
  46. {
  47. try
  48. {
  49. final String val = command.substring(22);
  50. if (Util.isDigit(val))
  51. {
  52. serverShutdown(activeChar, Integer.valueOf(val), false);
  53. }
  54. else
  55. {
  56. activeChar.sendMessage("Usage: //server_shutdown <seconds>");
  57. sendHtmlForm(activeChar);
  58. }
  59. }
  60. catch (StringIndexOutOfBoundsException e)
  61. {
  62. sendHtmlForm(activeChar);
  63. }
  64. }
  65. else if (command.startsWith("admin_server_restart"))
  66. {
  67. try
  68. {
  69. final String val = command.substring(21);
  70. if (Util.isDigit(val))
  71. {
  72. serverShutdown(activeChar, Integer.parseInt(val), true);
  73. }
  74. else
  75. {
  76. activeChar.sendMessage("Usage: //server_restart <seconds>");
  77. sendHtmlForm(activeChar);
  78. }
  79. }
  80. catch (StringIndexOutOfBoundsException e)
  81. {
  82. sendHtmlForm(activeChar);
  83. }
  84. }
  85. else if (command.startsWith("admin_server_abort"))
  86. {
  87. serverAbort(activeChar);
  88. }
  89. return true;
  90. }
  91. @Override
  92. public String[] getAdminCommandList()
  93. {
  94. return ADMIN_COMMANDS;
  95. }
  96. private void sendHtmlForm(L2PcInstance activeChar)
  97. {
  98. final NpcHtmlMessage adminReply = new NpcHtmlMessage();
  99. int t = GameTimeController.getInstance().getGameTime();
  100. int h = t / 60;
  101. int m = t % 60;
  102. SimpleDateFormat format = new SimpleDateFormat("h:mm a");
  103. Calendar cal = Calendar.getInstance();
  104. cal.set(Calendar.HOUR_OF_DAY, h);
  105. cal.set(Calendar.MINUTE, m);
  106. adminReply.setFile(activeChar.getHtmlPrefix(), "data/html/admin/shutdown.htm");
  107. adminReply.replace("%count%", String.valueOf(L2World.getInstance().getAllPlayersCount()));
  108. adminReply.replace("%used%", String.valueOf(Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()));
  109. adminReply.replace("%time%", String.valueOf(format.format(cal.getTime())));
  110. activeChar.sendPacket(adminReply);
  111. }
  112. private void serverShutdown(L2PcInstance activeChar, int seconds, boolean restart)
  113. {
  114. Shutdown.getInstance().startShutdown(activeChar, seconds, restart);
  115. }
  116. private void serverAbort(L2PcInstance activeChar)
  117. {
  118. Shutdown.getInstance().abort(activeChar);
  119. }
  120. }