/*
* Copyright (C) 2004-2014 L2J DataPack
*
* This file is part of L2J DataPack.
*
* L2J DataPack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* L2J DataPack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package handlers.admincommandhandlers;
import java.util.List;
import java.util.StringTokenizer;
import com.l2jserver.gameserver.handler.IAdminCommandHandler;
import com.l2jserver.gameserver.instancemanager.CastleManager;
import com.l2jserver.gameserver.instancemanager.CastleManorManager;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import com.l2jserver.gameserver.model.entity.Castle;
import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
import com.l2jserver.util.StringUtil;
import com.l2jserver.gameserver.datatables.MessageTable;
/**
* Admin comand handler for Manor System This class handles following admin commands: - manor_info = shows info about current manor state - manor_approve = approves settings for the next manor period - manor_setnext = changes manor settings to the next day's - manor_reset castle = resets all manor
* data for specified castle (or all) - manor_setmaintenance = sets manor system under maintenance mode - manor_save = saves all manor data into database - manor_disable = disables manor system
* @author l3x
*/
public class AdminManor implements IAdminCommandHandler
{
private static final String[] _adminCommands =
{
"admin_manor",
"admin_manor_approve",
"admin_manor_setnext",
"admin_manor_reset",
"admin_manor_setmaintenance",
"admin_manor_save",
"admin_manor_disable"
};
@Override
public boolean useAdminCommand(String command, L2PcInstance activeChar)
{
StringTokenizer st = new StringTokenizer(command);
command = st.nextToken();
if (command.equals("admin_manor"))
{
showMainPage(activeChar);
}
else if (command.equals("admin_manor_setnext"))
{
CastleManorManager.getInstance().setNextPeriod();
CastleManorManager.getInstance().setNewManorRefresh();
CastleManorManager.getInstance().updateManorRefresh();
/* MessageTable.Messages[1748]
activeChar.sendMessage("Manor System: set to next period");
*/
activeChar.sendMessage(1748);
showMainPage(activeChar);
}
else if (command.equals("admin_manor_approve"))
{
CastleManorManager.getInstance().approveNextPeriod();
CastleManorManager.getInstance().setNewPeriodApprove();
CastleManorManager.getInstance().updatePeriodApprove();
/* MessageTable.Messages[1749]
activeChar.sendMessage("Manor System: next period approved");
*/
activeChar.sendMessage(1749);
showMainPage(activeChar);
}
else if (command.equals("admin_manor_reset"))
{
int castleId = 0;
try
{
castleId = Integer.parseInt(st.nextToken());
}
catch (Exception e)
{
}
if (castleId > 0)
{
Castle castle = CastleManager.getInstance().getCastleById(castleId);
castle.resetManor();
/* MessageTable
activeChar.sendMessage("Manor data for " + castle.getName() + " was nulled");
*/
activeChar.sendMessage(MessageTable.Messages[1750].getExtra(1) + castle.getName() + MessageTable.Messages[1750].getExtra(2));
}
else
{
for (Castle castle : CastleManager.getInstance().getCastles())
{
castle.resetManor();
}
/* MessageTable.Messages[1751]
activeChar.sendMessage("Manor data was nulled");
*/
activeChar.sendMessage(1751);
}
showMainPage(activeChar);
}
else if (command.equals("admin_manor_setmaintenance"))
{
boolean mode = CastleManorManager.getInstance().isUnderMaintenance();
CastleManorManager.getInstance().setUnderMaintenance(!mode);
if (mode)
{
/* MessageTable.Messages[1752]
activeChar.sendMessage("Manor System: not under maintenance");
*/
activeChar.sendMessage(1752);
}
else
{
/* MessageTable.Messages[1753]
activeChar.sendMessage("Manor System: under maintenance");
*/
activeChar.sendMessage(1753);
}
showMainPage(activeChar);
}
else if (command.equals("admin_manor_save"))
{
CastleManorManager.getInstance().save();
/* MessageTable.Messages[1754]
activeChar.sendMessage("Manor System: all data saved");
*/
activeChar.sendMessage(1754);
showMainPage(activeChar);
}
else if (command.equals("admin_manor_disable"))
{
boolean mode = CastleManorManager.getInstance().isDisabled();
CastleManorManager.getInstance().setDisabled(!mode);
if (mode)
{
/* MessageTable.Messages[1755]
activeChar.sendMessage("Manor System: enabled");
*/
activeChar.sendMessage(1755);
}
else
{
/* MessageTable.Messages[1756]
activeChar.sendMessage("Manor System: disabled");
*/
activeChar.sendMessage(1756);
}
showMainPage(activeChar);
}
return true;
}
@Override
public String[] getAdminCommandList()
{
return _adminCommands;
}
private String formatTime(long millis)
{
String s = "";
int secs = (int) millis / 1000;
int mins = secs / 60;
secs -= mins * 60;
int hours = mins / 60;
mins -= hours * 60;
if (hours > 0)
{
s += hours + ":";
}
s += mins + ":";
s += secs;
return s;
}
private void showMainPage(L2PcInstance activeChar)
{
final NpcHtmlMessage adminReply = new NpcHtmlMessage();
final List<Castle> castles = CastleManager.getInstance().getCastles();
/* MessageTable
final StringBuilder replyMSG = StringUtil.startAppend(1000 + (castles.size() * 50), "<html><body>" + "<center><table width=270><tr>" + "<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>" + "<td width=180><center>Manor Info</center></td>" + "<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>" + "</tr></table><font color=\"LEVEL\"> [Manor System] </font></center><br>" + "<table width=\"100%\"><tr><td>" + "Disabled: ", CastleManorManager.getInstance().isDisabled() ? "yes" : "no", "</td><td>" + "Under Maintenance: ", CastleManorManager.getInstance().isUnderMaintenance() ? "yes" : "no", "</td></tr><tr><td>" + "Time to refresh: ", formatTime(CastleManorManager.getInstance().getMillisToManorRefresh()), "</td><td>" + "Time to approve: ", formatTime(CastleManorManager.getInstance().getMillisToNextPeriodApprove()), "</td></tr>" + "</table>" + "<center><table><tr><td>" + "<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>" + "<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>" + "<button value=\"", CastleManorManager.getInstance().isUnderMaintenance() ? "Set normal" : "Set mainteance", "\" action=\"bypass -h admin_manor_setmaintenance\" width=110 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td><td>" + "<button value=\"", CastleManorManager.getInstance().isDisabled() ? "Enable" : "Disable", "\" action=\"bypass -h admin_manor_disable\" width=110 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr><tr><td>" + "<button value=\"Refresh\" action=\"bypass -h admin_manor\" width=110 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td><td>" + "<button value=\"Back\" action=\"bypass -h admin_admin\" width=110 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr>" + "</table></center>" + "<br><center>Castle Information:<table width=\"100%\">" + "<tr><td></td><td>Current Period</td><td>Next Period</td></tr>");
*/
final StringBuilder replyMSG = StringUtil.startAppend(1000 + (castles.size() * 50), "<html><body>" + "<center><table width=270><tr>" + "<td width=45><button value=\"" + MessageTable.Messages[1757].getMessage() + "\" action=\"bypass -h admin_admin\" width=45 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td>" + "<td width=180><center>" + MessageTable.Messages[1758].getMessage() + "</center></td>" + "<td width=45><button value=\"" + MessageTable.Messages[1759].getMessage() + "\" action=\"bypass -h admin_admin2\" width=45 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td>" + "</tr></table><font color=\"LEVEL\">" + MessageTable.Messages[1760].getMessage() + "</font></center><br>" + "<table width=\"100%\"><tr><td>" + MessageTable.Messages[1761].getMessage(), CastleManorManager.getInstance().isDisabled() ? MessageTable.Messages[1762].getExtra(1) : MessageTable.Messages[1762].getExtra(2), "</td><td>" + MessageTable.Messages[1763].getMessage(), CastleManorManager.getInstance().isUnderMaintenance() ? MessageTable.Messages[1762].getExtra(1) : MessageTable.Messages[1762].getExtra(2), "</td></tr><tr><td>" + MessageTable.Messages[1764].getMessage(), formatTime(CastleManorManager.getInstance().getMillisToManorRefresh()), "</td><td>" + MessageTable.Messages[1765].getMessage(), formatTime(CastleManorManager.getInstance().getMillisToNextPeriodApprove()), "</td></tr>" + "</table>" + "<center><table><tr><td>" + "<button value=\"" + MessageTable.Messages[1766].getMessage() + "\" action=\"bypass -h admin_manor_setnext\" width=110 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td><td>" + "<button value=\"" + MessageTable.Messages[1767].getMessage() + "\" action=\"bypass -h admin_manor_approve\" width=110 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr><tr><td>" + "<button value=\"", CastleManorManager.getInstance().isUnderMaintenance() ? MessageTable.Messages[1768].getExtra(1) : MessageTable.Messages[1768].getExtra(2), "\" action=\"bypass -h admin_manor_setmaintenance\" width=110 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td><td>" + "<button value=\"", CastleManorManager.getInstance().isDisabled() ? MessageTable.Messages[1769].getExtra(1) : MessageTable.Messages[1769].getExtra(2), "\" action=\"bypass -h admin_manor_disable\" width=110 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr><tr><td>" + "<button value=\"" + MessageTable.Messages[1770].getMessage() + "\" action=\"bypass -h admin_manor\" width=110 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td><td>" + "<button value=\"" + MessageTable.Messages[1759].getMessage() + "\" action=\"bypass -h admin_admin\" width=110 height=21 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td></tr>" + "</table></center>" + "<br><center>" + MessageTable.Messages[1771].getMessage() + "<br1><table width=\"100%\">" + "<tr><td>" + MessageTable.Messages[1771].getExtra(1) + "</td><td>" + MessageTable.Messages[1772].getMessage() + "</td><td>" + MessageTable.Messages[1773].getMessage() + "</td></tr>");
for (Castle c : CastleManager.getInstance().getCastles())
{
/* l2jtw add CName
StringUtil.append(replyMSG, "<tr><td>", c.getName(), "</td>" + "<td>", String.valueOf(c.getManorCost(CastleManorManager.PERIOD_CURRENT)), "a</td>" + "<td>", String.valueOf(c.getManorCost(CastleManorManager.PERIOD_NEXT)), "a</td>" + "</tr>");
*/
StringUtil.append(replyMSG, "<tr><td>", c.getCName(), "</td>" + "<td>", String.valueOf(c.getManorCost(CastleManorManager.PERIOD_CURRENT)), "a</td>" + "<td>", String.valueOf(c.getManorCost(CastleManorManager.PERIOD_NEXT)), "a</td>" + "</tr>");
}
replyMSG.append("</table><br>" + "</body></html>");
adminReply.setHtml(replyMSG.toString());
activeChar.sendPacket(adminReply);
}
}