AdminManor.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.util.List;
  17. import java.util.StringTokenizer;
  18. import com.l2jserver.gameserver.handler.IAdminCommandHandler;
  19. import com.l2jserver.gameserver.instancemanager.CastleManager;
  20. import com.l2jserver.gameserver.instancemanager.CastleManorManager;
  21. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  22. import com.l2jserver.gameserver.model.entity.Castle;
  23. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  24. import com.l2jserver.util.StringUtil;
  25. /**
  26. * Admin comand handler for Manor System
  27. * This class handles following admin commands:
  28. * - manor_info = shows info about current manor state
  29. * - manor_approve = approves settings for the next manor period
  30. * - manor_setnext = changes manor settings to the next day's
  31. * - manor_reset castle = resets all manor data for specified castle (or all)
  32. * - manor_setmaintenance = sets manor system under maintenance mode
  33. * - manor_save = saves all manor data into database
  34. * - manor_disable = disables manor system
  35. *
  36. * @author l3x
  37. */
  38. public class AdminManor implements IAdminCommandHandler
  39. {
  40. private static final String[] _adminCommands =
  41. {
  42. "admin_manor",
  43. "admin_manor_approve",
  44. "admin_manor_setnext",
  45. "admin_manor_reset",
  46. "admin_manor_setmaintenance",
  47. "admin_manor_save",
  48. "admin_manor_disable"
  49. };
  50. public boolean useAdminCommand(String command, L2PcInstance activeChar)
  51. {
  52. StringTokenizer st = new StringTokenizer(command);
  53. command = st.nextToken();
  54. if (command.equals("admin_manor"))
  55. {
  56. showMainPage(activeChar);
  57. }
  58. else if (command.equals("admin_manor_setnext"))
  59. {
  60. CastleManorManager.getInstance().setNextPeriod();
  61. CastleManorManager.getInstance().setNewManorRefresh();
  62. CastleManorManager.getInstance().updateManorRefresh();
  63. activeChar.sendMessage("Manor System: set to next period");
  64. showMainPage(activeChar);
  65. }
  66. else if (command.equals("admin_manor_approve"))
  67. {
  68. CastleManorManager.getInstance().approveNextPeriod();
  69. CastleManorManager.getInstance().setNewPeriodApprove();
  70. CastleManorManager.getInstance().updatePeriodApprove();
  71. activeChar.sendMessage("Manor System: next period approved");
  72. showMainPage(activeChar);
  73. }
  74. else if (command.equals("admin_manor_reset"))
  75. {
  76. int castleId = 0;
  77. try
  78. {
  79. castleId = Integer.parseInt(st.nextToken());
  80. }
  81. catch (Exception e)
  82. {
  83. }
  84. if (castleId > 0)
  85. {
  86. Castle castle = CastleManager.getInstance().getCastleById(castleId);
  87. castle.resetManor();
  88. activeChar.sendMessage("Manor data for " + castle.getName() + " was nulled");
  89. }
  90. else
  91. {
  92. for (Castle castle : CastleManager.getInstance().getCastles())
  93. {
  94. castle.resetManor();
  95. }
  96. activeChar.sendMessage("Manor data was nulled");
  97. }
  98. showMainPage(activeChar);
  99. }
  100. else if (command.equals("admin_manor_setmaintenance"))
  101. {
  102. boolean mode = CastleManorManager.getInstance().isUnderMaintenance();
  103. CastleManorManager.getInstance().setUnderMaintenance(!mode);
  104. if (mode)
  105. activeChar.sendMessage("Manor System: not under maintenance");
  106. else
  107. activeChar.sendMessage("Manor System: under maintenance");
  108. showMainPage(activeChar);
  109. }
  110. else if (command.equals("admin_manor_save"))
  111. {
  112. CastleManorManager.getInstance().save();
  113. activeChar.sendMessage("Manor System: all data saved");
  114. showMainPage(activeChar);
  115. }
  116. else if (command.equals("admin_manor_disable"))
  117. {
  118. boolean mode = CastleManorManager.getInstance().isDisabled();
  119. CastleManorManager.getInstance().setDisabled(!mode);
  120. if (mode)
  121. activeChar.sendMessage("Manor System: enabled");
  122. else
  123. activeChar.sendMessage("Manor System: disabled");
  124. showMainPage(activeChar);
  125. }
  126. return true;
  127. }
  128. public String[] getAdminCommandList()
  129. {
  130. return _adminCommands;
  131. }
  132. private String formatTime(long millis)
  133. {
  134. String s = "";
  135. int secs = (int) millis / 1000;
  136. int mins = secs / 60;
  137. secs -= mins * 60;
  138. int hours = mins / 60;
  139. mins -= hours * 60;
  140. if (hours > 0)
  141. s += hours + ":";
  142. s += mins + ":";
  143. s += secs;
  144. return s;
  145. }
  146. private void showMainPage(L2PcInstance activeChar)
  147. {
  148. NpcHtmlMessage adminReply = new NpcHtmlMessage(5);
  149. final List<Castle> castles = CastleManager.getInstance().getCastles();
  150. final StringBuilder replyMSG = StringUtil.startAppend(
  151. 1000 + castles.size() * 50,
  152. "<html><body>" +
  153. "<center><table width=270><tr>"+
  154. "<td width=45><button value=\"Main\" action=\"bypass -h admin_admin\" width=45 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td>"+
  155. "<td width=180><center>Manor Info</center></td>"+
  156. "<td width=45><button value=\"Back\" action=\"bypass -h admin_admin2\" width=45 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td>"+
  157. "</tr></table><font color=\"LEVEL\"> [Manor System] </font></center><br>"+
  158. "<table width=\"100%\"><tr><td>" +
  159. "Disabled: ",
  160. CastleManorManager.getInstance().isDisabled() ? "yes" : "no",
  161. "</td><td>" +
  162. "Under Maintenance: ",
  163. CastleManorManager.getInstance().isUnderMaintenance() ? "yes" : "no",
  164. "</td></tr><tr><td>" +
  165. "Time to refresh: ",
  166. formatTime(CastleManorManager.getInstance().getMillisToManorRefresh()),
  167. "</td><td>" +
  168. "Time to approve: ",
  169. formatTime(CastleManorManager.getInstance().getMillisToNextPeriodApprove()),
  170. "</td></tr>" +
  171. "</table>" +
  172. "<center><table><tr><td>" +
  173. "<button value=\"Set Next\" action=\"bypass -h admin_manor_setnext\" width=110 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td><td>" +
  174. "<button value=\"Approve Next\" action=\"bypass -h admin_manor_approve\" width=110 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr><tr><td>" +
  175. "<button value=\"",
  176. CastleManorManager.getInstance().isUnderMaintenance() ? "Set normal" : "Set mainteance",
  177. "\" action=\"bypass -h admin_manor_setmaintenance\" width=110 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td><td>" +
  178. "<button value=\"",
  179. CastleManorManager.getInstance().isDisabled() ? "Enable" : "Disable",
  180. "\" action=\"bypass -h admin_manor_disable\" width=110 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr><tr><td>" +
  181. "<button value=\"Refresh\" action=\"bypass -h admin_manor\" width=110 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td><td>" +
  182. "<button value=\"Back\" action=\"bypass -h admin_admin\" width=110 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr>" +
  183. "</table></center>" +
  184. "<br><center>Castle Information:<table width=\"100%\">" +
  185. "<tr><td></td><td>Current Period</td><td>Next Period</td></tr>"
  186. );
  187. for (Castle c : CastleManager.getInstance().getCastles()) {
  188. StringUtil.append(replyMSG,
  189. "<tr><td>",
  190. c.getName(),
  191. "</td>" +
  192. "<td>",
  193. String.valueOf(c.getManorCost(CastleManorManager.PERIOD_CURRENT)),
  194. "a</td>" +
  195. "<td>",
  196. String.valueOf(c.getManorCost(CastleManorManager.PERIOD_NEXT)),
  197. "a</td>" +
  198. "</tr>");
  199. }
  200. replyMSG.append(
  201. "</table><br>" +
  202. "</body></html>");
  203. adminReply.setHtml(replyMSG.toString());
  204. activeChar.sendPacket(adminReply);
  205. }
  206. }