12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167 |
- /*
- * 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.quest;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.concurrent.locks.ReentrantReadWriteLock;
- import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock;
- import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javolution.util.FastMap;
- import com.l2jserver.Config;
- import com.l2jserver.L2DatabaseFactory;
- import com.l2jserver.gameserver.GameTimeController;
- import com.l2jserver.gameserver.ThreadPoolManager;
- import com.l2jserver.gameserver.cache.HtmCache;
- import com.l2jserver.gameserver.datatables.ItemTable;
- import com.l2jserver.gameserver.datatables.NpcTable;
- import com.l2jserver.gameserver.idfactory.IdFactory;
- import com.l2jserver.gameserver.instancemanager.QuestManager;
- import com.l2jserver.gameserver.instancemanager.ZoneManager;
- import com.l2jserver.gameserver.model.L2DropData;
- import com.l2jserver.gameserver.model.L2Object;
- import com.l2jserver.gameserver.model.L2Party;
- import com.l2jserver.gameserver.model.L2Spawn;
- import com.l2jserver.gameserver.model.Location;
- import com.l2jserver.gameserver.model.actor.L2Character;
- import com.l2jserver.gameserver.model.actor.L2Npc;
- import com.l2jserver.gameserver.model.actor.L2Trap;
- import com.l2jserver.gameserver.model.actor.instance.L2MonsterInstance;
- import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
- import com.l2jserver.gameserver.model.actor.instance.L2TrapInstance;
- import com.l2jserver.gameserver.model.actor.templates.L2NpcTemplate;
- import com.l2jserver.gameserver.model.itemcontainer.PcInventory;
- import com.l2jserver.gameserver.model.items.L2Item;
- import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
- import com.l2jserver.gameserver.model.olympiad.CompetitionType;
- import com.l2jserver.gameserver.model.skills.L2Skill;
- import com.l2jserver.gameserver.model.stats.Stats;
- import com.l2jserver.gameserver.model.zone.L2ZoneType;
- import com.l2jserver.gameserver.network.SystemMessageId;
- import com.l2jserver.gameserver.network.serverpackets.ActionFailed;
- import com.l2jserver.gameserver.network.serverpackets.InventoryUpdate;
- import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
- import com.l2jserver.gameserver.network.serverpackets.NpcQuestHtmlMessage;
- import com.l2jserver.gameserver.network.serverpackets.PlaySound;
- import com.l2jserver.gameserver.network.serverpackets.StatusUpdate;
- import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
- import com.l2jserver.gameserver.scripting.ManagedScript;
- import com.l2jserver.gameserver.scripting.ScriptManager;
- import com.l2jserver.gameserver.util.MinionList;
- import com.l2jserver.util.Rnd;
- import com.l2jserver.util.Util;
- /**
- * @author Luis Arias
- */
- public class Quest extends ManagedScript
- {
- protected static final Logger _log = Logger.getLogger(Quest.class.getName());
-
- /**
- * Map containing events from String value of the event.
- */
- private static Map<String, Quest> _allEventsS = new HashMap<>();
-
- /**
- * Map containing lists of timers from the name of the timer.
- */
- private final FastMap<String, List<QuestTimer>> _allEventTimers = new FastMap<>();
- private final List<Integer> _questInvolvedNpcs = new ArrayList<>();
-
- private final ReentrantReadWriteLock _rwLock = new ReentrantReadWriteLock();
- private final WriteLock _writeLock = _rwLock.writeLock();
- private final ReadLock _readLock = _rwLock.readLock();
-
- private final int _questId;
- private final String _name;
- private final String _descr;
- private final byte _initialState = State.CREATED;
- protected boolean _onEnterWorld = false;
- private boolean _isCustom = false;
- private boolean _isOlympiadUse = false;
-
- /**
- * <b>Note: questItemIds will be overridden by child classes.</b><br>
- * Ideally, it should be protected instead of public.<br>
- * However, quest scripts written in Jython will have trouble with protected, as Jython only knows private and public...<br>
- * In fact, protected will typically be considered private thus breaking the scripts.<br>
- * Leave this as public as a workaround.
- */
- public int[] questItemIds = null;
-
- private static final String DEFAULT_NO_QUEST_MSG = "<html><body>You are either not on a quest that involves this NPC, or you don't meet this NPC's minimum quest requirements.</body></html>";
- private static final String DEFAULT_ALREADY_COMPLETED_MSG = "<html><body>This quest has already been completed.</body></html>";
-
- private static final String QUEST_DELETE_FROM_CHAR_QUERY = "DELETE FROM character_quests WHERE charId=? AND name=?";
- private static final String QUEST_DELETE_FROM_CHAR_QUERY_NON_REPEATABLE_QUERY = "DELETE FROM character_quests WHERE charId=? AND name=? AND var!=?";
-
- private static final int RESET_HOUR = 6;
- private static final int RESET_MINUTES = 30;
-
- /**
- * @return the reset hour for a daily quest, could be overridden on a script.
- */
- public int getResetHour()
- {
- return RESET_HOUR;
- }
-
- /**
- * @return the reset minutes for a daily quest, could be overridden on a script.
- */
- public int getResetMinutes()
- {
- return RESET_MINUTES;
- }
-
- /**
- * @return a collection of the values contained in the _allEventsS map.
- */
- public static Collection<Quest> findAllEvents()
- {
- return _allEventsS.values();
- }
-
- /**
- * The Quest object constructor.<br>
- * Constructing a quest also calls the {@code init_LoadGlobalData} convenience method.
- * @param questId int pointing out the ID of the quest.
- * @param name String corresponding to the name of the quest.
- * @param descr String for the description of the quest.
- */
- public Quest(int questId, String name, String descr)
- {
- _questId = questId;
- _name = name;
- _descr = descr;
- _allEventTimers.shared();
- if (questId != 0)
- {
- QuestManager.getInstance().addQuest(this);
- }
- else
- {
- _allEventsS.put(name, this);
- }
- init_LoadGlobalData();
- }
-
- /**
- * The function init_LoadGlobalData is, by default, called by the constructor of all quests.<br>
- * Children of this class can implement this function in order to define what variables to load and what structures to save them in.<br>
- * By default, nothing is loaded.
- */
- protected void init_LoadGlobalData()
- {
-
- }
-
- /**
- * The function saveGlobalData is, by default, called at shutdown, for all quests, by the QuestManager.<br>
- * Children of this class can implement this function in order to convert their structures<br>
- * into <var, value> tuples and make calls to save them to the database, if needed.<br>
- * By default, nothing is saved.
- */
- public void saveGlobalData()
- {
-
- }
-
- /**
- * Trap actions:<br>
- * <ul>
- * <li>Triggered</li>
- * <li>Detected</li>
- * <li>Disarmed</li>
- * </ul>
- */
- public static enum TrapAction
- {
- TRAP_TRIGGERED,
- TRAP_DETECTED,
- TRAP_DISARMED
- }
-
- public static enum QuestEventType
- {
- ON_FIRST_TALK(false), // control the first dialog shown by NPCs when they are clicked (some quests must override the default npc action)
- QUEST_START(true), // onTalk action from start npcs
- ON_TALK(true), // onTalk action from npcs participating in a quest
- ON_ATTACK(true), // onAttack action triggered when a mob gets attacked by someone
- ON_KILL(true), // onKill action triggered when a mob gets killed.
- ON_SPAWN(true), // onSpawn action triggered when an NPC is spawned or respawned.
- ON_SKILL_SEE(true), // NPC or Mob saw a person casting a skill (regardless what the target is).
- ON_FACTION_CALL(true), // NPC or Mob saw a person casting a skill (regardless what the target is).
- ON_AGGRO_RANGE_ENTER(true), // a person came within the Npc/Mob's range
- ON_SPELL_FINISHED(true), // on spell finished action when npc finish casting skill
- ON_SKILL_LEARN(false), // control the AcquireSkill dialog from quest script
- ON_ENTER_ZONE(true), // on zone enter
- ON_EXIT_ZONE(true), // on zone exit
- ON_TRAP_ACTION(true), // on zone exit
- ON_ITEM_USE(true);
-
- // control whether this event type is allowed for the same npc template in multiple quests
- // or if the npc must be registered in at most one quest for the specified event
- private boolean _allowMultipleRegistration;
-
- QuestEventType(boolean allowMultipleRegistration)
- {
- _allowMultipleRegistration = allowMultipleRegistration;
- }
-
- public boolean isMultipleRegistrationAllowed()
- {
- return _allowMultipleRegistration;
- }
- }
-
- /**
- * @return the ID of the quest
- */
- public int getQuestIntId()
- {
- return _questId;
- }
-
- /**
- * Add a new quest state of this quest to the database.
- * @param player the owner of the newly created quest state
- * @return the newly created {@link QuestState} object
- */
- public QuestState newQuestState(L2PcInstance player)
- {
- return new QuestState(this, player, getInitialState());
- }
-
- /**
- * @return the initial state of the quest
- */
- public byte getInitialState()
- {
- return _initialState;
- }
-
- /**
- * @return the name of the quest
- */
- public String getName()
- {
- return _name;
- }
-
- /**
- * @return the description of the quest
- */
- public String getDescr()
- {
- return _descr;
- }
-
- /**
- * Add a timer to the quest (if it doesn't exist already) and start it.
- * @param name the name of the timer (also passed back as "event" in {@link #onAdvEvent(String, L2Npc, L2PcInstance)})
- * @param time time in ms for when to fire the timer
- * @param npc the npc associated with this timer (can be null)
- * @param player the player associated with this timer (can be null)
- * @see #startQuestTimer(String, long, L2Npc, L2PcInstance, boolean)
- */
- public void startQuestTimer(String name, long time, L2Npc npc, L2PcInstance player)
- {
- startQuestTimer(name, time, npc, player, false);
- }
-
- /**
- * Add a timer to the quest (if it doesn't exist already) and start it.
- * @param name the name of the timer (also passed back as "event" in {@link #onAdvEvent(String, L2Npc, L2PcInstance)})
- * @param time time in ms for when to fire the timer
- * @param npc the npc associated with this timer (can be null)
- * @param player the player associated with this timer (can be null)
- * @param repeating indicates whether the timer is repeatable or one-time.<br>
- * If {@code true}, the task is repeated every {@code time} milliseconds until explicitly stopped.
- */
- public void startQuestTimer(String name, long time, L2Npc npc, L2PcInstance player, boolean repeating)
- {
- List<QuestTimer> timers = _allEventTimers.get(name);
- // Add quest timer if timer doesn't already exist
- if (timers == null)
- {
- timers = new ArrayList<>();
- timers.add(new QuestTimer(this, name, time, npc, player, repeating));
- _allEventTimers.put(name, timers);
- }
- // a timer with this name exists, but may not be for the same set of npc and player
- else
- {
- // if there exists a timer with this name, allow the timer only if the [npc, player] set is unique
- // nulls act as wildcards
- if (getQuestTimer(name, npc, player) == null)
- {
- _writeLock.lock();
- try
- {
- timers.add(new QuestTimer(this, name, time, npc, player, repeating));
- }
- finally
- {
- _writeLock.unlock();
- }
- }
- }
- }
-
- /**
- * Get a quest timer that matches the provided name and parameters.
- * @param name the name of the quest timer to get
- * @param npc the NPC associated with the quest timer to get
- * @param player the player associated with the quest timer to get
- * @return the quest timer that matches the parameters of this function or {@code null} if nothing was found
- */
- public QuestTimer getQuestTimer(String name, L2Npc npc, L2PcInstance player)
- {
- final List<QuestTimer> timers = _allEventTimers.get(name);
- if (timers != null)
- {
- _readLock.lock();
- try
- {
- for (QuestTimer timer : timers)
- {
- if (timer != null)
- {
- if (timer.isMatch(this, name, npc, player))
- {
- return timer;
- }
- }
- }
- }
- finally
- {
- _readLock.unlock();
- }
- }
- return null;
- }
-
- /**
- * Cancel all quest timers with the specified name.
- * @param name the name of the quest timers to cancel
- */
- public void cancelQuestTimers(String name)
- {
- final List<QuestTimer> timers = _allEventTimers.get(name);
- if (timers != null)
- {
- _writeLock.lock();
- try
- {
- for (QuestTimer timer : timers)
- {
- if (timer != null)
- {
- timer.cancel();
- }
- }
- timers.clear();
- }
- finally
- {
- _writeLock.unlock();
- }
- }
- }
-
- /**
- * Cancel the quest timer that matches the specified name and parameters.
- * @param name the name of the quest timer to cancel
- * @param npc the NPC associated with the quest timer to cancel
- * @param player the player associated with the quest timer to cancel
- */
- public void cancelQuestTimer(String name, L2Npc npc, L2PcInstance player)
- {
- final QuestTimer timer = getQuestTimer(name, npc, player);
- if (timer != null)
- {
- timer.cancelAndRemove();
- }
- }
-
- /**
- * Remove a quest timer from the list of all timers.<br>
- * Note: does not stop the timer itself!
- * @param timer the {@link QuestState} object to remove
- */
- public void removeQuestTimer(QuestTimer timer)
- {
- if (timer != null)
- {
- final List<QuestTimer> timers = _allEventTimers.get(timer.getName());
- if (timers != null)
- {
- _writeLock.lock();
- try
- {
- timers.remove(timer);
- }
- finally
- {
- _writeLock.unlock();
- }
- }
- }
- }
-
- // These are methods to call within the core to call the quest events.
-
- /**
- * @param npc the NPC that was attacked
- * @param attacker the attacking player
- * @param damage the damage dealt to the NPC by the player
- * @param isPet if {@code true}, the attack was actually made by the player's pet
- * @param skill the skill used to attack the NPC (can be null)
- * @return {@code false} if there was an error or the message was sent, {@code true} otherwise
- */
- public final boolean notifyAttack(L2Npc npc, L2PcInstance attacker, int damage, boolean isPet, L2Skill skill)
- {
- String res = null;
- try
- {
- res = onAttack(npc, attacker, damage, isPet, skill);
- }
- catch (Exception e)
- {
- return showError(attacker, e);
- }
- return showResult(attacker, res);
- }
-
- /**
- * @param killer the character that killed the {@code victim}
- * @param victim the character that was killed by the {@code killer}
- * @param qs the quest state object of the player to be notified of this event
- * @return {@code false} if there was an error or the message was sent, {@code true} otherwise
- */
- public final boolean notifyDeath(L2Character killer, L2Character victim, QuestState qs)
- {
- String res = null;
- try
- {
- res = onDeath(killer, victim, qs);
- }
- catch (Exception e)
- {
- return showError(qs.getPlayer(), e);
- }
- return showResult(qs.getPlayer(), res);
- }
-
- /**
- * @param item
- * @param player
- * @return {@code false} if there was an error or the message was sent, {@code true} otherwise
- */
- public final boolean notifyItemUse(L2Item item, L2PcInstance player)
- {
- String res = null;
- try
- {
- res = onItemUse(item, player);
- }
- catch (Exception e)
- {
- return showError(player, e);
- }
- return showResult(player, res);
- }
-
- /**
- * @param instance
- * @param player
- * @param skill
- * @return {@code false} if there was an error or the message was sent, {@code true} otherwise
- */
- public final boolean notifySpellFinished(L2Npc instance, L2PcInstance player, L2Skill skill)
- {
- String res = null;
- try
- {
- res = onSpellFinished(instance, player, skill);
- }
- catch (Exception e)
- {
- return showError(player, e);
- }
- return showResult(player, res);
- }
-
- /**
- * Notify quest script when something happens with a trap.
- * @param trap the trap instance which triggers the notification
- * @param trigger the character which makes effect on the trap
- * @param action 0: trap casting its skill. 1: trigger detects the trap. 2: trigger removes the trap
- * @return {@code false} if the event was triggered successfully, {@code true} otherwise
- */
- public final boolean notifyTrapAction(L2Trap trap, L2Character trigger, TrapAction action)
- {
- String res = null;
- try
- {
- res = onTrapAction(trap, trigger, action);
- }
- catch (Exception e)
- {
- if (trigger.getActingPlayer() != null)
- {
- return showError(trigger.getActingPlayer(), e);
- }
- _log.log(Level.WARNING, "Exception on onTrapAction() in notifyTrapAction(): " + e.getMessage(), e);
- return true;
- }
- if (trigger.getActingPlayer() != null)
- {
- return showResult(trigger.getActingPlayer(), res);
- }
- return false;
- }
-
- /**
- * @param npc
- * @return {@code true} if there was an error, {@code false} otherwise
- */
- public final boolean notifySpawn(L2Npc npc)
- {
- try
- {
- onSpawn(npc);
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "Exception on onSpawn() in notifySpawn(): " + e.getMessage(), e);
- return true;
- }
- return false;
- }
-
- /**
- * @param event
- * @param npc
- * @param player
- * @return {@code false} if there was an error or the message was sent, {@code true} otherwise
- */
- public final boolean notifyEvent(String event, L2Npc npc, L2PcInstance player)
- {
- String res = null;
- try
- {
- res = onAdvEvent(event, npc, player);
- }
- catch (Exception e)
- {
- return showError(player, e);
- }
- return showResult(player, res);
- }
-
- /**
- * @param player
- * @return {@code false} if there was an error or the message was sent, {@code true} otherwise
- */
- public final boolean notifyEnterWorld(L2PcInstance player)
- {
- String res = null;
- try
- {
- res = onEnterWorld(player);
- }
- catch (Exception e)
- {
- return showError(player, e);
- }
- return showResult(player, res);
- }
-
- /**
- * @param npc
- * @param killer
- * @param isPet
- * @return {@code false} if there was an error or the message was sent, {@code true} otherwise
- */
- public final boolean notifyKill(L2Npc npc, L2PcInstance killer, boolean isPet)
- {
- String res = null;
- try
- {
- res = onKill(npc, killer, isPet);
- }
- catch (Exception e)
- {
- return showError(killer, e);
- }
- return showResult(killer, res);
- }
-
- /**
- * @param npc
- * @param qs
- * @return {@code false} if there was an error or the message was sent, {@code true} otherwise
- */
- public final boolean notifyTalk(L2Npc npc, QuestState qs)
- {
- String res = null;
- try
- {
- res = onTalk(npc, qs.getPlayer());
- }
- catch (Exception e)
- {
- return showError(qs.getPlayer(), e);
- }
- qs.getPlayer().setLastQuestNpcObject(npc.getObjectId());
- return showResult(qs.getPlayer(), res);
- }
-
- /**
- * Override the default NPC dialogs when a quest defines this for the given NPC.
- * @param npc the NPC whose dialogs to override
- * @param player the player talking to the NPC
- * @return {@code true} if the event was triggered successfully, {@code false} otherwise
- */
- public final boolean notifyFirstTalk(L2Npc npc, L2PcInstance player)
- {
- String res = null;
- try
- {
- res = onFirstTalk(npc, player);
- }
- catch (Exception e)
- {
- return showError(player, e);
- }
- // if the quest returns text to display, display it.
- if ((res != null) && (res.length() > 0))
- {
- return showResult(player, res);
- }
- // else tell the player that
- player.sendPacket(ActionFailed.STATIC_PACKET);
- // note: if the default html for this npc needs to be shown, onFirstTalk should
- // call npc.showChatWindow(player) and then return null.
- return true;
- }
-
- /**
- * TODO: Remove and replace with listeners.
- * @param npc
- * @param player
- * @return {@code false} if there was an error or the message was sent, {@code true} otherwise
- */
- public final boolean notifyAcquireSkillList(L2Npc npc, L2PcInstance player)
- {
- String res = null;
- try
- {
- res = onAcquireSkillList(npc, player);
- }
- catch (Exception e)
- {
- return showError(player, e);
- }
- return showResult(player, res);
- }
-
- /**
- * TODO: Remove and replace with listeners.
- * @param npc
- * @param player
- * @param skill
- * @return {@code false} if there was an error or the message was sent, {@code true} otherwise
- */
- public final boolean notifyAcquireSkillInfo(L2Npc npc, L2PcInstance player, L2Skill skill)
- {
- String res = null;
- try
- {
- res = onAcquireSkillInfo(npc, player, skill);
- }
- catch (Exception e)
- {
- return showError(player, e);
- }
- return showResult(player, res);
- }
-
- /**
- * TODO: Remove and replace with listeners.
- * @param npc
- * @param player
- * @param skill
- * @return {@code false} if there was an error or the message was sent, {@code true} otherwise
- */
- public final boolean notifyAcquireSkill(L2Npc npc, L2PcInstance player, L2Skill skill)
- {
- String res = null;
- try
- {
- res = onAcquireSkill(npc, player, skill);
- if (res != null)
- {
- if (res.equals("true"))
- {
- return true;
- }
- else if (res.equals("false"))
- {
- return false;
- }
- }
- }
- catch (Exception e)
- {
- return showError(player, e);
- }
- return showResult(player, res);
- }
-
- public class TmpOnSkillSee implements Runnable
- {
- private final L2Npc _npc;
- private final L2PcInstance _caster;
- private final L2Skill _skill;
- private final L2Object[] _targets;
- private final boolean _isPet;
-
- public TmpOnSkillSee(L2Npc npc, L2PcInstance caster, L2Skill skill, L2Object[] targets, boolean isPet)
- {
- _npc = npc;
- _caster = caster;
- _skill = skill;
- _targets = targets;
- _isPet = isPet;
- }
-
- @Override
- public void run()
- {
- String res = null;
- try
- {
- res = onSkillSee(_npc, _caster, _skill, _targets, _isPet);
- }
- catch (Exception e)
- {
- showError(_caster, e);
- }
- showResult(_caster, res);
-
- }
- }
-
- /**
- * @param npc
- * @param caster
- * @param skill
- * @param targets
- * @param isPet
- * @return {@code true}
- */
- public final boolean notifySkillSee(L2Npc npc, L2PcInstance caster, L2Skill skill, L2Object[] targets, boolean isPet)
- {
- ThreadPoolManager.getInstance().executeAi(new TmpOnSkillSee(npc, caster, skill, targets, isPet));
- return true;
- }
-
- /**
- * @param npc
- * @param caller
- * @param attacker
- * @param isPet
- * @return {@code false} if there was an error or the message was sent, {@code true} otherwise
- */
- public final boolean notifyFactionCall(L2Npc npc, L2Npc caller, L2PcInstance attacker, boolean isPet)
- {
- String res = null;
- try
- {
- res = onFactionCall(npc, caller, attacker, isPet);
- }
- catch (Exception e)
- {
- return showError(attacker, e);
- }
- return showResult(attacker, res);
- }
-
- public class TmpOnAggroEnter implements Runnable
- {
- private final L2Npc _npc;
- private final L2PcInstance _pc;
- private final boolean _isPet;
-
- public TmpOnAggroEnter(L2Npc npc, L2PcInstance pc, boolean isPet)
- {
- _npc = npc;
- _pc = pc;
- _isPet = isPet;
- }
-
- @Override
- public void run()
- {
- String res = null;
- try
- {
- res = onAggroRangeEnter(_npc, _pc, _isPet);
- }
- catch (Exception e)
- {
- showError(_pc, e);
- }
- showResult(_pc, res);
-
- }
- }
-
- /**
- * @param npc
- * @param player
- * @param isPet
- * @return {@code true}
- */
- public final boolean notifyAggroRangeEnter(L2Npc npc, L2PcInstance player, boolean isPet)
- {
- ThreadPoolManager.getInstance().executeAi(new TmpOnAggroEnter(npc, player, isPet));
- return true;
- }
-
- /**
- * @param character
- * @param zone
- * @return {@code false} if there was an error or the message was sent, {@code true} otherwise
- */
- public final boolean notifyEnterZone(L2Character character, L2ZoneType zone)
- {
- L2PcInstance player = character.getActingPlayer();
- String res = null;
- try
- {
- res = onEnterZone(character, zone);
- }
- catch (Exception e)
- {
- if (player != null)
- {
- return showError(player, e);
- }
- }
- if (player != null)
- {
- return showResult(player, res);
- }
- return true;
- }
-
- /**
- * @param character
- * @param zone
- * @return {@code false} if there was an error or the message was sent, {@code true} otherwise
- */
- public final boolean notifyExitZone(L2Character character, L2ZoneType zone)
- {
- L2PcInstance player = character.getActingPlayer();
- String res = null;
- try
- {
- res = onExitZone(character, zone);
- }
- catch (Exception e)
- {
- if (player != null)
- {
- return showError(player, e);
- }
- }
- if (player != null)
- {
- return showResult(player, res);
- }
- return true;
- }
-
- /**
- * @param winner
- * @param type {@code false} if there was an error, {@code true} otherwise
- */
- public final void notifyOlympiadWin(L2PcInstance winner, CompetitionType type)
- {
- try
- {
- onOlympiadWin(winner, type);
- }
- catch (Exception e)
- {
- showError(winner, e);
- }
- }
-
- /**
- * @param loser
- * @param type {@code false} if there was an error, {@code true} otherwise
- */
- public final void notifyOlympiadLose(L2PcInstance loser, CompetitionType type)
- {
- try
- {
- onOlympiadLose(loser, type);
- }
- catch (Exception e)
- {
- showError(loser, e);
- }
- }
-
- // These are methods that java calls to invoke scripts.
-
- /**
- * This function is called in place of {@link #onAttack(L2Npc, L2PcInstance, int, boolean, L2Skill)} if the former is not implemented.<br>
- * If a script contains both onAttack(..) implementations, then this method will never be called unless the script's {@link #onAttack(L2Npc, L2PcInstance, int, boolean, L2Skill)} explicitly calls this method.
- * @param npc this parameter contains a reference to the exact instance of the NPC that got attacked the NPC.
- * @param attacker this parameter contains a reference to the exact instance of the player who attacked.
- * @param damage this parameter represents the total damage that this attack has inflicted to the NPC.
- * @param isPet this parameter if it's {@code false} it denotes that the attacker was indeed the player, else it specifies that the damage was actually dealt by the player's pet.
- * @return
- */
- public String onAttack(L2Npc npc, L2PcInstance attacker, int damage, boolean isPet)
- {
- return null;
- }
-
- /**
- * This function is called whenever a player attacks an NPC that is registered for the quest.<br>
- * If is not overridden by a subclass, then default to the returned value of the simpler (and older) {@link #onAttack(L2Npc, L2PcInstance, int, boolean)} override.<br>
- * @param npc this parameter contains a reference to the exact instance of the NPC that got attacked.
- * @param attacker this parameter contains a reference to the exact instance of the player who attacked the NPC.
- * @param damage this parameter represents the total damage that this attack has inflicted to the NPC.
- * @param isPet this parameter if it's {@code false} it denotes that the attacker was indeed the player, else it specifies that the damage was actually dealt by the player's pet.
- * @param skill parameter is the skill that player used to attack NPC.
- * @return
- */
- public String onAttack(L2Npc npc, L2PcInstance attacker, int damage, boolean isPet, L2Skill skill)
- {
- return onAttack(npc, attacker, damage, isPet);
- }
-
- /**
- * This function is called whenever an <b>exact instance</b> of a character who was previously registered for this event dies.<br>
- * The registration for {@link #onDeath(L2Character, L2Character, QuestState)} events <b>is not</b> done via the quest itself, but it is instead handled by the QuestState of a particular player.
- * @param killer this parameter contains a reference to the exact instance of the NPC that <b>killed</b> the character.
- * @param victim this parameter contains a reference to the exact instance of the character that got killed.
- * @param qs this parameter contains a reference to the QuestState of whomever was interested (waiting) for this kill.
- * @return
- */
- public String onDeath(L2Character killer, L2Character victim, QuestState qs)
- {
- if (killer instanceof L2Npc)
- {
- return onAdvEvent("", (L2Npc) killer, qs.getPlayer());
- }
- return onAdvEvent("", null, qs.getPlayer());
- }
-
- /**
- * This function is called whenever a player clicks on a link in a quest dialog and whenever a timer fires.<br>
- * If is not overridden by a subclass, then default to the returned value of the simpler (and older) {@link #onEvent(String, QuestState)} override.<br>
- * If the player has a quest state, use it as parameter in the next call, otherwise return null.
- * @param event this parameter contains a string identifier for the event.<br>
- * Generally, this string is passed directly via the link.<br>
- * For example:<br>
- * <code>
- * <a action="bypass -h Quest 626_ADarkTwilight 31517-01.htm">hello</a>
- * </code><br>
- * The above link sets the event variable to "31517-01.htm" for the quest 626_ADarkTwilight.<br>
- * In the case of timers, this will be the name of the timer.<br>
- * This parameter serves as a sort of identifier.
- * @param npc this parameter contains a reference to the instance of NPC associated with this event.<br>
- * This may be the NPC registered in a timer, or the NPC with whom a player is speaking, etc.<br>
- * This parameter may be {@code null} in certain circumstances.
- * @param player this parameter contains a reference to the player participating in this function.<br>
- * It may be the player speaking to the NPC, or the player who caused a timer to start (and owns that timer).<br>
- * This parameter may be {@code null} in certain circumstances.
- * @return the text returned by the event (may be {@code null}, a filename or just text)
- */
- public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
- {
- final QuestState qs = player.getQuestState(getName());
- if (qs != null)
- {
- return onEvent(event, qs);
- }
- return null;
- }
-
- /**
- * This function is called in place of {@link #onAdvEvent(String, L2Npc, L2PcInstance)} if the former is not implemented.<br>
- * If a script contains both {@link #onAdvEvent(String, L2Npc, L2PcInstance)} and this implementation, then this method will never be called unless the script's {@link #onAdvEvent(String, L2Npc, L2PcInstance)} explicitly calls this method.
- * @param event this parameter contains a string identifier for the event.<br>
- * Generally, this string is passed directly via the link.<br>
- * For example:<br>
- * <code>
- * <a action="bypass -h Quest 626_ADarkTwilight 31517-01.htm">hello</a>
- * </code><br>
- * The above link sets the event variable to "31517-01.htm" for the quest 626_ADarkTwilight.<br>
- * In the case of timers, this will be the name of the timer.<br>
- * This parameter serves as a sort of identifier.
- * @param qs this parameter contains a reference to the quest state of the player who used the link or started the timer.
- * @return the text returned by the event (may be {@code null}, a filename or just text)
- */
- public String onEvent(String event, QuestState qs)
- {
- return null;
- }
-
- /**
- * This function is called whenever a player kills a NPC that is registered for the quest.
- * @param npc this parameter contains a reference to the exact instance of the NPC that got killed.
- * @param killer this parameter contains a reference to the exact instance of the player who killed the NPC.
- * @param isPet this parameter if it's {@code false} it denotes that the attacker was indeed the player, else it specifies that the killer was the player's pet.
- * @return the text returned by the event (may be {@code null}, a filename or just text)
- */
- public String onKill(L2Npc npc, L2PcInstance killer, boolean isPet)
- {
- return null;
- }
-
- /**
- * This function is called whenever a player clicks to the "Quest" link of an NPC that is registered for the quest.
- * @param npc this parameter contains a reference to the exact instance of the NPC that the player is talking with.
- * @param talker this parameter contains a reference to the exact instance of the player who is talking to the NPC.
- * @return the text returned by the event (may be {@code null}, a filename or just text)
- */
- public String onTalk(L2Npc npc, L2PcInstance talker)
- {
- return null;
- }
-
- /**
- * This function is called whenever a player talks to an NPC that is registered for the quest.<br>
- * That is, it is triggered from the very first click on the NPC, not via another dialog.<br>
- * <b>Note 1:</b><br>
- * Each NPC can be registered to at most one quest for triggering this function.<br>
- * In other words, the same one NPC cannot respond to an "onFirstTalk" request from two different quests.<br>
- * Attempting to register an NPC in two different quests for this function will result in one of the two registration being ignored.<br>
- * <b>Note 2:</b><br>
- * Since a Quest link isn't clicked in order to reach this, a quest state can be invalid within this function.<br>
- * The coder of the script may need to create a new quest state (if necessary).<br>
- * <b>Note 3:</b><br>
- * The returned value of onFirstTalk replaces the default HTML that would have otherwise been loaded from a sub-folder of DatapackRoot/game/data/html/.<br>
- * If you wish to show the default HTML, within onFirstTalk do npc.showChatWindow(player) and then return ""<br>
- * @param npc this parameter contains a reference to the exact instance of the NPC that the player is talking with.
- * @param player this parameter contains a reference to the exact instance of the player who is talking to the NPC.
- * @return the text returned by the event (may be {@code null}, a filename or just text)
- * @since <a href="http://trac.l2jserver.com/changeset/771">Jython AI support for "onFirstTalk"</a>
- */
- public String onFirstTalk(L2Npc npc, L2PcInstance player)
- {
- return null;
- }
-
- /**
- * This function is called whenever a player request a skill list.<br>
- * TODO: Cleanup or re-implement, since Skill Trees rework it's support was removed.
- * @param npc this parameter contains a reference to the exact instance of the NPC that the player requested the skill list.
- * @param player this parameter contains a reference to the exact instance of the player who requested the skill list.
- * @return
- */
- public String onAcquireSkillList(L2Npc npc, L2PcInstance player)
- {
- return null;
- }
-
- /**
- * This function is called whenever a player request a skill info.<br>
- * TODO: Cleanup or re-implement, since Skill Trees rework it's support was removed.
- * @param npc this parameter contains a reference to the exact instance of the NPC that the player requested the skill info.
- * @param player this parameter contains a reference to the exact instance of the player who requested the skill info.
- * @param skill this parameter contains a reference to the skill that the player requested its info.
- * @return
- */
- public String onAcquireSkillInfo(L2Npc npc, L2PcInstance player, L2Skill skill)
- {
- return null;
- }
-
- /**
- * This function is called whenever a player acquire a skill.<br>
- * TODO: Cleanup or re-implement, since Skill Trees rework it's support was removed.
- * @param npc this parameter contains a reference to the exact instance of the NPC that the player requested the skill.
- * @param player this parameter contains a reference to the exact instance of the player who requested the skill.
- * @param skill this parameter contains a reference to the skill that the player requested.
- * @return
- */
- public String onAcquireSkill(L2Npc npc, L2PcInstance player, L2Skill skill)
- {
- return null;
- }
-
- /**
- * This function is called whenever a player uses a quest item that has a quest events list<br>
- * TODO: complete this documentation and unhardcode it to work with all item uses not with those listed.
- * @param item the quest item that the player used
- * @param player the player who used the item
- * @return
- */
- public String onItemUse(L2Item item, L2PcInstance player)
- {
- return null;
- }
-
- /**
- * This function is called whenever a player casts a skill near a registered NPC (1000 distance).<br>
- * <b>Note:</b><br>
- * If a skill does damage, both onSkillSee(..) and onAttack(..) will be triggered for the damaged NPC!<br>
- * However, only onSkillSee(..) will be triggered if the skill does no damage,<br>
- * or if it damages an NPC who has no onAttack(..) registration while near another NPC who has an onSkillSee registration.<br>
- * TODO: confirm if the distance is 1000 and unhardcode.
- * @param npc the NPC that saw the skill
- * @param caster the player who cast the skill
- * @param skill the actual skill that was used
- * @param targets an array of all objects (can be any type of object, including mobs and players) that were affected by the skill
- * @param isPet if {@code true}, the skill was actually cast by the player's pet, not the player himself
- * @return
- */
- public String onSkillSee(L2Npc npc, L2PcInstance caster, L2Skill skill, L2Object[] targets, boolean isPet)
- {
- return null;
- }
-
- /**
- * This function is called whenever an NPC finishes casting a skill.
- * @param npc the NPC that casted the skill.
- * @param player the player who is the target of the skill. Can be {@code null}.
- * @param skill the actual skill that was used by the NPC.
- * @return
- */
- public String onSpellFinished(L2Npc npc, L2PcInstance player, L2Skill skill)
- {
- return null;
- }
-
- /**
- * This function is called whenever a trap action is performed.
- * @param trap this parameter contains a reference to the exact instance of the trap that was activated.
- * @param trigger this parameter contains a reference to the exact instance of the character that triggered the action.
- * @param action this parameter contains a reference to the action that was triggered.
- * @return
- */
- public String onTrapAction(L2Trap trap, L2Character trigger, TrapAction action)
- {
- return null;
- }
-
- /**
- * This function is called whenever an NPC spawns or re-spawns and passes a reference to the newly (re)spawned NPC.<br>
- * Currently the only function that has no reference to a player.<br>
- * It is useful for initializations, starting quest timers, displaying chat (NpcSay), and more.
- * @param npc this parameter contains a reference to the exact instance of the NPC who just (re)spawned.
- * @return
- */
- public String onSpawn(L2Npc npc)
- {
- return null;
- }
-
- /**
- * This function is called whenever an NPC is called by another NPC in the same faction.
- * @param npc this parameter contains a reference to the exact instance of the NPC who is being asked for help.
- * @param caller this parameter contains a reference to the exact instance of the NPC who is asking for help.<br>
- * @param attacker this parameter contains a reference to the exact instance of the player who attacked.
- * @param isPet this parameter if it's {@code false} it denotes that the attacker was indeed the player, else it specifies that the attacker was the player's pet.
- * @return
- */
- public String onFactionCall(L2Npc npc, L2Npc caller, L2PcInstance attacker, boolean isPet)
- {
- return null;
- }
-
- /**
- * This function is called whenever a player enters an NPC aggression range.
- * @param npc this parameter contains a reference to the exact instance of the NPC whose aggression range is being transgressed.
- * @param player this parameter contains a reference to the exact instance of the player who is entering the NPC's aggression range.
- * @param isPet this parameter if it's {@code false} it denotes that the character that entered the aggression range was indeed the player, else it specifies that the character was the player's pet.
- * @return
- */
- public String onAggroRangeEnter(L2Npc npc, L2PcInstance player, boolean isPet)
- {
- return null;
- }
-
- /**
- * This function is called whenever a player enters the game.
- * @param player this parameter contains a reference to the exact instance of the player who is entering to the world.
- * @return
- */
- public String onEnterWorld(L2PcInstance player)
- {
- return null;
- }
-
- /**
- * This function is called whenever a character enters a registered zone.
- * @param character this parameter contains a reference to the exact instance of the character who is entering the zone.
- * @param zone this parameter contains a reference to the zone.
- * @return
- */
- public String onEnterZone(L2Character character, L2ZoneType zone)
- {
- return null;
- }
-
- /**
- * This function is called whenever a character exits a registered zone.
- * @param character this parameter contains a reference to the exact instance of the character who is exiting the zone.
- * @param zone this parameter contains a reference to the zone.
- * @return
- */
- public String onExitZone(L2Character character, L2ZoneType zone)
- {
- return null;
- }
-
- /**
- * This function is called whenever a player wins an Olympiad Game.
- * @param winner this parameter contains a reference to the exact instance of the player who won the competition.
- * @param type this parameter contains a reference to the competition type.
- */
- public void onOlympiadWin(L2PcInstance winner, CompetitionType type)
- {
-
- }
-
- /**
- * This function is called whenever a player looses an Olympiad Game.
- * @param loser this parameter contains a reference to the exact instance of the player who lose the competition.
- * @param type this parameter contains a reference to the competition type.
- */
- public void onOlympiadLose(L2PcInstance loser, CompetitionType type)
- {
-
- }
-
- /**
- * Show an error message to the specified player.
- * @param player the player to whom to send the error (must be a GM)
- * @param t the {@link Throwable} to get the message/stacktrace from
- * @return {@code false}
- */
- public boolean showError(L2PcInstance player, Throwable t)
- {
- _log.log(Level.WARNING, getScriptFile().getAbsolutePath(), t);
- if (t.getMessage() == null)
- {
- _log.warning(getClass().getSimpleName() + ": " + t.getMessage());
- }
- if ((player != null) && player.getAccessLevel().isGm())
- {
- String res = "<html><body><title>Script error</title>" + Util.getStackTrace(t) + "</body></html>";
- return showResult(player, res);
- }
- return false;
- }
-
- /**
- * Show a message to the specified player.<br>
- * <u><i>Concept:</i></u><br>
- * Three cases are managed according to the value of the {@code res} parameter:<br>
- * <ul>
- * <li><u>{@code res} ends with ".htm" or ".html":</u> the contents of the specified HTML file are shown in a dialog window</li>
- * <li><u>{@code res} starts with "<html>":</u> the contents of the parameter are shown in a dialog window</li>
- * <li><u>all other cases :</u> the text contained in the parameter is shown in chat</li>
- * </ul>
- * @param player the player to whom to show the result
- * @param res the message to show to the player
- * @return {@code false} if the message was sent, {@code true} otherwise
- */
- public boolean showResult(L2PcInstance player, String res)
- {
- if ((res == null) || res.isEmpty() || (player == null))
- {
- return true;
- }
-
- if (res.endsWith(".htm") || res.endsWith(".html"))
- {
- showHtmlFile(player, res);
- }
- else if (res.startsWith("<html>"))
- {
- NpcHtmlMessage npcReply = new NpcHtmlMessage(5);
- npcReply.setHtml(res);
- npcReply.replace("%playername%", player.getName());
- player.sendPacket(npcReply);
- player.sendPacket(ActionFailed.STATIC_PACKET);
- }
- else
- {
- player.sendMessage(res);
- }
- return false;
- }
-
- /**
- * Loads all quest states and variables for the specified player.
- * @param player the player who is entering the world
- */
- public static final void playerEnter(L2PcInstance player)
- {
- Connection con = null;
- try
- {
- // Get list of quests owned by the player from database
- con = L2DatabaseFactory.getInstance().getConnection();
-
- PreparedStatement invalidQuestData = con.prepareStatement("DELETE FROM character_quests WHERE charId=? AND name=?");
- PreparedStatement invalidQuestDataVar = con.prepareStatement("DELETE FROM character_quests WHERE charId=? AND name=? AND var=?");
-
- PreparedStatement statement = con.prepareStatement("SELECT name,value FROM character_quests WHERE charId=? AND var=?");
- statement.setInt(1, player.getObjectId());
- statement.setString(2, "<state>");
- ResultSet rs = statement.executeQuery();
- while (rs.next())
- {
- // Get ID of the quest and ID of its state
- String questId = rs.getString("name");
- String statename = rs.getString("value");
-
- // Search quest associated with the ID
- Quest q = QuestManager.getInstance().getQuest(questId);
- if (q == null)
- {
- _log.finer("Unknown quest " + questId + " for player " + player.getName());
- if (Config.AUTODELETE_INVALID_QUEST_DATA)
- {
- invalidQuestData.setInt(1, player.getObjectId());
- invalidQuestData.setString(2, questId);
- invalidQuestData.executeUpdate();
- }
- continue;
- }
-
- // Create a new QuestState for the player that will be added to the player's list of quests
- new QuestState(q, player, State.getStateId(statename));
- }
- rs.close();
- invalidQuestData.close();
- statement.close();
-
- // Get list of quests owned by the player from the DB in order to add variables used in the quest.
- statement = con.prepareStatement("SELECT name,var,value FROM character_quests WHERE charId=? AND var<>?");
- statement.setInt(1, player.getObjectId());
- statement.setString(2, "<state>");
- rs = statement.executeQuery();
- while (rs.next())
- {
- String questId = rs.getString("name");
- String var = rs.getString("var");
- String value = rs.getString("value");
- // Get the QuestState saved in the loop before
- QuestState qs = player.getQuestState(questId);
- if (qs == null)
- {
- _log.finer("Lost variable " + var + " in quest " + questId + " for player " + player.getName());
- if (Config.AUTODELETE_INVALID_QUEST_DATA)
- {
- invalidQuestDataVar.setInt(1, player.getObjectId());
- invalidQuestDataVar.setString(2, questId);
- invalidQuestDataVar.setString(3, var);
- invalidQuestDataVar.executeUpdate();
- }
- continue;
- }
- // Add parameter to the quest
- qs.setInternal(var, value);
- }
- rs.close();
- invalidQuestDataVar.close();
- statement.close();
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "could not insert char quest:", e);
- }
- finally
- {
- L2DatabaseFactory.close(con);
- }
-
- // events
- for (String name : _allEventsS.keySet())
- {
- player.processQuestEvent(name, "enter");
- }
- }
-
- /**
- * Insert (or update) in the database variables that need to stay persistent for this quest after a reboot.<br>
- * This function is for storage of values that do not related to a specific player but are global for all characters.<br>
- * For example, if we need to disable a quest-gatekeeper until a certain time (as is done with some grand-boss gatekeepers), we can save that time in the DB.
- * @param var the name of the variable to save
- * @param value the value of the variable
- */
- public final void saveGlobalQuestVar(String var, String value)
- {
- Connection con = null;
- try
- {
- con = L2DatabaseFactory.getInstance().getConnection();
- PreparedStatement statement = con.prepareStatement("REPLACE INTO quest_global_data (quest_name,var,value) VALUES (?,?,?)");
- statement.setString(1, getName());
- statement.setString(2, var);
- statement.setString(3, value);
- statement.executeUpdate();
- statement.close();
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "could not insert global quest variable:", e);
- }
- finally
- {
- L2DatabaseFactory.close(con);
- }
- }
-
- /**
- * 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 to load
- * @return the current value of the specified variable, or an empty string if the variable does not exist
- */
- public final String loadGlobalQuestVar(String var)
- {
- String result = "";
- Connection con = null;
- try
- {
- con = L2DatabaseFactory.getInstance().getConnection();
- PreparedStatement statement;
- statement = con.prepareStatement("SELECT value FROM quest_global_data WHERE quest_name = ? AND var = ?");
- statement.setString(1, getName());
- statement.setString(2, var);
- ResultSet rs = statement.executeQuery();
- if (rs.first())
- {
- result = rs.getString(1);
- }
- rs.close();
- statement.close();
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "could not load global quest variable:", e);
- }
- finally
- {
- L2DatabaseFactory.close(con);
- }
- return result;
- }
-
- /**
- * Permanently delete from the database a global quest variable that was previously saved for this quest.
- * @param var the name of the variable to delete
- */
- public final void deleteGlobalQuestVar(String var)
- {
- Connection con = null;
- try
- {
- con = L2DatabaseFactory.getInstance().getConnection();
- PreparedStatement statement = con.prepareStatement("DELETE FROM quest_global_data WHERE quest_name = ? AND var = ?");
- statement.setString(1, getName());
- statement.setString(2, var);
- statement.executeUpdate();
- statement.close();
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "could not delete global quest variable:", e);
- }
- finally
- {
- L2DatabaseFactory.close(con);
- }
- }
-
- /**
- * Permanently delete from the database all global quest variables that were previously saved for this quest.
- */
- public final void deleteAllGlobalQuestVars()
- {
- Connection con = null;
- try
- {
- con = L2DatabaseFactory.getInstance().getConnection();
- PreparedStatement statement;
- statement = con.prepareStatement("DELETE FROM quest_global_data WHERE quest_name = ?");
- statement.setString(1, getName());
- statement.executeUpdate();
- statement.close();
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "could not delete global quest variables:", e);
- }
- finally
- {
- L2DatabaseFactory.close(con);
- }
- }
-
- /**
- * Insert in the database the quest for the player.
- * @param qs the {@link QuestState} object whose variable to insert
- * @param var the name of the variable
- * @param value the value of the variable
- */
- public static void createQuestVarInDb(QuestState qs, String var, String value)
- {
- Connection con = null;
- try
- {
- con = L2DatabaseFactory.getInstance().getConnection();
- PreparedStatement statement = con.prepareStatement("INSERT INTO character_quests (charId,name,var,value) VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE value=?");
- statement.setInt(1, qs.getPlayer().getObjectId());
- statement.setString(2, qs.getQuestName());
- statement.setString(3, var);
- statement.setString(4, value);
- statement.setString(5, value);
- statement.executeUpdate();
- statement.close();
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "could not insert char quest:", e);
- }
- finally
- {
- L2DatabaseFactory.close(con);
- }
- }
-
- /**
- * Update the value of the variable "var" for the specified quest in database
- * @param qs the {@link QuestState} object whose variable to update
- * @param var the name of the variable
- * @param value the value of the variable
- */
- public static void updateQuestVarInDb(QuestState qs, String var, String value)
- {
- Connection con = null;
- try
- {
- con = L2DatabaseFactory.getInstance().getConnection();
- PreparedStatement statement = con.prepareStatement("UPDATE character_quests SET value=? WHERE charId=? AND name=? AND var = ?");
- statement.setString(1, value);
- statement.setInt(2, qs.getPlayer().getObjectId());
- statement.setString(3, qs.getQuestName());
- statement.setString(4, var);
- statement.executeUpdate();
- statement.close();
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "could not update char quest:", e);
- }
- finally
- {
- L2DatabaseFactory.close(con);
- }
- }
-
- /**
- * Delete a variable of player's quest from the database.
- * @param qs the {@link QuestState} object whose variable to delete
- * @param var the name of the variable to delete
- */
- public static void deleteQuestVarInDb(QuestState qs, String var)
- {
- Connection con = null;
- try
- {
- con = L2DatabaseFactory.getInstance().getConnection();
- PreparedStatement statement;
- statement = con.prepareStatement("DELETE FROM character_quests WHERE charId=? AND name=? AND var=?");
- statement.setInt(1, qs.getPlayer().getObjectId());
- statement.setString(2, qs.getQuestName());
- statement.setString(3, var);
- statement.executeUpdate();
- statement.close();
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "could not delete char quest:", e);
- }
- finally
- {
- L2DatabaseFactory.close(con);
- }
- }
-
- /**
- * Delete from the database all variables and states of the specified quest state.
- * @param qs the {@link QuestState} object whose variables to delete
- * @param repeatable if {@code false}, the state variable will be preserved, otherwise it will be deleted as well
- */
- public static void deleteQuestInDb(QuestState qs, boolean repeatable)
- {
- Connection con = null;
- try
- {
- con = L2DatabaseFactory.getInstance().getConnection();
- final PreparedStatement statement;
- if (repeatable)
- {
- statement = con.prepareStatement(QUEST_DELETE_FROM_CHAR_QUERY);
- statement.setInt(1, qs.getPlayer().getObjectId());
- statement.setString(2, qs.getQuestName());
- }
- else
- {
- statement = con.prepareStatement(QUEST_DELETE_FROM_CHAR_QUERY_NON_REPEATABLE_QUERY);
- statement.setInt(1, qs.getPlayer().getObjectId());
- statement.setString(2, qs.getQuestName());
- statement.setString(3, "<state>");
- }
- statement.executeUpdate();
- statement.close();
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "could not delete char quest:", e);
- }
- finally
- {
- L2DatabaseFactory.close(con);
- }
- }
-
- /**
- * Create a database record for the specified quest state.
- * @param qs the {@link QuestState} object whose data to write in the database
- */
- public static void createQuestInDb(QuestState qs)
- {
- createQuestVarInDb(qs, "<state>", State.getStateName(qs.getState()));
- }
-
- /**
- * Update informations regarding quest in database.<br>
- * Actions:<br>
- * <ul>
- * <li>Get ID state of the quest recorded in object qs</li>
- * <li>Test if quest is completed. If true, add a star (*) before the ID state</li>
- * <li>Save in database the ID state (with or without the star) for the variable called "<state>" of the quest</li>
- * </ul>
- * @param qs : QuestState
- */
- public static void updateQuestInDb(QuestState qs)
- {
- String val = State.getStateName(qs.getState());
- updateQuestVarInDb(qs, "<state>", val);
- }
-
- /**
- * @param player the player whose language settings to use in finding the html of the right language
- * @return the default html for when no quest is available: "You are either not on a quest that involves this NPC.."
- */
- public static String getNoQuestMsg(L2PcInstance player)
- {
- final String result = HtmCache.getInstance().getHtm(player.getHtmlPrefix(), "data/html/noquest.htm");
- if ((result != null) && (result.length() > 0))
- {
- return result;
- }
-
- return DEFAULT_NO_QUEST_MSG;
- }
-
- /**
- * @param player the player whose language settings to use in finding the html of the right language
- * @return the default html for when no quest is already completed: "This quest has already been completed."
- */
- public static String getAlreadyCompletedMsg(L2PcInstance player)
- {
- final String result = HtmCache.getInstance().getHtm(player.getHtmlPrefix(), "data/html/alreadycompleted.htm");
- if ((result != null) && (result.length() > 0))
- {
- return result;
- }
-
- return DEFAULT_ALREADY_COMPLETED_MSG;
- }
-
- /**
- * Add this quest to the list of quests that the passed mob will respond to for the specified Event type.
- * @param npcId : id of the NPC to register
- * @param eventType : type of event being registered
- * @return L2NpcTemplate : Npc Template corresponding to the npcId, or null if the id is invalid
- */
- public L2NpcTemplate addEventId(int npcId, QuestEventType eventType)
- {
- try
- {
- L2NpcTemplate t = NpcTable.getInstance().getTemplate(npcId);
- if (t != null)
- {
- t.addQuestEvent(eventType, this);
- }
-
- if (!_questInvolvedNpcs.contains(Integer.valueOf(npcId)))
- {
- _questInvolvedNpcs.add(npcId);
- }
-
- return t;
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "Exception on addEventId(): " + e.getMessage(), e);
- return null;
- }
- }
-
- /**
- * Add the quest to the NPC's startQuest
- * @param npcIds
- * @return L2NpcTemplate : Start NPC
- */
- public L2NpcTemplate[] addStartNpc(int... npcIds)
- {
- L2NpcTemplate[] value = new L2NpcTemplate[npcIds.length];
- int i = 0;
- for (int npcId : npcIds)
- {
- value[i++] = addEventId(npcId, QuestEventType.QUEST_START);
- }
- return value;
- }
-
- public L2NpcTemplate addStartNpc(int npcId)
- {
- return addEventId(npcId, QuestEventType.QUEST_START);
- }
-
- /**
- * Add the quest to the NPC's first-talk (default action dialog)
- * @param npcIds
- * @return L2NpcTemplate : Start NPC
- */
- public L2NpcTemplate[] addFirstTalkId(int... npcIds)
- {
- L2NpcTemplate[] value = new L2NpcTemplate[npcIds.length];
- int i = 0;
- for (int npcId : npcIds)
- {
- value[i++] = addEventId(npcId, QuestEventType.ON_FIRST_TALK);
- }
- return value;
- }
-
- /**
- * @param npcId
- * @return
- */
- public L2NpcTemplate addFirstTalkId(int npcId)
- {
- return addEventId(npcId, QuestEventType.ON_FIRST_TALK);
- }
-
- /**
- * Add the NPC to the AcquireSkill dialog
- * @param npcIds
- * @return L2NpcTemplate : NPC
- */
- public L2NpcTemplate[] addAcquireSkillId(int... npcIds)
- {
- L2NpcTemplate[] value = new L2NpcTemplate[npcIds.length];
- int i = 0;
- for (int npcId : npcIds)
- {
- value[i++] = addEventId(npcId, QuestEventType.ON_SKILL_LEARN);
- }
- return value;
- }
-
- /**
- * @param npcId
- * @return
- */
- public L2NpcTemplate addAcquireSkillId(int npcId)
- {
- return addEventId(npcId, QuestEventType.ON_SKILL_LEARN);
- }
-
- /**
- * Add this quest to the list of quests that the passed mob will respond to for Attack Events.
- * @param npcIds
- * @return int : attackId
- */
- public L2NpcTemplate[] addAttackId(int... npcIds)
- {
- L2NpcTemplate[] value = new L2NpcTemplate[npcIds.length];
- int i = 0;
- for (int npcId : npcIds)
- {
- value[i++] = addEventId(npcId, QuestEventType.ON_ATTACK);
- }
- return value;
- }
-
- /**
- * @param npcId
- * @return
- */
- public L2NpcTemplate addAttackId(int npcId)
- {
- return addEventId(npcId, QuestEventType.ON_ATTACK);
- }
-
- /**
- * Add this quest to the list of quests that the passed mob will respond to for Kill Events.
- * @param killIds
- * @return int : killId
- */
- public L2NpcTemplate[] addKillId(int... killIds)
- {
- L2NpcTemplate[] value = new L2NpcTemplate[killIds.length];
- int i = 0;
- for (int killId : killIds)
- {
- value[i++] = addEventId(killId, QuestEventType.ON_KILL);
- }
- return value;
- }
-
- /**
- * @param npcId
- * @return
- */
- public L2NpcTemplate addKillId(int npcId)
- {
- return addEventId(npcId, QuestEventType.ON_KILL);
- }
-
- /**
- * Add this quest to the list of quests that the passed npc will respond to for Talk Events.
- * @param talkIds : ID of the NPC
- * @return int : ID of the NPC
- */
- public L2NpcTemplate[] addTalkId(int... talkIds)
- {
- L2NpcTemplate[] value = new L2NpcTemplate[talkIds.length];
- int i = 0;
- for (int talkId : talkIds)
- {
- value[i++] = addEventId(talkId, QuestEventType.ON_TALK);
- }
- return value;
- }
-
- /**
- * @param npcId
- * @return
- */
- public L2NpcTemplate addTalkId(int npcId)
- {
- return addEventId(npcId, QuestEventType.ON_TALK);
- }
-
- /**
- * Add this quest to the list of quests that the passed npc will respond to for Spawn Events.
- * @param npcIds : ID of the NPC
- * @return int : ID of the NPC
- */
- public L2NpcTemplate[] addSpawnId(int... npcIds)
- {
- L2NpcTemplate[] value = new L2NpcTemplate[npcIds.length];
- int i = 0;
- for (int npcId : npcIds)
- {
- value[i++] = addEventId(npcId, QuestEventType.ON_SPAWN);
- }
- return value;
- }
-
- /**
- * @param npcId
- * @return
- */
- public L2NpcTemplate addSpawnId(int npcId)
- {
- return addEventId(npcId, QuestEventType.ON_SPAWN);
- }
-
- /**
- * Add this quest to the list of quests that the passed npc will respond to for Skill-See Events.
- * @param npcIds : ID of the NPC
- * @return int : ID of the NPC
- */
- public L2NpcTemplate[] addSkillSeeId(int... npcIds)
- {
- L2NpcTemplate[] value = new L2NpcTemplate[npcIds.length];
- int i = 0;
- for (int npcId : npcIds)
- {
- value[i++] = addEventId(npcId, QuestEventType.ON_SKILL_SEE);
- }
- return value;
- }
-
- /**
- * @param npcId
- * @return
- */
- public L2NpcTemplate addSkillSeeId(int npcId)
- {
- return addEventId(npcId, QuestEventType.ON_SKILL_SEE);
- }
-
- /**
- * @param npcIds
- * @return
- */
- public L2NpcTemplate[] addSpellFinishedId(int... npcIds)
- {
- L2NpcTemplate[] value = new L2NpcTemplate[npcIds.length];
- int i = 0;
- for (int npcId : npcIds)
- {
- value[i++] = addEventId(npcId, QuestEventType.ON_SPELL_FINISHED);
- }
- return value;
- }
-
- /**
- * @param npcId
- * @return
- */
- public L2NpcTemplate addSpellFinishedId(int npcId)
- {
- return addEventId(npcId, QuestEventType.ON_SPELL_FINISHED);
- }
-
- /**
- * @param npcIds
- * @return
- */
- public L2NpcTemplate[] addTrapActionId(int... npcIds)
- {
- L2NpcTemplate[] value = new L2NpcTemplate[npcIds.length];
- int i = 0;
- for (int npcId : npcIds)
- {
- value[i++] = addEventId(npcId, QuestEventType.ON_TRAP_ACTION);
- }
- return value;
- }
-
- /**
- * @param npcId
- * @return
- */
- public L2NpcTemplate addTrapActionId(int npcId)
- {
- return addEventId(npcId, QuestEventType.ON_TRAP_ACTION);
- }
-
- /**
- * Add this quest to the list of quests that the passed npc will respond to for Faction Call Events.
- * @param npcIds : ID of the NPC
- * @return int : ID of the NPC
- */
- public L2NpcTemplate[] addFactionCallId(int... npcIds)
- {
- L2NpcTemplate[] value = new L2NpcTemplate[npcIds.length];
- int i = 0;
- for (int npcId : npcIds)
- {
- value[i++] = addEventId(npcId, QuestEventType.ON_FACTION_CALL);
- }
- return value;
- }
-
- /**
- * @param npcId
- * @return
- */
- public L2NpcTemplate addFactionCallId(int npcId)
- {
- return addEventId(npcId, QuestEventType.ON_FACTION_CALL);
- }
-
- /**
- * Add this quest to the list of quests that the passed npc will respond to for Character See Events.
- * @param npcIds : ID of the NPC
- * @return int : ID of the NPC
- */
- public L2NpcTemplate[] addAggroRangeEnterId(int... npcIds)
- {
- L2NpcTemplate[] value = new L2NpcTemplate[npcIds.length];
- int i = 0;
- for (int npcId : npcIds)
- {
- value[i++] = addEventId(npcId, QuestEventType.ON_AGGRO_RANGE_ENTER);
- }
- return value;
- }
-
- /**
- * @param npcId
- * @return
- */
- public L2NpcTemplate addAggroRangeEnterId(int npcId)
- {
- return addEventId(npcId, QuestEventType.ON_AGGRO_RANGE_ENTER);
- }
-
- /**
- * @param zoneIds
- * @return
- */
- public L2ZoneType[] addEnterZoneId(int... zoneIds)
- {
- L2ZoneType[] value = new L2ZoneType[zoneIds.length];
- int i = 0;
- for (int zoneId : zoneIds)
- {
- try
- {
- L2ZoneType zone = ZoneManager.getInstance().getZoneById(zoneId);
- if (zone != null)
- {
- zone.addQuestEvent(QuestEventType.ON_ENTER_ZONE, this);
- }
- value[i++] = zone;
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "Exception on addEnterZoneId(): " + e.getMessage(), e);
- continue;
- }
- }
-
- return value;
- }
-
- /**
- * @param zoneId
- * @return
- */
- public L2ZoneType addEnterZoneId(int zoneId)
- {
- try
- {
- L2ZoneType zone = ZoneManager.getInstance().getZoneById(zoneId);
- if (zone != null)
- {
- zone.addQuestEvent(QuestEventType.ON_ENTER_ZONE, this);
- }
- return zone;
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "Exception on addEnterZoneId(): " + e.getMessage(), e);
- return null;
- }
- }
-
- /**
- * @param zoneIds
- * @return
- */
- public L2ZoneType[] addExitZoneId(int... zoneIds)
- {
- L2ZoneType[] value = new L2ZoneType[zoneIds.length];
- int i = 0;
- for (int zoneId : zoneIds)
- {
- try
- {
- L2ZoneType zone = ZoneManager.getInstance().getZoneById(zoneId);
- if (zone != null)
- {
- zone.addQuestEvent(QuestEventType.ON_EXIT_ZONE, this);
- }
- value[i++] = zone;
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "Exception on addEnterZoneId(): " + e.getMessage(), e);
- continue;
- }
- }
-
- return value;
- }
-
- /**
- * @param zoneId
- * @return
- */
- public L2ZoneType addExitZoneId(int zoneId)
- {
- try
- {
- L2ZoneType zone = ZoneManager.getInstance().getZoneById(zoneId);
- if (zone != null)
- {
- zone.addQuestEvent(QuestEventType.ON_EXIT_ZONE, this);
- }
- return zone;
- }
- catch (Exception e)
- {
- _log.log(Level.WARNING, "Exception on addExitZoneId(): " + e.getMessage(), e);
- return null;
- }
- }
-
- /**
- * Use this method to get a random party member from a player's party.<br>
- * Useful when distributing rewards after killing an NPC.
- * @param player this parameter represents the player whom the party will taken.
- * @return {@code null} if {@code player} is {@code null}, {@code player} itself if the player does not have a party, and a random party member in all other cases
- */
- public L2PcInstance getRandomPartyMember(L2PcInstance player)
- {
- if (player == null)
- {
- return null;
- }
- final L2Party party = player.getParty();
- if ((party == null) || (party.getMembers().isEmpty()))
- {
- return player;
- }
- return party.getMembers().get(Rnd.get(party.getMembers().size()));
- }
-
- /**
- * Auxiliary function for party quests.<br>
- * Note: This function is only here because of how commonly it may be used by quest developers.<br>
- * For any variations on this function, the quest script can always handle things on its own.
- * @param player the instance of a player whose party is to be searched
- * @param value the value of the "cond" variable that must be matched
- * @return L2PcInstance: L2PcInstance for a random party member that matches the specified condition, or {@code null} if no match was found
- */
- public L2PcInstance getRandomPartyMember(L2PcInstance player, String value)
- {
- return getRandomPartyMember(player, "cond", value);
- }
-
- /**
- * Auxiliary function for party quests.<br>
- * Note: This function is only here because of how commonly it may be used by quest developers.<br>
- * For any variations on this function, the quest script can always handle things on its own.
- * @param player the instance of a player whose party is to be searched
- * @param var the quest variable to look for in party members. If {@code null}, it simply unconditionally returns a random party member
- * @param value the value of the specified quest variable the random party member must have
- * @return a random party member that matches the specified conditions or {@code null} if no match was found.<br>
- * If the {@code var} parameter is {@code null}, a random party member is selected without any conditions.<br>
- * The party member must be within a range of 1500 ingame units of the target of the reference player, or, if no target exists, within the same range of the player itself
- */
- public L2PcInstance getRandomPartyMember(L2PcInstance player, String var, String value)
- {
- // if no valid player instance is passed, there is nothing to check...
- if (player == null)
- {
- return null;
- }
-
- // for null var condition, return any random party member.
- if (var == null)
- {
- return getRandomPartyMember(player);
- }
-
- // normal cases...if the player is not in a party, check the player's state
- QuestState temp = null;
- L2Party party = player.getParty();
- // if this player is not in a party, just check if this player instance matches the conditions itself
- if ((party == null) || (party.getMembers().isEmpty()))
- {
- temp = player.getQuestState(getName());
- if ((temp != null) && (temp.get(var) != null) && (temp.get(var)).equalsIgnoreCase(value))
- {
- return player; // match
- }
-
- return null; // no match
- }
-
- // if the player is in a party, gather a list of all matching party members (possibly
- // including this player)
- List<L2PcInstance> candidates = new ArrayList<>();
- // get the target for enforcing distance limitations.
- L2Object target = player.getTarget();
- if (target == null)
- {
- target = player;
- }
-
- for (L2PcInstance partyMember : party.getMembers())
- {
- if (partyMember == null)
- {
- continue;
- }
- temp = partyMember.getQuestState(getName());
- if ((temp != null) && (temp.get(var) != null) && (temp.get(var)).equalsIgnoreCase(value) && partyMember.isInsideRadius(target, 1500, true, false))
- {
- candidates.add(partyMember);
- }
- }
- // if there was no match, return null...
- if (candidates.isEmpty())
- {
- return null;
- }
- // TODO where's the range check?
- // if a match was found from the party, return one of them at random.
- return candidates.get(Rnd.get(candidates.size()));
- }
-
- /**
- * Auxiliary function for party quests.<br>
- * Note: This function is only here because of how commonly it may be used by quest developers.<br>
- * For any variations on this function, the quest script can always handle things on its own.
- * @param player the player whose random party member is to be selected
- * @param state the quest state required of the random party member
- * @return {@code null} if nothing was selected or a random party member that has the specified quest state
- */
- public L2PcInstance getRandomPartyMemberState(L2PcInstance player, byte state)
- {
- // if no valid player instance is passed, there is nothing to check...
- if (player == null)
- {
- return null;
- }
-
- // normal cases...if the player is not in a partym check the player's state
- QuestState temp = null;
- L2Party party = player.getParty();
- // if this player is not in a party, just check if this player instance matches the conditions itself
- if ((party == null) || (party.getMembers().isEmpty()))
- {
- temp = player.getQuestState(getName());
- if ((temp != null) && (temp.getState() == state))
- {
- return player; // match
- }
-
- return null; // no match
- }
-
- // if the player is in a party, gather a list of all matching party members (possibly
- // including this player)
- List<L2PcInstance> candidates = new ArrayList<>();
-
- // get the target for enforcing distance limitations.
- L2Object target = player.getTarget();
- if (target == null)
- {
- target = player;
- }
-
- for (L2PcInstance partyMember : party.getMembers())
- {
- if (partyMember == null)
- {
- continue;
- }
- temp = partyMember.getQuestState(getName());
- if ((temp != null) && (temp.getState() == state) && partyMember.isInsideRadius(target, 1500, true, false))
- {
- candidates.add(partyMember);
- }
- }
- // if there was no match, return null...
- if (candidates.isEmpty())
- {
- return null;
- }
-
- // if a match was found from the party, return one of them at random.
- return candidates.get(Rnd.get(candidates.size()));
- }
-
- /**
- * Show HTML file to client
- * @param player
- * @param fileName
- * @return String : message sent to client
- */
- public String showHtmlFile(L2PcInstance player, String fileName)
- {
- boolean questwindow = true;
- if (fileName.endsWith(".html"))
- {
- questwindow = false;
- }
- int questId = getQuestIntId();
- // Create handler to file linked to the quest
- String content = getHtm(player.getHtmlPrefix(), fileName);
-
- if (player.getTarget() != null)
- {
- content = content.replaceAll("%objectId%", String.valueOf(player.getTarget().getObjectId()));
- }
-
- // Send message to client if message not empty
- if (content != null)
- {
- if (questwindow && (questId > 0) && (questId < 20000) && (questId != 999))
- {
- NpcQuestHtmlMessage npcReply = new NpcQuestHtmlMessage(5, questId);
- npcReply.setHtml(content);
- npcReply.replace("%playername%", player.getName());
- player.sendPacket(npcReply);
- }
- else
- {
- NpcHtmlMessage npcReply = new NpcHtmlMessage(5);
- npcReply.setHtml(content);
- npcReply.replace("%playername%", player.getName());
- player.sendPacket(npcReply);
- }
- player.sendPacket(ActionFailed.STATIC_PACKET);
- }
-
- return content;
- }
-
- /**
- * Return HTML file contents
- * @param prefix player's language prefix.
- * @param fileName the html file to be get.
- * @return
- */
- public String getHtm(String prefix, String fileName)
- {
- String content = HtmCache.getInstance().getHtm(prefix, "data/scripts/" + getDescr().toLowerCase() + "/" + getName() + "/" + fileName);
-
- if (content == null)
- {
- content = HtmCache.getInstance().getHtm(prefix, "data/scripts/quests/Q" + getName() + "/" + fileName);
- if (content == null)
- {
- content = HtmCache.getInstance().getHtmForce(prefix, "data/scripts/quests/" + getName() + "/" + fileName);
- }
- }
-
- return content;
- }
-
- /**
- * Add a temporary (quest) spawn
- * @param npcId
- * @param cha
- * @return instance of newly spawned npc
- */
- public L2Npc addSpawn(int npcId, L2Character cha)
- {
- return addSpawn(npcId, cha.getX(), cha.getY(), cha.getZ(), cha.getHeading(), false, 0, false);
- }
-
- /**
- * Add a temporary (quest) spawn
- * @param npcId
- * @param cha
- * @param isSummonSpawn
- * @return instance of newly spawned npc with summon animation
- */
- public L2Npc addSpawn(int npcId, L2Character cha, boolean isSummonSpawn)
- {
- return addSpawn(npcId, cha.getX(), cha.getY(), cha.getZ(), cha.getHeading(), false, 0, isSummonSpawn);
- }
-
- /**
- * @param npcId
- * @param x
- * @param y
- * @param z
- * @param heading
- * @param randomOffSet
- * @param despawnDelay
- * @return
- */
- public L2Npc addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffSet, long despawnDelay)
- {
- return addSpawn(npcId, x, y, z, heading, randomOffSet, despawnDelay, false);
- }
-
- /**
- * @param npcId
- * @param loc
- * @param randomOffSet
- * @param despawnDelay
- * @return
- */
- public L2Npc addSpawn(int npcId, Location loc, boolean randomOffSet, long despawnDelay)
- {
- return addSpawn(npcId, loc.getX(), loc.getY(), loc.getZ(), loc.getHeading(), randomOffSet, despawnDelay, false);
- }
-
- /**
- * @param npcId
- * @param x
- * @param y
- * @param z
- * @param heading
- * @param randomOffset
- * @param despawnDelay
- * @param isSummonSpawn
- * @return
- */
- public L2Npc addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, long despawnDelay, boolean isSummonSpawn)
- {
- return addSpawn(npcId, x, y, z, heading, randomOffset, despawnDelay, isSummonSpawn, 0);
- }
-
- /**
- * @param npcId
- * @param loc
- * @param randomOffset
- * @param despawnDelay
- * @param isSummonSpawn
- * @return
- */
- public L2Npc addSpawn(int npcId, Location loc, boolean randomOffset, long despawnDelay, boolean isSummonSpawn)
- {
- return addSpawn(npcId, loc.getX(), loc.getY(), loc.getZ(), loc.getHeading(), randomOffset, despawnDelay, isSummonSpawn, 0);
- }
-
- /**
- * @param npcId
- * @param x
- * @param y
- * @param z
- * @param heading
- * @param randomOffset
- * @param despawnDelay
- * @param isSummonSpawn
- * @param instanceId
- * @return
- */
- public L2Npc addSpawn(int npcId, int x, int y, int z, int heading, boolean randomOffset, long despawnDelay, boolean isSummonSpawn, int instanceId)
- {
- L2Npc result = null;
- try
- {
- L2NpcTemplate template = NpcTable.getInstance().getTemplate(npcId);
- if (template != null)
- {
- // Sometimes, even if the quest script specifies some xyz (for example npc.getX() etc) by the time the code
- // reaches here, xyz have become 0! Also, a questdev might have purposely set xy to 0,0...however,
- // the spawn code is coded such that if x=y=0, it looks into location for the spawn loc! This will NOT work
- // with quest spawns! For both of the above cases, we need a fail-safe spawn. For this, we use the
- // default spawn location, which is at the player's loc.
- if ((x == 0) && (y == 0))
- {
- _log.log(Level.SEVERE, "Failed to adjust bad locks for quest spawn! Spawn aborted!");
- return null;
- }
- if (randomOffset)
- {
- int offset;
-
- offset = Rnd.get(2); // Get the direction of the offset
- if (offset == 0)
- {
- // make offset negative
- offset = -1;
- }
- offset *= Rnd.get(50, 100);
- x += offset;
-
- offset = Rnd.get(2); // Get the direction of the offset
- if (offset == 0)
- {
- // make offset negative
- offset = -1;
- }
- offset *= Rnd.get(50, 100);
- y += offset;
- }
- L2Spawn spawn = new L2Spawn(template);
- spawn.setInstanceId(instanceId);
- spawn.setHeading(heading);
- spawn.setLocx(x);
- spawn.setLocy(y);
- spawn.setLocz(z);
- spawn.stopRespawn();
- result = spawn.spawnOne(isSummonSpawn);
-
- if (despawnDelay > 0)
- {
- result.scheduleDespawn(despawnDelay);
- }
-
- return result;
- }
- }
- catch (Exception e1)
- {
- _log.warning("Could not spawn Npc " + npcId + " Error: " + e1.getMessage());
- }
-
- return null;
- }
-
- /**
- * @param trapId
- * @param x
- * @param y
- * @param z
- * @param heading
- * @param skill
- * @param instanceId
- * @return
- */
- public L2Trap addTrap(int trapId, int x, int y, int z, int heading, L2Skill skill, int instanceId)
- {
- L2NpcTemplate TrapTemplate = NpcTable.getInstance().getTemplate(trapId);
- L2Trap trap = new L2TrapInstance(IdFactory.getInstance().getNextId(), TrapTemplate, instanceId, -1, skill);
- trap.setCurrentHp(trap.getMaxHp());
- trap.setCurrentMp(trap.getMaxMp());
- trap.setIsInvul(true);
- trap.setHeading(heading);
- // L2World.getInstance().storeObject(trap);
- trap.spawnMe(x, y, z);
-
- return trap;
- }
-
- /**
- * @param master
- * @param minionId
- * @return
- */
- public L2Npc addMinion(L2MonsterInstance master, int minionId)
- {
- return MinionList.spawnMinion(master, minionId);
- }
-
- /**
- * @return the registered quest items Ids.
- */
- public int[] getRegisteredItemIds()
- {
- return questItemIds;
- }
-
- @Override
- public String getScriptName()
- {
- return getName();
- }
-
- @Override
- public void setActive(boolean status)
- {
- // TODO implement me
- }
-
- @Override
- public boolean reload()
- {
- unload();
- return super.reload();
- }
-
- @Override
- public boolean unload()
- {
- return unload(true);
- }
-
- /**
- * @param removeFromList
- * @return
- */
- public boolean unload(boolean removeFromList)
- {
- saveGlobalData();
- // cancel all pending timers before reloading.
- // if timers ought to be restarted, the quest can take care of it
- // with its code (example: save global data indicating what timer must
- // be restarted).
- for (List<QuestTimer> timers : _allEventTimers.values())
- {
- _readLock.lock();
- try
- {
- for (QuestTimer timer : timers)
- {
- timer.cancel();
- }
- }
- finally
- {
- _readLock.unlock();
- }
- timers.clear();
- }
- _allEventTimers.clear();
-
- for (Integer npcId : _questInvolvedNpcs)
- {
- L2NpcTemplate template = NpcTable.getInstance().getTemplate(npcId.intValue());
- if (template != null)
- {
- template.removeQuest(this);
- }
- }
- _questInvolvedNpcs.clear();
-
- if (removeFromList)
- {
- return QuestManager.getInstance().removeQuest(this);
- }
- return true;
- }
-
- @Override
- public ScriptManager<?> getScriptManager()
- {
- return QuestManager.getInstance();
- }
-
- /**
- * @param val
- */
- public void setOnEnterWorld(boolean val)
- {
- _onEnterWorld = val;
- }
-
- /**
- * @return
- */
- public boolean getOnEnterWorld()
- {
- return _onEnterWorld;
- }
-
- /**
- * If a quest is set as custom, it will display it's name in the NPC Quest List.<br>
- * Retail quests are unhardcoded to display the name using a client string.
- * @param val if {@code true} the quest script will be set as custom quest.
- */
- public void setIsCustom(boolean val)
- {
- _isCustom = val;
- }
-
- /**
- * @return {@code true} if the quest script is a custom quest, {@code false} otherwise.
- */
- public boolean isCustomQuest()
- {
- return _isCustom;
- }
-
- /**
- * @param val
- */
- public void setOlympiadUse(boolean val)
- {
- _isOlympiadUse = val;
- }
-
- /**
- * @return {@code true} if the quest script is used for Olympiad quests, {@code false} otherwise.
- */
- public boolean isOlympiadUse()
- {
- return _isOlympiadUse;
- }
-
- /**
- * @param player this parameter contains a reference to the player to check.
- * @param itemId the item wanted to be count.
- * @return the quantity of one sort of item hold by the player.
- */
- public long getQuestItemsCount(L2PcInstance player, int itemId)
- {
- long count = 0;
- for (L2ItemInstance item : player.getInventory().getItems())
- {
- if ((item != null) && (item.getItemId() == itemId))
- {
- count += item.getCount();
- }
- }
- return count;
- }
-
- /**
- * @param player the player whose inventory to check for quest items
- * @param itemId the ID of the item to check for
- * @return {@code true} if the item exists in player's inventory, {@code false} otherwise
- */
- public boolean hasQuestItems(L2PcInstance player, int itemId)
- {
- return player.getInventory().getItemByItemId(itemId) != null;
- }
-
- /**
- * @param player the player whose inventory to check for quest items
- * @param itemIds a list or array of item IDs to check for
- * @return {@code true} if all items exist in player's inventory, {@code false} otherwise
- */
- public boolean hasQuestItems(L2PcInstance player, int... itemIds)
- {
- final PcInventory inv = player.getInventory();
- for (int itemId : itemIds)
- {
- if (inv.getItemByItemId(itemId) == null)
- {
- return false;
- }
- }
- return true;
- }
-
- /**
- * @param player the player whose item to check
- * @param itemId the ID of the item whose enchantment to check
- * @return the enchantment level of the item or 0 if no item was found
- */
- public int getEnchantLevel(L2PcInstance player, int itemId)
- {
- L2ItemInstance enchanteditem = player.getInventory().getItemByItemId(itemId);
-
- if (enchanteditem == null)
- {
- return 0;
- }
-
- return enchanteditem.getEnchantLevel();
- }
-
- /**
- * Give Adena to the player.
- * @param player the player to whom to give the Adena
- * @param count the amount of Adena to give
- * @param applyRates if {@code true}, quest rates will be applied
- */
- public void giveAdena(L2PcInstance player, long count, boolean applyRates)
- {
- giveItems(player, PcInventory.ADENA_ID, count, applyRates ? 0 : 1);
- }
-
- /**
- * Give reward to player using multipliers.
- * @param player the player to whom to give the item
- * @param itemId the ID of the item to give
- * @param count the amount of items to give
- */
- public void rewardItems(L2PcInstance player, int itemId, long count)
- {
- if (count <= 0)
- {
- return;
- }
-
- L2ItemInstance _tmpItem = ItemTable.getInstance().createDummyItem(itemId);
-
- if (_tmpItem == null)
- {
- return;
- }
-
- if (itemId == PcInventory.ADENA_ID)
- {
- count = (long) (count * Config.RATE_QUEST_REWARD_ADENA);
- }
- else if (Config.RATE_QUEST_REWARD_USE_MULTIPLIERS)
- {
- if (_tmpItem.isEtcItem())
- {
- switch (_tmpItem.getEtcItem().getItemType())
- {
- case POTION:
- count = (long) (count * Config.RATE_QUEST_REWARD_POTION);
- break;
- case SCRL_ENCHANT_WP:
- case SCRL_ENCHANT_AM:
- case SCROLL:
- count = (long) (count * Config.RATE_QUEST_REWARD_SCROLL);
- break;
- case RECIPE:
- count = (long) (count * Config.RATE_QUEST_REWARD_RECIPE);
- break;
- case MATERIAL:
- count = (long) (count * Config.RATE_QUEST_REWARD_MATERIAL);
- break;
- default:
- count = (long) (count * Config.RATE_QUEST_REWARD);
- }
- }
- }
- else
- {
- count = (long) (count * Config.RATE_QUEST_REWARD);
- }
-
- // Add items to player's inventory
- L2ItemInstance item = player.getInventory().addItem("Quest", itemId, count, player, player.getTarget());
- if (item == null)
- {
- return;
- }
-
- sendItemGetMessage(player, item, count);
- }
-
- /**
- * Send the system message and the status update packets to the player.
- * @param player the player that has got the item
- * @param item the item obtain by the player
- * @param count the item count
- */
- private void sendItemGetMessage(L2PcInstance player, L2ItemInstance item, long count)
- {
- // If item for reward is gold, send message of gold reward to client
- if (item.getItemId() == PcInventory.ADENA_ID)
- {
- SystemMessage smsg = SystemMessage.getSystemMessage(SystemMessageId.EARNED_S1_ADENA);
- smsg.addItemNumber(count);
- player.sendPacket(smsg);
- }
- // Otherwise, send message of object reward to client
- else
- {
- if (count > 1)
- {
- SystemMessage smsg = SystemMessage.getSystemMessage(SystemMessageId.EARNED_S2_S1_S);
- smsg.addItemName(item);
- smsg.addItemNumber(count);
- player.sendPacket(smsg);
- }
- else
- {
- SystemMessage smsg = SystemMessage.getSystemMessage(SystemMessageId.EARNED_ITEM_S1);
- smsg.addItemName(item);
- player.sendPacket(smsg);
- }
- }
- // send packets
- StatusUpdate su = new StatusUpdate(player);
- su.addAttribute(StatusUpdate.CUR_LOAD, player.getCurrentLoad());
- player.sendPacket(su);
- }
-
- /**
- * Give item/reward to the player
- * @param player
- * @param itemId
- * @param count
- */
- public void giveItems(L2PcInstance player, int itemId, long count)
- {
- giveItems(player, itemId, count, 0);
- }
-
- /**
- * @param player
- * @param itemId
- * @param count
- * @param enchantlevel
- */
- public void giveItems(L2PcInstance player, int itemId, long count, int enchantlevel)
- {
- if (count <= 0)
- {
- return;
- }
-
- // If item for reward is adena (ID=57), modify count with rate for quest reward if rates available
- if ((itemId == PcInventory.ADENA_ID) && !(enchantlevel > 0))
- {
- count = (long) (count * Config.RATE_QUEST_REWARD_ADENA);
- }
-
- // Add items to player's inventory
- L2ItemInstance item = player.getInventory().addItem("Quest", itemId, count, player, player.getTarget());
- if (item == null)
- {
- return;
- }
-
- // set enchant level for item if that item is not adena
- if ((enchantlevel > 0) && (itemId != PcInventory.ADENA_ID))
- {
- item.setEnchantLevel(enchantlevel);
- }
-
- sendItemGetMessage(player, item, count);
- }
-
- /**
- * @param player
- * @param itemId
- * @param count
- * @param attributeId
- * @param attributeLevel
- */
- public void giveItems(L2PcInstance player, int itemId, long count, byte attributeId, int attributeLevel)
- {
- if (count <= 0)
- {
- return;
- }
-
- // Add items to player's inventory
- L2ItemInstance item = player.getInventory().addItem("Quest", itemId, count, player, player.getTarget());
-
- if (item == null)
- {
- return;
- }
-
- // set enchant level for item if that item is not adena
- if ((attributeId >= 0) && (attributeLevel > 0))
- {
- item.setElementAttr(attributeId, attributeLevel);
- if (item.isEquipped())
- {
- item.updateElementAttrBonus(player);
- }
-
- InventoryUpdate iu = new InventoryUpdate();
- iu.addModifiedItem(item);
- player.sendPacket(iu);
- }
-
- sendItemGetMessage(player, item, count);
- }
-
- /**
- * Drop Quest item using Config.RATE_QUEST_DROP
- * @param player
- * @param itemId int Item Identifier of the item to be dropped
- * @param count (minCount, maxCount) long Quantity of items to be dropped
- * @param neededCount Quantity of items needed for quest
- * @param dropChance int Base chance of drop, same as in droplist
- * @param sound boolean indicating whether to play sound
- * @return boolean indicating whether player has requested number of items
- */
- public boolean dropQuestItems(L2PcInstance player, int itemId, int count, long neededCount, int dropChance, boolean sound)
- {
- return dropQuestItems(player, itemId, count, count, neededCount, dropChance, sound);
- }
-
- /**
- * @param player
- * @param itemId
- * @param minCount
- * @param maxCount
- * @param neededCount
- * @param dropChance
- * @param sound
- * @return
- */
- public boolean dropQuestItems(L2PcInstance player, int itemId, int minCount, int maxCount, long neededCount, int dropChance, boolean sound)
- {
- dropChance *= Config.RATE_QUEST_DROP / ((player.getParty() != null) ? player.getParty().getMemberCount() : 1);
- long currentCount = getQuestItemsCount(player, itemId);
-
- if ((neededCount > 0) && (currentCount >= neededCount))
- {
- return true;
- }
-
- if (currentCount >= neededCount)
- {
- return true;
- }
-
- long itemCount = 0;
- int random = Rnd.get(L2DropData.MAX_CHANCE);
-
- while (random < dropChance)
- {
- // Get the item quantity dropped
- if (minCount < maxCount)
- {
- itemCount += Rnd.get(minCount, maxCount);
- }
- else if (minCount == maxCount)
- {
- itemCount += minCount;
- }
- else
- {
- itemCount++;
- }
-
- // Prepare for next iteration if dropChance > L2DropData.MAX_CHANCE
- dropChance -= L2DropData.MAX_CHANCE;
- }
-
- if (itemCount > 0)
- {
- // if over neededCount, just fill the gap
- if ((neededCount > 0) && ((currentCount + itemCount) > neededCount))
- {
- itemCount = neededCount - currentCount;
- }
-
- // Inventory slot check
- if (!player.getInventory().validateCapacityByItemId(itemId))
- {
- return false;
- }
-
- // Give the item to Player
- player.addItem("Quest", itemId, itemCount, player.getTarget(), true);
-
- if (sound)
- {
- playSound(player, ((currentCount + itemCount) < neededCount) ? "Itemsound.quest_itemget" : "Itemsound.quest_middle");
- }
- }
-
- return ((neededCount > 0) && ((currentCount + itemCount) >= neededCount));
- }
-
- /**
- * Remove items from player's inventory when talking to NPC in order to have rewards.<br>
- * <u><i>Actions :</i></u>
- * <ul>
- * <li>Destroy quantity of items wanted</li>
- * <li>Send new inventory list to player</li>
- * </ul>
- * @param player
- * @param itemId : Identifier of the item
- * @param count : Quantity of items to destroy
- */
- public void takeItems(L2PcInstance player, int itemId, long count)
- {
- // Get object item from player's inventory list
- L2ItemInstance item = player.getInventory().getItemByItemId(itemId);
- if (item == null)
- {
- return;
- }
-
- // Tests on count value in order not to have negative value
- if ((count < 0) || (count > item.getCount()))
- {
- count = item.getCount();
- }
-
- // Destroy the quantity of items wanted
- if (item.isEquipped())
- {
- L2ItemInstance[] unequiped = player.getInventory().unEquipItemInBodySlotAndRecord(item.getItem().getBodyPart());
- InventoryUpdate iu = new InventoryUpdate();
- for (L2ItemInstance itm : unequiped)
- {
- iu.addModifiedItem(itm);
- }
- player.sendPacket(iu);
- player.broadcastUserInfo();
- }
- player.destroyItemByItemId("Quest", itemId, count, player, true);
- }
-
- /**
- * Send a packet in order to play sound at client terminal
- * @param player
- * @param sound
- */
- public void playSound(L2PcInstance player, String sound)
- {
- player.sendPacket(new PlaySound(sound));
- }
-
- /**
- * Add XP and SP as quest reward
- * @param player
- * @param exp
- * @param sp
- */
- public void addExpAndSp(L2PcInstance player, int exp, int sp)
- {
- player.addExpAndSp((int) player.calcStat(Stats.EXPSP_RATE, exp * Config.RATE_QUEST_REWARD_XP, null, null), (int) player.calcStat(Stats.EXPSP_RATE, sp * Config.RATE_QUEST_REWARD_SP, null, null));
- }
-
- /**
- * Gets a random integer number from 0 (inclusive) to {@code max} (exclusive).<br>
- * Use this method instead importing {@link com.l2jserver.util.Rnd} utility.
- * @param max this parameter represents the maximum value for randomization.
- * @return a random integer number from 0 to {@code max} - 1.
- */
- public static int getRandom(int max)
- {
- return Rnd.get(max);
- }
-
- /**
- * Gets a random integer number from {@code min} (inclusive) to {@code max} (inclusive).<br>
- * Use this method instead importing {@link com.l2jserver.util.Rnd} utility.
- * @param min this parameter represents the minimum value for randomization.
- * @param max this parameter represents the maximum value for randomization.
- * @return a random integer number from {@code min} to {@code max} .
- */
- public static int getRandom(int min, int max)
- {
- return Rnd.get(min, max);
- }
-
- /**
- * @param player this parameter is a reference to the player.
- * @param slot this parameter represents the location in the player's inventory.
- * @return the item Id of the item present in the inventory slot {@code slot} if it's not null, 0 otherwise.
- */
- public int getItemEquipped(L2PcInstance player, int slot)
- {
- return player.getInventory().getPaperdollItemId(slot);
- }
-
- /**
- * @return the number of ticks from the {@link com.l2jserver.gameserver.GameTimeController}.
- */
- public int getGameTicks()
- {
- return GameTimeController.getGameTicks();
- }
- }
|