Jump to content
  • 0

misterelax

Question

hello mates

/*
 * This program 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.
 * 
 * This program 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 com.l2jserver.gameserver.model.entity;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Calendar;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.logging.Logger;

import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
import com.l2jserver.gameserver.ThreadPoolManager;
import com.l2jserver.gameserver.model.L2World;
import com.l2jserver.gameserver.model.actor.instance.L2FactTeleporterInstance;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import com.l2jserver.gameserver.model.actor.instance.L2TpFlagInstance;
import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
import com.l2jserver.gameserver.util.Broadcast;
import com.l2jserver.util.StringUtil;

/**
 * @author Laikeriz
 */
public class FactionMaps
{
	private final static Logger _log = Logger.getLogger(FactionMaps.class.getName());
	
	public static Map<Integer, String> _all_maps = new HashMap<>();
	
	public static int _mapId = 0;
	public static String _mapName = "";
	public static int _maps_count = 0;
	public static boolean _voting = false;
	public static int[] _mapVotes = new int[10];
	public static String[] _mapNames = new String[10];
	private static long _nextMapIn = 0;
	
	
	public static boolean isVoting()
	{
		return _voting;
	}
	
	public static int getMapId()
	{
		return _mapId;
	}
	
	public static String getMapName()
	{
		return _mapName;
	}
	
	public static String getDelayUntilVoting()
	{
		long trim = _nextMapIn - Calendar.getInstance().getTimeInMillis();
		int h = 0;
		int m = 0;
		int s = 0;
		int k = 1 / 2;
		
		if ((trim / 3600000) >= 1)
		{
			h = Math.round((trim / 3600000) - k);
			trim -= h * 3600000;
		}
		if ((trim / 60) >= 1)
		{
			m = Math.round((trim / 60000) - k);
			trim -= m * 60000;
		}
		if ((trim / 1000) >= 1)
		{
			s = Math.round((trim / 1000) - k);
		}
		return h + "h. " + m + "m. " + s + "s.";
	}
	
	@SuppressWarnings("resource")
	public static void loadCurrentMap()
	{
		Connection con = null;
		try
		{
			con = ConnectionFactory.getInstance().getConnection();
			PreparedStatement stmt = con.prepareStatement("SELECT mapId,map_name FROM faction_maps WHERE current=1");
			ResultSet rset = stmt.executeQuery();
			while (rset.next())
			{
				_mapId = rset.getInt("mapId");
				_mapName = rset.getString("map_name");
			}
			rset.close();
			stmt.close();
			// Put all maps in FastMap
			PreparedStatement stmt_all = con.prepareStatement("SELECT mapId,map_name FROM faction_maps");
			ResultSet rset_all = stmt_all.executeQuery();
			while (rset_all.next())
			{
				_all_maps.put(rset_all.getInt("mapId"), rset_all.getString("map_name"));
			}
			rset_all.close();
			stmt_all.close();
		}
		catch (Exception e)
		{
			_log.warning("Load current map: " + e);
		}
		finally
		{
			try
			{
				con.close();
			}
			catch (Exception e)
			{
			}
		}
		ThreadPoolManager.getInstance().scheduleGeneral(() -> endVoting(true), 1000);
	}
	
	public static void beginVoting()
	{
		for (L2TpFlagInstance flag : L2FactTeleporterInstance._tpBlueFlags)
		{
			flag.deleteMe();
		}
		for (L2TpFlagInstance flag : L2FactTeleporterInstance._tpRedFlags)
		{
			flag.deleteMe();
		}
		L2FactTeleporterInstance._tpBlueFlags.clear();
		L2FactTeleporterInstance._tpRedFlags.clear();
		for (L2PcInstance player : L2World.getInstance().getAllPlayers().values())
		{
			player.setVotedForMap(false);
		}
		Broadcast.toAllOnlinePlayers("Voting for next faction map has begun. It will end in 60 seconds.");
		_voting = true;
		
		NpcHtmlMessage html = new NpcHtmlMessage(1);
		final StringBuilder tb = StringUtil.startAppend(3500, "<html><title>Faction Maps</title><body><center>");
		tb.append("<img src=\"L2UI_CH3.onscrmsg_pattern01_1\" width=300 height=32 align=left><br>");
		Connection con = null;
		try
		{
			con = ConnectionFactory.getInstance().getConnection();
			PreparedStatement statement = con.prepareStatement("SELECT * FROM faction_maps WHERE current=0");
			ResultSet rset = statement.executeQuery();
			for (int r = 0; r < _mapVotes.length; r++)
			{
				_mapVotes[r] = 0;
			}
			while (rset.next())
			{
				
				tb.append("<button value=\"" + rset.getString("map_name") + "\" action=\"bypass -h voteformap_" + rset.getInt("mapId") + "\" width=170 height=20 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\">");
				_mapNames[rset.getInt("mapId")] = rset.getString("map_name");
			}
			rset.close();
			statement.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				con.close();
			}
			catch (Exception e)
			{
				_log.info("Couldn't set player faction:" + " " + e);
			}
		}
		tb.append("<img src=\"L2UI_CH3.onscrmsg_pattern01_2\" width=300 height=32 align=left></center></body></html>");
		
		html.setHtml(tb.toString());
		for (L2PcInstance player : L2World.getInstance().getAllPlayers().values())
		{
			player.sendPacket(html);
		}
		try
		{
			Thread.sleep(60000);
		}
		catch (Exception e)
		{
		}
		endVoting(false);
	}
	
	@SuppressWarnings("resource")
	public static void endVoting(boolean onLoad)
	{
		_nextMapIn = Calendar.getInstance().getTimeInMillis() + 250000;
		if (!onLoad)
		{
			_voting = false;
			int mostVotes = 0;
			int mostMapId = 0;
			for (int i = 0; i < _mapVotes.length; i++)
			{
				if (_mapVotes[i] > mostVotes)
				{
					mostVotes = _mapVotes[i];
					mostMapId = i;
				}
			}
			
			if (mostVotes == 0)
			{
				mostMapId = 2;
			}
			_mapName = _mapNames[mostMapId];
			_mapId = mostMapId;
			Broadcast.toAllOnlinePlayers("Voting for faction maps has ended.");
			Broadcast.toAllOnlinePlayers("The next map will be: " + _mapName);
			L2TpFlagInstance.spawnFlags();
			
			@SuppressWarnings("resource")
			Connection con = null;
			try
			{
				con = ConnectionFactory.getInstance().getConnection();
				PreparedStatement statement = null;
				statement = con.prepareStatement("UPDATE faction_maps SET current=0");
				statement.execute();
				statement = con.prepareStatement("UPDATE faction_maps SET current=1 WHERE mapId=?");
				statement.setInt(1, _mapId);
				statement.execute();
				statement.close();
			}
			catch (Exception e)
			{
				_log.warning("End voting: " + e);
			}
			finally
			{
				try
				{
					con.close();
				}
				catch (Exception e)
				{
				}
			}
		}
		int durat = 4 * 60 * 1000;
		try
		{
			Thread.sleep(durat);
		}
		catch (Exception e)
		{
		}
		beginVoting();
	}
	
	public static int getNextMap(int mapid)
	{
		if (_mapId < _maps_count)
		{
			return _mapId + 1;
		}
		return 1;
	}
	
	public static class ValueComparator implements Comparator<Object>
	{
		Map<L2PcInstance, Integer> base;
		
		public ValueComparator(Map<L2PcInstance, Integer> base1)
		{
			this.base = base1;
		}
		
		@Override
		public int compare(Object a, Object b)
		{
			if (base.get(a) < base.get(b))
			{
				return 1;
			}
			else if (base.get(a) == base.get(b))
			{
				return 0;
			}
			else
			{
				return -1;
			}
		}
	}
	
	public static void voteForMap(int mapId)
	{
		_mapVotes[mapId] = _mapVotes[mapId] + 1;
	}
	
}

.So im trying to adapt DarthVaders script for factionmap,l2factteleporter and l2tpflaginstance on h5 l2j.So far everything work perfect except voting.The window as you can see is working but when i press any button window just close and nothing happen.When voting ends and next map load it seems to be the same so i believe is something wrong with bypass,but i cant figured out .Any help/idea?

post-162349-0-45864700-1474471343_thumb.png

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0

oh!!forgot to upload...

/*
 * Copyright (C) 2004-2015 L2J Server
 * 
 * This file is part of L2J Server.
 * 
 * L2J Server 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 Server 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 com.l2jserver.gameserver.network.clientpackets;

import java.util.StringTokenizer;
import java.util.logging.Level;

import com.l2jserver.Config;
import com.l2jserver.gameserver.ai.CtrlIntention;
import com.l2jserver.gameserver.data.xml.impl.AdminData;
import com.l2jserver.gameserver.enums.InstanceType;
import com.l2jserver.gameserver.enums.PlayerAction;
import com.l2jserver.gameserver.handler.AdminCommandHandler;
import com.l2jserver.gameserver.handler.BypassHandler;
import com.l2jserver.gameserver.handler.CommunityBoardHandler;
import com.l2jserver.gameserver.handler.IAdminCommandHandler;
import com.l2jserver.gameserver.handler.IBypassHandler;
import com.l2jserver.gameserver.model.L2Object;
import com.l2jserver.gameserver.model.L2World;
import com.l2jserver.gameserver.model.actor.L2Character;
import com.l2jserver.gameserver.model.actor.L2Npc;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import com.l2jserver.gameserver.model.entity.Hero;
import com.l2jserver.gameserver.model.events.EventDispatcher;
import com.l2jserver.gameserver.model.events.impl.character.npc.OnNpcManorBypass;
import com.l2jserver.gameserver.model.events.impl.character.player.OnPlayerBypass;
import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
import com.l2jserver.gameserver.network.SystemMessageId;
import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
import com.l2jserver.gameserver.network.serverpackets.ConfirmDlg;
import com.l2jserver.gameserver.network.serverpackets.MagicSkillUse;
import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
import com.l2jserver.gameserver.util.Broadcast;
import com.l2jserver.gameserver.util.GMAudit;
import com.l2jserver.gameserver.util.Util;

/**
 * RequestBypassToServer client packet implementation.
 * @author HorridoJoho
 */
public final class RequestBypassToServer extends L2GameClientPacket
{
	private static final String _C__23_REQUESTBYPASSTOSERVER = "[C] 23 RequestBypassToServer";
	// FIXME: This is for compatibility, will be changed when bypass functionality got an overhaul by NosBit
	private static final String[] _possibleNonHtmlCommands =
	{
		"_bbs",
		"bbs",
		"_mail",
		"_friend",
		"_match",
		"_diary",
		"_olympiad?command",
		"manor_menu_select"
	};
	
	// S
	private String _command;
	
	@Override
	protected void readImpl()
	{
		_command = readS();
	}
	
	@Override
	protected void runImpl()
	{
		final L2PcInstance activeChar = getClient().getActiveChar();
		if (activeChar == null)
		{
			return;
		}
		
		if (_command.isEmpty())
		{
			_log.warning("Player " + activeChar.getName() + " sent empty bypass!");
			activeChar.logout();
			return;
		}
		
		boolean requiresBypassValidation = true;
		for (String possibleNonHtmlCommand : _possibleNonHtmlCommands)
		{
			if (_command.startsWith(possibleNonHtmlCommand))
			{
				requiresBypassValidation = false;
				break;
			}
		}
		
		int bypassOriginId = 0;
		if (requiresBypassValidation)
		{
			bypassOriginId = activeChar.validateHtmlAction(_command);
			if (bypassOriginId == -1)
			{
				_log.warning("Player " + activeChar.getName() + " sent non cached bypass: '" + _command + "'");
				return;
			}
			
			if ((bypassOriginId > 0) && !Util.isInsideRangeOfObjectId(activeChar, bypassOriginId, L2Npc.INTERACTION_DISTANCE))
			{
				// No logging here, this could be a common case where the player has the html still open and run too far away and then clicks a html action
				return;
			}
		}
		
		if (!getClient().getFloodProtectors().getServerBypass().tryPerformAction(_command))
		{
			return;
		}
		
		try
		{
			if (_command.startsWith("admin_"))
			{
				String command = _command.split(" ")[0];
				
				IAdminCommandHandler ach = AdminCommandHandler.getInstance().getHandler(command);
				
				if (ach == null)
				{
					if (activeChar.isGM())
					{
						activeChar.sendMessage("The command " + command.substring(6) + " does not exist!");
					}
					_log.warning(activeChar + " requested not registered admin command '" + command + "'");
					return;
				}
				
				if (!AdminData.getInstance().hasAccess(command, activeChar.getAccessLevel()))
				{
					activeChar.sendMessage("You don't have the access rights to use this command!");
					_log.warning("Character " + activeChar.getName() + " tried to use admin command " + command + ", without proper access level!");
					return;
				}
				
				if (AdminData.getInstance().requireConfirm(command))
				{
					activeChar.setAdminConfirmCmd(_command);
					ConfirmDlg dlg = new ConfirmDlg(SystemMessageId.S1);
					dlg.addString("Are you sure you want execute command " + _command.substring(6) + " ?");
					activeChar.addAction(PlayerAction.ADMIN_COMMAND);
					activeChar.sendPacket(dlg);
				}
				else
				{
					if (Config.GMAUDIT)
					{
						GMAudit.auditGMAction(activeChar.getName() + " [" + activeChar.getObjectId() + "]", _command, (activeChar.getTarget() != null ? activeChar.getTarget().getName() : "no-target"));
					}
					
					ach.useAdminCommand(_command, activeChar);
				}
			}
			else if (_command.startsWith("voteformap_"))
			{
				
				int mapId = Integer.parseInt(_command.split("_")[1]);
				FactionMaps.voteForMap(mapId);
				activeChar.sendMessage("Your vote was added.");
				activeChar.setVotedForMap(true);
			}
			else if (CommunityBoardHandler.getInstance().isCommunityBoardCommand(_command))
			{
				CommunityBoardHandler.getInstance().handleParseCommand(_command, activeChar);
			}
			else if (_command.equals("come_here") && activeChar.isGM())
			{
				comeHere(activeChar);
			}
			
			else if (_command.startsWith("npc_"))
			{
				int endOfId = _command.indexOf('_', 5);
				String id;
				if (endOfId > 0)
				{
					id = _command.substring(4, endOfId);
				}
				else
				{
					id = _command.substring(4);
				}
				if (Util.isDigit(id))
				{
					L2Object object = L2World.getInstance().findObject(Integer.parseInt(id));
					
					if ((object != null) && object.isNpc() && (endOfId > 0) && activeChar.isInsideRadius(object, L2Npc.INTERACTION_DISTANCE, false, false))
					{
						((L2Npc) object).onBypassFeedback(activeChar, _command.substring(endOfId + 1));
					}
				}
				
				activeChar.sendPacket(ActionFailed.STATIC_PACKET);
			}
			else if (_command.startsWith("item_"))
			{
				int endOfId = _command.indexOf('_', 5);
				String id;
				if (endOfId > 0)
				{
					id = _command.substring(5, endOfId);
				}
				else
				{
					id = _command.substring(5);
				}
				try
				{
					final L2ItemInstance item = activeChar.getInventory().getItemByObjectId(Integer.parseInt(id));
					if ((item != null) && (endOfId > 0))
					{
						item.onBypassFeedback(activeChar, _command.substring(endOfId + 1));
					}
					
					activeChar.sendPacket(ActionFailed.STATIC_PACKET);
				}
				catch (NumberFormatException nfe)
				{
					_log.log(Level.WARNING, "NFE for command [" + _command + "]", nfe);
				}
			}
			else if (_command.startsWith("_match"))
			{
				String params = _command.substring(_command.indexOf("?") + 1);
				StringTokenizer st = new StringTokenizer(params, "&");
				int heroclass = Integer.parseInt(st.nextToken().split("=")[1]);
				int heropage = Integer.parseInt(st.nextToken().split("=")[1]);
				int heroid = Hero.getInstance().getHeroByClass(heroclass);
				if (heroid > 0)
				{
					Hero.getInstance().showHeroFights(activeChar, heroclass, heroid, heropage);
				}
			}
			
			else if (_command.startsWith("_diary"))
			{
				String params = _command.substring(_command.indexOf("?") + 1);
				StringTokenizer st = new StringTokenizer(params, "&");
				int heroclass = Integer.parseInt(st.nextToken().split("=")[1]);
				int heropage = Integer.parseInt(st.nextToken().split("=")[1]);
				int heroid = Hero.getInstance().getHeroByClass(heroclass);
				if (heroid > 0)
				{
					Hero.getInstance().showHeroDiary(activeChar, heroclass, heroid, heropage);
				}
			}
			else if (_command.startsWith("_olympiad?command"))
			{
				int arenaId = Integer.parseInt(_command.split("=")[2]);
				final IBypassHandler handler = BypassHandler.getInstance().getHandler("arenachange");
				if (handler != null)
				{
					handler.useBypass("arenachange " + (arenaId - 1), activeChar, null);
				}
			}
			else if (_command.startsWith("manor_menu_select"))
			{
				final L2Npc lastNpc = activeChar.getLastFolkNPC();
				if (Config.ALLOW_MANOR && (lastNpc != null) && lastNpc.canInteract(activeChar))
				{
					final String[] split = _command.substring(_command.indexOf("?") + 1).split("&");
					final int ask = Integer.parseInt(split[0].split("=")[1]);
					final int state = Integer.parseInt(split[1].split("=")[1]);
					final boolean time = split[2].split("=")[1].equals("1");
					EventDispatcher.getInstance().notifyEventAsync(new OnNpcManorBypass(activeChar, lastNpc, ask, state, time), lastNpc);
				}
			}
			else
			{
				final IBypassHandler handler = BypassHandler.getInstance().getHandler(_command);
				if (handler != null)
				{
					if (bypassOriginId > 0)
					{
						L2Object bypassOrigin = activeChar.getKnownList().getKnownObjects().get(bypassOriginId);
						if ((bypassOrigin != null) && bypassOrigin.isInstanceTypes(InstanceType.L2Character))
						{
							handler.useBypass(_command, activeChar, (L2Character) bypassOrigin);
						}
						else
						{
							handler.useBypass(_command, activeChar, null);
						}
					}
					else
					{
						handler.useBypass(_command, activeChar, null);
					}
				}
				else
				{
					_log.warning(getClient() + " sent not handled RequestBypassToServer: [" + _command + "]");
				}
			}
		}
		catch (Exception e)
		{
			_log.log(Level.WARNING, "Exception processing bypass from player " + activeChar.getName() + ": " + _command, e);
			
			if (activeChar.isGM())
			{
				StringBuilder sb = new StringBuilder(200);
				sb.append("<html><body>");
				sb.append("Bypass error: " + e + "<br1>");
				sb.append("Bypass command: " + _command + "<br1>");
				sb.append("StackTrace:<br1>");
				for (StackTraceElement ste : e.getStackTrace())
				{
					sb.append(ste.toString() + "<br1>");
				}
				sb.append("</body></html>");
				// item html
				final NpcHtmlMessage msg = new NpcHtmlMessage(0, 1, sb.toString());
				msg.disableValidation();
				activeChar.sendPacket(msg);
			}
		}
		
		EventDispatcher.getInstance().notifyEventAsync(new OnPlayerBypass(activeChar, _command), activeChar);
	}
	
	/**
	 * @param activeChar
	 */
	private static void comeHere(L2PcInstance activeChar)
	{
		L2Object obj = activeChar.getTarget();
		if (obj == null)
		{
			return;
		}
		if (obj instanceof L2Npc)
		{
			L2Npc temp = (L2Npc) obj;
			temp.setTarget(activeChar);
			temp.getAI().setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, activeChar.getLocation());
		}
	}
	
	@Override
	public String getType()
	{
		return _C__23_REQUESTBYPASSTOSERVER;
	}
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...