Jump to content
  • 0

[HELP]DeathMatch


Question

Posted

Hi could someone help me please.

DM I have works flawlessly but I need to edit it but will not do it alone. I need to neportovaly players but gave up to me apart. He did not get to reward only one player but the first 10 and that has always shown that html tech players and their 10 points. That's it.

Thank you all.

 

 

 

/*
* 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;
/**
* 
* @author SqueezeD Edited By KantikSGC
* 
*/

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javolution.text.TextBuilder;

import java.util.logging.Logger;

import com.l2jserver.Config;
import com.l2jserver.L2DatabaseFactory;
import com.l2jserver.gameserver.Announcements;
import com.l2jserver.gameserver.ThreadPoolManager;
import com.l2jserver.gameserver.datatables.NpcTable;
import com.l2jserver.gameserver.datatables.SpawnTable;
import com.l2jserver.gameserver.model.L2Effect;
import com.l2jserver.gameserver.model.L2Party;
import com.l2jserver.gameserver.model.L2Spawn;
import com.l2jserver.gameserver.model.actor.L2Summon;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import com.l2jserver.gameserver.model.actor.instance.L2PetInstance;
import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
import com.l2jserver.gameserver.network.serverpackets.MagicSkillUse;
import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
import com.l2jserver.gameserver.network.serverpackets.StatusUpdate;
import com.l2jserver.gameserver.templates.chars.L2NpcTemplate;

public class DM
{   
private final static Logger _log = Logger.getLogger(DM.class.getName());
public static String _eventName = new String(),
					 _eventDesc = new String(),
					 _joiningLocationName = new String();
public static Vector<String> _savePlayers = new Vector<String>();
public static Vector<L2PcInstance> _players = new Vector<L2PcInstance>();
public static boolean _joining = false,
					  _teleport = false,
					  _started = false,
					  _sitForced = false;
public static L2Spawn _npcSpawn;
public static L2PcInstance _topPlayer;
public static int _npcId = 0,
				  _npcX = 0,
				  _npcY = 0,
				  _npcZ = 0,
				  _rewardId = 0,
				  _rewardAmount = 0,
				  _topKills = 0,
				  _minlvl = 0,
				  _maxlvl = 0,
				  _playerColors = 0,
				  _playerX = 0,
				  _playerY = 0,
				  _playerZ = 0;

public static void setNpcPos(L2PcInstance activeChar)
{
	_npcX = activeChar.getX();
	_npcY = activeChar.getY();
	_npcZ = activeChar.getZ();
}

public static boolean checkMaxLevel(int maxlvl)
{
	if (_minlvl >= maxlvl)
		return false;

	return true;
}

public static boolean checkMinLevel(int minlvl)
{
	if (_maxlvl <= minlvl)
		return false;

	return true;
}

public static void setPlayersPos(L2PcInstance activeChar)
{
	_playerX = activeChar.getX();
	_playerY = activeChar.getY();
	_playerZ = activeChar.getZ();
}

public static boolean checkPlayerOk()
{
	if (_started || _teleport || _joining)
		return false;

	return true;
}

public static void startJoin(L2PcInstance activeChar)
{
	if (!startJoinOk())
	{
		if (!Config.DEBUG)
			_log.fine("DM Engine[startJoin(" + activeChar.getName() + ")]: startJoinOk() = false");
		return;
	}

	_joining = true;
	spawnEventNpc(activeChar);
	Announcements.getInstance().announceToAll(_eventName + "(DM): Joinable in " + _joiningLocationName + "!");
}

private static boolean startJoinOk()
{
	if (_started || _teleport || _joining || _eventName.equals("") ||
		_joiningLocationName.equals("") || _eventDesc.equals("") || _npcId == 0 ||
		_npcX == 0 || _npcY == 0 || _npcZ == 0 || _rewardId == 0 || _rewardAmount == 0 ||
		_playerX == 0 || _playerY == 0 || _playerZ == 0)
		return false;

	return true;
}

private static void spawnEventNpc(L2PcInstance activeChar)
{
	L2NpcTemplate tmpl = NpcTable.getInstance().getTemplate(_npcId);

	try
	{
		_npcSpawn = new L2Spawn(tmpl);

		_npcSpawn.setLocx(_npcX);
		_npcSpawn.setLocy(_npcY);
		_npcSpawn.setLocz(_npcZ);
		_npcSpawn.setAmount(1);
		_npcSpawn.setHeading(activeChar.getHeading());
		_npcSpawn.setRespawnDelay(1);

		SpawnTable.getInstance().addNewSpawn(_npcSpawn, false);

		_npcSpawn.init();
		_npcSpawn.getLastSpawn().getStatus().setCurrentHp(999999999);
		_npcSpawn.getLastSpawn().setTitle(_eventName);
		_npcSpawn.getLastSpawn()._isEventMobDM = true;
		_npcSpawn.getLastSpawn().isAggressive();
		_npcSpawn.getLastSpawn().decayMe();
		_npcSpawn.getLastSpawn().spawnMe(_npcSpawn.getLastSpawn().getX(), _npcSpawn.getLastSpawn().getY(), _npcSpawn.getLastSpawn().getZ());

		_npcSpawn.getLastSpawn().broadcastPacket(new MagicSkillUse(_npcSpawn.getLastSpawn(), _npcSpawn.getLastSpawn(), 1034, 1, 1, 1));
	}
	catch (Exception e)
	{
		_log.warning("DM Engine[spawnEventNpc(" + activeChar.getName() + ")]: exception: " + e.getMessage());
	}
}

public static void teleportStart()
{
	if (!_joining || _started || _teleport)
		return;

	_joining = false;
	Announcements.getInstance().announceToAll(_eventName + "(DM): Teleport to team spot in 20 seconds!");

	setUserData();
	ThreadPoolManager.getInstance().scheduleGeneral(new Runnable()
	{
		public void run()
		{
			DM.sit();

			for (L2PcInstance player : DM._players)
			{
				if (player !=  null)
				{
					if (Config.DM_ON_START_UNSUMMON_PET)
					{
						//Remove Summon's buffs
						if (player.getPet() != null)
						{
							L2Summon summon = player.getPet();
							for (L2Effect e : summon.getAllEffects())
								e.exit();

							if (summon instanceof L2PetInstance)
								summon.unSummon(player);
						}
					}

					if (Config.DM_ON_START_REMOVE_ALL_EFFECTS)
					{
						for (L2Effect e : player.getAllEffects())
						{
							if (e != null) e.exit();
						}
					}

					// Remove player from his party
					if (player.getParty() != null)
					{
						L2Party party = player.getParty();
						party.removePartyMember(player);
					}
					player.teleToLocation(_playerX, _playerY, _playerZ, false);
				}
			}
		}
	}, 20000);
	_teleport = true;
}

public static void startEvent(L2PcInstance activeChar)
{
	if (!startEventOk())
	{
		if (Config.DEBUG)
			_log.fine("DM Engine[startEvent(" + activeChar.getName() + ")]: startEventOk() = false");
		return;
	}

	_teleport = false;
	sit();
	Announcements.getInstance().announceToAll(_eventName + "(DM): Started. Go to kill your enemies!");
	_started = true;
}

private static boolean startEventOk()
{
	if (_joining || !_teleport || _started)
		return false;

	return true;
}

public static void setUserData()
{
	for (L2PcInstance player : _players)
	{
		player._originalNameColorDM = player.getAppearance().getNameColor();
		player._originalKarmaDM = player.getKarma();
		player._inEventDM = true;
		player._countDMkills = 0;
		player.getAppearance().setNameColor(_playerColors);
		player.setKarma(0);
		player.broadcastUserInfo();
	}
}

public static void removeUserData()
{
	for (L2PcInstance player : _players)
	{
		player.getAppearance().setNameColor(player._originalNameColorDM);
		player.setKarma(player._originalKarmaDM);
		player._inEventDM = false;
		player._countDMkills = 0;
		player.broadcastUserInfo();
	}
}

public static void finishEvent(L2PcInstance activeChar)
{
	if (!finishEventOk())
	{
		if (Config.DEBUG)
			_log.fine("DM Engine[finishEvent(" + activeChar.getName() + ")]: finishEventOk() = false");
		return;
	}

_started = false;
	unspawnEventNpc();
	processTopPlayer();

	if (_topKills == 0)
		Announcements.getInstance().announceToAll(_eventName + "(DM): No players win the match(nobody killed).");
	else
	{
		Announcements.getInstance().announceToAll(_eventName + "(DM): " + _topPlayer.getName() + " wins the match! " + _topKills + " kills.");
		rewardPlayer(activeChar);
	}

	teleportFinish();
}

private static boolean finishEventOk()
{
	if (!_started)
		return false;

	return true;
}

public static void processTopPlayer()
{
	for (L2PcInstance player : _players)
	{
		if (player._countDMkills > _topKills)
		{
			_topPlayer = player;
			_topKills = player._countDMkills;
		}
	}
}

/**
 * @param activeChar  
 */
public static void rewardPlayer(L2PcInstance activeChar)
{
	if (_topPlayer != null)
	{
		_topPlayer.addItem("DM Event: " + _eventName, _rewardId, _rewardAmount, _topPlayer, true);

		StatusUpdate su = new StatusUpdate(_topPlayer.getObjectId());
		su.addAttribute(StatusUpdate.CUR_LOAD, _topPlayer.getCurrentLoad());
		_topPlayer.sendPacket(su);

		NpcHtmlMessage nhm = new NpcHtmlMessage(5);
		TextBuilder replyMSG = new TextBuilder("");

		replyMSG.append("<html><body>You won the event. Look in your inventory for the reward.</body></html>");

		nhm.setHtml(replyMSG.toString());
		_topPlayer.sendPacket(nhm);

		// Send a Server->Client ActionFailed to the L2PcInstance in order to avoid that the client wait another packet
		_topPlayer.sendPacket(ActionFailed.STATIC_PACKET);
	}
}

public static void abortEvent()
{
	if (!_joining && !_teleport && !_started)
		return;

	_joining = false;
	_teleport = false;
	_started = false;
	unspawnEventNpc();
	Announcements.getInstance().announceToAll(_eventName + "(DM): Match aborted!");
	teleportFinish();
}

public static void sit()
{
	if (_sitForced)
		_sitForced = false;
	else
		_sitForced = true;

	for (L2PcInstance player : _players)
	{
		if (player != null)
		{
			if (_sitForced)
			{
				player.stopMove(null, false);
				player.abortAttack();
				player.abortCast();

				if (!player.isSitting())
					player.sitDown();
			}
			else
			{
				if (player.isSitting())
					player.standUp();
			}
		}
	}
}

public static void dumpData()
{
	_log.info("");
	_log.info("");

	if (!_joining && !_teleport && !_started)
	{
		_log.info("<<---------------------------------->>");
		_log.info(">> DM Engine infos dump (INACTIVE) <<");
		_log.info("<<--^----^^-----^----^^------^^----->>");
	}
	else if (_joining && !_teleport && !_started)
	{
		_log.info("<<--------------------------------->>");
		_log.info(">> DM Engine infos dump (JOINING) <<");
		_log.info("<<--^----^^-----^----^^------^----->>");
	}
	else if (!_joining && _teleport && !_started)
	{
		_log.info("<<---------------------------------->>");
		_log.info(">> DM Engine infos dump (TELEPORT) <<");
		_log.info("<<--^----^^-----^----^^------^^----->>");
	}
	else if (!_joining && !_teleport && _started)
	{
		_log.info("<<--------------------------------->>");
		_log.info(">> DM Engine infos dump (STARTED) <<");
		_log.info("<<--^----^^-----^----^^------^----->>");
	}

	_log.info("Name: " + _eventName);
	_log.info("Desc: " + _eventDesc);
	_log.info("Join location: " + _joiningLocationName);
	_log.info("Min lvl: " + _minlvl);
	_log.info("Max lvl: " + _maxlvl);

	_log.info("");
	_log.info("##################################");
	_log.info("# _players(Vector<L2PcInstance>) #");
	_log.info("##################################");

	_log.info("Total Players : " + _players.size());

	for (L2PcInstance player : _players)
	{
		if (player != null)
			_log.info("Name: " + player.getName()+ " kills :" + player._countDMkills);
	}

	_log.info("");
	_log.info("################################");
	_log.info("# _savePlayers(Vector<String>) #");
	_log.info("################################");

	for (String player : _savePlayers)
		_log.info("Name: " + player );

	_log.info("");
	_log.info("");
}

public static void loadData()
{
	_eventName = new String();
	_eventDesc = new String();
	_joiningLocationName = new String();
	_savePlayers = new Vector<String>();
	_players = new Vector<L2PcInstance>();
	_topPlayer = null;
	_npcSpawn = null;
	_joining = false;
	_teleport = false;
	_started = false;
	_sitForced = false;
	_npcId = 0;
	_npcX = 0;
	_npcY = 0;
	_npcZ = 0;
	_rewardId = 0;
	_rewardAmount = 0;
	_topKills = 0;
	_minlvl = 0;
	_maxlvl = 0;
	_playerColors = 0;
	_playerX = 0;
	_playerY = 0;
	_playerZ = 0;

	Connection con = null;
	try
	{
		PreparedStatement statement;
		ResultSet rs;


		con = L2DatabaseFactory.getInstance().getConnection();

		statement = con.prepareStatement("Select * from dm");
		rs = statement.executeQuery();

		while (rs.next())
		{
			_eventName = rs.getString("eventName");
			_eventDesc = rs.getString("eventDesc");
			_joiningLocationName = rs.getString("joiningLocation");
			_minlvl = rs.getInt("minlvl");
			_maxlvl = rs.getInt("maxlvl");
			_npcId = rs.getInt("npcId");
			_npcX = rs.getInt("npcX");
			_npcY = rs.getInt("npcY");
			_npcZ = rs.getInt("npcZ");
			_rewardId = rs.getInt("rewardId");
			_rewardAmount = rs.getInt("rewardAmount");
			_playerColors = rs.getInt("color");
			_playerX = rs.getInt("playerX");
			_playerY = rs.getInt("playerY");
			_playerZ = rs.getInt("playerZ");

		}
		statement.close();
	}
	catch (Exception e)
	{
		_log.warning("Exception: DM.loadData(): " + e.getMessage());
	}
	finally { try { if (con != null) con.close(); } catch (SQLException e) { e.printStackTrace(); } }
}

public static void saveData()
{
	Connection con = null;
	try
	{
		con = L2DatabaseFactory.getInstance().getConnection();
		PreparedStatement statement;

		statement = con.prepareStatement("Delete from dm");
		statement.execute();
		statement.close();

		statement = con.prepareStatement("INSERT INTO dm (eventName, eventDesc, joiningLocation, minlvl, maxlvl, npcId, npcX, npcY, npcZ, rewardId, rewardAmount, color, playerX, playerY, playerZ ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");  
		statement.setString(1, _eventName);
		statement.setString(2, _eventDesc);
		statement.setString(3, _joiningLocationName);
		statement.setInt(4, _minlvl);
		statement.setInt(5, _maxlvl);
		statement.setInt(6, _npcId);
		statement.setInt(7, _npcX);
		statement.setInt(8, _npcY);
		statement.setInt(9, _npcZ);
		statement.setInt(10, _rewardId);
		statement.setInt(11, _rewardAmount);
		statement.setInt(12, _playerColors);
		statement.setInt(13, _playerX);
		statement.setInt(14, _playerY);
		statement.setInt(15, _playerZ);
		statement.execute();
		statement.close();
	}
	catch (Exception e)
	{
		_log.warning("Exception: DM.saveData(): " + e.getMessage());
	}		
	finally { try { if (con != null) con.close(); } catch (SQLException e) { e.printStackTrace(); } }
}

public static void showEventHtml(L2PcInstance eventPlayer, String objectId)
{
	try
	{
		NpcHtmlMessage adminReply = new NpcHtmlMessage(0);
		TextBuilder replyMSG = new TextBuilder("<html><head><title>" + _eventName + "</title></head><body>");

		replyMSG.append("<center>");
		replyMSG.append("<table>");
		replyMSG.append("<tr>");
		replyMSG.append("<td>Name</td><td><font color=\"00FF00\">" + _eventName + "</font></td>");
		replyMSG.append("</tr>");
		replyMSG.append("<tr>");
		replyMSG.append("<td>Description</td><td><font color=\"00FF00\">" + _eventDesc + "</font></td>");
		replyMSG.append("</tr>");
		replyMSG.append("<tr>");
		replyMSG.append("<td></td><td><button value=\"Join\" action=\"bypass -h npc_" + objectId + "_dmevent_player_join\" width=50 height=15 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td>");

		if (!_started && !_joining)
		{
			replyMSG.append("<tr>");
			replyMSG.append("<td></td><td>Wait till the admin/gm start the participation.</td>");
			replyMSG.append("</tr>");
		}
		else if (!_started && _joining && eventPlayer.getLevel() >= _minlvl && eventPlayer.getLevel() <= _maxlvl)
		{
			if (_players.contains(eventPlayer))
			{
				replyMSG.append("<tr>");
				replyMSG.append("<td></td><td>You are already participating!</td>");
				replyMSG.append("</tr>");
				replyMSG.append("<tr>");
				replyMSG.append("<td>Wait till event start or</td>");
				replyMSG.append("<td><button value=\"remove\" action=\"bypass -h npc_" + objectId + "_dmevent_player_leave\" width=50 height=15 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"> your participation!</td>");
				replyMSG.append("</tr>");
			}
			else
			{
				replyMSG.append("<tr>");
				replyMSG.append("<td></td><td>You want to participate in the event?</td>");
				replyMSG.append("</tr>");
				replyMSG.append("<tr>");
				replyMSG.append("<td>Admin set min lvl : <font color=\"00FF00\">" + _minlvl + "</font></td>");
				replyMSG.append("</tr>");
				replyMSG.append("<tr>");
				replyMSG.append("<td>Admin set max lvl : <font color=\"00FF00\">" + _maxlvl + "</font></td>");
				replyMSG.append("</tr>");

				replyMSG.append("<tr>");
				replyMSG.append("<td></td><td><button value=\"Join\" action=\"bypass -h npc_" + objectId + "_dmevent_player_join\" width=50 height=15 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td>");

			}
		}
		else if (_started && !_joining)
		{
			replyMSG.append("<tr>");
			replyMSG.append("<td></td><td>DM match is in progress.</td>");
			replyMSG.append("</tr>");
		}
		else if (eventPlayer.getLevel() >= _minlvl || eventPlayer.getLevel() <= _maxlvl )
		{
			replyMSG.append("<tr>");
			replyMSG.append("<td>Your lvl</td><td><font color=\"00FF00\">" + eventPlayer.getLevel() +"</font></td>");
			replyMSG.append("</tr>");
			replyMSG.append("<tr>");
			replyMSG.append("<td>Admin set min lvl</td><td><font color=\"00FF00\">" + _minlvl + "</font></td>");
			replyMSG.append("</tr>");
			replyMSG.append("<tr>");
			replyMSG.append("<td>Admin set max lvl</td><td><font color=\"00FF00\">" + _maxlvl + "</font></td>");
			replyMSG.append("</tr>");
			replyMSG.append("<tr>");
			replyMSG.append("<td></td><td><font color=\"FFFF00\">You can't participate to this event.</font></td>");
			replyMSG.append("</tr>");
		}
		replyMSG.append("</table>");
		replyMSG.append("</center>");
		replyMSG.append("</body></html>");
		adminReply.setHtml(replyMSG.toString());
		eventPlayer.sendPacket(adminReply);

		// Send a Server->Client ActionFailed to the L2PcInstance in order to avoid that the client wait another packet
		eventPlayer.sendPacket(ActionFailed.STATIC_PACKET);
	}
	catch (Exception e)
	{
		_log.warning("DM Engine[showEventHtlm(" + eventPlayer.getName() + ", " + objectId + ")]: exception" + e.getMessage());
	}
}

public static void addPlayer(L2PcInstance player)
{
	if (!addPlayerOk(player))
		return;
	_players.add(player);
	player._originalNameColorDM = player.getAppearance().getNameColor();
	player._originalKarmaDM = player.getKarma();
	player._inEventDM = true;
	player._countDMkills = 0;
	_savePlayers.add(player.getName());

}

public static boolean addPlayerOk(L2PcInstance eventPlayer)
{

	if (eventPlayer._inEventDM)
	{
		eventPlayer.sendMessage("You are already participating in the event!"); 
		return false;
	}


	return true;
}

public static synchronized void addDisconnectedPlayer(L2PcInstance player)
{
	if ((_teleport || _started) || _savePlayers.contains(player.getName()))
	{
		if (Config.DM_ON_START_REMOVE_ALL_EFFECTS)
		{
			for (L2Effect e : player.getAllEffects())
			{
				if (e != null)
					e.exit();
			}
		}
		for (L2PcInstance p : _players)
		{
			if (p==null)
			{
				continue;
			}
			//check by name incase player got new objectId
			else if (p.getName().equals(player.getName()))
			{
				player._originalNameColorDM = player.getAppearance().getNameColor();
				player._originalKarmaDM = player.getKarma();
				player._inEventDM = true;
				player._countDMkills =p._countDMkills;
				_players.remove(p); //removing old object id from vector
				_players.add(player); //adding new objectId to vector
				break;
			}
		}

		player.getAppearance().setNameColor(_playerColors);
		player.setKarma(0);
		player.broadcastUserInfo();
		player.teleToLocation(_playerX, _playerY , _playerZ, false);
	}
}

public static void removePlayer(L2PcInstance player)
{
	if (player != null)
	{
		_players.remove(player);
	}
}

public static void cleanDM()
{
	for (L2PcInstance player : _players)
	{
		removePlayer(player);
	}

	_savePlayers = new Vector<String>();
	_topPlayer = null;
	_npcSpawn = null;
	_joining = false;
	_teleport = false;
	_started = false;
	_sitForced = false;
	_topKills = 0;
	_players = new Vector<L2PcInstance>();

}

public static void unspawnEventNpc()
{
	if (_npcSpawn == null)
		return;

	_npcSpawn.getLastSpawn().deleteMe();
	_npcSpawn.stopRespawn();
	SpawnTable.getInstance().deleteSpawn(_npcSpawn, true);
}

public static void teleportFinish()
{
	Announcements.getInstance().announceToAll(_eventName + "(DM): Teleport back to participation NPC in 20 seconds!");

	removeUserData();
	ThreadPoolManager.getInstance().scheduleGeneral(new Runnable()
	{
		public void run()
		{
			for (L2PcInstance player : _players)
			{
				if (player !=  null && player.isOnline())
					player.teleToLocation(_npcX, _npcY, _npcZ, false);
			} 
			cleanDM();
		}
	}, 20000);
}
}

2 answers to this question

Recommended Posts

Guest
This topic is now closed to further replies.


×
×
  • Create New...