123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- /*
- * 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/>.
- */
- /**
- *
- * @author FBIagent
- *
- */
- package net.sf.l2j.gameserver.handler.itemhandlers;
- import net.sf.l2j.gameserver.ThreadPoolManager;
- import net.sf.l2j.gameserver.datatables.NpcTable;
- import net.sf.l2j.gameserver.datatables.SummonItemsData;
- import net.sf.l2j.gameserver.handler.IItemHandler;
- import net.sf.l2j.gameserver.idfactory.IdFactory;
- import net.sf.l2j.gameserver.model.L2ItemInstance;
- import net.sf.l2j.gameserver.model.L2Spawn;
- import net.sf.l2j.gameserver.model.L2SummonItem;
- import net.sf.l2j.gameserver.model.L2World;
- import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
- import net.sf.l2j.gameserver.model.actor.instance.L2PetInstance;
- import net.sf.l2j.gameserver.model.actor.instance.L2PlayableInstance;
- import net.sf.l2j.gameserver.model.entity.TvTEvent;
- import net.sf.l2j.gameserver.network.SystemMessageId;
- import net.sf.l2j.gameserver.network.serverpackets.MagicSkillLaunched;
- import net.sf.l2j.gameserver.network.serverpackets.MagicSkillUse;
- import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
- import net.sf.l2j.gameserver.templates.L2NpcTemplate;
- import net.sf.l2j.gameserver.util.FloodProtector;
- public class SummonItems implements IItemHandler
- {
- public void useItem(L2PlayableInstance playable, L2ItemInstance item)
- {
- if (!(playable instanceof L2PcInstance))
- return;
- if (!TvTEvent.onItemSummon(playable.getObjectId()))
- return;
- L2PcInstance activeChar = (L2PcInstance)playable;
- if ( !FloodProtector.getInstance().tryPerformAction(activeChar.getObjectId(), FloodProtector.PROTECTED_ITEMPETSUMMON) ) return;
- if(activeChar.isSitting())
- {
- activeChar.sendPacket(new SystemMessage(SystemMessageId.CANT_MOVE_SITTING));
- return;
- }
- if (activeChar.inObserverMode())
- return;
- if (activeChar.isInOlympiadMode())
- {
- activeChar.sendPacket(new SystemMessage(SystemMessageId.THIS_ITEM_IS_NOT_AVAILABLE_FOR_THE_OLYMPIAD_EVENT));
- return;
- }
- L2SummonItem sitem = SummonItemsData.getInstance().getSummonItem(item.getItemId());
- if ((activeChar.getPet() != null || activeChar.isMounted()) && sitem.isPetSummon())
- {
- activeChar.sendPacket(new SystemMessage(SystemMessageId.YOU_ALREADY_HAVE_A_PET));
- return;
- }
- if (activeChar.isAttackingNow())
- {
- activeChar.sendPacket(new SystemMessage(SystemMessageId.YOU_CANNOT_SUMMON_IN_COMBAT));
- return;
- }
- if (activeChar.isCursedWeaponEquipped() && sitem.isPetSummon())
- {
- activeChar.sendPacket(new SystemMessage(SystemMessageId.STRIDER_CANT_BE_RIDDEN_WHILE_IN_BATTLE));
- return;
- }
- int npcID = sitem.getNpcId();
- if (npcID == 0)
- return;
- L2NpcTemplate npcTemplate = NpcTable.getInstance().getTemplate(npcID);
- if (npcTemplate == null)
- return;
- switch (sitem.getType())
- {
- case 0: // static summons (like Christmas tree)
- try
- {
- L2Spawn spawn = new L2Spawn(npcTemplate);
- if (spawn == null)
- return;
- spawn.setId(IdFactory.getInstance().getNextId());
- spawn.setLocx(activeChar.getX());
- spawn.setLocy(activeChar.getY());
- spawn.setLocz(activeChar.getZ());
- L2World.getInstance().storeObject(spawn.spawnOne(true));
- activeChar.destroyItem("Summon", item.getObjectId(), 1, null, false);
- activeChar.sendMessage("Created " + npcTemplate.name + " at x: " + spawn.getLocx() + " y: " + spawn.getLocy() + " z: " + spawn.getLocz());
- }
- catch (Exception e)
- {
- activeChar.sendMessage("Target is not ingame.");
- }
- break;
- case 1: // pet summons
- L2PetInstance petSummon = L2PetInstance.spawnPet(npcTemplate, activeChar, item);
- if (petSummon == null)
- break;
- petSummon.setTitle(activeChar.getName());
- if (!petSummon.isRespawned())
- {
- petSummon.setCurrentHp(petSummon.getMaxHp());
- petSummon.setCurrentMp(petSummon.getMaxMp());
- petSummon.getStat().setExp(petSummon.getExpForThisLevel());
- petSummon.setCurrentFed(petSummon.getMaxFed());
- }
- petSummon.setRunning();
- if (!petSummon.isRespawned())
- petSummon.store();
- activeChar.setPet(petSummon);
- activeChar.sendPacket(new MagicSkillUse(activeChar, 2046, 1, 1000, 600000));
- activeChar.sendPacket(new SystemMessage(SystemMessageId.SUMMON_A_PET));
- L2World.getInstance().storeObject(petSummon);
- petSummon.spawnMe(activeChar.getX()+50, activeChar.getY()+100, activeChar.getZ());
- petSummon.startFeed(false);
- item.setEnchantLevel(petSummon.getLevel());
- ThreadPoolManager.getInstance().scheduleGeneral(new PetSummonFinalizer(activeChar, petSummon), 900);
- if (petSummon.getCurrentFed() <= 0)
- ThreadPoolManager.getInstance().scheduleGeneral(new PetSummonFeedWait(activeChar, petSummon), 60000);
- else
- petSummon.startFeed(false);
- break;
- case 2: // wyvern
- activeChar.mount(sitem.getNpcId(), item.getObjectId());
- break;
- case 3: // Great Wolf
- activeChar.mount(sitem.getNpcId(), item.getObjectId());
- break;
- }
- }
- static class PetSummonFeedWait implements Runnable
- {
- private L2PcInstance _activeChar;
- private L2PetInstance _petSummon;
- PetSummonFeedWait(L2PcInstance activeChar, L2PetInstance petSummon)
- {
- _activeChar = activeChar;
- _petSummon = petSummon;
- }
- public void run()
- {
- try
- {
- if (_petSummon.getCurrentFed() <= 0 )
- _petSummon.unSummon(_activeChar);
- else
- _petSummon.startFeed(false);
- }
- catch (Throwable e)
- {
- }
- }
- }
- static class PetSummonFinalizer implements Runnable
- {
- private L2PcInstance _activeChar;
- private L2PetInstance _petSummon;
- PetSummonFinalizer(L2PcInstance activeChar, L2PetInstance petSummon)
- {
- _activeChar = activeChar;
- _petSummon = petSummon;
- }
- public void run()
- {
- try
- {
- _activeChar.sendPacket(new MagicSkillLaunched(_activeChar, 2046, 1));
- _petSummon.setFollowStatus(true);
- _petSummon.setShowSummonAnimation(false);
- }
- catch (Throwable e)
- {
- }
- }
- }
- public int[] getItemIds()
- {
- return SummonItemsData.getInstance().itemIDs();
- }
- }
|