12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268 |
- /*
- * 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.model.quest;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.util.Calendar;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
- import com.l2jserver.gameserver.cache.HtmCache;
- import com.l2jserver.gameserver.enums.QuestSound;
- import com.l2jserver.gameserver.enums.QuestType;
- import com.l2jserver.gameserver.instancemanager.QuestManager;
- 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.events.AbstractScript;
- import com.l2jserver.gameserver.model.holders.ItemHolder;
- import com.l2jserver.gameserver.model.itemcontainer.Inventory;
- import com.l2jserver.gameserver.network.serverpackets.ExShowQuestMark;
- import com.l2jserver.gameserver.network.serverpackets.PlaySound;
- import com.l2jserver.gameserver.network.serverpackets.QuestList;
- import com.l2jserver.gameserver.network.serverpackets.TutorialCloseHtml;
- import com.l2jserver.gameserver.network.serverpackets.TutorialEnableClientEvent;
- import com.l2jserver.gameserver.network.serverpackets.TutorialShowHtml;
- import com.l2jserver.gameserver.network.serverpackets.TutorialShowQuestionMark;
- import com.l2jserver.gameserver.util.Util;
- /**
- * Quest state class.
- * @author Luis Arias
- */
- public final class QuestState
- {
- protected static final Logger _log = Logger.getLogger(QuestState.class.getName());
-
- /** The name of the quest of this QuestState */
- private final String _questName;
-
- /** The "owner" of this QuestState object */
- private final L2PcInstance _player;
-
- /** The current state of the quest */
- private byte _state;
-
- /** A map of key->value pairs containing the quest state variables and their values */
- private Map<String, String> _vars;
-
- /**
- * boolean flag letting QuestStateManager know to exit quest when cleaning up
- */
- private boolean _isExitQuestOnCleanUp = false;
-
- /**
- * Constructor of the QuestState. Creates the QuestState object and sets the player's progress of the quest to this QuestState.
- * @param quest the {@link Quest} object associated with the QuestState
- * @param player the owner of this {@link QuestState} object
- * @param state the initial state of the quest
- */
- public QuestState(Quest quest, L2PcInstance player, byte state)
- {
- _questName = quest.getName();
- _player = player;
- _state = state;
-
- player.setQuestState(this);
- }
-
- /**
- * @return the name of the quest of this QuestState
- */
- public String getQuestName()
- {
- return _questName;
- }
-
- /**
- * @return the {@link Quest} object of this QuestState
- */
- public Quest getQuest()
- {
- return QuestManager.getInstance().getQuest(_questName);
- }
-
- /**
- * @return the {@link L2PcInstance} object of the owner of this QuestState
- */
- public L2PcInstance getPlayer()
- {
- return _player;
- }
-
- /**
- * @return the current State of this QuestState
- * @see com.l2jserver.gameserver.model.quest.State
- */
- public byte getState()
- {
- return _state;
- }
-
- /**
- * @return {@code true} if the State of this QuestState is CREATED, {@code false} otherwise
- * @see com.l2jserver.gameserver.model.quest.State
- */
- public boolean isCreated()
- {
- return (_state == State.CREATED);
- }
-
- /**
- * @return {@code true} if the State of this QuestState is STARTED, {@code false} otherwise
- * @see com.l2jserver.gameserver.model.quest.State
- */
- public boolean isStarted()
- {
- return (_state == State.STARTED);
- }
-
- /**
- * @return {@code true} if the State of this QuestState is COMPLETED, {@code false} otherwise
- * @see com.l2jserver.gameserver.model.quest.State
- */
- public boolean isCompleted()
- {
- return (_state == State.COMPLETED);
- }
-
- /**
- * @param state the new state of the quest to set
- * @return {@code true} if state was changed, {@code false} otherwise
- * @see #setState(byte state, boolean saveInDb)
- * @see com.l2jserver.gameserver.model.quest.State
- */
- public boolean setState(byte state)
- {
- return setState(state, true);
- }
-
- /**
- * Change the state of this quest to the specified value.
- * @param state the new state of the quest to set
- * @param saveInDb if {@code true}, will save the state change in the database
- * @return {@code true} if state was changed, {@code false} otherwise
- * @see com.l2jserver.gameserver.model.quest.State
- */
- public boolean setState(byte state, boolean saveInDb)
- {
- if (_state == state)
- {
- return false;
- }
- final boolean newQuest = isCreated();
- _state = state;
- if (saveInDb)
- {
- if (newQuest)
- {
- Quest.createQuestInDb(this);
- }
- else
- {
- Quest.updateQuestInDb(this);
- }
- }
-
- _player.sendPacket(new QuestList());
- return true;
- }
-
- /**
- * Add parameter used in quests.
- * @param var String pointing out the name of the variable for quest
- * @param val String pointing out the value of the variable for quest
- * @return String (equal to parameter "val")
- */
- public String setInternal(String var, String val)
- {
- if (_vars == null)
- {
- _vars = new HashMap<>();
- }
-
- if (val == null)
- {
- val = "";
- }
-
- _vars.put(var, val);
- return val;
- }
-
- public String set(String var, int val)
- {
- return set(var, Integer.toString(val));
- }
-
- /**
- * Return value of parameter "val" after adding the couple (var,val) in class variable "vars".<br>
- * Actions:<br>
- * <ul>
- * <li>Initialize class variable "vars" if is null.</li>
- * <li>Initialize parameter "val" if is null</li>
- * <li>Add/Update couple (var,val) in class variable Map "vars"</li>
- * <li>If the key represented by "var" exists in Map "vars", the couple (var,val) is updated in the database.<br>
- * The key is known as existing if the preceding value of the key (given as result of function put()) is not null.<br>
- * If the key doesn't exist, the couple is added/created in the database</li>
- * <ul>
- * @param var String indicating the name of the variable for quest
- * @param val String indicating the value of the variable for quest
- * @return String (equal to parameter "val")
- */
- public String set(String var, String val)
- {
- if (_vars == null)
- {
- _vars = new HashMap<>();
- }
-
- if (val == null)
- {
- val = "";
- }
-
- String old = _vars.put(var, val);
- if (old != null)
- {
- Quest.updateQuestVarInDb(this, var, val);
- }
- else
- {
- Quest.createQuestVarInDb(this, var, val);
- }
-
- if ("cond".equals(var))
- {
- try
- {
- int previousVal = 0;
- try
- {
- previousVal = Integer.parseInt(old);
- }
- catch (Exception ex)
- {
- previousVal = 0;
- }
- setCond(Integer.parseInt(val), previousVal);
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, _player.getName() + ", " + getQuestName() + " cond [" + val + "] is not an integer. Value stored, but no packet was sent: " + e.getMessage(), e);
- }
- }
-
- return val;
- }
-
- /**
- * Internally handles the progression of the quest so that it is ready for sending appropriate packets to the client.<br>
- * <u><i>Actions :</i></u><br>
- * <ul>
- * <li>Check if the new progress number resets the quest to a previous (smaller) step.</li>
- * <li>If not, check if quest progress steps have been skipped.</li>
- * <li>If skipped, prepare the variable completedStateFlags appropriately to be ready for sending to clients.</li>
- * <li>If no steps were skipped, flags do not need to be prepared...</li>
- * <li>If the passed step resets the quest to a previous step, reset such that steps after the parameter are not considered, while skipped steps before the parameter, if any, maintain their info.</li>
- * </ul>
- * @param cond the current quest progress condition (0 - 31 including)
- * @param old the previous quest progress condition to check against
- */
- private void setCond(int cond, int old)
- {
- if (cond == old)
- {
- return;
- }
-
- int completedStateFlags = 0;
- // cond 0 and 1 do not need completedStateFlags. Also, if cond > 1, the 1st step must
- // always exist (i.e. it can never be skipped). So if cond is 2, we can still safely
- // assume no steps have been skipped.
- // Finally, more than 31 steps CANNOT be supported in any way with skipping.
- if ((cond < 3) || (cond > 31))
- {
- unset("__compltdStateFlags");
- }
- else
- {
- completedStateFlags = getInt("__compltdStateFlags");
- }
-
- // case 1: No steps have been skipped so far...
- if (completedStateFlags == 0)
- {
- // check if this step also doesn't skip anything. If so, no further work is needed
- // also, in this case, no work is needed if the state is being reset to a smaller value
- // in those cases, skip forward to informing the client about the change...
-
- // ELSE, if we just now skipped for the first time...prepare the flags!!!
- if (cond > (old + 1))
- {
- // set the most significant bit to 1 (indicates that there exist skipped states)
- // also, ensure that the least significant bit is an 1 (the first step is never skipped, no matter
- // what the cond says)
- completedStateFlags = 0x80000001;
-
- // since no flag had been skipped until now, the least significant bits must all
- // be set to 1, up until "old" number of bits.
- completedStateFlags |= ((1 << old) - 1);
-
- // now, just set the bit corresponding to the passed cond to 1 (current step)
- completedStateFlags |= (1 << (cond - 1));
- set("__compltdStateFlags", String.valueOf(completedStateFlags));
- }
- }
- // case 2: There were exist previously skipped steps
- // if this is a push back to a previous step, clear all completion flags ahead
- else if (cond < old)
- {
- // note, this also unsets the flag indicating that there exist skips
- completedStateFlags &= ((1 << cond) - 1);
-
- // now, check if this resulted in no steps being skipped any more
- if (completedStateFlags == ((1 << cond) - 1))
- {
- unset("__compltdStateFlags");
- }
- else
- {
- // set the most significant bit back to 1 again, to correctly indicate that this skips states.
- // also, ensure that the least significant bit is an 1 (the first step is never skipped, no matter
- // what the cond says)
- completedStateFlags |= 0x80000001;
- set("__compltdStateFlags", String.valueOf(completedStateFlags));
- }
- }
- // If this moves forward, it changes nothing on previously skipped steps.
- // Just mark this state and we are done.
- else
- {
- completedStateFlags |= (1 << (cond - 1));
- set("__compltdStateFlags", String.valueOf(completedStateFlags));
- }
-
- // send a packet to the client to inform it of the quest progress (step change)
- _player.sendPacket(new QuestList());
-
- final Quest q = getQuest();
- if (!q.isCustomQuest() && (cond > 0))
- {
- _player.sendPacket(new ExShowQuestMark(q.getId()));
- }
- }
-
- /**
- * Removes a quest variable from the list of existing quest variables.
- * @param var the name of the variable to remove
- * @return the previous value of the variable or {@code null} if none were found
- */
- public String unset(String var)
- {
- if (_vars == null)
- {
- return null;
- }
-
- String old = _vars.remove(var);
- if (old != null)
- {
- Quest.deleteQuestVarInDb(this, var);
- }
- return old;
- }
-
- /**
- * Insert (or update) in the database variables that need to stay persistent for this player after a reboot. This function is for storage of values that are not related to a specific quest but are global instead, i.e. can be used by any script.
- * @param var the name of the variable to save
- * @param value the value of the variable
- */
- // TODO: these methods should not be here, they could be used by other classes to save some variables, but they can't because they require to create a QuestState first.
- public final void saveGlobalQuestVar(String var, String value)
- {
- try (Connection con = ConnectionFactory.getInstance().getConnection();
- PreparedStatement ps = con.prepareStatement("REPLACE INTO character_quest_global_data (charId, var, value) VALUES (?, ?, ?)"))
- {
- ps.setInt(1, _player.getObjectId());
- ps.setString(2, var);
- ps.setString(3, value);
- ps.executeUpdate();
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "Could not insert player's global quest variable: " + e.getMessage(), e);
- }
- }
-
- /**
- * Read from the database a previously saved variable for this quest.<br>
- * Due to performance considerations, this function should best be used only when the quest is first loaded.<br>
- * Subclasses of this class can define structures into which these loaded values can be saved.<br>
- * However, on-demand usage of this function throughout the script is not prohibited, only not recommended.<br>
- * Values read from this function were entered by calls to "saveGlobalQuestVar".
- * @param var the name of the variable whose value to get
- * @return the value of the variable or an empty string if the variable does not exist in the database
- */
- // TODO: these methods should not be here, they could be used by other classes to save some variables, but they can't because they require to create a QuestState first.
- public final String getGlobalQuestVar(String var)
- {
- String result = "";
- try (Connection con = ConnectionFactory.getInstance().getConnection();
- PreparedStatement ps = con.prepareStatement("SELECT value FROM character_quest_global_data WHERE charId = ? AND var = ?"))
- {
- ps.setInt(1, _player.getObjectId());
- ps.setString(2, var);
- try (ResultSet rs = ps.executeQuery())
- {
- if (rs.first())
- {
- result = rs.getString(1);
- }
- }
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "Could not load player's global quest variable: " + e.getMessage(), e);
- }
- return result;
- }
-
- /**
- * Permanently delete a global quest variable from the database.
- * @param var the name of the variable to delete
- */
- public final void deleteGlobalQuestVar(String var)
- {
- try (Connection con = ConnectionFactory.getInstance().getConnection();
- PreparedStatement ps = con.prepareStatement("DELETE FROM character_quest_global_data WHERE charId = ? AND var = ?"))
- {
- ps.setInt(1, _player.getObjectId());
- ps.setString(2, var);
- ps.executeUpdate();
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "could not delete player's global quest variable; charId = " + _player.getObjectId() + ", variable name = " + var + ". Exception: " + e.getMessage(), e);
- }
- }
-
- /**
- * @param var the name of the variable to get
- * @return the value of the variable from the list of quest variables
- */
- public String get(String var)
- {
- if (_vars == null)
- {
- return null;
- }
-
- return _vars.get(var);
- }
-
- /**
- * @param var the name of the variable to get
- * @return the integer value of the variable or 0 if the variable does not exist or its value is not an integer
- */
- public int getInt(String var)
- {
- if (_vars == null)
- {
- return 0;
- }
-
- final String variable = _vars.get(var);
- if ((variable == null) || variable.isEmpty())
- {
- return 0;
- }
-
- int varint = 0;
- try
- {
- varint = Integer.parseInt(variable);
- }
- catch (NumberFormatException nfe)
- {
- _log.log(Level.INFO, "Quest " + getQuestName() + ", method getInt(" + var + "), tried to parse a non-integer value (" + variable + "). Char Id: " + _player.getObjectId(), nfe);
- }
-
- return varint;
- }
-
- /**
- * Checks if the quest state progress ({@code cond}) is at the specified step.
- * @param condition the condition to check against
- * @return {@code true} if the quest condition is equal to {@code condition}, {@code false} otherwise
- * @see #getInt(String var)
- */
- public boolean isCond(int condition)
- {
- return (getInt("cond") == condition);
- }
-
- /**
- * Sets the quest state progress ({@code cond}) to the specified step.
- * @param value the new value of the quest state progress
- * @return this {@link QuestState} object
- * @see #set(String var, String val)
- * @see #setCond(int, boolean)
- */
- public QuestState setCond(int value)
- {
- if (isStarted())
- {
- set("cond", Integer.toString(value));
- }
- return this;
- }
-
- /**
- * @return the current quest progress ({@code cond})
- */
- public int getCond()
- {
- if (isStarted())
- {
- return getInt("cond");
- }
- return 0;
- }
-
- /**
- * Check if a given variable is set for this quest.
- * @param variable the variable to check
- * @return {@code true} if the variable is set, {@code false} otherwise
- * @see #get(String)
- * @see #getInt(String)
- * @see #getCond()
- */
- public boolean isSet(String variable)
- {
- return (get(variable) != null);
- }
-
- /**
- * Sets the quest state progress ({@code cond}) to the specified step.
- * @param value the new value of the quest state progress
- * @param playQuestMiddle if {@code true}, plays "ItemSound.quest_middle"
- * @return this {@link QuestState} object
- * @see #setCond(int value)
- * @see #set(String var, String val)
- */
- public QuestState setCond(int value, boolean playQuestMiddle)
- {
- if (!isStarted())
- {
- return this;
- }
- set("cond", String.valueOf(value));
-
- if (playQuestMiddle)
- {
- AbstractScript.playSound(_player, QuestSound.ITEMSOUND_QUEST_MIDDLE);
- }
- return this;
- }
-
- public QuestState setMemoState(int value)
- {
- set("memoState", String.valueOf(value));
- return this;
- }
-
- /**
- * @return the current Memo State
- */
- public int getMemoState()
- {
- if (isStarted())
- {
- return getInt("memoState");
- }
- return 0;
- }
-
- public boolean isMemoState(int memoState)
- {
- return (getInt("memoState") == memoState);
- }
-
- /**
- * Gets the memo state ex.
- * @param slot the slot where the value was saved
- * @return the memo state ex
- */
- public int getMemoStateEx(int slot)
- {
- if (isStarted())
- {
- return getInt("memoStateEx" + slot);
- }
- return 0;
- }
-
- /**
- * Sets the memo state ex.
- * @param slot the slot where the value will be saved
- * @param value the value
- * @return this QuestState
- */
- public QuestState setMemoStateEx(int slot, int value)
- {
- set("memoStateEx" + slot, String.valueOf(value));
- return this;
- }
-
- /**
- * Verifies if the given value is equal to the current memos state ex.
- * @param slot the slot where the value was saved
- * @param memoStateEx the value to verify
- * @return {@code true} if the values are equal, {@code false} otherwise
- */
- public boolean isMemoStateEx(int slot, int memoStateEx)
- {
- return (getMemoStateEx(slot) == memoStateEx);
- }
-
- /**
- * Add player to get notification of characters death
- * @param character the {@link L2Character} object of the character to get notification of death
- */
- public void addNotifyOfDeath(L2Character character)
- {
- if (!(character instanceof L2PcInstance))
- {
- return;
- }
-
- ((L2PcInstance) character).addNotifyQuestOfDeath(this);
- }
-
- // TODO: This all remains because of backward compatibility, should be cleared when all scripts are rewritten in java
-
- /**
- * Return the quantity of one sort of item hold by the player
- * @param itemId the Id of the item wanted to be count
- * @return long
- */
- public long getQuestItemsCount(int itemId)
- {
- return AbstractScript.getQuestItemsCount(_player, itemId);
- }
-
- /**
- * @param itemId the Id of the item required
- * @return true if item exists in player's inventory, false - if not
- */
- public boolean hasQuestItems(int itemId)
- {
- return AbstractScript.hasQuestItems(_player, itemId);
- }
-
- /**
- * @param itemIds list of items that are required
- * @return true if all items exists in player's inventory, false - if not
- */
- public boolean hasQuestItems(int... itemIds)
- {
- return AbstractScript.hasQuestItems(_player, itemIds);
- }
-
- /**
- * Return the level of enchantment on the weapon of the player(Done specifically for weapon SA's)
- * @param itemId Id of the item to check enchantment
- * @return int
- */
- public int getEnchantLevel(int itemId)
- {
- return AbstractScript.getEnchantLevel(_player, itemId);
- }
-
- /**
- * Give adena to the player
- * @param count
- * @param applyRates
- */
- public void giveAdena(long count, boolean applyRates)
- {
- giveItems(Inventory.ADENA_ID, count, applyRates ? 0 : 1);
- }
-
- /**
- * Give reward to player using multiplier's
- * @param item
- */
- public void rewardItems(ItemHolder item)
- {
- AbstractScript.rewardItems(_player, item);
- }
-
- /**
- * Give reward to player using multiplier's
- * @param itemId
- * @param count
- */
- public void rewardItems(int itemId, long count)
- {
- AbstractScript.rewardItems(_player, itemId, count);
- }
-
- /**
- * Give item/reward to the player
- * @param itemId
- * @param count
- */
- public void giveItems(int itemId, long count)
- {
- AbstractScript.giveItems(_player, itemId, count, 0);
- }
-
- public void giveItems(ItemHolder holder)
- {
- AbstractScript.giveItems(_player, holder.getId(), holder.getCount(), 0);
- }
-
- public void giveItems(int itemId, long count, int enchantlevel)
- {
- AbstractScript.giveItems(_player, itemId, count, enchantlevel);
- }
-
- public void giveItems(int itemId, long count, byte attributeId, int attributeLevel)
- {
- AbstractScript.giveItems(_player, itemId, count, attributeId, attributeLevel);
- }
-
- public boolean giveItemRandomly(int itemId, long amount, long limit, double dropChance, boolean playSound)
- {
- return AbstractScript.giveItemRandomly(_player, null, itemId, amount, amount, limit, dropChance, playSound);
- }
-
- public boolean giveItemRandomly(L2Npc npc, int itemId, long amount, long limit, double dropChance, boolean playSound)
- {
- return AbstractScript.giveItemRandomly(_player, npc, itemId, amount, amount, limit, dropChance, playSound);
- }
-
- public boolean giveItemRandomly(L2Npc npc, int itemId, long minAmount, long maxAmount, long limit, double dropChance, boolean playSound)
- {
- return AbstractScript.giveItemRandomly(_player, npc, itemId, minAmount, maxAmount, limit, dropChance, playSound);
- }
-
- // TODO: More radar functions need to be added when the radar class is complete.
- // BEGIN STUFF THAT WILL PROBABLY BE CHANGED
- public void addRadar(int x, int y, int z)
- {
- _player.getRadar().addMarker(x, y, z);
- }
-
- public void removeRadar(int x, int y, int z)
- {
- _player.getRadar().removeMarker(x, y, z);
- }
-
- public void clearRadar()
- {
- _player.getRadar().removeAllMarkers();
- }
-
- // END STUFF THAT WILL PROBABLY BE CHANGED
-
- /**
- * Remove items from player's inventory when talking to NPC in order to have rewards.<br>
- * Actions:<br>
- * <ul>
- * <li>Destroy quantity of items wanted</li>
- * <li>Send new inventory list to player</li>
- * </ul>
- * @param itemId Identifier of the item
- * @param count Quantity of items to destroy
- */
- public void takeItems(int itemId, long count)
- {
- AbstractScript.takeItems(_player, itemId, count);
- }
-
- /**
- * Send a packet in order to play a sound to the player.
- * @param sound the name of the sound to play
- */
- public void playSound(String sound)
- {
- AbstractScript.playSound(_player, sound);
- }
-
- /**
- * Send a packet in order to play a sound to the player.
- * @param sound the {@link QuestSound} object of the sound to play
- */
- public void playSound(QuestSound sound)
- {
- AbstractScript.playSound(_player, sound);
- }
-
- /**
- * Add XP and SP as quest reward
- * @param exp
- * @param sp
- */
- public void addExpAndSp(int exp, int sp)
- {
- AbstractScript.addExpAndSp(_player, exp, sp);
- }
-
- /**
- * @param loc
- * @return number of ticks from GameTimeController
- */
- public int getItemEquipped(int loc)
- {
- return AbstractScript.getItemEquipped(_player, loc);
- }
-
- /**
- * @return {@code true} if quest is to be exited on clean up by QuestStateManager, {@code false} otherwise
- */
- public final boolean isExitQuestOnCleanUp()
- {
- return _isExitQuestOnCleanUp;
- }
-
- /**
- * @param isExitQuestOnCleanUp {@code true} if quest is to be exited on clean up by QuestStateManager, {@code false} otherwise
- */
- public void setIsExitQuestOnCleanUp(boolean isExitQuestOnCleanUp)
- {
- _isExitQuestOnCleanUp = isExitQuestOnCleanUp;
- }
-
- /**
- * Start a timed event for a quest.<br>
- * Will call an event in onEvent/onAdvEvent.
- * @param name the name of the timer/event
- * @param time time in milliseconds till the event is executed
- */
- public void startQuestTimer(String name, long time)
- {
- getQuest().startQuestTimer(name, time, null, _player, false);
- }
-
- /**
- * Start a timed event for a quest.<br>
- * Will call an event in onEvent/onAdvEvent.
- * @param name the name of the timer/event
- * @param time time in milliseconds till the event is executed
- * @param npc the L2Npc associated with this event
- */
- public void startQuestTimer(String name, long time, L2Npc npc)
- {
- getQuest().startQuestTimer(name, time, npc, _player, false);
- }
-
- /**
- * Start a repeating timed event for a quest.<br>
- * Will call an event in onEvent/onAdvEvent.
- * @param name the name of the timer/event
- * @param time time in milliseconds till the event is executed/repeated
- */
- public void startRepeatingQuestTimer(String name, long time)
- {
- getQuest().startQuestTimer(name, time, null, _player, true);
- }
-
- /**
- * Start a repeating timed event for a quest.<br>
- * Will call an event in onEvent/onAdvEvent.
- * @param name the name of the timer/event
- * @param time time in milliseconds till the event is executed/repeated
- * @param npc the L2Npc associated with this event
- */
- public void startRepeatingQuestTimer(String name, long time, L2Npc npc)
- {
- getQuest().startQuestTimer(name, time, npc, _player, true);
- }
-
- /**
- * @param name the name of the QuestTimer required
- * @return the {@link QuestTimer} object with the specified name or {@code null} if it doesn't exist
- */
- public final QuestTimer getQuestTimer(String name)
- {
- return getQuest().getQuestTimer(name, null, _player);
- }
-
- // --- Spawn methods ---
- /**
- * Add a temporary spawn of the specified npc.<br>
- * Player's coordinates will be used for the spawn.
- * @param npcId the Id of the npc to spawn
- * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
- * @see #addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
- */
- public L2Npc addSpawn(int npcId)
- {
- return addSpawn(npcId, _player.getX(), _player.getY(), _player.getZ(), 0, false, 0, false);
- }
-
- /**
- * Add a temporary spawn of the specified npc.<br>
- * Player's coordinates will be used for the spawn.
- * @param npcId the Id of the npc to spawn
- * @param despawnDelay time in milliseconds till the npc is despawned (default: 0)
- * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
- * @see #addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
- */
- public L2Npc addSpawn(int npcId, int despawnDelay)
- {
- return addSpawn(npcId, _player.getX(), _player.getY(), _player.getZ(), 0, false, despawnDelay, false);
- }
-
- /**
- * Add a temporary spawn of the specified npc.
- * @param npcId the Id of the npc to spawn
- * @param x the X coordinate of the npc spawn location
- * @param y the Y coordinate of the npc spawn location
- * @param z the Z coordinate (height) of the npc spawn location
- * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
- * @see #addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
- */
- public L2Npc addSpawn(int npcId, int x, int y, int z)
- {
- return addSpawn(npcId, x, y, z, 0, false, 0, false);
- }
-
- /**
- * Add a temporary spawn of the specified npc.
- * @param npcId the Id of the npc to spawn
- * @param x the X coordinate of the npc spawn location
- * @param y the Y coordinate of the npc spawn location
- * @param z the Z coordinate (height) of the npc spawn location
- * @param despawnDelay time in milliseconds till the npc is despawned (default: 0)
- * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
- * @see #addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
- */
- public L2Npc addSpawn(int npcId, int x, int y, int z, int despawnDelay)
- {
- return addSpawn(npcId, x, y, z, 0, false, despawnDelay, false);
- }
-
- /**
- * Add a temporary spawn of the specified npc.
- * @param npcId the Id of the npc to spawn
- * @param cha the character whose coordinates will be used for the npc spawn
- * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
- * @see #addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
- */
- public L2Npc addSpawn(int npcId, L2Character cha)
- {
- return addSpawn(npcId, cha.getX(), cha.getY(), cha.getZ(), cha.getHeading(), true, 0, false);
- }
-
- /**
- * Add a temporary spawn of the specified npc.
- * @param npcId the Id of the npc to spawn
- * @param cha the character whose coordinates will be used for the npc spawn
- * @param despawnDelay time in milliseconds till the npc is despawned (default: 0)
- * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
- * @see #addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
- */
- public L2Npc addSpawn(int npcId, L2Character cha, int despawnDelay)
- {
- return addSpawn(npcId, cha.getX(), cha.getY(), cha.getZ(), cha.getHeading(), true, despawnDelay, false);
- }
-
- /**
- * Add a temporary spawn of the specified npc.
- * @param npcId the Id of the npc to spawn
- * @param cha the character whose coordinates will be used for the npc spawn
- * @param randomOffset if {@code true}, adds +/- 50~100 to X/Y coordinates of the spawn location
- * @param despawnDelay time in milliseconds till the npc is despawned (default: 0)
- * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
- * @see #addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
- */
- public L2Npc addSpawn(int npcId, L2Character cha, boolean randomOffset, int despawnDelay)
- {
- return addSpawn(npcId, cha.getX(), cha.getY(), cha.getZ(), cha.getHeading(), randomOffset, despawnDelay, false);
- }
-
- /**
- * Add a temporary spawn of the specified npc.
- * @param npcId the Id of the npc to spawn
- * @param x the X coordinate of the npc spawn location
- * @param y the Y coordinate of the npc spawn location
- * @param z the Z coordinate (height) of the npc spawn location
- * @param heading the heading of the npc
- * @param randomOffset if {@code true}, adds +/- 50~100 to X/Y coordinates of the spawn location
- * @param despawnDelay time in milliseconds till the npc is despawned (default: 0)
- * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
- * @see #addSpawn(int, int, int, int, int, boolean, int, boolean)
- */
- public L2Npc addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay)
- {
- return addSpawn(npcId, x, y, z, heading, randomOffset, despawnDelay, false);
- }
-
- /**
- * Add a temporary spawn of the specified npc.
- * @param npcId the Id of the npc to spawn
- * @param x the X coordinate of the npc spawn location
- * @param y the Y coordinate of the npc spawn location
- * @param z the Z coordinate (height) of the npc spawn location
- * @param heading the heading of the npc
- * @param randomOffset if {@code true}, adds +/- 50~100 to X/Y coordinates of the spawn location
- * @param despawnDelay time in milliseconds till the npc is despawned (default: 0)
- * @param isSummonSpawn if {@code true}, displays a summon animation on npc spawn (default: {@code false})
- * @return the {@link L2Npc} object of the newly spawned npc or {@code null} if the npc doesn't exist
- * @see #addSpawn(int)
- * @see #addSpawn(int, int)
- * @see #addSpawn(int, L2Character)
- * @see #addSpawn(int, L2Character, int)
- * @see #addSpawn(int, int, int, int)
- * @see #addSpawn(int, L2Character, boolean, int)
- * @see #addSpawn(int, int, int, int, int)
- * @see #addSpawn(int, int, int, int, int, boolean, int)
- * @see #addSpawn(int, int, int, int, int, boolean, int, boolean)
- */
- public L2Npc addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, int despawnDelay, boolean isSummonSpawn)
- {
- return AbstractScript.addSpawn(npcId, x, y, z, heading, randomOffset, despawnDelay, isSummonSpawn);
- }
-
- /**
- * Send an HTML file to the specified player.
- * @param filename the name of the HTML file to show
- * @return the contents of the HTML file that was sent to the player
- * @see #showHtmlFile(String, L2Npc)
- * @see Quest#showHtmlFile(L2PcInstance, String)
- * @see Quest#showHtmlFile(L2PcInstance, String, L2Npc)
- */
- public String showHtmlFile(String filename)
- {
- return showHtmlFile(filename, null);
- }
-
- /**
- * Send an HTML file to the specified player.
- * @param filename the name of the HTML file to show
- * @param npc the NPC that is showing the HTML file
- * @return the contents of the HTML file that was sent to the player
- * @see Quest#showHtmlFile(L2PcInstance, String)
- * @see Quest#showHtmlFile(L2PcInstance, String, L2Npc)
- */
- public String showHtmlFile(String filename, L2Npc npc)
- {
- return getQuest().showHtmlFile(_player, filename, npc);
- }
-
- /**
- * Set condition to 1, state to STARTED and play the "ItemSound.quest_accept".<br>
- * Works only if state is CREATED and the quest is not a custom quest.
- * @return the newly created {@code QuestState} object
- */
- public QuestState startQuest()
- {
- if (isCreated() && !getQuest().isCustomQuest())
- {
- set("cond", "1");
- setState(State.STARTED);
- playSound(QuestSound.ITEMSOUND_QUEST_ACCEPT);
- }
- return this;
- }
-
- /**
- * Finishes the quest and removes all quest items associated with this quest from the player's inventory.<br>
- * If {@code type} is {@code QuestType.ONE_TIME}, also removes all other quest data associated with this quest.
- * @param type the {@link QuestType} of the quest
- * @return this {@link QuestState} object
- * @see #exitQuest(QuestType type, boolean playExitQuest)
- * @see #exitQuest(boolean repeatable)
- * @see #exitQuest(boolean repeatable, boolean playExitQuest)
- */
- public QuestState exitQuest(QuestType type)
- {
- switch (type)
- {
- case DAILY:
- {
- exitQuest(false);
- setRestartTime();
- break;
- }
- // case ONE_TIME:
- // case REPEATABLE:
- default:
- {
- exitQuest(type == QuestType.REPEATABLE);
- break;
- }
- }
- return this;
- }
-
- /**
- * Finishes the quest and removes all quest items associated with this quest from the player's inventory.<br>
- * If {@code type} is {@code QuestType.ONE_TIME}, also removes all other quest data associated with this quest.
- * @param type the {@link QuestType} of the quest
- * @param playExitQuest if {@code true}, plays "ItemSound.quest_finish"
- * @return this {@link QuestState} object
- * @see #exitQuest(QuestType type)
- * @see #exitQuest(boolean repeatable)
- * @see #exitQuest(boolean repeatable, boolean playExitQuest)
- */
- public QuestState exitQuest(QuestType type, boolean playExitQuest)
- {
- exitQuest(type);
- if (playExitQuest)
- {
- playSound(QuestSound.ITEMSOUND_QUEST_FINISH);
- }
- return this;
- }
-
- /**
- * Finishes the quest and removes all quest items associated with this quest from the player's inventory.<br>
- * If {@code repeatable} is set to {@code false}, also removes all other quest data associated with this quest.
- * @param repeatable if {@code true}, deletes all data and variables of this quest, otherwise keeps them
- * @return this {@link QuestState} object
- * @see #exitQuest(QuestType type)
- * @see #exitQuest(QuestType type, boolean playExitQuest)
- * @see #exitQuest(boolean repeatable, boolean playExitQuest)
- */
- public QuestState exitQuest(boolean repeatable)
- {
- _player.removeNotifyQuestOfDeath(this);
-
- if (!isStarted())
- {
- return this;
- }
-
- // Clean registered quest items
- getQuest().removeRegisteredQuestItems(_player);
-
- Quest.deleteQuestInDb(this, repeatable);
- if (repeatable)
- {
- _player.delQuestState(getQuestName());
- _player.sendPacket(new QuestList());
- }
- else
- {
- setState(State.COMPLETED);
- }
- _vars = null;
- return this;
- }
-
- /**
- * Finishes the quest and removes all quest items associated with this quest from the player's inventory.<br>
- * If {@code repeatable} is set to {@code false}, also removes all other quest data associated with this quest.
- * @param repeatable if {@code true}, deletes all data and variables of this quest, otherwise keeps them
- * @param playExitQuest if {@code true}, plays "ItemSound.quest_finish"
- * @return this {@link QuestState} object
- * @see #exitQuest(QuestType type)
- * @see #exitQuest(QuestType type, boolean playExitQuest)
- * @see #exitQuest(boolean repeatable)
- */
- public QuestState exitQuest(boolean repeatable, boolean playExitQuest)
- {
- exitQuest(repeatable);
- if (playExitQuest)
- {
- playSound(QuestSound.ITEMSOUND_QUEST_FINISH);
- }
- return this;
- }
-
- public void showQuestionMark(int number)
- {
- _player.sendPacket(new TutorialShowQuestionMark(number));
- }
-
- // TODO make tutorial voices the same as quest sounds
- public void playTutorialVoice(String voice)
- {
- _player.sendPacket(new PlaySound(2, voice, 0, 0, _player.getX(), _player.getY(), _player.getZ()));
- }
-
- /**
- * Used only in 255_Tutorial
- * @param html
- */
- public void showTutorialHTML(String html)
- {
- String text = HtmCache.getInstance().getHtm(_player.getHtmlPrefix(), "data/scripts/quests/255_Tutorial/" + html);
- if (text == null)
- {
- _log.warning("missing html page data/scripts/quests/255_Tutorial/" + html);
- text = "<html><body>File data/scripts/quests/255_Tutorial/" + html + " not found or file is empty.</body></html>";
- }
- _player.sendPacket(new TutorialShowHtml(text));
- }
-
- /**
- * Used only in 255_Tutorial
- */
- public void closeTutorialHtml()
- {
- _player.sendPacket(TutorialCloseHtml.STATIC_PACKET);
- }
-
- /**
- * Used only in 255_Tutorial
- * @param number
- */
- public void onTutorialClientEvent(int number)
- {
- _player.sendPacket(new TutorialEnableClientEvent(number));
- }
-
- /**
- * Set the restart time for the daily quests.<br>
- * The time is hardcoded at {@link Quest#getResetHour()} hours, {@link Quest#getResetMinutes()} minutes of the following day.<br>
- * It can be overridden in scripts (quests).
- */
- public void setRestartTime()
- {
- final Calendar reDo = Calendar.getInstance();
- if (reDo.get(Calendar.HOUR_OF_DAY) >= getQuest().getResetHour())
- {
- reDo.add(Calendar.DATE, 1);
- }
- reDo.set(Calendar.HOUR_OF_DAY, getQuest().getResetHour());
- reDo.set(Calendar.MINUTE, getQuest().getResetMinutes());
- set("restartTime", String.valueOf(reDo.getTimeInMillis()));
- }
-
- /**
- * Check if a daily quest is available to be started over.
- * @return {@code true} if the quest is available, {@code false} otherwise.
- */
- public boolean isNowAvailable()
- {
- final String val = get("restartTime");
- return ((val == null) || !Util.isDigit(val)) || (Long.parseLong(val) <= System.currentTimeMillis());
- }
- }
|