AdminShutdown.java 4.2 KB

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