123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476 |
- /*
- * Copyright (C) 2004-2013 L2J Server
- *
- * This file is part of L2J Server.
- *
- * L2J Server is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * L2J Server is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package com.l2jserver.gameserver.model.skills;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.concurrent.ConcurrentHashMap;
- import java.util.concurrent.ScheduledFuture;
- import com.l2jserver.gameserver.GameTimeController;
- import com.l2jserver.gameserver.ThreadPoolManager;
- import com.l2jserver.gameserver.model.CharEffectList;
- import com.l2jserver.gameserver.model.actor.L2Character;
- import com.l2jserver.gameserver.model.effects.AbstractEffect;
- import com.l2jserver.gameserver.model.effects.EffectTaskInfo;
- import com.l2jserver.gameserver.model.effects.EffectTickTask;
- import com.l2jserver.gameserver.model.stats.Env;
- import com.l2jserver.gameserver.model.stats.Formulas;
- import com.l2jserver.gameserver.network.SystemMessageId;
- import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
- /**
- * Buff Info.<br>
- * Complex DTO that holds all the information for a given buff (or debuff or dance/song) set of effects issued by an skill.
- * @author Zoey76
- */
- public final class BuffInfo
- {
- // Data
- /** Data. */
- private final Env _env;
- /** The effects. */
- private final List<AbstractEffect> _effects = new ArrayList<>(1);
- // Tasks
- /** Effect tasks for ticks. */
- private Map<AbstractEffect, EffectTaskInfo> _tasks;
- /** Task for effect ending. */
- private BuffTimeTask _effectTimeTask;
- /** Scheduled future. */
- private ScheduledFuture<?> _scheduledFutureTimeTask;
- // Time and ticks
- /** Abnormal time. */
- private int _abnormalTime;
- /** The game ticks at the start of this effect. */
- private final int _periodStartTicks;
- // Misc
- /** If {@code true} then this effect has been cancelled. */
- private boolean _isRemoved = false;
- /** If {@code true} then this effect is in use (or has been stop because an Herb took place). */
- private boolean _isInUse = true;
-
- /**
- * Buff Info constructor.
- * @param env the cast data
- */
- public BuffInfo(Env env)
- {
- _env = env;
- _abnormalTime = Formulas.calcEffectAbnormalTime(env);
- _periodStartTicks = GameTimeController.getInstance().getGameTicks();
- }
-
- /**
- * Gets the effects on this buff info.
- * @return the effects
- */
- public List<AbstractEffect> getEffects()
- {
- return _effects;
- }
-
- /**
- * Adds an effect to this buff info.
- * @param effect the effect to add
- */
- public void addEffect(AbstractEffect effect)
- {
- _effects.add(effect);
- }
-
- /**
- * Adds an effect task to this buff info.<br>
- * Uses double-checked locking to initialize the map if it's necessary.
- * @param effect the effect that owns the task
- * @param effectTaskInfo the task info
- */
- private void addTask(AbstractEffect effect, EffectTaskInfo effectTaskInfo)
- {
- if (_tasks == null)
- {
- synchronized (this)
- {
- if (_tasks == null)
- {
- _tasks = new ConcurrentHashMap<>();
- }
- }
- }
- _tasks.put(effect, effectTaskInfo);
- }
-
- /**
- * Gets the task for the given effect.
- * @param effect the effect
- * @return the task
- */
- private EffectTaskInfo getEffectTask(AbstractEffect effect)
- {
- return (_tasks == null) ? null : _tasks.get(effect);
- }
-
- /**
- * Gets the skill that created this buff info.
- * @return the skill
- */
- public L2Skill getSkill()
- {
- return _env.getSkill();
- }
-
- public Env getEnv()
- {
- return _env;
- }
-
- /**
- * Gets the calculated abnormal time.
- * @return the abnormal time
- */
- public int getAbnormalTime()
- {
- return _abnormalTime;
- }
-
- /**
- * Sets the abnormal time.
- * @param abnormalTime the abnormal time to set
- */
- public void setAbnormalTime(int abnormalTime)
- {
- _abnormalTime = abnormalTime;
- }
-
- /**
- * Gets the period start ticks.
- * @return the period start
- */
- public int getPeriodStartTicks()
- {
- return _periodStartTicks;
- }
-
- /**
- * Get the remaining time in seconds for this buff info.
- * @return the elapsed time
- */
- public int getTime()
- {
- return _abnormalTime - ((GameTimeController.getInstance().getGameTicks() - _periodStartTicks) / GameTimeController.TICKS_PER_SECOND);
- }
-
- /**
- * Verify if this buff info has been cancelled.
- * @return {@code true} if this buff info has been cancelled, {@code false} otherwise
- */
- public boolean isRemoved()
- {
- return _isRemoved;
- }
-
- /**
- * Set the buff info to removed.
- * @param val the value to set
- */
- public void setRemoved(boolean val)
- {
- _isRemoved = val;
- }
-
- /**
- * Verify if this buff info is in use.
- * @return {@code true} if this buff info is in use, {@code false} otherwise
- */
- public boolean isInUse()
- {
- return _isInUse;
- }
-
- /**
- * Set the buff info to in use.
- * @param val the value to set
- */
- public void setInUse(boolean val)
- {
- _isInUse = val;
- }
-
- /**
- * Gets the character that launched the buff.
- * @return the effector
- */
- public L2Character getEffector()
- {
- return _env.getCharacter();
- }
-
- /**
- * Gets the target of the skill.
- * @return the effected
- */
- public L2Character getEffected()
- {
- return _env.getTarget();
- }
-
- /**
- * Stops all the effects for this buff info.<br>
- * Removes effects stats.<br>
- * <b>It will not remove the buff info from the effect list</b>.<br>
- * Instead call {@link CharEffectList#stopSkillEffects(boolean, L2Skill)}
- * @param removed if {@code true} the skill will be handled as removed
- */
- public void stopAllEffects(boolean removed)
- {
- setRemoved(removed);
- // Cancels the task that will end this buff info
- if ((_scheduledFutureTimeTask != null) && !_scheduledFutureTimeTask.isCancelled())
- {
- _scheduledFutureTimeTask.cancel(false);
- }
-
- // Remove stats
- removeStats();
-
- finishEffects();
- }
-
- public void initializeEffects()
- {
- // When effects are initialized, the successfully landed.
- if (_env.getTarget().isPlayer())
- {
- final SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.YOU_FEEL_S1_EFFECT);
- sm.addSkillName(_env.getSkill());
- _env.getTarget().sendPacket(sm);
- }
-
- // Creates a task that will stop all the effects.
- if (_abnormalTime > 0)
- {
- _effectTimeTask = new BuffTimeTask(this);
- _scheduledFutureTimeTask = ThreadPoolManager.getInstance().scheduleEffectAtFixedRate(_effectTimeTask, 0, 1000L);
- }
-
- for (AbstractEffect effect : _effects)
- {
- if (effect.isInstant())
- {
- // TODO: Log, instant effects shouldn't be added.
- continue;
- }
-
- // Call on start.
- effect.onStart(this);
-
- // If it's a continuous effect, if has ticks schedule a task with period, otherwise schedule a simple task to end it.
- if (effect.getTicks() > 0)
- {
- // The task for the effect ticks
- final EffectTickTask effectTask = new EffectTickTask(this, effect);
- final ScheduledFuture<?> scheduledFuture = ThreadPoolManager.getInstance().scheduleEffectAtFixedRate(effectTask, effect.getTicks() * 1000L, effect.getTicks() * 1000L);
- // Adds the task for ticking
- addTask(effect, new EffectTaskInfo(effectTask, scheduledFuture));
- }
-
- // Add stats.
- _env.getTarget().addStatFuncs(effect.getStatFuncs(_env));
-
- applyAbnormalVisualEffects(_env.getTarget(), effect);
- }
- }
-
- /**
- * Called on each tick.<br>
- * Verify if the effect should end and the effect task should be cancelled.
- * @param effect the effect that is ticking
- * @param tickCount the tick count
- */
- public void onTick(AbstractEffect effect, int tickCount)
- {
- boolean continueForever = false;
- // If the effect is in use, allow it to affect the effected.
- if (_isInUse)
- {
- // Callback for on action time event.
- continueForever = effect.onActionTime(this);
- }
-
- // Skills without abnormal time should tick indefinitely, unless they fail on action time.
- if (_env.getSkill().getAbnormalTime() > 0)
- {
- // Checks made in seconds, although tasks run in milliseconds, it'll increase margin for effects that run indefinitely.
- final long elapsedTime = effect.getTicks() * tickCount;
- if (elapsedTime >= _env.getSkill().getAbnormalTime())
- {
- final EffectTaskInfo task = getEffectTask(effect);
- if (task != null)
- {
- task.getScheduledFuture().cancel(true); // Allow to finish current run.
- _env.getTarget().getEffectList().remove(this); // Remove the buff from the effect list.
- }
- }
- }
- else if (!continueForever)
- {
- final EffectTaskInfo task = getEffectTask(effect);
- if (task != null)
- {
- task.getScheduledFuture().cancel(true); // Allow to finish current run.
- _env.getTarget().getEffectList().remove(this); // Remove the buff from the effect list.
- }
- }
- }
-
- public void finishEffects()
- {
- for (AbstractEffect effect : _effects)
- {
- removeAbnormalVisualEffects(_env.getTarget(), effect); // TODO: Implement correctly.
-
- // Instant effects shouldn't call onExit(..).
- if ((effect != null) && !effect.isInstant())
- {
- effect.onExit(this);
- }
- }
-
- // Cancels the ticking task.
- if (_tasks != null)
- {
- for (Entry<AbstractEffect, EffectTaskInfo> entry : _tasks.entrySet())
- {
- entry.getValue().getScheduledFuture().cancel(true);
- }
- }
-
- // Sends the proper system message.
- SystemMessageId smId = null;
- if (_env.getSkill().isToggle())
- {
- smId = SystemMessageId.S1_HAS_BEEN_ABORTED;
- }
- else if (isRemoved())
- {
- smId = SystemMessageId.EFFECT_S1_HAS_BEEN_REMOVED;
- }
- else if (!_env.getSkill().isPassive())
- {
- smId = SystemMessageId.S1_HAS_WORN_OFF;
- }
-
- if (smId != null)
- {
- final SystemMessage sm = SystemMessage.getSystemMessage(smId);
- sm.addSkillName(_env.getSkill());
- _env.getTarget().sendPacket(sm);
- }
-
- if (this == _env.getTarget().getEffectList().getShortBuff())
- {
- _env.getTarget().getEffectList().shortBuffStatusUpdate(null);
- }
- }
-
- /**
- * Applies all the abnormal visual effects to the effected.<br>
- * TODO: Shouldn't be read from effect template, but from skill template.
- * @param effected the target of the skill
- * @param effect the effect
- */
- private static void applyAbnormalVisualEffects(L2Character effected, AbstractEffect effect)
- {
- if (effect.getAbnormalEffect() != AbnormalVisualEffect.NULL)
- {
- effected.startAbnormalEffect(effect.getAbnormalEffect());
- }
-
- if (effect.getSpecialEffect() != null)
- {
- effected.startSpecialEffect(effect.getSpecialEffect());
- }
-
- if ((effect.getEventEffect() != AbnormalVisualEffect.NULL) && effected.isPlayer())
- {
- effected.getActingPlayer().startEventEffect(effect.getEventEffect());
- }
- }
-
- /**
- * Removes all the abnormal visual effects from the effected.<br>
- * TODO: Shouldn't be read from effect template, but from skill template.
- * @param effected the target of the skill
- * @param effect the effect
- */
- private static void removeAbnormalVisualEffects(L2Character effected, AbstractEffect effect)
- {
- if (effect.getAbnormalEffect() != AbnormalVisualEffect.NULL)
- {
- effected.stopAbnormalEffect(effect.getAbnormalEffect());
- }
-
- if (effect.getSpecialEffect() != null)
- {
- effected.stopSpecialEffect(effect.getSpecialEffect());
- }
-
- if ((effect.getEventEffect() != AbnormalVisualEffect.NULL) && effected.isPlayer())
- {
- effected.getActingPlayer().stopEventEffect(effect.getEventEffect());
- }
- }
-
- public void addStats()
- {
- for (AbstractEffect effect : _effects)
- {
- _env.getTarget().addStatFuncs(effect.getStatFuncs(_env));
- }
- }
-
- public void removeStats()
- {
- for (AbstractEffect effect : _effects)
- {
- _env.getTarget().removeStatsOwner(effect);
- }
- // TODO: This should be removed when all effects are properly managed.
- _env.getTarget().removeStatsOwner(_env.getSkill());
- }
-
- /**
- * Gets the effect tick count.
- * @param effect the effect
- * @return the current tick count
- */
- public int getTickCount(AbstractEffect effect)
- {
- if (_tasks != null)
- {
- final EffectTaskInfo effectTaskInfo = _tasks.get(effect);
- if (effectTaskInfo != null)
- {
- return effectTaskInfo.getEffectTask().getTickCount();
- }
- }
- return 0;
- }
- }
|