AdminShutdown.java 3.7 KB

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