/*
* 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 handlers.itemhandlers;
import com.l2jserver.gameserver.data.xml.impl.NpcData;
import com.l2jserver.gameserver.handler.IItemHandler;
import com.l2jserver.gameserver.model.L2Spawn;
import com.l2jserver.gameserver.model.actor.L2Npc;
import com.l2jserver.gameserver.model.actor.L2Playable;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
import com.l2jserver.gameserver.network.SystemMessageId;
/**
* Custom item handler to allow items to summon NPCs.
* @author Zoey76
* updated by mrnapz
*/
public final class SummonNpc implements IItemHandler
{
private static final int[][] _itemIdNpcIdNpcLifeTimes =
{
{
00000, //YOUR ITEM ID
11111, //YOUR NPC ID
60 //SPAWN TIME
}
};
@Override
public boolean useItem(L2Playable playable, L2ItemInstance item, boolean forceUse)
{
if ((playable == null) || (item == null) || !(playable instanceof L2PcInstance))
{
return false;
}
final L2PcInstance activeChar = playable.getActingPlayer();
if (!activeChar.getFloodProtectors().getItemPetSummon().tryPerformAction("summon items"))
{
return false;
}
if (activeChar.getBlockCheckerArena() != -1)
{
return false;
}
if (activeChar.inObserverMode())
{
return false;
}
if (activeChar.isInOlympiadMode())
{
activeChar.sendPacket(SystemMessageId.YOU_CANNOT_USE_THAT_ITEM_IN_A_OLYMPIAD_MATCH);
return false;
}
if (activeChar.isAllSkillsDisabled() || activeChar.isCastingNow())
{
return false;
}
L2NpcTemplate npcTemplate = null;
int lifeTime = 0;
final int itemId = item.getId();
for (int[] data : _itemIdNpcIdNpcLifeTimes)
{
if (data[0] == itemId)
{
npcTemplate = NpcData.getInstance().getTemplate(data[1]);
lifeTime = data[2];
break;
}
}
activeChar.stopMove(null, false);
if ((npcTemplate != null) && (lifeTime > 0))
{
activeChar.stopMove(null, false);
try
{
final L2Spawn spawn = new L2Spawn(npcTemplate);
if (activeChar.destroyItem("Consume", item.getObjectId(), 0, null, true))
{
spawn.setX(activeChar.getX());
spawn.setY(activeChar.getY());
spawn.setZ(activeChar.getZ());
spawn.setHeading(activeChar.getHeading());
final L2Npc npc = spawn.doSpawn(true);
npc.setTitle(activeChar.getName());
npc.setIsRunning(false); // Broadcast info
npc.scheduleDespawn(lifeTime * 1000L);
activeChar.sendMessage("Summoned " + npcTemplate.getName() + ".");
}
else
{
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
return true;
}
}