Skip to content
Snippets Groups Projects
ItemSkillsTemplate.java 6.3 KiB
Newer Older
DrLecter's avatar
DrLecter committed
/*
Zoey76's avatar
Zoey76 committed
 * Copyright © 2004-2023 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/>.
DrLecter's avatar
DrLecter committed
 */
Zoey76's avatar
Zoey76 committed
package com.l2jserver.datapack.handlers.itemhandlers;
DrLecter's avatar
DrLecter committed

import com.l2jserver.gameserver.ai.CtrlIntention;
import com.l2jserver.gameserver.handler.IItemHandler;
import com.l2jserver.gameserver.model.actor.L2Playable;
import com.l2jserver.gameserver.model.entity.TvTEvent;
Zoey76's avatar
Zoey76 committed
import com.l2jserver.gameserver.model.holders.SkillHolder;
import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
Nos's avatar
Nos committed
import com.l2jserver.gameserver.model.skills.Skill;
DrLecter's avatar
DrLecter committed
import com.l2jserver.gameserver.network.SystemMessageId;
import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
import com.l2jserver.gameserver.network.serverpackets.SystemMessage;

/**
 * Template for item skills handler.
 * @author Zoey76
DrLecter's avatar
DrLecter committed
 */
Zoey76's avatar
Zoey76 committed
public class ItemSkillsTemplate implements IItemHandler {
	@Override
Zoey76's avatar
Zoey76 committed
	public boolean useItem(L2Playable playable, L2ItemInstance item, boolean forceUse) {
		if (!playable.isPlayer() && !playable.isPet()) {
Zoey76's avatar
Zoey76 committed
			return false;
Zoey76's avatar
Zoey76 committed
		if (!TvTEvent.onScrollUse(playable.getObjectId())) {
DrLecter's avatar
DrLecter committed
			playable.sendPacket(ActionFailed.STATIC_PACKET);
Zoey76's avatar
Zoey76 committed
			return false;
Zoey76's avatar
Zoey76 committed
		// Pets can use items only when they are tradable.
Zoey76's avatar
Zoey76 committed
		if (playable.isPet() && !item.isTradeable()) {
			playable.sendPacket(SystemMessageId.ITEM_NOT_FOR_PETS);
Zoey76's avatar
Zoey76 committed
			return false;
		}
		
		// Verify that item is not under reuse.
Zoey76's avatar
Zoey76 committed
		if (!checkReuse(playable, null, item)) {
Zoey76's avatar
Zoey76 committed
			return false;
DrLecter's avatar
DrLecter committed
		}
		
		final SkillHolder[] skills = item.getEtcItem().getSkills();
Zoey76's avatar
Zoey76 committed
		if (skills == null) {
Zoey76's avatar
Zoey76 committed
			_log.info("Item " + item + " does not have registered any skill for handler.");
			return false;
		}
		
		boolean hasConsumeSkill = false;
		
Zoey76's avatar
Zoey76 committed
		for (SkillHolder skillInfo : skills) {
			if (skillInfo == null) {
Zoey76's avatar
Zoey76 committed
				continue;
			}
			
Nos's avatar
Nos committed
			Skill itemSkill = skillInfo.getSkill();
Zoey76's avatar
Zoey76 committed
			if (itemSkill != null) {
				if (itemSkill.getItemConsumeId() > 0) {
					hasConsumeSkill = true;
				}
				
Maneco2's avatar
Maneco2 committed
				if (!itemSkill.checkCondition(playable, itemSkill.getTargetType().getTarget(itemSkill, playable, playable.getTarget()), false)) {
Zoey76's avatar
Zoey76 committed
					return false;
Zoey76's avatar
Zoey76 committed
				if (playable.isSkillDisabled(itemSkill)) {
					return false;
				}
				
				// Verify that skill is not under reuse.
Zoey76's avatar
Zoey76 committed
				if (!checkReuse(playable, itemSkill, item)) {
Zoey76's avatar
Zoey76 committed
					return false;
				}
				if (!item.isPotion() && !item.isElixir() && !item.isScroll() && !item.getItem().hasExImmediateEffect() && playable.isCastingNow()) {
Zoey76's avatar
Zoey76 committed
					return false;
				}
				
				// Send message to the master.
Zoey76's avatar
Zoey76 committed
				if (playable.isPet()) {
Zoey76's avatar
Zoey76 committed
					SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.PET_USES_S1);
					sm.addSkillName(itemSkill);
					playable.sendPacket(sm);
Zoey76's avatar
Zoey76 committed
				if (itemSkill.isSimultaneousCast() || ((item.getItem().hasImmediateEffect() || item.getItem().hasExImmediateEffect()) && itemSkill.isStatic())) {
Zoey76's avatar
Zoey76 committed
					playable.doSimultaneousCast(itemSkill);
Zoey76's avatar
Zoey76 committed
				} else {
Zoey76's avatar
Zoey76 committed
					playable.getAI().setIntention(CtrlIntention.AI_INTENTION_IDLE);
Zoey76's avatar
Zoey76 committed
					if (!playable.useMagic(itemSkill, forceUse, false)) {
Zoey76's avatar
Zoey76 committed
						return false;
Zoey76's avatar
Zoey76 committed
				if (itemSkill.getReuseDelay() > 0) {
					playable.addTimeStamp(itemSkill, itemSkill.getReuseDelay());
Zoey76's avatar
Zoey76 committed
		if (checkConsume(item, hasConsumeSkill)) {
			if (!playable.destroyItem("Consume", item.getObjectId(), 1, playable, false)) {
Zealar's avatar
Zealar committed
				playable.sendPacket(SystemMessageId.NOT_ENOUGH_ITEMS);
				return false;
			}
		}
		
Zoey76's avatar
Zoey76 committed
		return true;
Zealar's avatar
Zealar committed
	/**
	 * @param item the item being used
	 * @param hasConsumeSkill
Zealar's avatar
Zealar committed
	 * @return {@code true} check if item use consume item, {@code false} otherwise
	 */
Zoey76's avatar
Zoey76 committed
	private boolean checkConsume(L2ItemInstance item, boolean hasConsumeSkill) {
Zoey76's avatar
Zoey76 committed
		switch (item.getItem().getDefaultAction()) {
Zealar's avatar
Zealar committed
			case CAPSULE:
Zoey76's avatar
Zoey76 committed
			case SKILL_REDUCE: {
				if (!hasConsumeSkill && item.getItem().hasImmediateEffect()) {
Zealar's avatar
Zealar committed
					return true;
				}
			}
		}
Zoey76's avatar
Zoey76 committed
	/**
	 * @param playable the character using the item or skill
	 * @param skill the skill being used, can be null
	 * @param item the item being used
	 * @return {@code true} if the the item or skill to check is available, {@code false} otherwise
Zoey76's avatar
Zoey76 committed
	private boolean checkReuse(L2Playable playable, Skill skill, L2ItemInstance item) {
		final long remainingTime = (skill != null) ? playable.getSkillRemainingReuseTime(skill.getReuseHashCode()) : playable.getItemRemainingReuseTime(item.getObjectId());
Zoey76's avatar
Zoey76 committed
		final boolean isAvailable = remainingTime <= 0;
Zoey76's avatar
Zoey76 committed
		if (playable.isPlayer()) {
			if (!isAvailable) {
				final int hours = (int) (remainingTime / 3600000L);
				final int minutes = (int) (remainingTime % 3600000L) / 60000;
				final int seconds = (int) ((remainingTime / 1000) % 60);
				SystemMessage sm = null;
Zoey76's avatar
Zoey76 committed
				if (hours > 0) {
					sm = SystemMessage.getSystemMessage(SystemMessageId.S2_HOURS_S3_MINUTES_S4_SECONDS_REMAINING_FOR_REUSE_S1);
Zoey76's avatar
Zoey76 committed
					if ((skill == null) || skill.isStatic()) {
Zoey76's avatar
Zoey76 committed
					} else {
					sm.addInt(hours);
					sm.addInt(minutes);
Zoey76's avatar
Zoey76 committed
				} else if (minutes > 0) {
					sm = SystemMessage.getSystemMessage(SystemMessageId.S2_MINUTES_S3_SECONDS_REMAINING_FOR_REUSE_S1);
Zoey76's avatar
Zoey76 committed
					if ((skill == null) || skill.isStatic()) {
Zoey76's avatar
Zoey76 committed
					} else {
					sm.addInt(minutes);
Zoey76's avatar
Zoey76 committed
				} else {
					sm = SystemMessage.getSystemMessage(SystemMessageId.S2_SECONDS_REMAINING_FOR_REUSE_S1);
Zoey76's avatar
Zoey76 committed
					if ((skill == null) || skill.isStatic()) {
Zoey76's avatar
Zoey76 committed
					} else {
				sm.addInt(seconds);
Zoey76's avatar
Zoey76 committed
		return isAvailable;