Prevent tapping into System.currentTimeMillis with each Timestamp call.

This commit is contained in:
MobiusDevelopment
2019-11-04 17:01:20 +00:00
parent cd918658d1
commit 01f16fdd82
71 changed files with 1583 additions and 2048 deletions
@@ -23,7 +23,7 @@ import org.l2jmobius.gameserver.model.skills.Skill;
* Simple class containing all necessary information to maintain<br> * Simple class containing all necessary information to maintain<br>
* valid time stamps and reuse for skills and items reuse upon re-login.<br> * valid time stamps and reuse for skills and items reuse upon re-login.<br>
* <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b> * <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b>
* @author Yesod, Zoey76 * @author Yesod, Zoey76, Mobius
*/ */
public class TimeStamp public class TimeStamp
{ {
@@ -36,7 +36,7 @@ public class TimeStamp
/** Item or skill reuse time. */ /** Item or skill reuse time. */
private final long _reuse; private final long _reuse;
/** Time stamp. */ /** Time stamp. */
private final long _stamp; private volatile long _stamp;
/** Shared reuse group. */ /** Shared reuse group. */
private final int _group; private final int _group;
@@ -52,7 +52,7 @@ public class TimeStamp
_id2 = skill.getLevel(); _id2 = skill.getLevel();
_id3 = skill.getSubLevel(); _id3 = skill.getSubLevel();
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = -1; _group = -1;
} }
@@ -68,7 +68,7 @@ public class TimeStamp
_id2 = item.getObjectId(); _id2 = item.getObjectId();
_id3 = 0; _id3 = 0;
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = item.getSharedReuseGroup(); _group = item.getSharedReuseGroup();
} }
@@ -151,7 +151,16 @@ public class TimeStamp
*/ */
public long getRemaining() public long getRemaining()
{ {
return Math.max(_stamp - System.currentTimeMillis(), 0); if (_stamp == 0)
{
return 0;
}
final long remainingTime = Math.max(_stamp - System.currentTimeMillis(), 0);
if (remainingTime == 0)
{
_stamp = 0;
}
return remainingTime;
} }
/** /**
@@ -160,6 +169,15 @@ public class TimeStamp
*/ */
public boolean hasNotPassed() public boolean hasNotPassed()
{ {
return System.currentTimeMillis() < _stamp; if (_stamp == 0)
{
return false;
}
final boolean hasNotPassed = System.currentTimeMillis() < _stamp;
if (!hasNotPassed)
{
_stamp = 0;
}
return hasNotPassed;
} }
} }
@@ -212,11 +212,11 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
/** Map containing all skills of this character. */ /** Map containing all skills of this character. */
private final Map<Integer, Skill> _skills = new ConcurrentSkipListMap<>(); private final Map<Integer, Skill> _skills = new ConcurrentSkipListMap<>();
/** Map containing the skill reuse time stamps. */ /** Map containing the skill reuse time stamps. */
private volatile Map<Long, TimeStamp> _reuseTimeStampsSkills = null; private final Map<Long, TimeStamp> _reuseTimeStampsSkills = new ConcurrentHashMap<>();
/** Map containing the item reuse time stamps. */ /** Map containing the item reuse time stamps. */
private volatile Map<Integer, TimeStamp> _reuseTimeStampsItems = null; private final Map<Integer, TimeStamp> _reuseTimeStampsItems = new ConcurrentHashMap<>();
/** Map containing all the disabled skills. */ /** Map containing all the disabled skills. */
private volatile Map<Long, Long> _disabledSkills = null; private final Map<Long, Long> _disabledSkills = new ConcurrentHashMap<>();
private boolean _allSkillsDisabled; private boolean _allSkillsDisabled;
private final byte[] _zones = new byte[ZoneId.getZoneCount()]; private final byte[] _zones = new byte[ZoneId.getZoneCount()];
@@ -1342,16 +1342,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStampItem(ItemInstance item, long reuse, long systime) public void addTimeStampItem(ItemInstance item, long reuse, long systime)
{ {
if (_reuseTimeStampsItems == null)
{
synchronized (this)
{
if (_reuseTimeStampsItems == null)
{
_reuseTimeStampsItems = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime)); _reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime));
} }
@@ -1360,9 +1350,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param itemObjId the item object ID * @param itemObjId the item object ID
* @return if the item has a reuse time stamp, the remaining time, otherwise -1 * @return if the item has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getItemRemainingReuseTime(int itemObjId) public long getItemRemainingReuseTime(int itemObjId)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsItems != null) ? _reuseTimeStampsItems.get(itemObjId) : null; final TimeStamp reuseStamp = _reuseTimeStampsItems.get(itemObjId);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -1373,13 +1363,18 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public long getReuseDelayOnGroup(int group) public long getReuseDelayOnGroup(int group)
{ {
if ((group > 0) && (_reuseTimeStampsItems != null)) if ((group > 0) && !_reuseTimeStampsItems.isEmpty())
{ {
final long currentTime = System.currentTimeMillis();
for (TimeStamp ts : _reuseTimeStampsItems.values()) for (TimeStamp ts : _reuseTimeStampsItems.values())
{ {
if ((ts.getSharedReuseGroup() == group) && ts.hasNotPassed()) if (ts.getSharedReuseGroup() == group)
{ {
return ts.getRemaining(); final long stamp = ts.getStamp();
if (currentTime < stamp)
{
return Math.max(stamp - currentTime, 0);
}
} }
} }
} }
@@ -1414,16 +1409,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStamp(Skill skill, long reuse, long systime) public void addTimeStamp(Skill skill, long reuse, long systime)
{ {
if (_reuseTimeStampsSkills == null)
{
synchronized (this)
{
if (_reuseTimeStampsSkills == null)
{
_reuseTimeStampsSkills = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime)); _reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime));
} }
@@ -1431,33 +1416,27 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* Removes a skill reuse time stamp. * Removes a skill reuse time stamp.
* @param skill the skill to remove * @param skill the skill to remove
*/ */
public synchronized void removeTimeStamp(Skill skill) public void removeTimeStamp(Skill skill)
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.remove(skill.getReuseHashCode()); _reuseTimeStampsSkills.remove(skill.getReuseHashCode());
} }
}
/** /**
* Removes all skill reuse time stamps. * Removes all skill reuse time stamps.
*/ */
public synchronized void resetTimeStamps() public void resetTimeStamps()
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.clear(); _reuseTimeStampsSkills.clear();
} }
}
/** /**
* Gets the skill remaining reuse time for a given skill hash code. * Gets the skill remaining reuse time for a given skill hash code.
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return if the skill has a reuse time stamp, the remaining time, otherwise -1 * @return if the skill has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getSkillRemainingReuseTime(long hashCode) public long getSkillRemainingReuseTime(long hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -1466,9 +1445,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return {@code true} if the skill is under reuse time, {@code false} otherwise * @return {@code true} if the skill is under reuse time, {@code false} otherwise
*/ */
public synchronized boolean hasSkillReuse(long hashCode) public boolean hasSkillReuse(long hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return (reuseStamp != null) && reuseStamp.hasNotPassed(); return (reuseStamp != null) && reuseStamp.hasNotPassed();
} }
@@ -1479,7 +1458,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public synchronized TimeStamp getSkillReuseTimeStamp(long hashCode) public synchronized TimeStamp getSkillReuseTimeStamp(long hashCode)
{ {
return _reuseTimeStampsSkills != null ? _reuseTimeStampsSkills.get(hashCode) : null; return _reuseTimeStampsSkills.get(hashCode);
} }
/** /**
@@ -1497,7 +1476,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void enableSkill(Skill skill) public void enableSkill(Skill skill)
{ {
if ((skill == null) || (_disabledSkills == null)) if (skill == null)
{ {
return; return;
} }
@@ -1516,31 +1495,16 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
{ {
return; return;
} }
if (_disabledSkills == null)
{
synchronized (this)
{
if (_disabledSkills == null)
{
_disabledSkills = new ConcurrentHashMap<>();
}
}
}
_disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE); _disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE);
} }
/** /**
* Removes all the disabled skills. * Removes all the disabled skills.
*/ */
public synchronized void resetDisabledSkills() public void resetDisabledSkills()
{
if (_disabledSkills != null)
{ {
_disabledSkills.clear(); _disabledSkills.clear();
} }
}
/** /**
* Verifies if the skill is disabled. * Verifies if the skill is disabled.
@@ -1570,7 +1534,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
return true; return true;
} }
if (_disabledSkills == null) if (_disabledSkills.isEmpty())
{ {
return false; return false;
} }
@@ -7260,6 +7260,7 @@ public class PlayerInstance extends Playable
int buff_index = 0; int buff_index = 0;
final List<Long> storedSkills = new ArrayList<>(); final List<Long> storedSkills = new ArrayList<>();
final long currentTime = System.currentTimeMillis();
// Store all effect data along with calulated remaining // Store all effect data along with calulated remaining
// reuse delays for matching skills. 'restore_type'= 0. // reuse delays for matching skills. 'restore_type'= 0.
@@ -7319,8 +7320,8 @@ public class PlayerInstance extends Playable
statement.setInt(5, info.getTime()); statement.setInt(5, info.getTime());
final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode()); final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode());
statement.setLong(6, (t != null) && t.hasNotPassed() ? t.getReuse() : 0); statement.setLong(6, (t != null) && (currentTime < t.getStamp()) ? t.getReuse() : 0);
statement.setDouble(7, (t != null) && t.hasNotPassed() ? t.getStamp() : 0); statement.setDouble(7, (t != null) && (currentTime < t.getStamp()) ? t.getStamp() : 0);
statement.setInt(8, 0); // Store type 0, active buffs/debuffs. statement.setInt(8, 0); // Store type 0, active buffs/debuffs.
statement.setInt(9, _classIndex); statement.setInt(9, _classIndex);
@@ -7330,10 +7331,7 @@ public class PlayerInstance extends Playable
} }
// Skills under reuse. // Skills under reuse.
final Map<Long, TimeStamp> reuseTimeStamps = getSkillReuseTimeStamps(); for (Entry<Long, TimeStamp> ts : getSkillReuseTimeStamps().entrySet())
if (reuseTimeStamps != null)
{
for (Entry<Long, TimeStamp> ts : reuseTimeStamps.entrySet())
{ {
final long hash = ts.getKey(); final long hash = ts.getKey();
if (storedSkills.contains(hash)) if (storedSkills.contains(hash))
@@ -7342,7 +7340,7 @@ public class PlayerInstance extends Playable
} }
final TimeStamp t = ts.getValue(); final TimeStamp t = ts.getValue();
if ((t != null) && t.hasNotPassed()) if ((t != null) && (currentTime < t.getStamp()))
{ {
storedSkills.add(hash); storedSkills.add(hash);
@@ -7359,7 +7357,6 @@ public class PlayerInstance extends Playable
statement.addBatch(); statement.addBatch();
} }
} }
}
statement.executeBatch(); statement.executeBatch();
} }
@@ -7379,12 +7376,10 @@ public class PlayerInstance extends Playable
ps1.setInt(1, getObjectId()); ps1.setInt(1, getObjectId());
ps1.execute(); ps1.execute();
final Map<Integer, TimeStamp> itemReuseTimeStamps = getItemReuseTimeStamps(); final long currentTime = System.currentTimeMillis();
if (itemReuseTimeStamps != null) for (TimeStamp ts : getItemReuseTimeStamps().values())
{ {
for (TimeStamp ts : itemReuseTimeStamps.values()) if ((ts != null) && (currentTime < ts.getStamp()))
{
if ((ts != null) && ts.hasNotPassed())
{ {
ps2.setInt(1, getObjectId()); ps2.setInt(1, getObjectId());
ps2.setInt(2, ts.getItemId()); ps2.setInt(2, ts.getItemId());
@@ -7396,7 +7391,6 @@ public class PlayerInstance extends Playable
} }
ps2.executeBatch(); ps2.executeBatch();
} }
}
catch (Exception e) catch (Exception e)
{ {
LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e); LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e);
@@ -18,38 +18,33 @@ package org.l2jmobius.gameserver.network.serverpackets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import org.l2jmobius.commons.network.PacketWriter; import org.l2jmobius.commons.network.PacketWriter;
import org.l2jmobius.gameserver.data.xml.impl.SkillData; import org.l2jmobius.gameserver.data.xml.impl.SkillData;
import org.l2jmobius.gameserver.model.TimeStamp; import org.l2jmobius.gameserver.model.TimeStamp;
import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
import org.l2jmobius.gameserver.model.skills.Skill;
import org.l2jmobius.gameserver.network.OutgoingPackets; import org.l2jmobius.gameserver.network.OutgoingPackets;
/** /**
* Skill Cool Time server packet implementation. * Skill Cool Time server packet implementation.
* @author KenM, Zoey76 * @author KenM, Zoey76, Mobius
*/ */
public class SkillCoolTime implements IClientOutgoingPacket public class SkillCoolTime implements IClientOutgoingPacket
{ {
private final long _currentTime;
private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>(); private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>();
public SkillCoolTime(PlayerInstance player) public SkillCoolTime(PlayerInstance player)
{ {
final Map<Long, TimeStamp> skillReuseTimeStamps = player.getSkillReuseTimeStamps(); _currentTime = System.currentTimeMillis();
if (skillReuseTimeStamps != null) for (TimeStamp ts : player.getSkillReuseTimeStamps().values())
{ {
for (TimeStamp ts : skillReuseTimeStamps.values()) if ((_currentTime < ts.getStamp()) && !SkillData.getInstance().getSkill(ts.getSkillId(), ts.getSkillLvl(), ts.getSkillSubLvl()).isNotBroadcastable())
{
final Skill skill = SkillData.getInstance().getSkill(ts.getSkillId(), ts.getSkillLvl(), ts.getSkillSubLvl());
if (ts.hasNotPassed() && !skill.isNotBroadcastable())
{ {
_skillReuseTimeStamps.add(ts); _skillReuseTimeStamps.add(ts);
} }
} }
} }
}
@Override @Override
public boolean write(PacketWriter packet) public boolean write(PacketWriter packet)
@@ -62,7 +57,7 @@ public class SkillCoolTime implements IClientOutgoingPacket
packet.writeD(ts.getSkillId()); packet.writeD(ts.getSkillId());
packet.writeD(0x00); // ? packet.writeD(0x00); // ?
packet.writeD((int) ts.getReuse() / 1000); packet.writeD((int) ts.getReuse() / 1000);
packet.writeD((int) ts.getRemaining() / 1000); packet.writeD((int) Math.max(ts.getStamp() - _currentTime, 0) / 1000);
} }
return true; return true;
} }
@@ -23,7 +23,7 @@ import org.l2jmobius.gameserver.model.skills.Skill;
* Simple class containing all necessary information to maintain<br> * Simple class containing all necessary information to maintain<br>
* valid time stamps and reuse for skills and items reuse upon re-login.<br> * valid time stamps and reuse for skills and items reuse upon re-login.<br>
* <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b> * <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b>
* @author Yesod, Zoey76 * @author Yesod, Zoey76, Mobius
*/ */
public class TimeStamp public class TimeStamp
{ {
@@ -36,7 +36,7 @@ public class TimeStamp
/** Item or skill reuse time. */ /** Item or skill reuse time. */
private final long _reuse; private final long _reuse;
/** Time stamp. */ /** Time stamp. */
private final long _stamp; private volatile long _stamp;
/** Shared reuse group. */ /** Shared reuse group. */
private final int _group; private final int _group;
@@ -52,7 +52,7 @@ public class TimeStamp
_id2 = skill.getLevel(); _id2 = skill.getLevel();
_id3 = skill.getSubLevel(); _id3 = skill.getSubLevel();
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = -1; _group = -1;
} }
@@ -68,7 +68,7 @@ public class TimeStamp
_id2 = item.getObjectId(); _id2 = item.getObjectId();
_id3 = 0; _id3 = 0;
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = item.getSharedReuseGroup(); _group = item.getSharedReuseGroup();
} }
@@ -151,7 +151,16 @@ public class TimeStamp
*/ */
public long getRemaining() public long getRemaining()
{ {
return Math.max(_stamp - System.currentTimeMillis(), 0); if (_stamp == 0)
{
return 0;
}
final long remainingTime = Math.max(_stamp - System.currentTimeMillis(), 0);
if (remainingTime == 0)
{
_stamp = 0;
}
return remainingTime;
} }
/** /**
@@ -160,6 +169,15 @@ public class TimeStamp
*/ */
public boolean hasNotPassed() public boolean hasNotPassed()
{ {
return System.currentTimeMillis() < _stamp; if (_stamp == 0)
{
return false;
}
final boolean hasNotPassed = System.currentTimeMillis() < _stamp;
if (!hasNotPassed)
{
_stamp = 0;
}
return hasNotPassed;
} }
} }
@@ -212,11 +212,11 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
/** Map containing all skills of this character. */ /** Map containing all skills of this character. */
private final Map<Integer, Skill> _skills = new ConcurrentSkipListMap<>(); private final Map<Integer, Skill> _skills = new ConcurrentSkipListMap<>();
/** Map containing the skill reuse time stamps. */ /** Map containing the skill reuse time stamps. */
private volatile Map<Long, TimeStamp> _reuseTimeStampsSkills = null; private final Map<Long, TimeStamp> _reuseTimeStampsSkills = new ConcurrentHashMap<>();
/** Map containing the item reuse time stamps. */ /** Map containing the item reuse time stamps. */
private volatile Map<Integer, TimeStamp> _reuseTimeStampsItems = null; private final Map<Integer, TimeStamp> _reuseTimeStampsItems = new ConcurrentHashMap<>();
/** Map containing all the disabled skills. */ /** Map containing all the disabled skills. */
private volatile Map<Long, Long> _disabledSkills = null; private final Map<Long, Long> _disabledSkills = new ConcurrentHashMap<>();
private boolean _allSkillsDisabled; private boolean _allSkillsDisabled;
private final byte[] _zones = new byte[ZoneId.getZoneCount()]; private final byte[] _zones = new byte[ZoneId.getZoneCount()];
@@ -1342,16 +1342,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStampItem(ItemInstance item, long reuse, long systime) public void addTimeStampItem(ItemInstance item, long reuse, long systime)
{ {
if (_reuseTimeStampsItems == null)
{
synchronized (this)
{
if (_reuseTimeStampsItems == null)
{
_reuseTimeStampsItems = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime)); _reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime));
} }
@@ -1360,9 +1350,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param itemObjId the item object ID * @param itemObjId the item object ID
* @return if the item has a reuse time stamp, the remaining time, otherwise -1 * @return if the item has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getItemRemainingReuseTime(int itemObjId) public long getItemRemainingReuseTime(int itemObjId)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsItems != null) ? _reuseTimeStampsItems.get(itemObjId) : null; final TimeStamp reuseStamp = _reuseTimeStampsItems.get(itemObjId);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -1373,13 +1363,18 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public long getReuseDelayOnGroup(int group) public long getReuseDelayOnGroup(int group)
{ {
if ((group > 0) && (_reuseTimeStampsItems != null)) if ((group > 0) && !_reuseTimeStampsItems.isEmpty())
{ {
final long currentTime = System.currentTimeMillis();
for (TimeStamp ts : _reuseTimeStampsItems.values()) for (TimeStamp ts : _reuseTimeStampsItems.values())
{ {
if ((ts.getSharedReuseGroup() == group) && ts.hasNotPassed()) if (ts.getSharedReuseGroup() == group)
{ {
return ts.getRemaining(); final long stamp = ts.getStamp();
if (currentTime < stamp)
{
return Math.max(stamp - currentTime, 0);
}
} }
} }
} }
@@ -1414,16 +1409,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStamp(Skill skill, long reuse, long systime) public void addTimeStamp(Skill skill, long reuse, long systime)
{ {
if (_reuseTimeStampsSkills == null)
{
synchronized (this)
{
if (_reuseTimeStampsSkills == null)
{
_reuseTimeStampsSkills = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime)); _reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime));
} }
@@ -1431,33 +1416,27 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* Removes a skill reuse time stamp. * Removes a skill reuse time stamp.
* @param skill the skill to remove * @param skill the skill to remove
*/ */
public synchronized void removeTimeStamp(Skill skill) public void removeTimeStamp(Skill skill)
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.remove(skill.getReuseHashCode()); _reuseTimeStampsSkills.remove(skill.getReuseHashCode());
} }
}
/** /**
* Removes all skill reuse time stamps. * Removes all skill reuse time stamps.
*/ */
public synchronized void resetTimeStamps() public void resetTimeStamps()
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.clear(); _reuseTimeStampsSkills.clear();
} }
}
/** /**
* Gets the skill remaining reuse time for a given skill hash code. * Gets the skill remaining reuse time for a given skill hash code.
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return if the skill has a reuse time stamp, the remaining time, otherwise -1 * @return if the skill has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getSkillRemainingReuseTime(long hashCode) public long getSkillRemainingReuseTime(long hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -1466,9 +1445,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return {@code true} if the skill is under reuse time, {@code false} otherwise * @return {@code true} if the skill is under reuse time, {@code false} otherwise
*/ */
public synchronized boolean hasSkillReuse(long hashCode) public boolean hasSkillReuse(long hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return (reuseStamp != null) && reuseStamp.hasNotPassed(); return (reuseStamp != null) && reuseStamp.hasNotPassed();
} }
@@ -1479,7 +1458,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public synchronized TimeStamp getSkillReuseTimeStamp(long hashCode) public synchronized TimeStamp getSkillReuseTimeStamp(long hashCode)
{ {
return _reuseTimeStampsSkills != null ? _reuseTimeStampsSkills.get(hashCode) : null; return _reuseTimeStampsSkills.get(hashCode);
} }
/** /**
@@ -1497,7 +1476,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void enableSkill(Skill skill) public void enableSkill(Skill skill)
{ {
if ((skill == null) || (_disabledSkills == null)) if (skill == null)
{ {
return; return;
} }
@@ -1516,31 +1495,16 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
{ {
return; return;
} }
if (_disabledSkills == null)
{
synchronized (this)
{
if (_disabledSkills == null)
{
_disabledSkills = new ConcurrentHashMap<>();
}
}
}
_disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE); _disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE);
} }
/** /**
* Removes all the disabled skills. * Removes all the disabled skills.
*/ */
public synchronized void resetDisabledSkills() public void resetDisabledSkills()
{
if (_disabledSkills != null)
{ {
_disabledSkills.clear(); _disabledSkills.clear();
} }
}
/** /**
* Verifies if the skill is disabled. * Verifies if the skill is disabled.
@@ -1570,7 +1534,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
return true; return true;
} }
if (_disabledSkills == null) if (_disabledSkills.isEmpty())
{ {
return false; return false;
} }
@@ -7267,6 +7267,7 @@ public class PlayerInstance extends Playable
int buff_index = 0; int buff_index = 0;
final List<Long> storedSkills = new ArrayList<>(); final List<Long> storedSkills = new ArrayList<>();
final long currentTime = System.currentTimeMillis();
// Store all effect data along with calulated remaining // Store all effect data along with calulated remaining
// reuse delays for matching skills. 'restore_type'= 0. // reuse delays for matching skills. 'restore_type'= 0.
@@ -7326,8 +7327,8 @@ public class PlayerInstance extends Playable
statement.setInt(5, info.getTime()); statement.setInt(5, info.getTime());
final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode()); final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode());
statement.setLong(6, (t != null) && t.hasNotPassed() ? t.getReuse() : 0); statement.setLong(6, (t != null) && (currentTime < t.getStamp()) ? t.getReuse() : 0);
statement.setDouble(7, (t != null) && t.hasNotPassed() ? t.getStamp() : 0); statement.setDouble(7, (t != null) && (currentTime < t.getStamp()) ? t.getStamp() : 0);
statement.setInt(8, 0); // Store type 0, active buffs/debuffs. statement.setInt(8, 0); // Store type 0, active buffs/debuffs.
statement.setInt(9, _classIndex); statement.setInt(9, _classIndex);
@@ -7337,10 +7338,7 @@ public class PlayerInstance extends Playable
} }
// Skills under reuse. // Skills under reuse.
final Map<Long, TimeStamp> reuseTimeStamps = getSkillReuseTimeStamps(); for (Entry<Long, TimeStamp> ts : getSkillReuseTimeStamps().entrySet())
if (reuseTimeStamps != null)
{
for (Entry<Long, TimeStamp> ts : reuseTimeStamps.entrySet())
{ {
final long hash = ts.getKey(); final long hash = ts.getKey();
if (storedSkills.contains(hash)) if (storedSkills.contains(hash))
@@ -7349,7 +7347,7 @@ public class PlayerInstance extends Playable
} }
final TimeStamp t = ts.getValue(); final TimeStamp t = ts.getValue();
if ((t != null) && t.hasNotPassed()) if ((t != null) && (currentTime < t.getStamp()))
{ {
storedSkills.add(hash); storedSkills.add(hash);
@@ -7366,7 +7364,6 @@ public class PlayerInstance extends Playable
statement.addBatch(); statement.addBatch();
} }
} }
}
statement.executeBatch(); statement.executeBatch();
} }
@@ -7386,12 +7383,10 @@ public class PlayerInstance extends Playable
ps1.setInt(1, getObjectId()); ps1.setInt(1, getObjectId());
ps1.execute(); ps1.execute();
final Map<Integer, TimeStamp> itemReuseTimeStamps = getItemReuseTimeStamps(); final long currentTime = System.currentTimeMillis();
if (itemReuseTimeStamps != null) for (TimeStamp ts : getItemReuseTimeStamps().values())
{ {
for (TimeStamp ts : itemReuseTimeStamps.values()) if ((ts != null) && (currentTime < ts.getStamp()))
{
if ((ts != null) && ts.hasNotPassed())
{ {
ps2.setInt(1, getObjectId()); ps2.setInt(1, getObjectId());
ps2.setInt(2, ts.getItemId()); ps2.setInt(2, ts.getItemId());
@@ -7403,7 +7398,6 @@ public class PlayerInstance extends Playable
} }
ps2.executeBatch(); ps2.executeBatch();
} }
}
catch (Exception e) catch (Exception e)
{ {
LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e); LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e);
@@ -18,38 +18,33 @@ package org.l2jmobius.gameserver.network.serverpackets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import org.l2jmobius.commons.network.PacketWriter; import org.l2jmobius.commons.network.PacketWriter;
import org.l2jmobius.gameserver.data.xml.impl.SkillData; import org.l2jmobius.gameserver.data.xml.impl.SkillData;
import org.l2jmobius.gameserver.model.TimeStamp; import org.l2jmobius.gameserver.model.TimeStamp;
import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
import org.l2jmobius.gameserver.model.skills.Skill;
import org.l2jmobius.gameserver.network.OutgoingPackets; import org.l2jmobius.gameserver.network.OutgoingPackets;
/** /**
* Skill Cool Time server packet implementation. * Skill Cool Time server packet implementation.
* @author KenM, Zoey76 * @author KenM, Zoey76, Mobius
*/ */
public class SkillCoolTime implements IClientOutgoingPacket public class SkillCoolTime implements IClientOutgoingPacket
{ {
private final long _currentTime;
private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>(); private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>();
public SkillCoolTime(PlayerInstance player) public SkillCoolTime(PlayerInstance player)
{ {
final Map<Long, TimeStamp> skillReuseTimeStamps = player.getSkillReuseTimeStamps(); _currentTime = System.currentTimeMillis();
if (skillReuseTimeStamps != null) for (TimeStamp ts : player.getSkillReuseTimeStamps().values())
{ {
for (TimeStamp ts : skillReuseTimeStamps.values()) if ((_currentTime < ts.getStamp()) && !SkillData.getInstance().getSkill(ts.getSkillId(), ts.getSkillLvl(), ts.getSkillSubLvl()).isNotBroadcastable())
{
final Skill skill = SkillData.getInstance().getSkill(ts.getSkillId(), ts.getSkillLvl(), ts.getSkillSubLvl());
if (ts.hasNotPassed() && !skill.isNotBroadcastable())
{ {
_skillReuseTimeStamps.add(ts); _skillReuseTimeStamps.add(ts);
} }
} }
} }
}
@Override @Override
public boolean write(PacketWriter packet) public boolean write(PacketWriter packet)
@@ -62,7 +57,7 @@ public class SkillCoolTime implements IClientOutgoingPacket
packet.writeD(ts.getSkillId()); packet.writeD(ts.getSkillId());
packet.writeD(0x00); // ? packet.writeD(0x00); // ?
packet.writeD((int) ts.getReuse() / 1000); packet.writeD((int) ts.getReuse() / 1000);
packet.writeD((int) ts.getRemaining() / 1000); packet.writeD((int) Math.max(ts.getStamp() - _currentTime, 0) / 1000);
} }
return true; return true;
} }
@@ -23,7 +23,7 @@ import org.l2jmobius.gameserver.model.skills.Skill;
* Simple class containing all necessary information to maintain<br> * Simple class containing all necessary information to maintain<br>
* valid time stamps and reuse for skills and items reuse upon re-login.<br> * valid time stamps and reuse for skills and items reuse upon re-login.<br>
* <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b> * <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b>
* @author Yesod, Zoey76 * @author Yesod, Zoey76, Mobius
*/ */
public class TimeStamp public class TimeStamp
{ {
@@ -36,7 +36,7 @@ public class TimeStamp
/** Item or skill reuse time. */ /** Item or skill reuse time. */
private final long _reuse; private final long _reuse;
/** Time stamp. */ /** Time stamp. */
private final long _stamp; private volatile long _stamp;
/** Shared reuse group. */ /** Shared reuse group. */
private final int _group; private final int _group;
@@ -52,7 +52,7 @@ public class TimeStamp
_id2 = skill.getLevel(); _id2 = skill.getLevel();
_id3 = skill.getSubLevel(); _id3 = skill.getSubLevel();
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = -1; _group = -1;
} }
@@ -68,7 +68,7 @@ public class TimeStamp
_id2 = item.getObjectId(); _id2 = item.getObjectId();
_id3 = 0; _id3 = 0;
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = item.getSharedReuseGroup(); _group = item.getSharedReuseGroup();
} }
@@ -151,7 +151,16 @@ public class TimeStamp
*/ */
public long getRemaining() public long getRemaining()
{ {
return Math.max(_stamp - System.currentTimeMillis(), 0); if (_stamp == 0)
{
return 0;
}
final long remainingTime = Math.max(_stamp - System.currentTimeMillis(), 0);
if (remainingTime == 0)
{
_stamp = 0;
}
return remainingTime;
} }
/** /**
@@ -160,6 +169,15 @@ public class TimeStamp
*/ */
public boolean hasNotPassed() public boolean hasNotPassed()
{ {
return System.currentTimeMillis() < _stamp; if (_stamp == 0)
{
return false;
}
final boolean hasNotPassed = System.currentTimeMillis() < _stamp;
if (!hasNotPassed)
{
_stamp = 0;
}
return hasNotPassed;
} }
} }
@@ -212,11 +212,11 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
/** Map containing all skills of this character. */ /** Map containing all skills of this character. */
private final Map<Integer, Skill> _skills = new ConcurrentSkipListMap<>(); private final Map<Integer, Skill> _skills = new ConcurrentSkipListMap<>();
/** Map containing the skill reuse time stamps. */ /** Map containing the skill reuse time stamps. */
private volatile Map<Long, TimeStamp> _reuseTimeStampsSkills = null; private final Map<Long, TimeStamp> _reuseTimeStampsSkills = new ConcurrentHashMap<>();
/** Map containing the item reuse time stamps. */ /** Map containing the item reuse time stamps. */
private volatile Map<Integer, TimeStamp> _reuseTimeStampsItems = null; private final Map<Integer, TimeStamp> _reuseTimeStampsItems = new ConcurrentHashMap<>();
/** Map containing all the disabled skills. */ /** Map containing all the disabled skills. */
private volatile Map<Long, Long> _disabledSkills = null; private final Map<Long, Long> _disabledSkills = new ConcurrentHashMap<>();
private boolean _allSkillsDisabled; private boolean _allSkillsDisabled;
private final byte[] _zones = new byte[ZoneId.getZoneCount()]; private final byte[] _zones = new byte[ZoneId.getZoneCount()];
@@ -1342,16 +1342,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStampItem(ItemInstance item, long reuse, long systime) public void addTimeStampItem(ItemInstance item, long reuse, long systime)
{ {
if (_reuseTimeStampsItems == null)
{
synchronized (this)
{
if (_reuseTimeStampsItems == null)
{
_reuseTimeStampsItems = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime)); _reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime));
} }
@@ -1360,9 +1350,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param itemObjId the item object ID * @param itemObjId the item object ID
* @return if the item has a reuse time stamp, the remaining time, otherwise -1 * @return if the item has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getItemRemainingReuseTime(int itemObjId) public long getItemRemainingReuseTime(int itemObjId)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsItems != null) ? _reuseTimeStampsItems.get(itemObjId) : null; final TimeStamp reuseStamp = _reuseTimeStampsItems.get(itemObjId);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -1373,13 +1363,18 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public long getReuseDelayOnGroup(int group) public long getReuseDelayOnGroup(int group)
{ {
if ((group > 0) && (_reuseTimeStampsItems != null)) if ((group > 0) && !_reuseTimeStampsItems.isEmpty())
{ {
final long currentTime = System.currentTimeMillis();
for (TimeStamp ts : _reuseTimeStampsItems.values()) for (TimeStamp ts : _reuseTimeStampsItems.values())
{ {
if ((ts.getSharedReuseGroup() == group) && ts.hasNotPassed()) if (ts.getSharedReuseGroup() == group)
{ {
return ts.getRemaining(); final long stamp = ts.getStamp();
if (currentTime < stamp)
{
return Math.max(stamp - currentTime, 0);
}
} }
} }
} }
@@ -1414,16 +1409,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStamp(Skill skill, long reuse, long systime) public void addTimeStamp(Skill skill, long reuse, long systime)
{ {
if (_reuseTimeStampsSkills == null)
{
synchronized (this)
{
if (_reuseTimeStampsSkills == null)
{
_reuseTimeStampsSkills = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime)); _reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime));
} }
@@ -1431,33 +1416,27 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* Removes a skill reuse time stamp. * Removes a skill reuse time stamp.
* @param skill the skill to remove * @param skill the skill to remove
*/ */
public synchronized void removeTimeStamp(Skill skill) public void removeTimeStamp(Skill skill)
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.remove(skill.getReuseHashCode()); _reuseTimeStampsSkills.remove(skill.getReuseHashCode());
} }
}
/** /**
* Removes all skill reuse time stamps. * Removes all skill reuse time stamps.
*/ */
public synchronized void resetTimeStamps() public void resetTimeStamps()
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.clear(); _reuseTimeStampsSkills.clear();
} }
}
/** /**
* Gets the skill remaining reuse time for a given skill hash code. * Gets the skill remaining reuse time for a given skill hash code.
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return if the skill has a reuse time stamp, the remaining time, otherwise -1 * @return if the skill has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getSkillRemainingReuseTime(long hashCode) public long getSkillRemainingReuseTime(long hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -1466,9 +1445,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return {@code true} if the skill is under reuse time, {@code false} otherwise * @return {@code true} if the skill is under reuse time, {@code false} otherwise
*/ */
public synchronized boolean hasSkillReuse(long hashCode) public boolean hasSkillReuse(long hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return (reuseStamp != null) && reuseStamp.hasNotPassed(); return (reuseStamp != null) && reuseStamp.hasNotPassed();
} }
@@ -1479,7 +1458,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public synchronized TimeStamp getSkillReuseTimeStamp(long hashCode) public synchronized TimeStamp getSkillReuseTimeStamp(long hashCode)
{ {
return _reuseTimeStampsSkills != null ? _reuseTimeStampsSkills.get(hashCode) : null; return _reuseTimeStampsSkills.get(hashCode);
} }
/** /**
@@ -1497,7 +1476,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void enableSkill(Skill skill) public void enableSkill(Skill skill)
{ {
if ((skill == null) || (_disabledSkills == null)) if (skill == null)
{ {
return; return;
} }
@@ -1516,31 +1495,16 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
{ {
return; return;
} }
if (_disabledSkills == null)
{
synchronized (this)
{
if (_disabledSkills == null)
{
_disabledSkills = new ConcurrentHashMap<>();
}
}
}
_disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE); _disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE);
} }
/** /**
* Removes all the disabled skills. * Removes all the disabled skills.
*/ */
public synchronized void resetDisabledSkills() public void resetDisabledSkills()
{
if (_disabledSkills != null)
{ {
_disabledSkills.clear(); _disabledSkills.clear();
} }
}
/** /**
* Verifies if the skill is disabled. * Verifies if the skill is disabled.
@@ -1570,7 +1534,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
return true; return true;
} }
if (_disabledSkills == null) if (_disabledSkills.isEmpty())
{ {
return false; return false;
} }
@@ -7269,6 +7269,7 @@ public class PlayerInstance extends Playable
int buff_index = 0; int buff_index = 0;
final List<Long> storedSkills = new ArrayList<>(); final List<Long> storedSkills = new ArrayList<>();
final long currentTime = System.currentTimeMillis();
// Store all effect data along with calulated remaining // Store all effect data along with calulated remaining
// reuse delays for matching skills. 'restore_type'= 0. // reuse delays for matching skills. 'restore_type'= 0.
@@ -7328,8 +7329,8 @@ public class PlayerInstance extends Playable
statement.setInt(5, info.getTime()); statement.setInt(5, info.getTime());
final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode()); final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode());
statement.setLong(6, (t != null) && t.hasNotPassed() ? t.getReuse() : 0); statement.setLong(6, (t != null) && (currentTime < t.getStamp()) ? t.getReuse() : 0);
statement.setDouble(7, (t != null) && t.hasNotPassed() ? t.getStamp() : 0); statement.setDouble(7, (t != null) && (currentTime < t.getStamp()) ? t.getStamp() : 0);
statement.setInt(8, 0); // Store type 0, active buffs/debuffs. statement.setInt(8, 0); // Store type 0, active buffs/debuffs.
statement.setInt(9, _classIndex); statement.setInt(9, _classIndex);
@@ -7339,10 +7340,7 @@ public class PlayerInstance extends Playable
} }
// Skills under reuse. // Skills under reuse.
final Map<Long, TimeStamp> reuseTimeStamps = getSkillReuseTimeStamps(); for (Entry<Long, TimeStamp> ts : getSkillReuseTimeStamps().entrySet())
if (reuseTimeStamps != null)
{
for (Entry<Long, TimeStamp> ts : reuseTimeStamps.entrySet())
{ {
final long hash = ts.getKey(); final long hash = ts.getKey();
if (storedSkills.contains(hash)) if (storedSkills.contains(hash))
@@ -7351,7 +7349,7 @@ public class PlayerInstance extends Playable
} }
final TimeStamp t = ts.getValue(); final TimeStamp t = ts.getValue();
if ((t != null) && t.hasNotPassed()) if ((t != null) && (currentTime < t.getStamp()))
{ {
storedSkills.add(hash); storedSkills.add(hash);
@@ -7368,7 +7366,6 @@ public class PlayerInstance extends Playable
statement.addBatch(); statement.addBatch();
} }
} }
}
statement.executeBatch(); statement.executeBatch();
} }
@@ -7388,12 +7385,10 @@ public class PlayerInstance extends Playable
ps1.setInt(1, getObjectId()); ps1.setInt(1, getObjectId());
ps1.execute(); ps1.execute();
final Map<Integer, TimeStamp> itemReuseTimeStamps = getItemReuseTimeStamps(); final long currentTime = System.currentTimeMillis();
if (itemReuseTimeStamps != null) for (TimeStamp ts : getItemReuseTimeStamps().values())
{ {
for (TimeStamp ts : itemReuseTimeStamps.values()) if ((ts != null) && (currentTime < ts.getStamp()))
{
if ((ts != null) && ts.hasNotPassed())
{ {
ps2.setInt(1, getObjectId()); ps2.setInt(1, getObjectId());
ps2.setInt(2, ts.getItemId()); ps2.setInt(2, ts.getItemId());
@@ -7405,7 +7400,6 @@ public class PlayerInstance extends Playable
} }
ps2.executeBatch(); ps2.executeBatch();
} }
}
catch (Exception e) catch (Exception e)
{ {
LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e); LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e);
@@ -18,38 +18,33 @@ package org.l2jmobius.gameserver.network.serverpackets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import org.l2jmobius.commons.network.PacketWriter; import org.l2jmobius.commons.network.PacketWriter;
import org.l2jmobius.gameserver.data.xml.impl.SkillData; import org.l2jmobius.gameserver.data.xml.impl.SkillData;
import org.l2jmobius.gameserver.model.TimeStamp; import org.l2jmobius.gameserver.model.TimeStamp;
import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
import org.l2jmobius.gameserver.model.skills.Skill;
import org.l2jmobius.gameserver.network.OutgoingPackets; import org.l2jmobius.gameserver.network.OutgoingPackets;
/** /**
* Skill Cool Time server packet implementation. * Skill Cool Time server packet implementation.
* @author KenM, Zoey76 * @author KenM, Zoey76, Mobius
*/ */
public class SkillCoolTime implements IClientOutgoingPacket public class SkillCoolTime implements IClientOutgoingPacket
{ {
private final long _currentTime;
private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>(); private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>();
public SkillCoolTime(PlayerInstance player) public SkillCoolTime(PlayerInstance player)
{ {
final Map<Long, TimeStamp> skillReuseTimeStamps = player.getSkillReuseTimeStamps(); _currentTime = System.currentTimeMillis();
if (skillReuseTimeStamps != null) for (TimeStamp ts : player.getSkillReuseTimeStamps().values())
{ {
for (TimeStamp ts : skillReuseTimeStamps.values()) if ((_currentTime < ts.getStamp()) && !SkillData.getInstance().getSkill(ts.getSkillId(), ts.getSkillLvl(), ts.getSkillSubLvl()).isNotBroadcastable())
{
final Skill skill = SkillData.getInstance().getSkill(ts.getSkillId(), ts.getSkillLvl(), ts.getSkillSubLvl());
if (ts.hasNotPassed() && !skill.isNotBroadcastable())
{ {
_skillReuseTimeStamps.add(ts); _skillReuseTimeStamps.add(ts);
} }
} }
} }
}
@Override @Override
public boolean write(PacketWriter packet) public boolean write(PacketWriter packet)
@@ -62,7 +57,7 @@ public class SkillCoolTime implements IClientOutgoingPacket
packet.writeD(ts.getSkillId()); packet.writeD(ts.getSkillId());
packet.writeD(0x00); // ? packet.writeD(0x00); // ?
packet.writeD((int) ts.getReuse() / 1000); packet.writeD((int) ts.getReuse() / 1000);
packet.writeD((int) ts.getRemaining() / 1000); packet.writeD((int) Math.max(ts.getStamp() - _currentTime, 0) / 1000);
} }
return true; return true;
} }
@@ -23,7 +23,7 @@ import org.l2jmobius.gameserver.model.skills.Skill;
* Simple class containing all necessary information to maintain<br> * Simple class containing all necessary information to maintain<br>
* valid time stamps and reuse for skills and items reuse upon re-login.<br> * valid time stamps and reuse for skills and items reuse upon re-login.<br>
* <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b> * <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b>
* @author Yesod, Zoey76 * @author Yesod, Zoey76, Mobius
*/ */
public class TimeStamp public class TimeStamp
{ {
@@ -36,7 +36,7 @@ public class TimeStamp
/** Item or skill reuse time. */ /** Item or skill reuse time. */
private final long _reuse; private final long _reuse;
/** Time stamp. */ /** Time stamp. */
private final long _stamp; private volatile long _stamp;
/** Shared reuse group. */ /** Shared reuse group. */
private final int _group; private final int _group;
@@ -52,7 +52,7 @@ public class TimeStamp
_id2 = skill.getLevel(); _id2 = skill.getLevel();
_id3 = skill.getSubLevel(); _id3 = skill.getSubLevel();
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = -1; _group = -1;
} }
@@ -68,7 +68,7 @@ public class TimeStamp
_id2 = item.getObjectId(); _id2 = item.getObjectId();
_id3 = 0; _id3 = 0;
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = item.getSharedReuseGroup(); _group = item.getSharedReuseGroup();
} }
@@ -151,7 +151,16 @@ public class TimeStamp
*/ */
public long getRemaining() public long getRemaining()
{ {
return Math.max(_stamp - System.currentTimeMillis(), 0); if (_stamp == 0)
{
return 0;
}
final long remainingTime = Math.max(_stamp - System.currentTimeMillis(), 0);
if (remainingTime == 0)
{
_stamp = 0;
}
return remainingTime;
} }
/** /**
@@ -160,6 +169,15 @@ public class TimeStamp
*/ */
public boolean hasNotPassed() public boolean hasNotPassed()
{ {
return System.currentTimeMillis() < _stamp; if (_stamp == 0)
{
return false;
}
final boolean hasNotPassed = System.currentTimeMillis() < _stamp;
if (!hasNotPassed)
{
_stamp = 0;
}
return hasNotPassed;
} }
} }
@@ -212,11 +212,11 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
/** Map containing all skills of this character. */ /** Map containing all skills of this character. */
private final Map<Integer, Skill> _skills = new ConcurrentSkipListMap<>(); private final Map<Integer, Skill> _skills = new ConcurrentSkipListMap<>();
/** Map containing the skill reuse time stamps. */ /** Map containing the skill reuse time stamps. */
private volatile Map<Long, TimeStamp> _reuseTimeStampsSkills = null; private final Map<Long, TimeStamp> _reuseTimeStampsSkills = new ConcurrentHashMap<>();
/** Map containing the item reuse time stamps. */ /** Map containing the item reuse time stamps. */
private volatile Map<Integer, TimeStamp> _reuseTimeStampsItems = null; private final Map<Integer, TimeStamp> _reuseTimeStampsItems = new ConcurrentHashMap<>();
/** Map containing all the disabled skills. */ /** Map containing all the disabled skills. */
private volatile Map<Long, Long> _disabledSkills = null; private final Map<Long, Long> _disabledSkills = new ConcurrentHashMap<>();
private boolean _allSkillsDisabled; private boolean _allSkillsDisabled;
private final byte[] _zones = new byte[ZoneId.getZoneCount()]; private final byte[] _zones = new byte[ZoneId.getZoneCount()];
@@ -1342,16 +1342,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStampItem(ItemInstance item, long reuse, long systime) public void addTimeStampItem(ItemInstance item, long reuse, long systime)
{ {
if (_reuseTimeStampsItems == null)
{
synchronized (this)
{
if (_reuseTimeStampsItems == null)
{
_reuseTimeStampsItems = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime)); _reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime));
} }
@@ -1360,9 +1350,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param itemObjId the item object ID * @param itemObjId the item object ID
* @return if the item has a reuse time stamp, the remaining time, otherwise -1 * @return if the item has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getItemRemainingReuseTime(int itemObjId) public long getItemRemainingReuseTime(int itemObjId)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsItems != null) ? _reuseTimeStampsItems.get(itemObjId) : null; final TimeStamp reuseStamp = _reuseTimeStampsItems.get(itemObjId);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -1373,13 +1363,18 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public long getReuseDelayOnGroup(int group) public long getReuseDelayOnGroup(int group)
{ {
if ((group > 0) && (_reuseTimeStampsItems != null)) if ((group > 0) && !_reuseTimeStampsItems.isEmpty())
{ {
final long currentTime = System.currentTimeMillis();
for (TimeStamp ts : _reuseTimeStampsItems.values()) for (TimeStamp ts : _reuseTimeStampsItems.values())
{ {
if ((ts.getSharedReuseGroup() == group) && ts.hasNotPassed()) if (ts.getSharedReuseGroup() == group)
{ {
return ts.getRemaining(); final long stamp = ts.getStamp();
if (currentTime < stamp)
{
return Math.max(stamp - currentTime, 0);
}
} }
} }
} }
@@ -1414,16 +1409,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStamp(Skill skill, long reuse, long systime) public void addTimeStamp(Skill skill, long reuse, long systime)
{ {
if (_reuseTimeStampsSkills == null)
{
synchronized (this)
{
if (_reuseTimeStampsSkills == null)
{
_reuseTimeStampsSkills = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime)); _reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime));
} }
@@ -1431,33 +1416,27 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* Removes a skill reuse time stamp. * Removes a skill reuse time stamp.
* @param skill the skill to remove * @param skill the skill to remove
*/ */
public synchronized void removeTimeStamp(Skill skill) public void removeTimeStamp(Skill skill)
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.remove(skill.getReuseHashCode()); _reuseTimeStampsSkills.remove(skill.getReuseHashCode());
} }
}
/** /**
* Removes all skill reuse time stamps. * Removes all skill reuse time stamps.
*/ */
public synchronized void resetTimeStamps() public void resetTimeStamps()
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.clear(); _reuseTimeStampsSkills.clear();
} }
}
/** /**
* Gets the skill remaining reuse time for a given skill hash code. * Gets the skill remaining reuse time for a given skill hash code.
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return if the skill has a reuse time stamp, the remaining time, otherwise -1 * @return if the skill has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getSkillRemainingReuseTime(long hashCode) public long getSkillRemainingReuseTime(long hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -1466,9 +1445,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return {@code true} if the skill is under reuse time, {@code false} otherwise * @return {@code true} if the skill is under reuse time, {@code false} otherwise
*/ */
public synchronized boolean hasSkillReuse(long hashCode) public boolean hasSkillReuse(long hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return (reuseStamp != null) && reuseStamp.hasNotPassed(); return (reuseStamp != null) && reuseStamp.hasNotPassed();
} }
@@ -1479,7 +1458,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public synchronized TimeStamp getSkillReuseTimeStamp(long hashCode) public synchronized TimeStamp getSkillReuseTimeStamp(long hashCode)
{ {
return _reuseTimeStampsSkills != null ? _reuseTimeStampsSkills.get(hashCode) : null; return _reuseTimeStampsSkills.get(hashCode);
} }
/** /**
@@ -1497,7 +1476,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void enableSkill(Skill skill) public void enableSkill(Skill skill)
{ {
if ((skill == null) || (_disabledSkills == null)) if (skill == null)
{ {
return; return;
} }
@@ -1516,31 +1495,16 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
{ {
return; return;
} }
if (_disabledSkills == null)
{
synchronized (this)
{
if (_disabledSkills == null)
{
_disabledSkills = new ConcurrentHashMap<>();
}
}
}
_disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE); _disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE);
} }
/** /**
* Removes all the disabled skills. * Removes all the disabled skills.
*/ */
public synchronized void resetDisabledSkills() public void resetDisabledSkills()
{
if (_disabledSkills != null)
{ {
_disabledSkills.clear(); _disabledSkills.clear();
} }
}
/** /**
* Verifies if the skill is disabled. * Verifies if the skill is disabled.
@@ -1570,7 +1534,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
return true; return true;
} }
if (_disabledSkills == null) if (_disabledSkills.isEmpty())
{ {
return false; return false;
} }
@@ -7264,6 +7264,7 @@ public class PlayerInstance extends Playable
int buff_index = 0; int buff_index = 0;
final List<Long> storedSkills = new ArrayList<>(); final List<Long> storedSkills = new ArrayList<>();
final long currentTime = System.currentTimeMillis();
// Store all effect data along with calulated remaining // Store all effect data along with calulated remaining
// reuse delays for matching skills. 'restore_type'= 0. // reuse delays for matching skills. 'restore_type'= 0.
@@ -7323,8 +7324,8 @@ public class PlayerInstance extends Playable
statement.setInt(5, info.getTime()); statement.setInt(5, info.getTime());
final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode()); final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode());
statement.setLong(6, (t != null) && t.hasNotPassed() ? t.getReuse() : 0); statement.setLong(6, (t != null) && (currentTime < t.getStamp()) ? t.getReuse() : 0);
statement.setDouble(7, (t != null) && t.hasNotPassed() ? t.getStamp() : 0); statement.setDouble(7, (t != null) && (currentTime < t.getStamp()) ? t.getStamp() : 0);
statement.setInt(8, 0); // Store type 0, active buffs/debuffs. statement.setInt(8, 0); // Store type 0, active buffs/debuffs.
statement.setInt(9, _classIndex); statement.setInt(9, _classIndex);
@@ -7334,10 +7335,7 @@ public class PlayerInstance extends Playable
} }
// Skills under reuse. // Skills under reuse.
final Map<Long, TimeStamp> reuseTimeStamps = getSkillReuseTimeStamps(); for (Entry<Long, TimeStamp> ts : getSkillReuseTimeStamps().entrySet())
if (reuseTimeStamps != null)
{
for (Entry<Long, TimeStamp> ts : reuseTimeStamps.entrySet())
{ {
final long hash = ts.getKey(); final long hash = ts.getKey();
if (storedSkills.contains(hash)) if (storedSkills.contains(hash))
@@ -7346,7 +7344,7 @@ public class PlayerInstance extends Playable
} }
final TimeStamp t = ts.getValue(); final TimeStamp t = ts.getValue();
if ((t != null) && t.hasNotPassed()) if ((t != null) && (currentTime < t.getStamp()))
{ {
storedSkills.add(hash); storedSkills.add(hash);
@@ -7363,7 +7361,6 @@ public class PlayerInstance extends Playable
statement.addBatch(); statement.addBatch();
} }
} }
}
statement.executeBatch(); statement.executeBatch();
} }
@@ -7383,12 +7380,10 @@ public class PlayerInstance extends Playable
ps1.setInt(1, getObjectId()); ps1.setInt(1, getObjectId());
ps1.execute(); ps1.execute();
final Map<Integer, TimeStamp> itemReuseTimeStamps = getItemReuseTimeStamps(); final long currentTime = System.currentTimeMillis();
if (itemReuseTimeStamps != null) for (TimeStamp ts : getItemReuseTimeStamps().values())
{ {
for (TimeStamp ts : itemReuseTimeStamps.values()) if ((ts != null) && (currentTime < ts.getStamp()))
{
if ((ts != null) && ts.hasNotPassed())
{ {
ps2.setInt(1, getObjectId()); ps2.setInt(1, getObjectId());
ps2.setInt(2, ts.getItemId()); ps2.setInt(2, ts.getItemId());
@@ -7400,7 +7395,6 @@ public class PlayerInstance extends Playable
} }
ps2.executeBatch(); ps2.executeBatch();
} }
}
catch (Exception e) catch (Exception e)
{ {
LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e); LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e);
@@ -18,38 +18,33 @@ package org.l2jmobius.gameserver.network.serverpackets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import org.l2jmobius.commons.network.PacketWriter; import org.l2jmobius.commons.network.PacketWriter;
import org.l2jmobius.gameserver.data.xml.impl.SkillData; import org.l2jmobius.gameserver.data.xml.impl.SkillData;
import org.l2jmobius.gameserver.model.TimeStamp; import org.l2jmobius.gameserver.model.TimeStamp;
import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
import org.l2jmobius.gameserver.model.skills.Skill;
import org.l2jmobius.gameserver.network.OutgoingPackets; import org.l2jmobius.gameserver.network.OutgoingPackets;
/** /**
* Skill Cool Time server packet implementation. * Skill Cool Time server packet implementation.
* @author KenM, Zoey76 * @author KenM, Zoey76, Mobius
*/ */
public class SkillCoolTime implements IClientOutgoingPacket public class SkillCoolTime implements IClientOutgoingPacket
{ {
private final long _currentTime;
private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>(); private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>();
public SkillCoolTime(PlayerInstance player) public SkillCoolTime(PlayerInstance player)
{ {
final Map<Long, TimeStamp> skillReuseTimeStamps = player.getSkillReuseTimeStamps(); _currentTime = System.currentTimeMillis();
if (skillReuseTimeStamps != null) for (TimeStamp ts : player.getSkillReuseTimeStamps().values())
{ {
for (TimeStamp ts : skillReuseTimeStamps.values()) if ((_currentTime < ts.getStamp()) && !SkillData.getInstance().getSkill(ts.getSkillId(), ts.getSkillLvl(), ts.getSkillSubLvl()).isNotBroadcastable())
{
final Skill skill = SkillData.getInstance().getSkill(ts.getSkillId(), ts.getSkillLvl(), ts.getSkillSubLvl());
if (ts.hasNotPassed() && !skill.isNotBroadcastable())
{ {
_skillReuseTimeStamps.add(ts); _skillReuseTimeStamps.add(ts);
} }
} }
} }
}
@Override @Override
public boolean write(PacketWriter packet) public boolean write(PacketWriter packet)
@@ -62,7 +57,7 @@ public class SkillCoolTime implements IClientOutgoingPacket
packet.writeD(ts.getSkillId()); packet.writeD(ts.getSkillId());
packet.writeD(0x00); // ? packet.writeD(0x00); // ?
packet.writeD((int) ts.getReuse() / 1000); packet.writeD((int) ts.getReuse() / 1000);
packet.writeD((int) ts.getRemaining() / 1000); packet.writeD((int) Math.max(ts.getStamp() - _currentTime, 0) / 1000);
} }
return true; return true;
} }
@@ -23,7 +23,7 @@ import org.l2jmobius.gameserver.model.skills.Skill;
* Simple class containing all necessary information to maintain<br> * Simple class containing all necessary information to maintain<br>
* valid time stamps and reuse for skills and items reuse upon re-login.<br> * valid time stamps and reuse for skills and items reuse upon re-login.<br>
* <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b> * <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b>
* @author Yesod, Zoey76 * @author Yesod, Zoey76, Mobius
*/ */
public class TimeStamp public class TimeStamp
{ {
@@ -36,7 +36,7 @@ public class TimeStamp
/** Item or skill reuse time. */ /** Item or skill reuse time. */
private final long _reuse; private final long _reuse;
/** Time stamp. */ /** Time stamp. */
private final long _stamp; private volatile long _stamp;
/** Shared reuse group. */ /** Shared reuse group. */
private final int _group; private final int _group;
@@ -52,7 +52,7 @@ public class TimeStamp
_id2 = skill.getLevel(); _id2 = skill.getLevel();
_id3 = skill.getSubLevel(); _id3 = skill.getSubLevel();
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = -1; _group = -1;
} }
@@ -68,7 +68,7 @@ public class TimeStamp
_id2 = item.getObjectId(); _id2 = item.getObjectId();
_id3 = 0; _id3 = 0;
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = item.getSharedReuseGroup(); _group = item.getSharedReuseGroup();
} }
@@ -151,7 +151,16 @@ public class TimeStamp
*/ */
public long getRemaining() public long getRemaining()
{ {
return Math.max(_stamp - System.currentTimeMillis(), 0); if (_stamp == 0)
{
return 0;
}
final long remainingTime = Math.max(_stamp - System.currentTimeMillis(), 0);
if (remainingTime == 0)
{
_stamp = 0;
}
return remainingTime;
} }
/** /**
@@ -160,6 +169,15 @@ public class TimeStamp
*/ */
public boolean hasNotPassed() public boolean hasNotPassed()
{ {
return System.currentTimeMillis() < _stamp; if (_stamp == 0)
{
return false;
}
final boolean hasNotPassed = System.currentTimeMillis() < _stamp;
if (!hasNotPassed)
{
_stamp = 0;
}
return hasNotPassed;
} }
} }
@@ -212,11 +212,11 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
/** Map containing all skills of this character. */ /** Map containing all skills of this character. */
private final Map<Integer, Skill> _skills = new ConcurrentSkipListMap<>(); private final Map<Integer, Skill> _skills = new ConcurrentSkipListMap<>();
/** Map containing the skill reuse time stamps. */ /** Map containing the skill reuse time stamps. */
private volatile Map<Long, TimeStamp> _reuseTimeStampsSkills = null; private final Map<Long, TimeStamp> _reuseTimeStampsSkills = new ConcurrentHashMap<>();
/** Map containing the item reuse time stamps. */ /** Map containing the item reuse time stamps. */
private volatile Map<Integer, TimeStamp> _reuseTimeStampsItems = null; private final Map<Integer, TimeStamp> _reuseTimeStampsItems = new ConcurrentHashMap<>();
/** Map containing all the disabled skills. */ /** Map containing all the disabled skills. */
private volatile Map<Long, Long> _disabledSkills = null; private final Map<Long, Long> _disabledSkills = new ConcurrentHashMap<>();
private boolean _allSkillsDisabled; private boolean _allSkillsDisabled;
private final byte[] _zones = new byte[ZoneId.getZoneCount()]; private final byte[] _zones = new byte[ZoneId.getZoneCount()];
@@ -1342,16 +1342,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStampItem(ItemInstance item, long reuse, long systime) public void addTimeStampItem(ItemInstance item, long reuse, long systime)
{ {
if (_reuseTimeStampsItems == null)
{
synchronized (this)
{
if (_reuseTimeStampsItems == null)
{
_reuseTimeStampsItems = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime)); _reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime));
} }
@@ -1360,9 +1350,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param itemObjId the item object ID * @param itemObjId the item object ID
* @return if the item has a reuse time stamp, the remaining time, otherwise -1 * @return if the item has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getItemRemainingReuseTime(int itemObjId) public long getItemRemainingReuseTime(int itemObjId)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsItems != null) ? _reuseTimeStampsItems.get(itemObjId) : null; final TimeStamp reuseStamp = _reuseTimeStampsItems.get(itemObjId);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -1373,13 +1363,18 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public long getReuseDelayOnGroup(int group) public long getReuseDelayOnGroup(int group)
{ {
if ((group > 0) && (_reuseTimeStampsItems != null)) if ((group > 0) && !_reuseTimeStampsItems.isEmpty())
{ {
final long currentTime = System.currentTimeMillis();
for (TimeStamp ts : _reuseTimeStampsItems.values()) for (TimeStamp ts : _reuseTimeStampsItems.values())
{ {
if ((ts.getSharedReuseGroup() == group) && ts.hasNotPassed()) if (ts.getSharedReuseGroup() == group)
{ {
return ts.getRemaining(); final long stamp = ts.getStamp();
if (currentTime < stamp)
{
return Math.max(stamp - currentTime, 0);
}
} }
} }
} }
@@ -1414,16 +1409,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStamp(Skill skill, long reuse, long systime) public void addTimeStamp(Skill skill, long reuse, long systime)
{ {
if (_reuseTimeStampsSkills == null)
{
synchronized (this)
{
if (_reuseTimeStampsSkills == null)
{
_reuseTimeStampsSkills = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime)); _reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime));
} }
@@ -1431,33 +1416,27 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* Removes a skill reuse time stamp. * Removes a skill reuse time stamp.
* @param skill the skill to remove * @param skill the skill to remove
*/ */
public synchronized void removeTimeStamp(Skill skill) public void removeTimeStamp(Skill skill)
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.remove(skill.getReuseHashCode()); _reuseTimeStampsSkills.remove(skill.getReuseHashCode());
} }
}
/** /**
* Removes all skill reuse time stamps. * Removes all skill reuse time stamps.
*/ */
public synchronized void resetTimeStamps() public void resetTimeStamps()
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.clear(); _reuseTimeStampsSkills.clear();
} }
}
/** /**
* Gets the skill remaining reuse time for a given skill hash code. * Gets the skill remaining reuse time for a given skill hash code.
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return if the skill has a reuse time stamp, the remaining time, otherwise -1 * @return if the skill has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getSkillRemainingReuseTime(long hashCode) public long getSkillRemainingReuseTime(long hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -1466,9 +1445,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return {@code true} if the skill is under reuse time, {@code false} otherwise * @return {@code true} if the skill is under reuse time, {@code false} otherwise
*/ */
public synchronized boolean hasSkillReuse(long hashCode) public boolean hasSkillReuse(long hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return (reuseStamp != null) && reuseStamp.hasNotPassed(); return (reuseStamp != null) && reuseStamp.hasNotPassed();
} }
@@ -1479,7 +1458,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public synchronized TimeStamp getSkillReuseTimeStamp(long hashCode) public synchronized TimeStamp getSkillReuseTimeStamp(long hashCode)
{ {
return _reuseTimeStampsSkills != null ? _reuseTimeStampsSkills.get(hashCode) : null; return _reuseTimeStampsSkills.get(hashCode);
} }
/** /**
@@ -1497,7 +1476,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void enableSkill(Skill skill) public void enableSkill(Skill skill)
{ {
if ((skill == null) || (_disabledSkills == null)) if (skill == null)
{ {
return; return;
} }
@@ -1516,31 +1495,16 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
{ {
return; return;
} }
if (_disabledSkills == null)
{
synchronized (this)
{
if (_disabledSkills == null)
{
_disabledSkills = new ConcurrentHashMap<>();
}
}
}
_disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE); _disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE);
} }
/** /**
* Removes all the disabled skills. * Removes all the disabled skills.
*/ */
public synchronized void resetDisabledSkills() public void resetDisabledSkills()
{
if (_disabledSkills != null)
{ {
_disabledSkills.clear(); _disabledSkills.clear();
} }
}
/** /**
* Verifies if the skill is disabled. * Verifies if the skill is disabled.
@@ -1570,7 +1534,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
return true; return true;
} }
if (_disabledSkills == null) if (_disabledSkills.isEmpty())
{ {
return false; return false;
} }
@@ -7242,6 +7242,7 @@ public class PlayerInstance extends Playable
int buff_index = 0; int buff_index = 0;
final List<Long> storedSkills = new ArrayList<>(); final List<Long> storedSkills = new ArrayList<>();
final long currentTime = System.currentTimeMillis();
// Store all effect data along with calulated remaining // Store all effect data along with calulated remaining
// reuse delays for matching skills. 'restore_type'= 0. // reuse delays for matching skills. 'restore_type'= 0.
@@ -7301,8 +7302,8 @@ public class PlayerInstance extends Playable
statement.setInt(5, info.getTime()); statement.setInt(5, info.getTime());
final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode()); final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode());
statement.setLong(6, (t != null) && t.hasNotPassed() ? t.getReuse() : 0); statement.setLong(6, (t != null) && (currentTime < t.getStamp()) ? t.getReuse() : 0);
statement.setDouble(7, (t != null) && t.hasNotPassed() ? t.getStamp() : 0); statement.setDouble(7, (t != null) && (currentTime < t.getStamp()) ? t.getStamp() : 0);
statement.setInt(8, 0); // Store type 0, active buffs/debuffs. statement.setInt(8, 0); // Store type 0, active buffs/debuffs.
statement.setInt(9, _classIndex); statement.setInt(9, _classIndex);
@@ -7312,10 +7313,7 @@ public class PlayerInstance extends Playable
} }
// Skills under reuse. // Skills under reuse.
final Map<Long, TimeStamp> reuseTimeStamps = getSkillReuseTimeStamps(); for (Entry<Long, TimeStamp> ts : getSkillReuseTimeStamps().entrySet())
if (reuseTimeStamps != null)
{
for (Entry<Long, TimeStamp> ts : reuseTimeStamps.entrySet())
{ {
final long hash = ts.getKey(); final long hash = ts.getKey();
if (storedSkills.contains(hash)) if (storedSkills.contains(hash))
@@ -7324,7 +7322,7 @@ public class PlayerInstance extends Playable
} }
final TimeStamp t = ts.getValue(); final TimeStamp t = ts.getValue();
if ((t != null) && t.hasNotPassed()) if ((t != null) && (currentTime < t.getStamp()))
{ {
storedSkills.add(hash); storedSkills.add(hash);
@@ -7341,7 +7339,6 @@ public class PlayerInstance extends Playable
statement.addBatch(); statement.addBatch();
} }
} }
}
statement.executeBatch(); statement.executeBatch();
} }
@@ -7361,12 +7358,10 @@ public class PlayerInstance extends Playable
ps1.setInt(1, getObjectId()); ps1.setInt(1, getObjectId());
ps1.execute(); ps1.execute();
final Map<Integer, TimeStamp> itemReuseTimeStamps = getItemReuseTimeStamps(); final long currentTime = System.currentTimeMillis();
if (itemReuseTimeStamps != null) for (TimeStamp ts : getItemReuseTimeStamps().values())
{ {
for (TimeStamp ts : itemReuseTimeStamps.values()) if ((ts != null) && (currentTime < ts.getStamp()))
{
if ((ts != null) && ts.hasNotPassed())
{ {
ps2.setInt(1, getObjectId()); ps2.setInt(1, getObjectId());
ps2.setInt(2, ts.getItemId()); ps2.setInt(2, ts.getItemId());
@@ -7378,7 +7373,6 @@ public class PlayerInstance extends Playable
} }
ps2.executeBatch(); ps2.executeBatch();
} }
}
catch (Exception e) catch (Exception e)
{ {
LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e); LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e);
@@ -18,38 +18,33 @@ package org.l2jmobius.gameserver.network.serverpackets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import org.l2jmobius.commons.network.PacketWriter; import org.l2jmobius.commons.network.PacketWriter;
import org.l2jmobius.gameserver.data.xml.impl.SkillData; import org.l2jmobius.gameserver.data.xml.impl.SkillData;
import org.l2jmobius.gameserver.model.TimeStamp; import org.l2jmobius.gameserver.model.TimeStamp;
import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
import org.l2jmobius.gameserver.model.skills.Skill;
import org.l2jmobius.gameserver.network.OutgoingPackets; import org.l2jmobius.gameserver.network.OutgoingPackets;
/** /**
* Skill Cool Time server packet implementation. * Skill Cool Time server packet implementation.
* @author KenM, Zoey76 * @author KenM, Zoey76, Mobius
*/ */
public class SkillCoolTime implements IClientOutgoingPacket public class SkillCoolTime implements IClientOutgoingPacket
{ {
private final long _currentTime;
private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>(); private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>();
public SkillCoolTime(PlayerInstance player) public SkillCoolTime(PlayerInstance player)
{ {
final Map<Long, TimeStamp> skillReuseTimeStamps = player.getSkillReuseTimeStamps(); _currentTime = System.currentTimeMillis();
if (skillReuseTimeStamps != null) for (TimeStamp ts : player.getSkillReuseTimeStamps().values())
{ {
for (TimeStamp ts : skillReuseTimeStamps.values()) if ((_currentTime < ts.getStamp()) && !SkillData.getInstance().getSkill(ts.getSkillId(), ts.getSkillLvl(), ts.getSkillSubLvl()).isNotBroadcastable())
{
final Skill skill = SkillData.getInstance().getSkill(ts.getSkillId(), ts.getSkillLvl(), ts.getSkillSubLvl());
if (ts.hasNotPassed() && !skill.isNotBroadcastable())
{ {
_skillReuseTimeStamps.add(ts); _skillReuseTimeStamps.add(ts);
} }
} }
} }
}
@Override @Override
public boolean write(PacketWriter packet) public boolean write(PacketWriter packet)
@@ -62,7 +57,7 @@ public class SkillCoolTime implements IClientOutgoingPacket
packet.writeD(ts.getSkillId()); packet.writeD(ts.getSkillId());
packet.writeD(0x00); // ? packet.writeD(0x00); // ?
packet.writeD((int) ts.getReuse() / 1000); packet.writeD((int) ts.getReuse() / 1000);
packet.writeD((int) ts.getRemaining() / 1000); packet.writeD((int) Math.max(ts.getStamp() - _currentTime, 0) / 1000);
} }
return true; return true;
} }
@@ -23,7 +23,7 @@ import org.l2jmobius.gameserver.model.skills.Skill;
* Simple class containing all necessary information to maintain<br> * Simple class containing all necessary information to maintain<br>
* valid time stamps and reuse for skills and items reuse upon re-login.<br> * valid time stamps and reuse for skills and items reuse upon re-login.<br>
* <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b> * <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b>
* @author Yesod, Zoey76 * @author Yesod, Zoey76, Mobius
*/ */
public class TimeStamp public class TimeStamp
{ {
@@ -36,7 +36,7 @@ public class TimeStamp
/** Item or skill reuse time. */ /** Item or skill reuse time. */
private final long _reuse; private final long _reuse;
/** Time stamp. */ /** Time stamp. */
private final long _stamp; private volatile long _stamp;
/** Shared reuse group. */ /** Shared reuse group. */
private final int _group; private final int _group;
@@ -52,7 +52,7 @@ public class TimeStamp
_id2 = skill.getLevel(); _id2 = skill.getLevel();
_id3 = skill.getSubLevel(); _id3 = skill.getSubLevel();
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = -1; _group = -1;
} }
@@ -68,7 +68,7 @@ public class TimeStamp
_id2 = item.getObjectId(); _id2 = item.getObjectId();
_id3 = 0; _id3 = 0;
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = item.getSharedReuseGroup(); _group = item.getSharedReuseGroup();
} }
@@ -151,7 +151,16 @@ public class TimeStamp
*/ */
public long getRemaining() public long getRemaining()
{ {
return Math.max(_stamp - System.currentTimeMillis(), 0); if (_stamp == 0)
{
return 0;
}
final long remainingTime = Math.max(_stamp - System.currentTimeMillis(), 0);
if (remainingTime == 0)
{
_stamp = 0;
}
return remainingTime;
} }
/** /**
@@ -160,6 +169,15 @@ public class TimeStamp
*/ */
public boolean hasNotPassed() public boolean hasNotPassed()
{ {
return System.currentTimeMillis() < _stamp; if (_stamp == 0)
{
return false;
}
final boolean hasNotPassed = System.currentTimeMillis() < _stamp;
if (!hasNotPassed)
{
_stamp = 0;
}
return hasNotPassed;
} }
} }
@@ -212,11 +212,11 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
/** Map containing all skills of this character. */ /** Map containing all skills of this character. */
private final Map<Integer, Skill> _skills = new ConcurrentSkipListMap<>(); private final Map<Integer, Skill> _skills = new ConcurrentSkipListMap<>();
/** Map containing the skill reuse time stamps. */ /** Map containing the skill reuse time stamps. */
private volatile Map<Long, TimeStamp> _reuseTimeStampsSkills = null; private final Map<Long, TimeStamp> _reuseTimeStampsSkills = new ConcurrentHashMap<>();
/** Map containing the item reuse time stamps. */ /** Map containing the item reuse time stamps. */
private volatile Map<Integer, TimeStamp> _reuseTimeStampsItems = null; private final Map<Integer, TimeStamp> _reuseTimeStampsItems = new ConcurrentHashMap<>();
/** Map containing all the disabled skills. */ /** Map containing all the disabled skills. */
private volatile Map<Long, Long> _disabledSkills = null; private final Map<Long, Long> _disabledSkills = new ConcurrentHashMap<>();
private boolean _allSkillsDisabled; private boolean _allSkillsDisabled;
private final byte[] _zones = new byte[ZoneId.getZoneCount()]; private final byte[] _zones = new byte[ZoneId.getZoneCount()];
@@ -1342,16 +1342,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStampItem(ItemInstance item, long reuse, long systime) public void addTimeStampItem(ItemInstance item, long reuse, long systime)
{ {
if (_reuseTimeStampsItems == null)
{
synchronized (this)
{
if (_reuseTimeStampsItems == null)
{
_reuseTimeStampsItems = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime)); _reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime));
} }
@@ -1360,9 +1350,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param itemObjId the item object ID * @param itemObjId the item object ID
* @return if the item has a reuse time stamp, the remaining time, otherwise -1 * @return if the item has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getItemRemainingReuseTime(int itemObjId) public long getItemRemainingReuseTime(int itemObjId)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsItems != null) ? _reuseTimeStampsItems.get(itemObjId) : null; final TimeStamp reuseStamp = _reuseTimeStampsItems.get(itemObjId);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -1373,13 +1363,18 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public long getReuseDelayOnGroup(int group) public long getReuseDelayOnGroup(int group)
{ {
if ((group > 0) && (_reuseTimeStampsItems != null)) if ((group > 0) && !_reuseTimeStampsItems.isEmpty())
{ {
final long currentTime = System.currentTimeMillis();
for (TimeStamp ts : _reuseTimeStampsItems.values()) for (TimeStamp ts : _reuseTimeStampsItems.values())
{ {
if ((ts.getSharedReuseGroup() == group) && ts.hasNotPassed()) if (ts.getSharedReuseGroup() == group)
{ {
return ts.getRemaining(); final long stamp = ts.getStamp();
if (currentTime < stamp)
{
return Math.max(stamp - currentTime, 0);
}
} }
} }
} }
@@ -1414,16 +1409,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStamp(Skill skill, long reuse, long systime) public void addTimeStamp(Skill skill, long reuse, long systime)
{ {
if (_reuseTimeStampsSkills == null)
{
synchronized (this)
{
if (_reuseTimeStampsSkills == null)
{
_reuseTimeStampsSkills = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime)); _reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime));
} }
@@ -1431,33 +1416,27 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* Removes a skill reuse time stamp. * Removes a skill reuse time stamp.
* @param skill the skill to remove * @param skill the skill to remove
*/ */
public synchronized void removeTimeStamp(Skill skill) public void removeTimeStamp(Skill skill)
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.remove(skill.getReuseHashCode()); _reuseTimeStampsSkills.remove(skill.getReuseHashCode());
} }
}
/** /**
* Removes all skill reuse time stamps. * Removes all skill reuse time stamps.
*/ */
public synchronized void resetTimeStamps() public void resetTimeStamps()
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.clear(); _reuseTimeStampsSkills.clear();
} }
}
/** /**
* Gets the skill remaining reuse time for a given skill hash code. * Gets the skill remaining reuse time for a given skill hash code.
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return if the skill has a reuse time stamp, the remaining time, otherwise -1 * @return if the skill has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getSkillRemainingReuseTime(long hashCode) public long getSkillRemainingReuseTime(long hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -1466,9 +1445,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return {@code true} if the skill is under reuse time, {@code false} otherwise * @return {@code true} if the skill is under reuse time, {@code false} otherwise
*/ */
public synchronized boolean hasSkillReuse(long hashCode) public boolean hasSkillReuse(long hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return (reuseStamp != null) && reuseStamp.hasNotPassed(); return (reuseStamp != null) && reuseStamp.hasNotPassed();
} }
@@ -1479,7 +1458,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public synchronized TimeStamp getSkillReuseTimeStamp(long hashCode) public synchronized TimeStamp getSkillReuseTimeStamp(long hashCode)
{ {
return _reuseTimeStampsSkills != null ? _reuseTimeStampsSkills.get(hashCode) : null; return _reuseTimeStampsSkills.get(hashCode);
} }
/** /**
@@ -1497,7 +1476,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void enableSkill(Skill skill) public void enableSkill(Skill skill)
{ {
if ((skill == null) || (_disabledSkills == null)) if (skill == null)
{ {
return; return;
} }
@@ -1516,31 +1495,16 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
{ {
return; return;
} }
if (_disabledSkills == null)
{
synchronized (this)
{
if (_disabledSkills == null)
{
_disabledSkills = new ConcurrentHashMap<>();
}
}
}
_disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE); _disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE);
} }
/** /**
* Removes all the disabled skills. * Removes all the disabled skills.
*/ */
public synchronized void resetDisabledSkills() public void resetDisabledSkills()
{
if (_disabledSkills != null)
{ {
_disabledSkills.clear(); _disabledSkills.clear();
} }
}
/** /**
* Verifies if the skill is disabled. * Verifies if the skill is disabled.
@@ -1570,7 +1534,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
return true; return true;
} }
if (_disabledSkills == null) if (_disabledSkills.isEmpty())
{ {
return false; return false;
} }
@@ -7242,6 +7242,7 @@ public class PlayerInstance extends Playable
int buff_index = 0; int buff_index = 0;
final List<Long> storedSkills = new ArrayList<>(); final List<Long> storedSkills = new ArrayList<>();
final long currentTime = System.currentTimeMillis();
// Store all effect data along with calulated remaining // Store all effect data along with calulated remaining
// reuse delays for matching skills. 'restore_type'= 0. // reuse delays for matching skills. 'restore_type'= 0.
@@ -7301,8 +7302,8 @@ public class PlayerInstance extends Playable
statement.setInt(5, info.getTime()); statement.setInt(5, info.getTime());
final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode()); final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode());
statement.setLong(6, (t != null) && t.hasNotPassed() ? t.getReuse() : 0); statement.setLong(6, (t != null) && (currentTime < t.getStamp()) ? t.getReuse() : 0);
statement.setDouble(7, (t != null) && t.hasNotPassed() ? t.getStamp() : 0); statement.setDouble(7, (t != null) && (currentTime < t.getStamp()) ? t.getStamp() : 0);
statement.setInt(8, 0); // Store type 0, active buffs/debuffs. statement.setInt(8, 0); // Store type 0, active buffs/debuffs.
statement.setInt(9, _classIndex); statement.setInt(9, _classIndex);
@@ -7312,10 +7313,7 @@ public class PlayerInstance extends Playable
} }
// Skills under reuse. // Skills under reuse.
final Map<Long, TimeStamp> reuseTimeStamps = getSkillReuseTimeStamps(); for (Entry<Long, TimeStamp> ts : getSkillReuseTimeStamps().entrySet())
if (reuseTimeStamps != null)
{
for (Entry<Long, TimeStamp> ts : reuseTimeStamps.entrySet())
{ {
final long hash = ts.getKey(); final long hash = ts.getKey();
if (storedSkills.contains(hash)) if (storedSkills.contains(hash))
@@ -7324,7 +7322,7 @@ public class PlayerInstance extends Playable
} }
final TimeStamp t = ts.getValue(); final TimeStamp t = ts.getValue();
if ((t != null) && t.hasNotPassed()) if ((t != null) && (currentTime < t.getStamp()))
{ {
storedSkills.add(hash); storedSkills.add(hash);
@@ -7341,7 +7339,6 @@ public class PlayerInstance extends Playable
statement.addBatch(); statement.addBatch();
} }
} }
}
statement.executeBatch(); statement.executeBatch();
} }
@@ -7361,12 +7358,10 @@ public class PlayerInstance extends Playable
ps1.setInt(1, getObjectId()); ps1.setInt(1, getObjectId());
ps1.execute(); ps1.execute();
final Map<Integer, TimeStamp> itemReuseTimeStamps = getItemReuseTimeStamps(); final long currentTime = System.currentTimeMillis();
if (itemReuseTimeStamps != null) for (TimeStamp ts : getItemReuseTimeStamps().values())
{ {
for (TimeStamp ts : itemReuseTimeStamps.values()) if ((ts != null) && (currentTime < ts.getStamp()))
{
if ((ts != null) && ts.hasNotPassed())
{ {
ps2.setInt(1, getObjectId()); ps2.setInt(1, getObjectId());
ps2.setInt(2, ts.getItemId()); ps2.setInt(2, ts.getItemId());
@@ -7378,7 +7373,6 @@ public class PlayerInstance extends Playable
} }
ps2.executeBatch(); ps2.executeBatch();
} }
}
catch (Exception e) catch (Exception e)
{ {
LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e); LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e);
@@ -18,38 +18,33 @@ package org.l2jmobius.gameserver.network.serverpackets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import org.l2jmobius.commons.network.PacketWriter; import org.l2jmobius.commons.network.PacketWriter;
import org.l2jmobius.gameserver.data.xml.impl.SkillData; import org.l2jmobius.gameserver.data.xml.impl.SkillData;
import org.l2jmobius.gameserver.model.TimeStamp; import org.l2jmobius.gameserver.model.TimeStamp;
import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
import org.l2jmobius.gameserver.model.skills.Skill;
import org.l2jmobius.gameserver.network.OutgoingPackets; import org.l2jmobius.gameserver.network.OutgoingPackets;
/** /**
* Skill Cool Time server packet implementation. * Skill Cool Time server packet implementation.
* @author KenM, Zoey76 * @author KenM, Zoey76, Mobius
*/ */
public class SkillCoolTime implements IClientOutgoingPacket public class SkillCoolTime implements IClientOutgoingPacket
{ {
private final long _currentTime;
private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>(); private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>();
public SkillCoolTime(PlayerInstance player) public SkillCoolTime(PlayerInstance player)
{ {
final Map<Long, TimeStamp> skillReuseTimeStamps = player.getSkillReuseTimeStamps(); _currentTime = System.currentTimeMillis();
if (skillReuseTimeStamps != null) for (TimeStamp ts : player.getSkillReuseTimeStamps().values())
{ {
for (TimeStamp ts : skillReuseTimeStamps.values()) if ((_currentTime < ts.getStamp()) && !SkillData.getInstance().getSkill(ts.getSkillId(), ts.getSkillLvl(), ts.getSkillSubLvl()).isNotBroadcastable())
{
final Skill skill = SkillData.getInstance().getSkill(ts.getSkillId(), ts.getSkillLvl(), ts.getSkillSubLvl());
if (ts.hasNotPassed() && !skill.isNotBroadcastable())
{ {
_skillReuseTimeStamps.add(ts); _skillReuseTimeStamps.add(ts);
} }
} }
} }
}
@Override @Override
public boolean write(PacketWriter packet) public boolean write(PacketWriter packet)
@@ -62,7 +57,7 @@ public class SkillCoolTime implements IClientOutgoingPacket
packet.writeD(ts.getSkillId()); packet.writeD(ts.getSkillId());
packet.writeD(0x00); // ? packet.writeD(0x00); // ?
packet.writeD((int) ts.getReuse() / 1000); packet.writeD((int) ts.getReuse() / 1000);
packet.writeD((int) ts.getRemaining() / 1000); packet.writeD((int) Math.max(ts.getStamp() - _currentTime, 0) / 1000);
} }
return true; return true;
} }
@@ -23,7 +23,7 @@ import org.l2jmobius.gameserver.model.skills.Skill;
* Simple class containing all necessary information to maintain<br> * Simple class containing all necessary information to maintain<br>
* valid time stamps and reuse for skills and items reuse upon re-login.<br> * valid time stamps and reuse for skills and items reuse upon re-login.<br>
* <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b> * <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b>
* @author Yesod, Zoey76 * @author Yesod, Zoey76, Mobius
*/ */
public class TimeStamp public class TimeStamp
{ {
@@ -36,7 +36,7 @@ public class TimeStamp
/** Item or skill reuse time. */ /** Item or skill reuse time. */
private final long _reuse; private final long _reuse;
/** Time stamp. */ /** Time stamp. */
private final long _stamp; private volatile long _stamp;
/** Shared reuse group. */ /** Shared reuse group. */
private final int _group; private final int _group;
@@ -52,7 +52,7 @@ public class TimeStamp
_id2 = skill.getLevel(); _id2 = skill.getLevel();
_id3 = skill.getSubLevel(); _id3 = skill.getSubLevel();
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = -1; _group = -1;
} }
@@ -68,7 +68,7 @@ public class TimeStamp
_id2 = item.getObjectId(); _id2 = item.getObjectId();
_id3 = 0; _id3 = 0;
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = item.getSharedReuseGroup(); _group = item.getSharedReuseGroup();
} }
@@ -151,7 +151,16 @@ public class TimeStamp
*/ */
public long getRemaining() public long getRemaining()
{ {
return Math.max(_stamp - System.currentTimeMillis(), 0); if (_stamp == 0)
{
return 0;
}
final long remainingTime = Math.max(_stamp - System.currentTimeMillis(), 0);
if (remainingTime == 0)
{
_stamp = 0;
}
return remainingTime;
} }
/** /**
@@ -160,6 +169,15 @@ public class TimeStamp
*/ */
public boolean hasNotPassed() public boolean hasNotPassed()
{ {
return System.currentTimeMillis() < _stamp; if (_stamp == 0)
{
return false;
}
final boolean hasNotPassed = System.currentTimeMillis() < _stamp;
if (!hasNotPassed)
{
_stamp = 0;
}
return hasNotPassed;
} }
} }
@@ -212,11 +212,11 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
/** Map containing all skills of this character. */ /** Map containing all skills of this character. */
private final Map<Integer, Skill> _skills = new ConcurrentSkipListMap<>(); private final Map<Integer, Skill> _skills = new ConcurrentSkipListMap<>();
/** Map containing the skill reuse time stamps. */ /** Map containing the skill reuse time stamps. */
private volatile Map<Long, TimeStamp> _reuseTimeStampsSkills = null; private final Map<Long, TimeStamp> _reuseTimeStampsSkills = new ConcurrentHashMap<>();
/** Map containing the item reuse time stamps. */ /** Map containing the item reuse time stamps. */
private volatile Map<Integer, TimeStamp> _reuseTimeStampsItems = null; private final Map<Integer, TimeStamp> _reuseTimeStampsItems = new ConcurrentHashMap<>();
/** Map containing all the disabled skills. */ /** Map containing all the disabled skills. */
private volatile Map<Long, Long> _disabledSkills = null; private final Map<Long, Long> _disabledSkills = new ConcurrentHashMap<>();
private boolean _allSkillsDisabled; private boolean _allSkillsDisabled;
private final byte[] _zones = new byte[ZoneId.getZoneCount()]; private final byte[] _zones = new byte[ZoneId.getZoneCount()];
@@ -1342,16 +1342,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStampItem(ItemInstance item, long reuse, long systime) public void addTimeStampItem(ItemInstance item, long reuse, long systime)
{ {
if (_reuseTimeStampsItems == null)
{
synchronized (this)
{
if (_reuseTimeStampsItems == null)
{
_reuseTimeStampsItems = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime)); _reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime));
} }
@@ -1360,9 +1350,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param itemObjId the item object ID * @param itemObjId the item object ID
* @return if the item has a reuse time stamp, the remaining time, otherwise -1 * @return if the item has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getItemRemainingReuseTime(int itemObjId) public long getItemRemainingReuseTime(int itemObjId)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsItems != null) ? _reuseTimeStampsItems.get(itemObjId) : null; final TimeStamp reuseStamp = _reuseTimeStampsItems.get(itemObjId);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -1373,13 +1363,18 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public long getReuseDelayOnGroup(int group) public long getReuseDelayOnGroup(int group)
{ {
if ((group > 0) && (_reuseTimeStampsItems != null)) if ((group > 0) && !_reuseTimeStampsItems.isEmpty())
{ {
final long currentTime = System.currentTimeMillis();
for (TimeStamp ts : _reuseTimeStampsItems.values()) for (TimeStamp ts : _reuseTimeStampsItems.values())
{ {
if ((ts.getSharedReuseGroup() == group) && ts.hasNotPassed()) if (ts.getSharedReuseGroup() == group)
{ {
return ts.getRemaining(); final long stamp = ts.getStamp();
if (currentTime < stamp)
{
return Math.max(stamp - currentTime, 0);
}
} }
} }
} }
@@ -1414,16 +1409,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStamp(Skill skill, long reuse, long systime) public void addTimeStamp(Skill skill, long reuse, long systime)
{ {
if (_reuseTimeStampsSkills == null)
{
synchronized (this)
{
if (_reuseTimeStampsSkills == null)
{
_reuseTimeStampsSkills = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime)); _reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime));
} }
@@ -1431,33 +1416,27 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* Removes a skill reuse time stamp. * Removes a skill reuse time stamp.
* @param skill the skill to remove * @param skill the skill to remove
*/ */
public synchronized void removeTimeStamp(Skill skill) public void removeTimeStamp(Skill skill)
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.remove(skill.getReuseHashCode()); _reuseTimeStampsSkills.remove(skill.getReuseHashCode());
} }
}
/** /**
* Removes all skill reuse time stamps. * Removes all skill reuse time stamps.
*/ */
public synchronized void resetTimeStamps() public void resetTimeStamps()
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.clear(); _reuseTimeStampsSkills.clear();
} }
}
/** /**
* Gets the skill remaining reuse time for a given skill hash code. * Gets the skill remaining reuse time for a given skill hash code.
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return if the skill has a reuse time stamp, the remaining time, otherwise -1 * @return if the skill has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getSkillRemainingReuseTime(long hashCode) public long getSkillRemainingReuseTime(long hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -1466,9 +1445,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return {@code true} if the skill is under reuse time, {@code false} otherwise * @return {@code true} if the skill is under reuse time, {@code false} otherwise
*/ */
public synchronized boolean hasSkillReuse(long hashCode) public boolean hasSkillReuse(long hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return (reuseStamp != null) && reuseStamp.hasNotPassed(); return (reuseStamp != null) && reuseStamp.hasNotPassed();
} }
@@ -1479,7 +1458,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public synchronized TimeStamp getSkillReuseTimeStamp(long hashCode) public synchronized TimeStamp getSkillReuseTimeStamp(long hashCode)
{ {
return _reuseTimeStampsSkills != null ? _reuseTimeStampsSkills.get(hashCode) : null; return _reuseTimeStampsSkills.get(hashCode);
} }
/** /**
@@ -1497,7 +1476,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void enableSkill(Skill skill) public void enableSkill(Skill skill)
{ {
if ((skill == null) || (_disabledSkills == null)) if (skill == null)
{ {
return; return;
} }
@@ -1516,31 +1495,16 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
{ {
return; return;
} }
if (_disabledSkills == null)
{
synchronized (this)
{
if (_disabledSkills == null)
{
_disabledSkills = new ConcurrentHashMap<>();
}
}
}
_disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE); _disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE);
} }
/** /**
* Removes all the disabled skills. * Removes all the disabled skills.
*/ */
public synchronized void resetDisabledSkills() public void resetDisabledSkills()
{
if (_disabledSkills != null)
{ {
_disabledSkills.clear(); _disabledSkills.clear();
} }
}
/** /**
* Verifies if the skill is disabled. * Verifies if the skill is disabled.
@@ -1570,7 +1534,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
return true; return true;
} }
if (_disabledSkills == null) if (_disabledSkills.isEmpty())
{ {
return false; return false;
} }
@@ -7243,6 +7243,7 @@ public class PlayerInstance extends Playable
int buff_index = 0; int buff_index = 0;
final List<Long> storedSkills = new ArrayList<>(); final List<Long> storedSkills = new ArrayList<>();
final long currentTime = System.currentTimeMillis();
// Store all effect data along with calulated remaining // Store all effect data along with calulated remaining
// reuse delays for matching skills. 'restore_type'= 0. // reuse delays for matching skills. 'restore_type'= 0.
@@ -7302,8 +7303,8 @@ public class PlayerInstance extends Playable
statement.setInt(5, info.getTime()); statement.setInt(5, info.getTime());
final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode()); final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode());
statement.setLong(6, (t != null) && t.hasNotPassed() ? t.getReuse() : 0); statement.setLong(6, (t != null) && (currentTime < t.getStamp()) ? t.getReuse() : 0);
statement.setDouble(7, (t != null) && t.hasNotPassed() ? t.getStamp() : 0); statement.setDouble(7, (t != null) && (currentTime < t.getStamp()) ? t.getStamp() : 0);
statement.setInt(8, 0); // Store type 0, active buffs/debuffs. statement.setInt(8, 0); // Store type 0, active buffs/debuffs.
statement.setInt(9, _classIndex); statement.setInt(9, _classIndex);
@@ -7313,10 +7314,7 @@ public class PlayerInstance extends Playable
} }
// Skills under reuse. // Skills under reuse.
final Map<Long, TimeStamp> reuseTimeStamps = getSkillReuseTimeStamps(); for (Entry<Long, TimeStamp> ts : getSkillReuseTimeStamps().entrySet())
if (reuseTimeStamps != null)
{
for (Entry<Long, TimeStamp> ts : reuseTimeStamps.entrySet())
{ {
final long hash = ts.getKey(); final long hash = ts.getKey();
if (storedSkills.contains(hash)) if (storedSkills.contains(hash))
@@ -7325,7 +7323,7 @@ public class PlayerInstance extends Playable
} }
final TimeStamp t = ts.getValue(); final TimeStamp t = ts.getValue();
if ((t != null) && t.hasNotPassed()) if ((t != null) && (currentTime < t.getStamp()))
{ {
storedSkills.add(hash); storedSkills.add(hash);
@@ -7342,7 +7340,6 @@ public class PlayerInstance extends Playable
statement.addBatch(); statement.addBatch();
} }
} }
}
statement.executeBatch(); statement.executeBatch();
} }
@@ -7362,12 +7359,10 @@ public class PlayerInstance extends Playable
ps1.setInt(1, getObjectId()); ps1.setInt(1, getObjectId());
ps1.execute(); ps1.execute();
final Map<Integer, TimeStamp> itemReuseTimeStamps = getItemReuseTimeStamps(); final long currentTime = System.currentTimeMillis();
if (itemReuseTimeStamps != null) for (TimeStamp ts : getItemReuseTimeStamps().values())
{ {
for (TimeStamp ts : itemReuseTimeStamps.values()) if ((ts != null) && (currentTime < ts.getStamp()))
{
if ((ts != null) && ts.hasNotPassed())
{ {
ps2.setInt(1, getObjectId()); ps2.setInt(1, getObjectId());
ps2.setInt(2, ts.getItemId()); ps2.setInt(2, ts.getItemId());
@@ -7379,7 +7374,6 @@ public class PlayerInstance extends Playable
} }
ps2.executeBatch(); ps2.executeBatch();
} }
}
catch (Exception e) catch (Exception e)
{ {
LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e); LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e);
@@ -18,38 +18,33 @@ package org.l2jmobius.gameserver.network.serverpackets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import org.l2jmobius.commons.network.PacketWriter; import org.l2jmobius.commons.network.PacketWriter;
import org.l2jmobius.gameserver.data.xml.impl.SkillData; import org.l2jmobius.gameserver.data.xml.impl.SkillData;
import org.l2jmobius.gameserver.model.TimeStamp; import org.l2jmobius.gameserver.model.TimeStamp;
import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
import org.l2jmobius.gameserver.model.skills.Skill;
import org.l2jmobius.gameserver.network.OutgoingPackets; import org.l2jmobius.gameserver.network.OutgoingPackets;
/** /**
* Skill Cool Time server packet implementation. * Skill Cool Time server packet implementation.
* @author KenM, Zoey76 * @author KenM, Zoey76, Mobius
*/ */
public class SkillCoolTime implements IClientOutgoingPacket public class SkillCoolTime implements IClientOutgoingPacket
{ {
private final long _currentTime;
private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>(); private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>();
public SkillCoolTime(PlayerInstance player) public SkillCoolTime(PlayerInstance player)
{ {
final Map<Long, TimeStamp> skillReuseTimeStamps = player.getSkillReuseTimeStamps(); _currentTime = System.currentTimeMillis();
if (skillReuseTimeStamps != null) for (TimeStamp ts : player.getSkillReuseTimeStamps().values())
{ {
for (TimeStamp ts : skillReuseTimeStamps.values()) if ((_currentTime < ts.getStamp()) && !SkillData.getInstance().getSkill(ts.getSkillId(), ts.getSkillLvl(), ts.getSkillSubLvl()).isNotBroadcastable())
{
final Skill skill = SkillData.getInstance().getSkill(ts.getSkillId(), ts.getSkillLvl(), ts.getSkillSubLvl());
if (ts.hasNotPassed() && !skill.isNotBroadcastable())
{ {
_skillReuseTimeStamps.add(ts); _skillReuseTimeStamps.add(ts);
} }
} }
} }
}
@Override @Override
public boolean write(PacketWriter packet) public boolean write(PacketWriter packet)
@@ -62,7 +57,7 @@ public class SkillCoolTime implements IClientOutgoingPacket
packet.writeD(ts.getSkillId()); packet.writeD(ts.getSkillId());
packet.writeD(0x00); // ? packet.writeD(0x00); // ?
packet.writeD((int) ts.getReuse() / 1000); packet.writeD((int) ts.getReuse() / 1000);
packet.writeD((int) ts.getRemaining() / 1000); packet.writeD((int) Math.max(ts.getStamp() - _currentTime, 0) / 1000);
} }
return true; return true;
} }
@@ -23,7 +23,7 @@ import org.l2jmobius.gameserver.model.skills.Skill;
* Simple class containing all necessary information to maintain<br> * Simple class containing all necessary information to maintain<br>
* valid time stamps and reuse for skills and items reuse upon re-login.<br> * valid time stamps and reuse for skills and items reuse upon re-login.<br>
* <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b> * <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b>
* @author Yesod, Zoey76 * @author Yesod, Zoey76, Mobius
*/ */
public class TimeStamp public class TimeStamp
{ {
@@ -36,7 +36,7 @@ public class TimeStamp
/** Item or skill reuse time. */ /** Item or skill reuse time. */
private final long _reuse; private final long _reuse;
/** Time stamp. */ /** Time stamp. */
private final long _stamp; private volatile long _stamp;
/** Shared reuse group. */ /** Shared reuse group. */
private final int _group; private final int _group;
@@ -52,7 +52,7 @@ public class TimeStamp
_id2 = skill.getLevel(); _id2 = skill.getLevel();
_id3 = skill.getSubLevel(); _id3 = skill.getSubLevel();
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = -1; _group = -1;
} }
@@ -68,7 +68,7 @@ public class TimeStamp
_id2 = item.getObjectId(); _id2 = item.getObjectId();
_id3 = 0; _id3 = 0;
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = item.getSharedReuseGroup(); _group = item.getSharedReuseGroup();
} }
@@ -151,7 +151,16 @@ public class TimeStamp
*/ */
public long getRemaining() public long getRemaining()
{ {
return Math.max(_stamp - System.currentTimeMillis(), 0); if (_stamp == 0)
{
return 0;
}
final long remainingTime = Math.max(_stamp - System.currentTimeMillis(), 0);
if (remainingTime == 0)
{
_stamp = 0;
}
return remainingTime;
} }
/** /**
@@ -160,6 +169,15 @@ public class TimeStamp
*/ */
public boolean hasNotPassed() public boolean hasNotPassed()
{ {
return System.currentTimeMillis() < _stamp; if (_stamp == 0)
{
return false;
}
final boolean hasNotPassed = System.currentTimeMillis() < _stamp;
if (!hasNotPassed)
{
_stamp = 0;
}
return hasNotPassed;
} }
} }
@@ -212,11 +212,11 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
/** Map containing all skills of this character. */ /** Map containing all skills of this character. */
private final Map<Integer, Skill> _skills = new ConcurrentSkipListMap<>(); private final Map<Integer, Skill> _skills = new ConcurrentSkipListMap<>();
/** Map containing the skill reuse time stamps. */ /** Map containing the skill reuse time stamps. */
private volatile Map<Long, TimeStamp> _reuseTimeStampsSkills = null; private final Map<Long, TimeStamp> _reuseTimeStampsSkills = new ConcurrentHashMap<>();
/** Map containing the item reuse time stamps. */ /** Map containing the item reuse time stamps. */
private volatile Map<Integer, TimeStamp> _reuseTimeStampsItems = null; private final Map<Integer, TimeStamp> _reuseTimeStampsItems = new ConcurrentHashMap<>();
/** Map containing all the disabled skills. */ /** Map containing all the disabled skills. */
private volatile Map<Long, Long> _disabledSkills = null; private final Map<Long, Long> _disabledSkills = new ConcurrentHashMap<>();
private boolean _allSkillsDisabled; private boolean _allSkillsDisabled;
private final byte[] _zones = new byte[ZoneId.getZoneCount()]; private final byte[] _zones = new byte[ZoneId.getZoneCount()];
@@ -1342,16 +1342,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStampItem(ItemInstance item, long reuse, long systime) public void addTimeStampItem(ItemInstance item, long reuse, long systime)
{ {
if (_reuseTimeStampsItems == null)
{
synchronized (this)
{
if (_reuseTimeStampsItems == null)
{
_reuseTimeStampsItems = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime)); _reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime));
} }
@@ -1360,9 +1350,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param itemObjId the item object ID * @param itemObjId the item object ID
* @return if the item has a reuse time stamp, the remaining time, otherwise -1 * @return if the item has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getItemRemainingReuseTime(int itemObjId) public long getItemRemainingReuseTime(int itemObjId)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsItems != null) ? _reuseTimeStampsItems.get(itemObjId) : null; final TimeStamp reuseStamp = _reuseTimeStampsItems.get(itemObjId);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -1373,13 +1363,18 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public long getReuseDelayOnGroup(int group) public long getReuseDelayOnGroup(int group)
{ {
if ((group > 0) && (_reuseTimeStampsItems != null)) if ((group > 0) && !_reuseTimeStampsItems.isEmpty())
{ {
final long currentTime = System.currentTimeMillis();
for (TimeStamp ts : _reuseTimeStampsItems.values()) for (TimeStamp ts : _reuseTimeStampsItems.values())
{ {
if ((ts.getSharedReuseGroup() == group) && ts.hasNotPassed()) if (ts.getSharedReuseGroup() == group)
{ {
return ts.getRemaining(); final long stamp = ts.getStamp();
if (currentTime < stamp)
{
return Math.max(stamp - currentTime, 0);
}
} }
} }
} }
@@ -1414,16 +1409,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStamp(Skill skill, long reuse, long systime) public void addTimeStamp(Skill skill, long reuse, long systime)
{ {
if (_reuseTimeStampsSkills == null)
{
synchronized (this)
{
if (_reuseTimeStampsSkills == null)
{
_reuseTimeStampsSkills = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime)); _reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime));
} }
@@ -1431,33 +1416,27 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* Removes a skill reuse time stamp. * Removes a skill reuse time stamp.
* @param skill the skill to remove * @param skill the skill to remove
*/ */
public synchronized void removeTimeStamp(Skill skill) public void removeTimeStamp(Skill skill)
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.remove(skill.getReuseHashCode()); _reuseTimeStampsSkills.remove(skill.getReuseHashCode());
} }
}
/** /**
* Removes all skill reuse time stamps. * Removes all skill reuse time stamps.
*/ */
public synchronized void resetTimeStamps() public void resetTimeStamps()
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.clear(); _reuseTimeStampsSkills.clear();
} }
}
/** /**
* Gets the skill remaining reuse time for a given skill hash code. * Gets the skill remaining reuse time for a given skill hash code.
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return if the skill has a reuse time stamp, the remaining time, otherwise -1 * @return if the skill has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getSkillRemainingReuseTime(long hashCode) public long getSkillRemainingReuseTime(long hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -1466,9 +1445,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return {@code true} if the skill is under reuse time, {@code false} otherwise * @return {@code true} if the skill is under reuse time, {@code false} otherwise
*/ */
public synchronized boolean hasSkillReuse(long hashCode) public boolean hasSkillReuse(long hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return (reuseStamp != null) && reuseStamp.hasNotPassed(); return (reuseStamp != null) && reuseStamp.hasNotPassed();
} }
@@ -1479,7 +1458,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public synchronized TimeStamp getSkillReuseTimeStamp(long hashCode) public synchronized TimeStamp getSkillReuseTimeStamp(long hashCode)
{ {
return _reuseTimeStampsSkills != null ? _reuseTimeStampsSkills.get(hashCode) : null; return _reuseTimeStampsSkills.get(hashCode);
} }
/** /**
@@ -1497,7 +1476,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void enableSkill(Skill skill) public void enableSkill(Skill skill)
{ {
if ((skill == null) || (_disabledSkills == null)) if (skill == null)
{ {
return; return;
} }
@@ -1516,31 +1495,16 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
{ {
return; return;
} }
if (_disabledSkills == null)
{
synchronized (this)
{
if (_disabledSkills == null)
{
_disabledSkills = new ConcurrentHashMap<>();
}
}
}
_disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE); _disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE);
} }
/** /**
* Removes all the disabled skills. * Removes all the disabled skills.
*/ */
public synchronized void resetDisabledSkills() public void resetDisabledSkills()
{
if (_disabledSkills != null)
{ {
_disabledSkills.clear(); _disabledSkills.clear();
} }
}
/** /**
* Verifies if the skill is disabled. * Verifies if the skill is disabled.
@@ -1570,7 +1534,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
return true; return true;
} }
if (_disabledSkills == null) if (_disabledSkills.isEmpty())
{ {
return false; return false;
} }
@@ -7204,6 +7204,7 @@ public class PlayerInstance extends Playable
int buff_index = 0; int buff_index = 0;
final List<Long> storedSkills = new ArrayList<>(); final List<Long> storedSkills = new ArrayList<>();
final long currentTime = System.currentTimeMillis();
// Store all effect data along with calulated remaining // Store all effect data along with calulated remaining
// reuse delays for matching skills. 'restore_type'= 0. // reuse delays for matching skills. 'restore_type'= 0.
@@ -7263,8 +7264,8 @@ public class PlayerInstance extends Playable
statement.setInt(5, info.getTime()); statement.setInt(5, info.getTime());
final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode()); final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode());
statement.setLong(6, (t != null) && t.hasNotPassed() ? t.getReuse() : 0); statement.setLong(6, (t != null) && (currentTime < t.getStamp()) ? t.getReuse() : 0);
statement.setDouble(7, (t != null) && t.hasNotPassed() ? t.getStamp() : 0); statement.setDouble(7, (t != null) && (currentTime < t.getStamp()) ? t.getStamp() : 0);
statement.setInt(8, 0); // Store type 0, active buffs/debuffs. statement.setInt(8, 0); // Store type 0, active buffs/debuffs.
statement.setInt(9, _classIndex); statement.setInt(9, _classIndex);
@@ -7274,10 +7275,7 @@ public class PlayerInstance extends Playable
} }
// Skills under reuse. // Skills under reuse.
final Map<Long, TimeStamp> reuseTimeStamps = getSkillReuseTimeStamps(); for (Entry<Long, TimeStamp> ts : getSkillReuseTimeStamps().entrySet())
if (reuseTimeStamps != null)
{
for (Entry<Long, TimeStamp> ts : reuseTimeStamps.entrySet())
{ {
final long hash = ts.getKey(); final long hash = ts.getKey();
if (storedSkills.contains(hash)) if (storedSkills.contains(hash))
@@ -7286,7 +7284,7 @@ public class PlayerInstance extends Playable
} }
final TimeStamp t = ts.getValue(); final TimeStamp t = ts.getValue();
if ((t != null) && t.hasNotPassed()) if ((t != null) && (currentTime < t.getStamp()))
{ {
storedSkills.add(hash); storedSkills.add(hash);
@@ -7303,7 +7301,6 @@ public class PlayerInstance extends Playable
statement.addBatch(); statement.addBatch();
} }
} }
}
statement.executeBatch(); statement.executeBatch();
} }
@@ -7323,12 +7320,10 @@ public class PlayerInstance extends Playable
ps1.setInt(1, getObjectId()); ps1.setInt(1, getObjectId());
ps1.execute(); ps1.execute();
final Map<Integer, TimeStamp> itemReuseTimeStamps = getItemReuseTimeStamps(); final long currentTime = System.currentTimeMillis();
if (itemReuseTimeStamps != null) for (TimeStamp ts : getItemReuseTimeStamps().values())
{ {
for (TimeStamp ts : itemReuseTimeStamps.values()) if ((ts != null) && (currentTime < ts.getStamp()))
{
if ((ts != null) && ts.hasNotPassed())
{ {
ps2.setInt(1, getObjectId()); ps2.setInt(1, getObjectId());
ps2.setInt(2, ts.getItemId()); ps2.setInt(2, ts.getItemId());
@@ -7340,7 +7335,6 @@ public class PlayerInstance extends Playable
} }
ps2.executeBatch(); ps2.executeBatch();
} }
}
catch (Exception e) catch (Exception e)
{ {
LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e); LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e);
@@ -18,38 +18,33 @@ package org.l2jmobius.gameserver.network.serverpackets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import org.l2jmobius.commons.network.PacketWriter; import org.l2jmobius.commons.network.PacketWriter;
import org.l2jmobius.gameserver.data.xml.impl.SkillData; import org.l2jmobius.gameserver.data.xml.impl.SkillData;
import org.l2jmobius.gameserver.model.TimeStamp; import org.l2jmobius.gameserver.model.TimeStamp;
import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
import org.l2jmobius.gameserver.model.skills.Skill;
import org.l2jmobius.gameserver.network.OutgoingPackets; import org.l2jmobius.gameserver.network.OutgoingPackets;
/** /**
* Skill Cool Time server packet implementation. * Skill Cool Time server packet implementation.
* @author KenM, Zoey76 * @author KenM, Zoey76, Mobius
*/ */
public class SkillCoolTime implements IClientOutgoingPacket public class SkillCoolTime implements IClientOutgoingPacket
{ {
private final long _currentTime;
private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>(); private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>();
public SkillCoolTime(PlayerInstance player) public SkillCoolTime(PlayerInstance player)
{ {
final Map<Long, TimeStamp> skillReuseTimeStamps = player.getSkillReuseTimeStamps(); _currentTime = System.currentTimeMillis();
if (skillReuseTimeStamps != null) for (TimeStamp ts : player.getSkillReuseTimeStamps().values())
{ {
for (TimeStamp ts : skillReuseTimeStamps.values()) if ((_currentTime < ts.getStamp()) && !SkillData.getInstance().getSkill(ts.getSkillId(), ts.getSkillLvl(), ts.getSkillSubLvl()).isNotBroadcastable())
{
final Skill skill = SkillData.getInstance().getSkill(ts.getSkillId(), ts.getSkillLvl(), ts.getSkillSubLvl());
if (ts.hasNotPassed() && !skill.isNotBroadcastable())
{ {
_skillReuseTimeStamps.add(ts); _skillReuseTimeStamps.add(ts);
} }
} }
} }
}
@Override @Override
public boolean write(PacketWriter packet) public boolean write(PacketWriter packet)
@@ -62,7 +57,7 @@ public class SkillCoolTime implements IClientOutgoingPacket
packet.writeD(ts.getSkillId()); packet.writeD(ts.getSkillId());
packet.writeD(0x00); // ? packet.writeD(0x00); // ?
packet.writeD((int) ts.getReuse() / 1000); packet.writeD((int) ts.getReuse() / 1000);
packet.writeD((int) ts.getRemaining() / 1000); packet.writeD((int) Math.max(ts.getStamp() - _currentTime, 0) / 1000);
} }
return true; return true;
} }
@@ -93,7 +93,16 @@ public class Timestamp
*/ */
public long getRemaining() public long getRemaining()
{ {
return Math.max(_stamp - System.currentTimeMillis(), 0); if (_stamp == 0)
{
return 0;
}
final long remainingTime = Math.max(_stamp - System.currentTimeMillis(), 0);
if (remainingTime == 0)
{
_stamp = 0;
}
return remainingTime;
} }
/** /**
@@ -102,6 +111,15 @@ public class Timestamp
*/ */
public boolean hasNotPassed() public boolean hasNotPassed()
{ {
return System.currentTimeMillis() < _stamp; if (_stamp == 0)
{
return false;
}
final boolean hasNotPassed = System.currentTimeMillis() < _stamp;
if (!hasNotPassed)
{
_stamp = 0;
}
return hasNotPassed;
} }
} }
@@ -9693,9 +9693,9 @@ public class PlayerInstance extends Playable
final Effect[] effects = getAllEffects(); final Effect[] effects = getAllEffects();
statement = con.prepareStatement(ADD_SKILL_SAVE); statement = con.prepareStatement(ADD_SKILL_SAVE);
final List<Integer> storedSkills = new ArrayList<>();
int buff_index = 0; int buff_index = 0;
final List<Integer> storedSkills = new ArrayList<>();
final long currentTime = System.currentTimeMillis();
for (Effect effect : effects) for (Effect effect : effects)
{ {
@@ -9717,8 +9717,8 @@ public class PlayerInstance extends Playable
if (_reuseTimestamps.containsKey(effect.getSkill().getId())) if (_reuseTimestamps.containsKey(effect.getSkill().getId()))
{ {
final Timestamp t = _reuseTimestamps.get(effect.getSkill().getId()); final Timestamp t = _reuseTimestamps.get(effect.getSkill().getId());
statement.setLong(6, t.hasNotPassed() ? t.getReuse() : 0); statement.setLong(6, currentTime < t.getStamp() ? t.getReuse() : 0);
statement.setLong(7, t.hasNotPassed() ? t.getStamp() : 0); statement.setLong(7, currentTime < t.getStamp() ? t.getStamp() : 0);
} }
else else
{ {
@@ -9734,7 +9734,7 @@ public class PlayerInstance extends Playable
// Store the reuse delays of remaining skills which lost effect but still under reuse delay. 'restore_type' 1. // Store the reuse delays of remaining skills which lost effect but still under reuse delay. 'restore_type' 1.
for (Timestamp t : _reuseTimestamps.values()) for (Timestamp t : _reuseTimestamps.values())
{ {
if (t.hasNotPassed()) if (currentTime < t.getStamp())
{ {
final int skillId = t.getSkillId(); final int skillId = t.getSkillId();
final int skillLvl = t.getSkillLevel(); final int skillLvl = t.getSkillLevel();
@@ -21,12 +21,18 @@ import java.util.Collection;
import org.l2jmobius.gameserver.model.Timestamp; import org.l2jmobius.gameserver.model.Timestamp;
import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
/**
* Skill Cool Time server packet implementation.
* @author KenM, Zoey76, Mobius
*/
public class SkillCoolTime extends GameServerPacket public class SkillCoolTime extends GameServerPacket
{ {
private final long _currentTime;
public Collection<Timestamp> _reuseTimestamps; public Collection<Timestamp> _reuseTimestamps;
public SkillCoolTime(PlayerInstance player) public SkillCoolTime(PlayerInstance player)
{ {
_currentTime = System.currentTimeMillis();
_reuseTimestamps = player.getReuseTimeStamps(); _reuseTimestamps = player.getReuseTimeStamps();
} }
@@ -40,12 +46,12 @@ public class SkillCoolTime extends GameServerPacket
} }
writeC(0xc1); writeC(0xc1);
writeD(_reuseTimestamps.size()); writeD(_reuseTimestamps.size());
for (Timestamp reuseTimestamp : _reuseTimestamps) for (Timestamp ts : _reuseTimestamps)
{ {
writeD(reuseTimestamp.getSkillId()); writeD(ts.getSkillId());
writeD(reuseTimestamp.getSkillLevel()); writeD(ts.getSkillLevel());
writeD((int) reuseTimestamp.getReuse() / 1000); writeD((int) ts.getReuse() / 1000);
writeD((int) reuseTimestamp.getRemaining() / 1000); writeD((int) Math.max(ts.getStamp() - _currentTime, 0) / 1000);
} }
} }
} }
@@ -23,7 +23,7 @@ import org.l2jmobius.gameserver.model.skills.Skill;
* Simple class containing all necessary information to maintain<br> * Simple class containing all necessary information to maintain<br>
* valid time stamps and reuse for skills and items reuse upon re-login.<br> * valid time stamps and reuse for skills and items reuse upon re-login.<br>
* <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b> * <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b>
* @author Yesod, Zoey76 * @author Yesod, Zoey76, Mobius
*/ */
public class TimeStamp public class TimeStamp
{ {
@@ -34,7 +34,7 @@ public class TimeStamp
/** Item or skill reuse time. */ /** Item or skill reuse time. */
private final long _reuse; private final long _reuse;
/** Time stamp. */ /** Time stamp. */
private final long _stamp; private volatile long _stamp;
/** Shared reuse group. */ /** Shared reuse group. */
private final int _group; private final int _group;
@@ -49,7 +49,7 @@ public class TimeStamp
_id1 = skill.getId(); _id1 = skill.getId();
_id2 = skill.getLevel(); _id2 = skill.getLevel();
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = -1; _group = -1;
} }
@@ -64,7 +64,7 @@ public class TimeStamp
_id1 = item.getId(); _id1 = item.getId();
_id2 = item.getObjectId(); _id2 = item.getObjectId();
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = item.getSharedReuseGroup(); _group = item.getSharedReuseGroup();
} }
@@ -138,7 +138,16 @@ public class TimeStamp
*/ */
public long getRemaining() public long getRemaining()
{ {
return Math.max(_stamp - System.currentTimeMillis(), 0); if (_stamp == 0)
{
return 0;
}
final long remainingTime = Math.max(_stamp - System.currentTimeMillis(), 0);
if (remainingTime == 0)
{
_stamp = 0;
}
return remainingTime;
} }
/** /**
@@ -147,6 +156,15 @@ public class TimeStamp
*/ */
public boolean hasNotPassed() public boolean hasNotPassed()
{ {
return System.currentTimeMillis() < _stamp; if (_stamp == 0)
{
return false;
}
final boolean hasNotPassed = System.currentTimeMillis() < _stamp;
if (!hasNotPassed)
{
_stamp = 0;
}
return hasNotPassed;
} }
} }
@@ -216,11 +216,11 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
/** Map containing all skills of this character. */ /** Map containing all skills of this character. */
private final Map<Integer, Skill> _skills = new ConcurrentHashMap<>(); private final Map<Integer, Skill> _skills = new ConcurrentHashMap<>();
/** Map containing the skill reuse time stamps. */ /** Map containing the skill reuse time stamps. */
private volatile Map<Integer, TimeStamp> _reuseTimeStampsSkills = null; private final Map<Integer, TimeStamp> _reuseTimeStampsSkills = new ConcurrentHashMap<>();
/** Map containing the item reuse time stamps. */ /** Map containing the item reuse time stamps. */
private volatile Map<Integer, TimeStamp> _reuseTimeStampsItems = null; private final Map<Integer, TimeStamp> _reuseTimeStampsItems = new ConcurrentHashMap<>();
/** Map containing all the disabled skills. */ /** Map containing all the disabled skills. */
private volatile Map<Integer, Long> _disabledSkills = null; private final Map<Integer, Long> _disabledSkills = new ConcurrentHashMap<>();
private boolean _allSkillsDisabled; private boolean _allSkillsDisabled;
private final byte[] _zones = new byte[ZoneId.getZoneCount()]; private final byte[] _zones = new byte[ZoneId.getZoneCount()];
@@ -2102,16 +2102,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStampItem(ItemInstance item, long reuse, long systime) public void addTimeStampItem(ItemInstance item, long reuse, long systime)
{ {
if (_reuseTimeStampsItems == null)
{
synchronized (this)
{
if (_reuseTimeStampsItems == null)
{
_reuseTimeStampsItems = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime)); _reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime));
} }
@@ -2120,9 +2110,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param itemObjId the item object ID * @param itemObjId the item object ID
* @return if the item has a reuse time stamp, the remaining time, otherwise -1 * @return if the item has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getItemRemainingReuseTime(int itemObjId) public long getItemRemainingReuseTime(int itemObjId)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsItems != null) ? _reuseTimeStampsItems.get(itemObjId) : null; final TimeStamp reuseStamp = _reuseTimeStampsItems.get(itemObjId);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -2133,13 +2123,18 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public long getReuseDelayOnGroup(int group) public long getReuseDelayOnGroup(int group)
{ {
if ((group > 0) && (_reuseTimeStampsItems != null)) if ((group > 0) && !_reuseTimeStampsItems.isEmpty())
{ {
final long currentTime = System.currentTimeMillis();
for (TimeStamp ts : _reuseTimeStampsItems.values()) for (TimeStamp ts : _reuseTimeStampsItems.values())
{ {
if ((ts.getSharedReuseGroup() == group) && ts.hasNotPassed()) if (ts.getSharedReuseGroup() == group)
{ {
return ts.getRemaining(); final long stamp = ts.getStamp();
if (currentTime < stamp)
{
return Math.max(stamp - currentTime, 0);
}
} }
} }
} }
@@ -2174,16 +2169,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStamp(Skill skill, long reuse, long systime) public void addTimeStamp(Skill skill, long reuse, long systime)
{ {
if (_reuseTimeStampsSkills == null)
{
synchronized (this)
{
if (_reuseTimeStampsSkills == null)
{
_reuseTimeStampsSkills = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime)); _reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime));
} }
@@ -2191,33 +2176,27 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* Removes a skill reuse time stamp. * Removes a skill reuse time stamp.
* @param skill the skill to remove * @param skill the skill to remove
*/ */
public synchronized void removeTimeStamp(Skill skill) public void removeTimeStamp(Skill skill)
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.remove(skill.getReuseHashCode()); _reuseTimeStampsSkills.remove(skill.getReuseHashCode());
} }
}
/** /**
* Removes all skill reuse time stamps. * Removes all skill reuse time stamps.
*/ */
public synchronized void resetTimeStamps() public void resetTimeStamps()
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.clear(); _reuseTimeStampsSkills.clear();
} }
}
/** /**
* Gets the skill remaining reuse time for a given skill hash code. * Gets the skill remaining reuse time for a given skill hash code.
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return if the skill has a reuse time stamp, the remaining time, otherwise -1 * @return if the skill has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getSkillRemainingReuseTime(int hashCode) public long getSkillRemainingReuseTime(int hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -2226,9 +2205,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return {@code true} if the skill is under reuse time, {@code false} otherwise * @return {@code true} if the skill is under reuse time, {@code false} otherwise
*/ */
public synchronized boolean hasSkillReuse(int hashCode) public boolean hasSkillReuse(int hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return (reuseStamp != null) && reuseStamp.hasNotPassed(); return (reuseStamp != null) && reuseStamp.hasNotPassed();
} }
@@ -2237,9 +2216,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return if the skill has a reuse time stamp, the skill reuse time stamp, otherwise {@code null} * @return if the skill has a reuse time stamp, the skill reuse time stamp, otherwise {@code null}
*/ */
public synchronized TimeStamp getSkillReuseTimeStamp(int hashCode) public TimeStamp getSkillReuseTimeStamp(int hashCode)
{ {
return _reuseTimeStampsSkills != null ? _reuseTimeStampsSkills.get(hashCode) : null; return _reuseTimeStampsSkills.get(hashCode);
} }
/** /**
@@ -2257,7 +2236,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void enableSkill(Skill skill) public void enableSkill(Skill skill)
{ {
if ((skill == null) || (_disabledSkills == null)) if (skill == null)
{ {
return; return;
} }
@@ -2276,31 +2255,16 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
{ {
return; return;
} }
if (_disabledSkills == null)
{
synchronized (this)
{
if (_disabledSkills == null)
{
_disabledSkills = new ConcurrentHashMap<>();
}
}
}
_disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE); _disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE);
} }
/** /**
* Removes all the disabled skills. * Removes all the disabled skills.
*/ */
public synchronized void resetDisabledSkills() public void resetDisabledSkills()
{
if (_disabledSkills != null)
{ {
_disabledSkills.clear(); _disabledSkills.clear();
} }
}
/** /**
* Verifies if the skill is disabled. * Verifies if the skill is disabled.
@@ -2325,7 +2289,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
return true; return true;
} }
if (_disabledSkills == null) if (_disabledSkills.isEmpty())
{ {
return false; return false;
} }
@@ -7589,6 +7589,7 @@ public class PlayerInstance extends Playable
int buff_index = 0; int buff_index = 0;
final List<Integer> storedSkills = new ArrayList<>(); final List<Integer> storedSkills = new ArrayList<>();
final long currentTime = System.currentTimeMillis();
// Store all effect data along with calulated remaining // Store all effect data along with calulated remaining
// reuse delays for matching skills. 'restore_type'= 0. // reuse delays for matching skills. 'restore_type'= 0.
@@ -7634,8 +7635,8 @@ public class PlayerInstance extends Playable
statement.setInt(4, info.getTime()); statement.setInt(4, info.getTime());
final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode()); final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode());
statement.setLong(5, (t != null) && t.hasNotPassed() ? t.getReuse() : 0); statement.setLong(5, (t != null) && (currentTime < t.getStamp()) ? t.getReuse() : 0);
statement.setLong(6, (t != null) && t.hasNotPassed() ? t.getStamp() : 0); statement.setLong(6, (t != null) && (currentTime < t.getStamp()) ? t.getStamp() : 0);
statement.setInt(7, 0); // Store type 0, active buffs/debuffs. statement.setInt(7, 0); // Store type 0, active buffs/debuffs.
statement.setInt(8, _classIndex); statement.setInt(8, _classIndex);
@@ -7645,10 +7646,7 @@ public class PlayerInstance extends Playable
} }
// Skills under reuse. // Skills under reuse.
final Map<Integer, TimeStamp> reuseTimeStamps = getSkillReuseTimeStamps(); for (Entry<Integer, TimeStamp> ts : getSkillReuseTimeStamps().entrySet())
if (reuseTimeStamps != null)
{
for (Entry<Integer, TimeStamp> ts : reuseTimeStamps.entrySet())
{ {
final int hash = ts.getKey(); final int hash = ts.getKey();
if (storedSkills.contains(hash)) if (storedSkills.contains(hash))
@@ -7657,7 +7655,7 @@ public class PlayerInstance extends Playable
} }
final TimeStamp t = ts.getValue(); final TimeStamp t = ts.getValue();
if ((t != null) && t.hasNotPassed()) if ((t != null) && (currentTime < t.getStamp()))
{ {
storedSkills.add(hash); storedSkills.add(hash);
@@ -7673,7 +7671,6 @@ public class PlayerInstance extends Playable
statement.addBatch(); statement.addBatch();
} }
} }
}
statement.executeBatch(); statement.executeBatch();
} }
@@ -7693,12 +7690,10 @@ public class PlayerInstance extends Playable
ps1.setInt(1, getObjectId()); ps1.setInt(1, getObjectId());
ps1.execute(); ps1.execute();
final Map<Integer, TimeStamp> itemReuseTimeStamps = getItemReuseTimeStamps(); final long currentTime = System.currentTimeMillis();
if (itemReuseTimeStamps != null) for (TimeStamp ts : getItemReuseTimeStamps().values())
{ {
for (TimeStamp ts : itemReuseTimeStamps.values()) if ((ts != null) && (currentTime < ts.getStamp()))
{
if ((ts != null) && ts.hasNotPassed())
{ {
ps2.setInt(1, getObjectId()); ps2.setInt(1, getObjectId());
ps2.setInt(2, ts.getItemId()); ps2.setInt(2, ts.getItemId());
@@ -7710,7 +7705,6 @@ public class PlayerInstance extends Playable
} }
ps2.executeBatch(); ps2.executeBatch();
} }
}
catch (Exception e) catch (Exception e)
{ {
LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e); LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e);
@@ -18,7 +18,6 @@ package org.l2jmobius.gameserver.network.serverpackets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import org.l2jmobius.commons.network.PacketWriter; import org.l2jmobius.commons.network.PacketWriter;
import org.l2jmobius.gameserver.model.TimeStamp; import org.l2jmobius.gameserver.model.TimeStamp;
@@ -27,26 +26,24 @@ import org.l2jmobius.gameserver.network.OutgoingPackets;
/** /**
* Skill Cool Time server packet implementation. * Skill Cool Time server packet implementation.
* @author KenM, Zoey76 * @author KenM, Zoey76, Mobius
*/ */
public class SkillCoolTime implements IClientOutgoingPacket public class SkillCoolTime implements IClientOutgoingPacket
{ {
private final long _currentTime;
private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>(); private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>();
public SkillCoolTime(PlayerInstance player) public SkillCoolTime(PlayerInstance player)
{ {
final Map<Integer, TimeStamp> skillReuseTimeStamps = player.getSkillReuseTimeStamps(); _currentTime = System.currentTimeMillis();
if (skillReuseTimeStamps != null) for (TimeStamp ts : player.getSkillReuseTimeStamps().values())
{ {
for (TimeStamp ts : skillReuseTimeStamps.values()) if (_currentTime < ts.getStamp())
{
if (ts.hasNotPassed())
{ {
_skillReuseTimeStamps.add(ts); _skillReuseTimeStamps.add(ts);
} }
} }
} }
}
@Override @Override
public boolean write(PacketWriter packet) public boolean write(PacketWriter packet)
@@ -58,7 +55,7 @@ public class SkillCoolTime implements IClientOutgoingPacket
packet.writeD(ts.getSkillId()); packet.writeD(ts.getSkillId());
packet.writeD(ts.getSkillLvl()); packet.writeD(ts.getSkillLvl());
packet.writeD((int) ts.getReuse() / 1000); packet.writeD((int) ts.getReuse() / 1000);
packet.writeD((int) ts.getRemaining() / 1000); packet.writeD((int) Math.max(ts.getStamp() - _currentTime, 0) / 1000);
} }
return true; return true;
} }
@@ -23,7 +23,7 @@ import org.l2jmobius.gameserver.model.skills.Skill;
* Simple class containing all necessary information to maintain<br> * Simple class containing all necessary information to maintain<br>
* valid time stamps and reuse for skills and items reuse upon re-login.<br> * valid time stamps and reuse for skills and items reuse upon re-login.<br>
* <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b> * <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b>
* @author Yesod, Zoey76 * @author Yesod, Zoey76, Mobius
*/ */
public class TimeStamp public class TimeStamp
{ {
@@ -34,7 +34,7 @@ public class TimeStamp
/** Item or skill reuse time. */ /** Item or skill reuse time. */
private final long _reuse; private final long _reuse;
/** Time stamp. */ /** Time stamp. */
private final long _stamp; private volatile long _stamp;
/** Shared reuse group. */ /** Shared reuse group. */
private final int _group; private final int _group;
@@ -49,7 +49,7 @@ public class TimeStamp
_id1 = skill.getId(); _id1 = skill.getId();
_id2 = skill.getLevel(); _id2 = skill.getLevel();
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = -1; _group = -1;
} }
@@ -64,7 +64,7 @@ public class TimeStamp
_id1 = item.getId(); _id1 = item.getId();
_id2 = item.getObjectId(); _id2 = item.getObjectId();
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = item.getSharedReuseGroup(); _group = item.getSharedReuseGroup();
} }
@@ -138,7 +138,16 @@ public class TimeStamp
*/ */
public long getRemaining() public long getRemaining()
{ {
return Math.max(_stamp - System.currentTimeMillis(), 0); if (_stamp == 0)
{
return 0;
}
final long remainingTime = Math.max(_stamp - System.currentTimeMillis(), 0);
if (remainingTime == 0)
{
_stamp = 0;
}
return remainingTime;
} }
/** /**
@@ -147,6 +156,15 @@ public class TimeStamp
*/ */
public boolean hasNotPassed() public boolean hasNotPassed()
{ {
return System.currentTimeMillis() < _stamp; if (_stamp == 0)
{
return false;
}
final boolean hasNotPassed = System.currentTimeMillis() < _stamp;
if (!hasNotPassed)
{
_stamp = 0;
}
return hasNotPassed;
} }
} }
@@ -217,11 +217,11 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
/** Map containing all skills of this character. */ /** Map containing all skills of this character. */
private final Map<Integer, Skill> _skills = new ConcurrentHashMap<>(); private final Map<Integer, Skill> _skills = new ConcurrentHashMap<>();
/** Map containing the skill reuse time stamps. */ /** Map containing the skill reuse time stamps. */
private volatile Map<Integer, TimeStamp> _reuseTimeStampsSkills = null; private final Map<Integer, TimeStamp> _reuseTimeStampsSkills = new ConcurrentHashMap<>();
/** Map containing the item reuse time stamps. */ /** Map containing the item reuse time stamps. */
private volatile Map<Integer, TimeStamp> _reuseTimeStampsItems = null; private final Map<Integer, TimeStamp> _reuseTimeStampsItems = new ConcurrentHashMap<>();
/** Map containing all the disabled skills. */ /** Map containing all the disabled skills. */
private volatile Map<Integer, Long> _disabledSkills = null; private final Map<Integer, Long> _disabledSkills = new ConcurrentHashMap<>();
private boolean _allSkillsDisabled; private boolean _allSkillsDisabled;
private final byte[] _zones = new byte[ZoneId.getZoneCount()]; private final byte[] _zones = new byte[ZoneId.getZoneCount()];
@@ -2104,16 +2104,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStampItem(ItemInstance item, long reuse, long systime) public void addTimeStampItem(ItemInstance item, long reuse, long systime)
{ {
if (_reuseTimeStampsItems == null)
{
synchronized (this)
{
if (_reuseTimeStampsItems == null)
{
_reuseTimeStampsItems = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime)); _reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime));
} }
@@ -2122,9 +2112,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param itemObjId the item object ID * @param itemObjId the item object ID
* @return if the item has a reuse time stamp, the remaining time, otherwise -1 * @return if the item has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getItemRemainingReuseTime(int itemObjId) public long getItemRemainingReuseTime(int itemObjId)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsItems != null) ? _reuseTimeStampsItems.get(itemObjId) : null; final TimeStamp reuseStamp = _reuseTimeStampsItems.get(itemObjId);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -2135,13 +2125,18 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public long getReuseDelayOnGroup(int group) public long getReuseDelayOnGroup(int group)
{ {
if ((group > 0) && (_reuseTimeStampsItems != null)) if ((group > 0) && !_reuseTimeStampsItems.isEmpty())
{ {
final long currentTime = System.currentTimeMillis();
for (TimeStamp ts : _reuseTimeStampsItems.values()) for (TimeStamp ts : _reuseTimeStampsItems.values())
{ {
if ((ts.getSharedReuseGroup() == group) && ts.hasNotPassed()) if (ts.getSharedReuseGroup() == group)
{ {
return ts.getRemaining(); final long stamp = ts.getStamp();
if (currentTime < stamp)
{
return Math.max(stamp - currentTime, 0);
}
} }
} }
} }
@@ -2176,16 +2171,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStamp(Skill skill, long reuse, long systime) public void addTimeStamp(Skill skill, long reuse, long systime)
{ {
if (_reuseTimeStampsSkills == null)
{
synchronized (this)
{
if (_reuseTimeStampsSkills == null)
{
_reuseTimeStampsSkills = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime)); _reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime));
} }
@@ -2193,33 +2178,27 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* Removes a skill reuse time stamp. * Removes a skill reuse time stamp.
* @param skill the skill to remove * @param skill the skill to remove
*/ */
public synchronized void removeTimeStamp(Skill skill) public void removeTimeStamp(Skill skill)
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.remove(skill.getReuseHashCode()); _reuseTimeStampsSkills.remove(skill.getReuseHashCode());
} }
}
/** /**
* Removes all skill reuse time stamps. * Removes all skill reuse time stamps.
*/ */
public synchronized void resetTimeStamps() public void resetTimeStamps()
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.clear(); _reuseTimeStampsSkills.clear();
} }
}
/** /**
* Gets the skill remaining reuse time for a given skill hash code. * Gets the skill remaining reuse time for a given skill hash code.
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return if the skill has a reuse time stamp, the remaining time, otherwise -1 * @return if the skill has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getSkillRemainingReuseTime(int hashCode) public long getSkillRemainingReuseTime(int hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -2228,9 +2207,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return {@code true} if the skill is under reuse time, {@code false} otherwise * @return {@code true} if the skill is under reuse time, {@code false} otherwise
*/ */
public synchronized boolean hasSkillReuse(int hashCode) public boolean hasSkillReuse(int hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return (reuseStamp != null) && reuseStamp.hasNotPassed(); return (reuseStamp != null) && reuseStamp.hasNotPassed();
} }
@@ -2239,9 +2218,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return if the skill has a reuse time stamp, the skill reuse time stamp, otherwise {@code null} * @return if the skill has a reuse time stamp, the skill reuse time stamp, otherwise {@code null}
*/ */
public synchronized TimeStamp getSkillReuseTimeStamp(int hashCode) public TimeStamp getSkillReuseTimeStamp(int hashCode)
{ {
return _reuseTimeStampsSkills != null ? _reuseTimeStampsSkills.get(hashCode) : null; return _reuseTimeStampsSkills.get(hashCode);
} }
/** /**
@@ -2259,7 +2238,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void enableSkill(Skill skill) public void enableSkill(Skill skill)
{ {
if ((skill == null) || (_disabledSkills == null)) if (skill == null)
{ {
return; return;
} }
@@ -2278,31 +2257,16 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
{ {
return; return;
} }
if (_disabledSkills == null)
{
synchronized (this)
{
if (_disabledSkills == null)
{
_disabledSkills = new ConcurrentHashMap<>();
}
}
}
_disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE); _disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE);
} }
/** /**
* Removes all the disabled skills. * Removes all the disabled skills.
*/ */
public synchronized void resetDisabledSkills() public void resetDisabledSkills()
{
if (_disabledSkills != null)
{ {
_disabledSkills.clear(); _disabledSkills.clear();
} }
}
/** /**
* Verifies if the skill is disabled. * Verifies if the skill is disabled.
@@ -2327,7 +2291,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
return true; return true;
} }
if (_disabledSkills == null) if (_disabledSkills.isEmpty())
{ {
return false; return false;
} }
@@ -7467,6 +7467,7 @@ public class PlayerInstance extends Playable
int buff_index = 0; int buff_index = 0;
final List<Integer> storedSkills = new ArrayList<>(); final List<Integer> storedSkills = new ArrayList<>();
final long currentTime = System.currentTimeMillis();
// Store all effect data along with calulated remaining // Store all effect data along with calulated remaining
// reuse delays for matching skills. 'restore_type'= 0. // reuse delays for matching skills. 'restore_type'= 0.
@@ -7512,8 +7513,8 @@ public class PlayerInstance extends Playable
statement.setInt(4, info.getTime()); statement.setInt(4, info.getTime());
final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode()); final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode());
statement.setLong(5, (t != null) && t.hasNotPassed() ? t.getReuse() : 0); statement.setLong(5, (t != null) && (currentTime < t.getStamp()) ? t.getReuse() : 0);
statement.setLong(6, (t != null) && t.hasNotPassed() ? t.getStamp() : 0); statement.setLong(6, (t != null) && (currentTime < t.getStamp()) ? t.getStamp() : 0);
statement.setInt(7, 0); // Store type 0, active buffs/debuffs. statement.setInt(7, 0); // Store type 0, active buffs/debuffs.
statement.setInt(8, _classIndex); statement.setInt(8, _classIndex);
@@ -7523,10 +7524,7 @@ public class PlayerInstance extends Playable
} }
// Skills under reuse. // Skills under reuse.
final Map<Integer, TimeStamp> reuseTimeStamps = getSkillReuseTimeStamps(); for (Entry<Integer, TimeStamp> ts : getSkillReuseTimeStamps().entrySet())
if (reuseTimeStamps != null)
{
for (Entry<Integer, TimeStamp> ts : reuseTimeStamps.entrySet())
{ {
final int hash = ts.getKey(); final int hash = ts.getKey();
if (storedSkills.contains(hash)) if (storedSkills.contains(hash))
@@ -7535,7 +7533,7 @@ public class PlayerInstance extends Playable
} }
final TimeStamp t = ts.getValue(); final TimeStamp t = ts.getValue();
if ((t != null) && t.hasNotPassed()) if ((t != null) && (currentTime < t.getStamp()))
{ {
storedSkills.add(hash); storedSkills.add(hash);
@@ -7551,7 +7549,6 @@ public class PlayerInstance extends Playable
statement.addBatch(); statement.addBatch();
} }
} }
}
statement.executeBatch(); statement.executeBatch();
} }
@@ -7571,12 +7568,10 @@ public class PlayerInstance extends Playable
ps1.setInt(1, getObjectId()); ps1.setInt(1, getObjectId());
ps1.execute(); ps1.execute();
final Map<Integer, TimeStamp> itemReuseTimeStamps = getItemReuseTimeStamps(); final long currentTime = System.currentTimeMillis();
if (itemReuseTimeStamps != null) for (TimeStamp ts : getItemReuseTimeStamps().values())
{ {
for (TimeStamp ts : itemReuseTimeStamps.values()) if ((ts != null) && (currentTime < ts.getStamp()))
{
if ((ts != null) && ts.hasNotPassed())
{ {
ps2.setInt(1, getObjectId()); ps2.setInt(1, getObjectId());
ps2.setInt(2, ts.getItemId()); ps2.setInt(2, ts.getItemId());
@@ -7588,7 +7583,6 @@ public class PlayerInstance extends Playable
} }
ps2.executeBatch(); ps2.executeBatch();
} }
}
catch (Exception e) catch (Exception e)
{ {
LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e); LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e);
@@ -18,7 +18,6 @@ package org.l2jmobius.gameserver.network.serverpackets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import org.l2jmobius.commons.network.PacketWriter; import org.l2jmobius.commons.network.PacketWriter;
import org.l2jmobius.gameserver.model.TimeStamp; import org.l2jmobius.gameserver.model.TimeStamp;
@@ -27,26 +26,24 @@ import org.l2jmobius.gameserver.network.OutgoingPackets;
/** /**
* Skill Cool Time server packet implementation. * Skill Cool Time server packet implementation.
* @author KenM, Zoey76 * @author KenM, Zoey76, Mobius
*/ */
public class SkillCoolTime implements IClientOutgoingPacket public class SkillCoolTime implements IClientOutgoingPacket
{ {
private final long _currentTime;
private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>(); private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>();
public SkillCoolTime(PlayerInstance player) public SkillCoolTime(PlayerInstance player)
{ {
final Map<Integer, TimeStamp> skillReuseTimeStamps = player.getSkillReuseTimeStamps(); _currentTime = System.currentTimeMillis();
if (skillReuseTimeStamps != null) for (TimeStamp ts : player.getSkillReuseTimeStamps().values())
{ {
for (TimeStamp ts : skillReuseTimeStamps.values()) if (_currentTime < ts.getStamp())
{
if (ts.hasNotPassed())
{ {
_skillReuseTimeStamps.add(ts); _skillReuseTimeStamps.add(ts);
} }
} }
} }
}
@Override @Override
public boolean write(PacketWriter packet) public boolean write(PacketWriter packet)
@@ -58,7 +55,7 @@ public class SkillCoolTime implements IClientOutgoingPacket
packet.writeD(ts.getSkillId()); packet.writeD(ts.getSkillId());
packet.writeD(ts.getSkillLvl()); packet.writeD(ts.getSkillLvl());
packet.writeD((int) ts.getReuse() / 1000); packet.writeD((int) ts.getReuse() / 1000);
packet.writeD((int) ts.getRemaining() / 1000); packet.writeD((int) Math.max(ts.getStamp() - _currentTime, 0) / 1000);
} }
return true; return true;
} }
@@ -23,7 +23,7 @@ import org.l2jmobius.gameserver.model.skills.Skill;
* Simple class containing all necessary information to maintain<br> * Simple class containing all necessary information to maintain<br>
* valid time stamps and reuse for skills and items reuse upon re-login.<br> * valid time stamps and reuse for skills and items reuse upon re-login.<br>
* <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b> * <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b>
* @author Yesod, Zoey76 * @author Yesod, Zoey76, Mobius
*/ */
public class TimeStamp public class TimeStamp
{ {
@@ -36,7 +36,7 @@ public class TimeStamp
/** Item or skill reuse time. */ /** Item or skill reuse time. */
private final long _reuse; private final long _reuse;
/** Time stamp. */ /** Time stamp. */
private final long _stamp; private volatile long _stamp;
/** Shared reuse group. */ /** Shared reuse group. */
private final int _group; private final int _group;
@@ -52,7 +52,7 @@ public class TimeStamp
_id2 = skill.getLevel(); _id2 = skill.getLevel();
_id3 = skill.getSubLevel(); _id3 = skill.getSubLevel();
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = -1; _group = -1;
} }
@@ -68,7 +68,7 @@ public class TimeStamp
_id2 = item.getObjectId(); _id2 = item.getObjectId();
_id3 = 0; _id3 = 0;
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = item.getSharedReuseGroup(); _group = item.getSharedReuseGroup();
} }
@@ -151,7 +151,16 @@ public class TimeStamp
*/ */
public long getRemaining() public long getRemaining()
{ {
return Math.max(_stamp - System.currentTimeMillis(), 0); if (_stamp == 0)
{
return 0;
}
final long remainingTime = Math.max(_stamp - System.currentTimeMillis(), 0);
if (remainingTime == 0)
{
_stamp = 0;
}
return remainingTime;
} }
/** /**
@@ -160,6 +169,15 @@ public class TimeStamp
*/ */
public boolean hasNotPassed() public boolean hasNotPassed()
{ {
return System.currentTimeMillis() < _stamp; if (_stamp == 0)
{
return false;
}
final boolean hasNotPassed = System.currentTimeMillis() < _stamp;
if (!hasNotPassed)
{
_stamp = 0;
}
return hasNotPassed;
} }
} }
@@ -211,11 +211,11 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
/** Map containing all skills of this character. */ /** Map containing all skills of this character. */
private final Map<Integer, Skill> _skills = new ConcurrentSkipListMap<>(); private final Map<Integer, Skill> _skills = new ConcurrentSkipListMap<>();
/** Map containing the skill reuse time stamps. */ /** Map containing the skill reuse time stamps. */
private volatile Map<Long, TimeStamp> _reuseTimeStampsSkills = null; private final Map<Long, TimeStamp> _reuseTimeStampsSkills = new ConcurrentHashMap<>();
/** Map containing the item reuse time stamps. */ /** Map containing the item reuse time stamps. */
private volatile Map<Integer, TimeStamp> _reuseTimeStampsItems = null; private final Map<Integer, TimeStamp> _reuseTimeStampsItems = new ConcurrentHashMap<>();
/** Map containing all the disabled skills. */ /** Map containing all the disabled skills. */
private volatile Map<Long, Long> _disabledSkills = null; private final Map<Long, Long> _disabledSkills = new ConcurrentHashMap<>();
private boolean _allSkillsDisabled; private boolean _allSkillsDisabled;
private final byte[] _zones = new byte[ZoneId.getZoneCount()]; private final byte[] _zones = new byte[ZoneId.getZoneCount()];
@@ -1341,16 +1341,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStampItem(ItemInstance item, long reuse, long systime) public void addTimeStampItem(ItemInstance item, long reuse, long systime)
{ {
if (_reuseTimeStampsItems == null)
{
synchronized (this)
{
if (_reuseTimeStampsItems == null)
{
_reuseTimeStampsItems = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime)); _reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime));
} }
@@ -1359,9 +1349,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param itemObjId the item object ID * @param itemObjId the item object ID
* @return if the item has a reuse time stamp, the remaining time, otherwise -1 * @return if the item has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getItemRemainingReuseTime(int itemObjId) public long getItemRemainingReuseTime(int itemObjId)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsItems != null) ? _reuseTimeStampsItems.get(itemObjId) : null; final TimeStamp reuseStamp = _reuseTimeStampsItems.get(itemObjId);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -1372,13 +1362,18 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public long getReuseDelayOnGroup(int group) public long getReuseDelayOnGroup(int group)
{ {
if ((group > 0) && (_reuseTimeStampsItems != null)) if ((group > 0) && !_reuseTimeStampsItems.isEmpty())
{ {
final long currentTime = System.currentTimeMillis();
for (TimeStamp ts : _reuseTimeStampsItems.values()) for (TimeStamp ts : _reuseTimeStampsItems.values())
{ {
if ((ts.getSharedReuseGroup() == group) && ts.hasNotPassed()) if (ts.getSharedReuseGroup() == group)
{ {
return ts.getRemaining(); final long stamp = ts.getStamp();
if (currentTime < stamp)
{
return Math.max(stamp - currentTime, 0);
}
} }
} }
} }
@@ -1413,16 +1408,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStamp(Skill skill, long reuse, long systime) public void addTimeStamp(Skill skill, long reuse, long systime)
{ {
if (_reuseTimeStampsSkills == null)
{
synchronized (this)
{
if (_reuseTimeStampsSkills == null)
{
_reuseTimeStampsSkills = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime)); _reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime));
} }
@@ -1430,33 +1415,27 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* Removes a skill reuse time stamp. * Removes a skill reuse time stamp.
* @param skill the skill to remove * @param skill the skill to remove
*/ */
public synchronized void removeTimeStamp(Skill skill) public void removeTimeStamp(Skill skill)
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.remove(skill.getReuseHashCode()); _reuseTimeStampsSkills.remove(skill.getReuseHashCode());
} }
}
/** /**
* Removes all skill reuse time stamps. * Removes all skill reuse time stamps.
*/ */
public synchronized void resetTimeStamps() public void resetTimeStamps()
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.clear(); _reuseTimeStampsSkills.clear();
} }
}
/** /**
* Gets the skill remaining reuse time for a given skill hash code. * Gets the skill remaining reuse time for a given skill hash code.
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return if the skill has a reuse time stamp, the remaining time, otherwise -1 * @return if the skill has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getSkillRemainingReuseTime(long hashCode) public long getSkillRemainingReuseTime(long hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -1465,9 +1444,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return {@code true} if the skill is under reuse time, {@code false} otherwise * @return {@code true} if the skill is under reuse time, {@code false} otherwise
*/ */
public synchronized boolean hasSkillReuse(long hashCode) public boolean hasSkillReuse(long hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return (reuseStamp != null) && reuseStamp.hasNotPassed(); return (reuseStamp != null) && reuseStamp.hasNotPassed();
} }
@@ -1478,7 +1457,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public synchronized TimeStamp getSkillReuseTimeStamp(long hashCode) public synchronized TimeStamp getSkillReuseTimeStamp(long hashCode)
{ {
return _reuseTimeStampsSkills != null ? _reuseTimeStampsSkills.get(hashCode) : null; return _reuseTimeStampsSkills.get(hashCode);
} }
/** /**
@@ -1496,7 +1475,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void enableSkill(Skill skill) public void enableSkill(Skill skill)
{ {
if ((skill == null) || (_disabledSkills == null)) if (skill == null)
{ {
return; return;
} }
@@ -1515,31 +1494,16 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
{ {
return; return;
} }
if (_disabledSkills == null)
{
synchronized (this)
{
if (_disabledSkills == null)
{
_disabledSkills = new ConcurrentHashMap<>();
}
}
}
_disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE); _disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE);
} }
/** /**
* Removes all the disabled skills. * Removes all the disabled skills.
*/ */
public synchronized void resetDisabledSkills() public void resetDisabledSkills()
{
if (_disabledSkills != null)
{ {
_disabledSkills.clear(); _disabledSkills.clear();
} }
}
/** /**
* Verifies if the skill is disabled. * Verifies if the skill is disabled.
@@ -1569,7 +1533,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
return true; return true;
} }
if (_disabledSkills == null) if (_disabledSkills.isEmpty())
{ {
return false; return false;
} }
@@ -7231,6 +7231,7 @@ public class PlayerInstance extends Playable
int buff_index = 0; int buff_index = 0;
final List<Long> storedSkills = new ArrayList<>(); final List<Long> storedSkills = new ArrayList<>();
final long currentTime = System.currentTimeMillis();
// Store all effect data along with calulated remaining // Store all effect data along with calulated remaining
// reuse delays for matching skills. 'restore_type'= 0. // reuse delays for matching skills. 'restore_type'= 0.
@@ -7290,8 +7291,8 @@ public class PlayerInstance extends Playable
statement.setInt(5, info.getTime()); statement.setInt(5, info.getTime());
final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode()); final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode());
statement.setLong(6, (t != null) && t.hasNotPassed() ? t.getReuse() : 0); statement.setLong(6, (t != null) && (currentTime < t.getStamp()) ? t.getReuse() : 0);
statement.setDouble(7, (t != null) && t.hasNotPassed() ? t.getStamp() : 0); statement.setDouble(7, (t != null) && (currentTime < t.getStamp()) ? t.getStamp() : 0);
statement.setInt(8, 0); // Store type 0, active buffs/debuffs. statement.setInt(8, 0); // Store type 0, active buffs/debuffs.
statement.setInt(9, _classIndex); statement.setInt(9, _classIndex);
@@ -7301,10 +7302,7 @@ public class PlayerInstance extends Playable
} }
// Skills under reuse. // Skills under reuse.
final Map<Long, TimeStamp> reuseTimeStamps = getSkillReuseTimeStamps(); for (Entry<Long, TimeStamp> ts : getSkillReuseTimeStamps().entrySet())
if (reuseTimeStamps != null)
{
for (Entry<Long, TimeStamp> ts : reuseTimeStamps.entrySet())
{ {
final long hash = ts.getKey(); final long hash = ts.getKey();
if (storedSkills.contains(hash)) if (storedSkills.contains(hash))
@@ -7313,7 +7311,7 @@ public class PlayerInstance extends Playable
} }
final TimeStamp t = ts.getValue(); final TimeStamp t = ts.getValue();
if ((t != null) && t.hasNotPassed()) if ((t != null) && (currentTime < t.getStamp()))
{ {
storedSkills.add(hash); storedSkills.add(hash);
@@ -7330,7 +7328,6 @@ public class PlayerInstance extends Playable
statement.addBatch(); statement.addBatch();
} }
} }
}
statement.executeBatch(); statement.executeBatch();
} }
@@ -7350,12 +7347,10 @@ public class PlayerInstance extends Playable
ps1.setInt(1, getObjectId()); ps1.setInt(1, getObjectId());
ps1.execute(); ps1.execute();
final Map<Integer, TimeStamp> itemReuseTimeStamps = getItemReuseTimeStamps(); final long currentTime = System.currentTimeMillis();
if (itemReuseTimeStamps != null) for (TimeStamp ts : getItemReuseTimeStamps().values())
{ {
for (TimeStamp ts : itemReuseTimeStamps.values()) if ((ts != null) && (currentTime < ts.getStamp()))
{
if ((ts != null) && ts.hasNotPassed())
{ {
ps2.setInt(1, getObjectId()); ps2.setInt(1, getObjectId());
ps2.setInt(2, ts.getItemId()); ps2.setInt(2, ts.getItemId());
@@ -7367,7 +7362,6 @@ public class PlayerInstance extends Playable
} }
ps2.executeBatch(); ps2.executeBatch();
} }
}
catch (Exception e) catch (Exception e)
{ {
LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e); LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e);
@@ -18,38 +18,33 @@ package org.l2jmobius.gameserver.network.serverpackets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import org.l2jmobius.commons.network.PacketWriter; import org.l2jmobius.commons.network.PacketWriter;
import org.l2jmobius.gameserver.data.xml.impl.SkillData; import org.l2jmobius.gameserver.data.xml.impl.SkillData;
import org.l2jmobius.gameserver.model.TimeStamp; import org.l2jmobius.gameserver.model.TimeStamp;
import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
import org.l2jmobius.gameserver.model.skills.Skill;
import org.l2jmobius.gameserver.network.OutgoingPackets; import org.l2jmobius.gameserver.network.OutgoingPackets;
/** /**
* Skill Cool Time server packet implementation. * Skill Cool Time server packet implementation.
* @author KenM, Zoey76 * @author KenM, Zoey76, Mobius
*/ */
public class SkillCoolTime implements IClientOutgoingPacket public class SkillCoolTime implements IClientOutgoingPacket
{ {
private final long _currentTime;
private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>(); private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>();
public SkillCoolTime(PlayerInstance player) public SkillCoolTime(PlayerInstance player)
{ {
final Map<Long, TimeStamp> skillReuseTimeStamps = player.getSkillReuseTimeStamps(); _currentTime = System.currentTimeMillis();
if (skillReuseTimeStamps != null) for (TimeStamp ts : player.getSkillReuseTimeStamps().values())
{ {
for (TimeStamp ts : skillReuseTimeStamps.values()) if ((_currentTime < ts.getStamp()) && !SkillData.getInstance().getSkill(ts.getSkillId(), ts.getSkillLvl(), ts.getSkillSubLvl()).isNotBroadcastable())
{
final Skill skill = SkillData.getInstance().getSkill(ts.getSkillId(), ts.getSkillLvl(), ts.getSkillSubLvl());
if (ts.hasNotPassed() && !skill.isNotBroadcastable())
{ {
_skillReuseTimeStamps.add(ts); _skillReuseTimeStamps.add(ts);
} }
} }
} }
}
@Override @Override
public boolean write(PacketWriter packet) public boolean write(PacketWriter packet)
@@ -62,7 +57,7 @@ public class SkillCoolTime implements IClientOutgoingPacket
packet.writeD(ts.getSkillId()); packet.writeD(ts.getSkillId());
packet.writeD(0x00); // ? packet.writeD(0x00); // ?
packet.writeD((int) ts.getReuse() / 1000); packet.writeD((int) ts.getReuse() / 1000);
packet.writeD((int) ts.getRemaining() / 1000); packet.writeD((int) Math.max(ts.getStamp() - _currentTime, 0) / 1000);
} }
return true; return true;
} }
@@ -23,7 +23,7 @@ import org.l2jmobius.gameserver.model.skills.Skill;
* Simple class containing all necessary information to maintain<br> * Simple class containing all necessary information to maintain<br>
* valid time stamps and reuse for skills and items reuse upon re-login.<br> * valid time stamps and reuse for skills and items reuse upon re-login.<br>
* <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b> * <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b>
* @author Yesod, Zoey76 * @author Yesod, Zoey76, Mobius
*/ */
public class TimeStamp public class TimeStamp
{ {
@@ -36,7 +36,7 @@ public class TimeStamp
/** Item or skill reuse time. */ /** Item or skill reuse time. */
private final long _reuse; private final long _reuse;
/** Time stamp. */ /** Time stamp. */
private final long _stamp; private volatile long _stamp;
/** Shared reuse group. */ /** Shared reuse group. */
private final int _group; private final int _group;
@@ -52,7 +52,7 @@ public class TimeStamp
_id2 = skill.getLevel(); _id2 = skill.getLevel();
_id3 = skill.getSubLevel(); _id3 = skill.getSubLevel();
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = -1; _group = -1;
} }
@@ -68,7 +68,7 @@ public class TimeStamp
_id2 = item.getObjectId(); _id2 = item.getObjectId();
_id3 = 0; _id3 = 0;
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = item.getSharedReuseGroup(); _group = item.getSharedReuseGroup();
} }
@@ -151,7 +151,16 @@ public class TimeStamp
*/ */
public long getRemaining() public long getRemaining()
{ {
return Math.max(_stamp - System.currentTimeMillis(), 0); if (_stamp == 0)
{
return 0;
}
final long remainingTime = Math.max(_stamp - System.currentTimeMillis(), 0);
if (remainingTime == 0)
{
_stamp = 0;
}
return remainingTime;
} }
/** /**
@@ -160,6 +169,15 @@ public class TimeStamp
*/ */
public boolean hasNotPassed() public boolean hasNotPassed()
{ {
return System.currentTimeMillis() < _stamp; if (_stamp == 0)
{
return false;
}
final boolean hasNotPassed = System.currentTimeMillis() < _stamp;
if (!hasNotPassed)
{
_stamp = 0;
}
return hasNotPassed;
} }
} }
@@ -211,11 +211,11 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
/** Map containing all skills of this character. */ /** Map containing all skills of this character. */
private final Map<Integer, Skill> _skills = new ConcurrentSkipListMap<>(); private final Map<Integer, Skill> _skills = new ConcurrentSkipListMap<>();
/** Map containing the skill reuse time stamps. */ /** Map containing the skill reuse time stamps. */
private volatile Map<Long, TimeStamp> _reuseTimeStampsSkills = null; private final Map<Long, TimeStamp> _reuseTimeStampsSkills = new ConcurrentHashMap<>();
/** Map containing the item reuse time stamps. */ /** Map containing the item reuse time stamps. */
private volatile Map<Integer, TimeStamp> _reuseTimeStampsItems = null; private final Map<Integer, TimeStamp> _reuseTimeStampsItems = new ConcurrentHashMap<>();
/** Map containing all the disabled skills. */ /** Map containing all the disabled skills. */
private volatile Map<Long, Long> _disabledSkills = null; private final Map<Long, Long> _disabledSkills = new ConcurrentHashMap<>();
private boolean _allSkillsDisabled; private boolean _allSkillsDisabled;
private final byte[] _zones = new byte[ZoneId.getZoneCount()]; private final byte[] _zones = new byte[ZoneId.getZoneCount()];
@@ -1341,16 +1341,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStampItem(ItemInstance item, long reuse, long systime) public void addTimeStampItem(ItemInstance item, long reuse, long systime)
{ {
if (_reuseTimeStampsItems == null)
{
synchronized (this)
{
if (_reuseTimeStampsItems == null)
{
_reuseTimeStampsItems = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime)); _reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime));
} }
@@ -1359,9 +1349,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param itemObjId the item object ID * @param itemObjId the item object ID
* @return if the item has a reuse time stamp, the remaining time, otherwise -1 * @return if the item has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getItemRemainingReuseTime(int itemObjId) public long getItemRemainingReuseTime(int itemObjId)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsItems != null) ? _reuseTimeStampsItems.get(itemObjId) : null; final TimeStamp reuseStamp = _reuseTimeStampsItems.get(itemObjId);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -1372,13 +1362,18 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public long getReuseDelayOnGroup(int group) public long getReuseDelayOnGroup(int group)
{ {
if ((group > 0) && (_reuseTimeStampsItems != null)) if ((group > 0) && !_reuseTimeStampsItems.isEmpty())
{ {
final long currentTime = System.currentTimeMillis();
for (TimeStamp ts : _reuseTimeStampsItems.values()) for (TimeStamp ts : _reuseTimeStampsItems.values())
{ {
if ((ts.getSharedReuseGroup() == group) && ts.hasNotPassed()) if (ts.getSharedReuseGroup() == group)
{ {
return ts.getRemaining(); final long stamp = ts.getStamp();
if (currentTime < stamp)
{
return Math.max(stamp - currentTime, 0);
}
} }
} }
} }
@@ -1413,16 +1408,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStamp(Skill skill, long reuse, long systime) public void addTimeStamp(Skill skill, long reuse, long systime)
{ {
if (_reuseTimeStampsSkills == null)
{
synchronized (this)
{
if (_reuseTimeStampsSkills == null)
{
_reuseTimeStampsSkills = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime)); _reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime));
} }
@@ -1430,33 +1415,27 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* Removes a skill reuse time stamp. * Removes a skill reuse time stamp.
* @param skill the skill to remove * @param skill the skill to remove
*/ */
public synchronized void removeTimeStamp(Skill skill) public void removeTimeStamp(Skill skill)
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.remove(skill.getReuseHashCode()); _reuseTimeStampsSkills.remove(skill.getReuseHashCode());
} }
}
/** /**
* Removes all skill reuse time stamps. * Removes all skill reuse time stamps.
*/ */
public synchronized void resetTimeStamps() public void resetTimeStamps()
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.clear(); _reuseTimeStampsSkills.clear();
} }
}
/** /**
* Gets the skill remaining reuse time for a given skill hash code. * Gets the skill remaining reuse time for a given skill hash code.
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return if the skill has a reuse time stamp, the remaining time, otherwise -1 * @return if the skill has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getSkillRemainingReuseTime(long hashCode) public long getSkillRemainingReuseTime(long hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -1465,9 +1444,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return {@code true} if the skill is under reuse time, {@code false} otherwise * @return {@code true} if the skill is under reuse time, {@code false} otherwise
*/ */
public synchronized boolean hasSkillReuse(long hashCode) public boolean hasSkillReuse(long hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return (reuseStamp != null) && reuseStamp.hasNotPassed(); return (reuseStamp != null) && reuseStamp.hasNotPassed();
} }
@@ -1478,7 +1457,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public synchronized TimeStamp getSkillReuseTimeStamp(long hashCode) public synchronized TimeStamp getSkillReuseTimeStamp(long hashCode)
{ {
return _reuseTimeStampsSkills != null ? _reuseTimeStampsSkills.get(hashCode) : null; return _reuseTimeStampsSkills.get(hashCode);
} }
/** /**
@@ -1496,7 +1475,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void enableSkill(Skill skill) public void enableSkill(Skill skill)
{ {
if ((skill == null) || (_disabledSkills == null)) if (skill == null)
{ {
return; return;
} }
@@ -1515,31 +1494,16 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
{ {
return; return;
} }
if (_disabledSkills == null)
{
synchronized (this)
{
if (_disabledSkills == null)
{
_disabledSkills = new ConcurrentHashMap<>();
}
}
}
_disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE); _disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE);
} }
/** /**
* Removes all the disabled skills. * Removes all the disabled skills.
*/ */
public synchronized void resetDisabledSkills() public void resetDisabledSkills()
{
if (_disabledSkills != null)
{ {
_disabledSkills.clear(); _disabledSkills.clear();
} }
}
/** /**
* Verifies if the skill is disabled. * Verifies if the skill is disabled.
@@ -1569,7 +1533,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
return true; return true;
} }
if (_disabledSkills == null) if (_disabledSkills.isEmpty())
{ {
return false; return false;
} }
@@ -7232,6 +7232,7 @@ public class PlayerInstance extends Playable
int buff_index = 0; int buff_index = 0;
final List<Long> storedSkills = new ArrayList<>(); final List<Long> storedSkills = new ArrayList<>();
final long currentTime = System.currentTimeMillis();
// Store all effect data along with calulated remaining // Store all effect data along with calulated remaining
// reuse delays for matching skills. 'restore_type'= 0. // reuse delays for matching skills. 'restore_type'= 0.
@@ -7291,8 +7292,8 @@ public class PlayerInstance extends Playable
statement.setInt(5, info.getTime()); statement.setInt(5, info.getTime());
final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode()); final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode());
statement.setLong(6, (t != null) && t.hasNotPassed() ? t.getReuse() : 0); statement.setLong(6, (t != null) && (currentTime < t.getStamp()) ? t.getReuse() : 0);
statement.setDouble(7, (t != null) && t.hasNotPassed() ? t.getStamp() : 0); statement.setDouble(7, (t != null) && (currentTime < t.getStamp()) ? t.getStamp() : 0);
statement.setInt(8, 0); // Store type 0, active buffs/debuffs. statement.setInt(8, 0); // Store type 0, active buffs/debuffs.
statement.setInt(9, _classIndex); statement.setInt(9, _classIndex);
@@ -7302,10 +7303,7 @@ public class PlayerInstance extends Playable
} }
// Skills under reuse. // Skills under reuse.
final Map<Long, TimeStamp> reuseTimeStamps = getSkillReuseTimeStamps(); for (Entry<Long, TimeStamp> ts : getSkillReuseTimeStamps().entrySet())
if (reuseTimeStamps != null)
{
for (Entry<Long, TimeStamp> ts : reuseTimeStamps.entrySet())
{ {
final long hash = ts.getKey(); final long hash = ts.getKey();
if (storedSkills.contains(hash)) if (storedSkills.contains(hash))
@@ -7314,7 +7312,7 @@ public class PlayerInstance extends Playable
} }
final TimeStamp t = ts.getValue(); final TimeStamp t = ts.getValue();
if ((t != null) && t.hasNotPassed()) if ((t != null) && (currentTime < t.getStamp()))
{ {
storedSkills.add(hash); storedSkills.add(hash);
@@ -7331,7 +7329,6 @@ public class PlayerInstance extends Playable
statement.addBatch(); statement.addBatch();
} }
} }
}
statement.executeBatch(); statement.executeBatch();
} }
@@ -7351,12 +7348,10 @@ public class PlayerInstance extends Playable
ps1.setInt(1, getObjectId()); ps1.setInt(1, getObjectId());
ps1.execute(); ps1.execute();
final Map<Integer, TimeStamp> itemReuseTimeStamps = getItemReuseTimeStamps(); final long currentTime = System.currentTimeMillis();
if (itemReuseTimeStamps != null) for (TimeStamp ts : getItemReuseTimeStamps().values())
{ {
for (TimeStamp ts : itemReuseTimeStamps.values()) if ((ts != null) && (currentTime < ts.getStamp()))
{
if ((ts != null) && ts.hasNotPassed())
{ {
ps2.setInt(1, getObjectId()); ps2.setInt(1, getObjectId());
ps2.setInt(2, ts.getItemId()); ps2.setInt(2, ts.getItemId());
@@ -7368,7 +7363,6 @@ public class PlayerInstance extends Playable
} }
ps2.executeBatch(); ps2.executeBatch();
} }
}
catch (Exception e) catch (Exception e)
{ {
LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e); LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e);
@@ -18,38 +18,33 @@ package org.l2jmobius.gameserver.network.serverpackets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import org.l2jmobius.commons.network.PacketWriter; import org.l2jmobius.commons.network.PacketWriter;
import org.l2jmobius.gameserver.data.xml.impl.SkillData; import org.l2jmobius.gameserver.data.xml.impl.SkillData;
import org.l2jmobius.gameserver.model.TimeStamp; import org.l2jmobius.gameserver.model.TimeStamp;
import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
import org.l2jmobius.gameserver.model.skills.Skill;
import org.l2jmobius.gameserver.network.OutgoingPackets; import org.l2jmobius.gameserver.network.OutgoingPackets;
/** /**
* Skill Cool Time server packet implementation. * Skill Cool Time server packet implementation.
* @author KenM, Zoey76 * @author KenM, Zoey76, Mobius
*/ */
public class SkillCoolTime implements IClientOutgoingPacket public class SkillCoolTime implements IClientOutgoingPacket
{ {
private final long _currentTime;
private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>(); private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>();
public SkillCoolTime(PlayerInstance player) public SkillCoolTime(PlayerInstance player)
{ {
final Map<Long, TimeStamp> skillReuseTimeStamps = player.getSkillReuseTimeStamps(); _currentTime = System.currentTimeMillis();
if (skillReuseTimeStamps != null) for (TimeStamp ts : player.getSkillReuseTimeStamps().values())
{ {
for (TimeStamp ts : skillReuseTimeStamps.values()) if ((_currentTime < ts.getStamp()) && !SkillData.getInstance().getSkill(ts.getSkillId(), ts.getSkillLvl(), ts.getSkillSubLvl()).isNotBroadcastable())
{
final Skill skill = SkillData.getInstance().getSkill(ts.getSkillId(), ts.getSkillLvl(), ts.getSkillSubLvl());
if (ts.hasNotPassed() && !skill.isNotBroadcastable())
{ {
_skillReuseTimeStamps.add(ts); _skillReuseTimeStamps.add(ts);
} }
} }
} }
}
@Override @Override
public boolean write(PacketWriter packet) public boolean write(PacketWriter packet)
@@ -62,7 +57,7 @@ public class SkillCoolTime implements IClientOutgoingPacket
packet.writeD(ts.getSkillId()); packet.writeD(ts.getSkillId());
packet.writeD(0x00); // ? packet.writeD(0x00); // ?
packet.writeD((int) ts.getReuse() / 1000); packet.writeD((int) ts.getReuse() / 1000);
packet.writeD((int) ts.getRemaining() / 1000); packet.writeD((int) Math.max(ts.getStamp() - _currentTime, 0) / 1000);
} }
return true; return true;
} }
@@ -23,7 +23,7 @@ import org.l2jmobius.gameserver.model.skills.Skill;
* Simple class containing all necessary information to maintain<br> * Simple class containing all necessary information to maintain<br>
* valid time stamps and reuse for skills and items reuse upon re-login.<br> * valid time stamps and reuse for skills and items reuse upon re-login.<br>
* <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b> * <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b>
* @author Yesod, Zoey76 * @author Yesod, Zoey76, Mobius
*/ */
public class TimeStamp public class TimeStamp
{ {
@@ -36,7 +36,7 @@ public class TimeStamp
/** Item or skill reuse time. */ /** Item or skill reuse time. */
private final long _reuse; private final long _reuse;
/** Time stamp. */ /** Time stamp. */
private final long _stamp; private volatile long _stamp;
/** Shared reuse group. */ /** Shared reuse group. */
private final int _group; private final int _group;
@@ -52,7 +52,7 @@ public class TimeStamp
_id2 = skill.getLevel(); _id2 = skill.getLevel();
_id3 = skill.getSubLevel(); _id3 = skill.getSubLevel();
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = -1; _group = -1;
} }
@@ -68,7 +68,7 @@ public class TimeStamp
_id2 = item.getObjectId(); _id2 = item.getObjectId();
_id3 = 0; _id3 = 0;
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = item.getSharedReuseGroup(); _group = item.getSharedReuseGroup();
} }
@@ -151,7 +151,16 @@ public class TimeStamp
*/ */
public long getRemaining() public long getRemaining()
{ {
return Math.max(_stamp - System.currentTimeMillis(), 0); if (_stamp == 0)
{
return 0;
}
final long remainingTime = Math.max(_stamp - System.currentTimeMillis(), 0);
if (remainingTime == 0)
{
_stamp = 0;
}
return remainingTime;
} }
/** /**
@@ -160,6 +169,15 @@ public class TimeStamp
*/ */
public boolean hasNotPassed() public boolean hasNotPassed()
{ {
return System.currentTimeMillis() < _stamp; if (_stamp == 0)
{
return false;
}
final boolean hasNotPassed = System.currentTimeMillis() < _stamp;
if (!hasNotPassed)
{
_stamp = 0;
}
return hasNotPassed;
} }
} }
@@ -211,11 +211,11 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
/** Map containing all skills of this character. */ /** Map containing all skills of this character. */
private final Map<Integer, Skill> _skills = new ConcurrentSkipListMap<>(); private final Map<Integer, Skill> _skills = new ConcurrentSkipListMap<>();
/** Map containing the skill reuse time stamps. */ /** Map containing the skill reuse time stamps. */
private volatile Map<Long, TimeStamp> _reuseTimeStampsSkills = null; private final Map<Long, TimeStamp> _reuseTimeStampsSkills = new ConcurrentHashMap<>();
/** Map containing the item reuse time stamps. */ /** Map containing the item reuse time stamps. */
private volatile Map<Integer, TimeStamp> _reuseTimeStampsItems = null; private final Map<Integer, TimeStamp> _reuseTimeStampsItems = new ConcurrentHashMap<>();
/** Map containing all the disabled skills. */ /** Map containing all the disabled skills. */
private volatile Map<Long, Long> _disabledSkills = null; private final Map<Long, Long> _disabledSkills = new ConcurrentHashMap<>();
private boolean _allSkillsDisabled; private boolean _allSkillsDisabled;
private final byte[] _zones = new byte[ZoneId.getZoneCount()]; private final byte[] _zones = new byte[ZoneId.getZoneCount()];
@@ -1341,16 +1341,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStampItem(ItemInstance item, long reuse, long systime) public void addTimeStampItem(ItemInstance item, long reuse, long systime)
{ {
if (_reuseTimeStampsItems == null)
{
synchronized (this)
{
if (_reuseTimeStampsItems == null)
{
_reuseTimeStampsItems = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime)); _reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime));
} }
@@ -1359,9 +1349,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param itemObjId the item object ID * @param itemObjId the item object ID
* @return if the item has a reuse time stamp, the remaining time, otherwise -1 * @return if the item has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getItemRemainingReuseTime(int itemObjId) public long getItemRemainingReuseTime(int itemObjId)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsItems != null) ? _reuseTimeStampsItems.get(itemObjId) : null; final TimeStamp reuseStamp = _reuseTimeStampsItems.get(itemObjId);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -1372,13 +1362,18 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public long getReuseDelayOnGroup(int group) public long getReuseDelayOnGroup(int group)
{ {
if ((group > 0) && (_reuseTimeStampsItems != null)) if ((group > 0) && !_reuseTimeStampsItems.isEmpty())
{ {
final long currentTime = System.currentTimeMillis();
for (TimeStamp ts : _reuseTimeStampsItems.values()) for (TimeStamp ts : _reuseTimeStampsItems.values())
{ {
if ((ts.getSharedReuseGroup() == group) && ts.hasNotPassed()) if (ts.getSharedReuseGroup() == group)
{ {
return ts.getRemaining(); final long stamp = ts.getStamp();
if (currentTime < stamp)
{
return Math.max(stamp - currentTime, 0);
}
} }
} }
} }
@@ -1413,16 +1408,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStamp(Skill skill, long reuse, long systime) public void addTimeStamp(Skill skill, long reuse, long systime)
{ {
if (_reuseTimeStampsSkills == null)
{
synchronized (this)
{
if (_reuseTimeStampsSkills == null)
{
_reuseTimeStampsSkills = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime)); _reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime));
} }
@@ -1430,33 +1415,27 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* Removes a skill reuse time stamp. * Removes a skill reuse time stamp.
* @param skill the skill to remove * @param skill the skill to remove
*/ */
public synchronized void removeTimeStamp(Skill skill) public void removeTimeStamp(Skill skill)
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.remove(skill.getReuseHashCode()); _reuseTimeStampsSkills.remove(skill.getReuseHashCode());
} }
}
/** /**
* Removes all skill reuse time stamps. * Removes all skill reuse time stamps.
*/ */
public synchronized void resetTimeStamps() public void resetTimeStamps()
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.clear(); _reuseTimeStampsSkills.clear();
} }
}
/** /**
* Gets the skill remaining reuse time for a given skill hash code. * Gets the skill remaining reuse time for a given skill hash code.
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return if the skill has a reuse time stamp, the remaining time, otherwise -1 * @return if the skill has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getSkillRemainingReuseTime(long hashCode) public long getSkillRemainingReuseTime(long hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -1465,9 +1444,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return {@code true} if the skill is under reuse time, {@code false} otherwise * @return {@code true} if the skill is under reuse time, {@code false} otherwise
*/ */
public synchronized boolean hasSkillReuse(long hashCode) public boolean hasSkillReuse(long hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return (reuseStamp != null) && reuseStamp.hasNotPassed(); return (reuseStamp != null) && reuseStamp.hasNotPassed();
} }
@@ -1478,7 +1457,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public synchronized TimeStamp getSkillReuseTimeStamp(long hashCode) public synchronized TimeStamp getSkillReuseTimeStamp(long hashCode)
{ {
return _reuseTimeStampsSkills != null ? _reuseTimeStampsSkills.get(hashCode) : null; return _reuseTimeStampsSkills.get(hashCode);
} }
/** /**
@@ -1496,7 +1475,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void enableSkill(Skill skill) public void enableSkill(Skill skill)
{ {
if ((skill == null) || (_disabledSkills == null)) if (skill == null)
{ {
return; return;
} }
@@ -1515,31 +1494,16 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
{ {
return; return;
} }
if (_disabledSkills == null)
{
synchronized (this)
{
if (_disabledSkills == null)
{
_disabledSkills = new ConcurrentHashMap<>();
}
}
}
_disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE); _disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE);
} }
/** /**
* Removes all the disabled skills. * Removes all the disabled skills.
*/ */
public synchronized void resetDisabledSkills() public void resetDisabledSkills()
{
if (_disabledSkills != null)
{ {
_disabledSkills.clear(); _disabledSkills.clear();
} }
}
/** /**
* Verifies if the skill is disabled. * Verifies if the skill is disabled.
@@ -1569,7 +1533,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
return true; return true;
} }
if (_disabledSkills == null) if (_disabledSkills.isEmpty())
{ {
return false; return false;
} }
@@ -7217,6 +7217,7 @@ public class PlayerInstance extends Playable
int buff_index = 0; int buff_index = 0;
final List<Long> storedSkills = new ArrayList<>(); final List<Long> storedSkills = new ArrayList<>();
final long currentTime = System.currentTimeMillis();
// Store all effect data along with calulated remaining // Store all effect data along with calulated remaining
// reuse delays for matching skills. 'restore_type'= 0. // reuse delays for matching skills. 'restore_type'= 0.
@@ -7276,8 +7277,8 @@ public class PlayerInstance extends Playable
statement.setInt(5, info.getTime()); statement.setInt(5, info.getTime());
final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode()); final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode());
statement.setLong(6, (t != null) && t.hasNotPassed() ? t.getReuse() : 0); statement.setLong(6, (t != null) && (currentTime < t.getStamp()) ? t.getReuse() : 0);
statement.setDouble(7, (t != null) && t.hasNotPassed() ? t.getStamp() : 0); statement.setDouble(7, (t != null) && (currentTime < t.getStamp()) ? t.getStamp() : 0);
statement.setInt(8, 0); // Store type 0, active buffs/debuffs. statement.setInt(8, 0); // Store type 0, active buffs/debuffs.
statement.setInt(9, _classIndex); statement.setInt(9, _classIndex);
@@ -7287,10 +7288,7 @@ public class PlayerInstance extends Playable
} }
// Skills under reuse. // Skills under reuse.
final Map<Long, TimeStamp> reuseTimeStamps = getSkillReuseTimeStamps(); for (Entry<Long, TimeStamp> ts : getSkillReuseTimeStamps().entrySet())
if (reuseTimeStamps != null)
{
for (Entry<Long, TimeStamp> ts : reuseTimeStamps.entrySet())
{ {
final long hash = ts.getKey(); final long hash = ts.getKey();
if (storedSkills.contains(hash)) if (storedSkills.contains(hash))
@@ -7299,7 +7297,7 @@ public class PlayerInstance extends Playable
} }
final TimeStamp t = ts.getValue(); final TimeStamp t = ts.getValue();
if ((t != null) && t.hasNotPassed()) if ((t != null) && (currentTime < t.getStamp()))
{ {
storedSkills.add(hash); storedSkills.add(hash);
@@ -7316,7 +7314,6 @@ public class PlayerInstance extends Playable
statement.addBatch(); statement.addBatch();
} }
} }
}
statement.executeBatch(); statement.executeBatch();
} }
@@ -7336,12 +7333,10 @@ public class PlayerInstance extends Playable
ps1.setInt(1, getObjectId()); ps1.setInt(1, getObjectId());
ps1.execute(); ps1.execute();
final Map<Integer, TimeStamp> itemReuseTimeStamps = getItemReuseTimeStamps(); final long currentTime = System.currentTimeMillis();
if (itemReuseTimeStamps != null) for (TimeStamp ts : getItemReuseTimeStamps().values())
{ {
for (TimeStamp ts : itemReuseTimeStamps.values()) if ((ts != null) && (currentTime < ts.getStamp()))
{
if ((ts != null) && ts.hasNotPassed())
{ {
ps2.setInt(1, getObjectId()); ps2.setInt(1, getObjectId());
ps2.setInt(2, ts.getItemId()); ps2.setInt(2, ts.getItemId());
@@ -7353,7 +7348,6 @@ public class PlayerInstance extends Playable
} }
ps2.executeBatch(); ps2.executeBatch();
} }
}
catch (Exception e) catch (Exception e)
{ {
LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e); LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e);
@@ -18,38 +18,33 @@ package org.l2jmobius.gameserver.network.serverpackets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import org.l2jmobius.commons.network.PacketWriter; import org.l2jmobius.commons.network.PacketWriter;
import org.l2jmobius.gameserver.data.xml.impl.SkillData; import org.l2jmobius.gameserver.data.xml.impl.SkillData;
import org.l2jmobius.gameserver.model.TimeStamp; import org.l2jmobius.gameserver.model.TimeStamp;
import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
import org.l2jmobius.gameserver.model.skills.Skill;
import org.l2jmobius.gameserver.network.OutgoingPackets; import org.l2jmobius.gameserver.network.OutgoingPackets;
/** /**
* Skill Cool Time server packet implementation. * Skill Cool Time server packet implementation.
* @author KenM, Zoey76 * @author KenM, Zoey76, Mobius
*/ */
public class SkillCoolTime implements IClientOutgoingPacket public class SkillCoolTime implements IClientOutgoingPacket
{ {
private final long _currentTime;
private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>(); private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>();
public SkillCoolTime(PlayerInstance player) public SkillCoolTime(PlayerInstance player)
{ {
final Map<Long, TimeStamp> skillReuseTimeStamps = player.getSkillReuseTimeStamps(); _currentTime = System.currentTimeMillis();
if (skillReuseTimeStamps != null) for (TimeStamp ts : player.getSkillReuseTimeStamps().values())
{ {
for (TimeStamp ts : skillReuseTimeStamps.values()) if ((_currentTime < ts.getStamp()) && !SkillData.getInstance().getSkill(ts.getSkillId(), ts.getSkillLvl(), ts.getSkillSubLvl()).isNotBroadcastable())
{
final Skill skill = SkillData.getInstance().getSkill(ts.getSkillId(), ts.getSkillLvl(), ts.getSkillSubLvl());
if (ts.hasNotPassed() && !skill.isNotBroadcastable())
{ {
_skillReuseTimeStamps.add(ts); _skillReuseTimeStamps.add(ts);
} }
} }
} }
}
@Override @Override
public boolean write(PacketWriter packet) public boolean write(PacketWriter packet)
@@ -62,7 +57,7 @@ public class SkillCoolTime implements IClientOutgoingPacket
packet.writeD(ts.getSkillId()); packet.writeD(ts.getSkillId());
packet.writeD(0x00); // ? packet.writeD(0x00); // ?
packet.writeD((int) ts.getReuse() / 1000); packet.writeD((int) ts.getReuse() / 1000);
packet.writeD((int) ts.getRemaining() / 1000); packet.writeD((int) Math.max(ts.getStamp() - _currentTime, 0) / 1000);
} }
return true; return true;
} }
@@ -23,7 +23,7 @@ import org.l2jmobius.gameserver.model.skills.Skill;
* Simple class containing all necessary information to maintain<br> * Simple class containing all necessary information to maintain<br>
* valid time stamps and reuse for skills and items reuse upon re-login.<br> * valid time stamps and reuse for skills and items reuse upon re-login.<br>
* <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b> * <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b>
* @author Yesod, Zoey76 * @author Yesod, Zoey76, Mobius
*/ */
public class TimeStamp public class TimeStamp
{ {
@@ -36,7 +36,7 @@ public class TimeStamp
/** Item or skill reuse time. */ /** Item or skill reuse time. */
private final long _reuse; private final long _reuse;
/** Time stamp. */ /** Time stamp. */
private final long _stamp; private volatile long _stamp;
/** Shared reuse group. */ /** Shared reuse group. */
private final int _group; private final int _group;
@@ -52,7 +52,7 @@ public class TimeStamp
_id2 = skill.getLevel(); _id2 = skill.getLevel();
_id3 = skill.getSubLevel(); _id3 = skill.getSubLevel();
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = -1; _group = -1;
} }
@@ -68,7 +68,7 @@ public class TimeStamp
_id2 = item.getObjectId(); _id2 = item.getObjectId();
_id3 = 0; _id3 = 0;
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = item.getSharedReuseGroup(); _group = item.getSharedReuseGroup();
} }
@@ -151,7 +151,16 @@ public class TimeStamp
*/ */
public long getRemaining() public long getRemaining()
{ {
return Math.max(_stamp - System.currentTimeMillis(), 0); if (_stamp == 0)
{
return 0;
}
final long remainingTime = Math.max(_stamp - System.currentTimeMillis(), 0);
if (remainingTime == 0)
{
_stamp = 0;
}
return remainingTime;
} }
/** /**
@@ -160,6 +169,15 @@ public class TimeStamp
*/ */
public boolean hasNotPassed() public boolean hasNotPassed()
{ {
return System.currentTimeMillis() < _stamp; if (_stamp == 0)
{
return false;
}
final boolean hasNotPassed = System.currentTimeMillis() < _stamp;
if (!hasNotPassed)
{
_stamp = 0;
}
return hasNotPassed;
} }
} }
@@ -212,11 +212,11 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
/** Map containing all skills of this character. */ /** Map containing all skills of this character. */
private final Map<Integer, Skill> _skills = new ConcurrentSkipListMap<>(); private final Map<Integer, Skill> _skills = new ConcurrentSkipListMap<>();
/** Map containing the skill reuse time stamps. */ /** Map containing the skill reuse time stamps. */
private volatile Map<Long, TimeStamp> _reuseTimeStampsSkills = null; private final Map<Long, TimeStamp> _reuseTimeStampsSkills = new ConcurrentHashMap<>();
/** Map containing the item reuse time stamps. */ /** Map containing the item reuse time stamps. */
private volatile Map<Integer, TimeStamp> _reuseTimeStampsItems = null; private final Map<Integer, TimeStamp> _reuseTimeStampsItems = new ConcurrentHashMap<>();
/** Map containing all the disabled skills. */ /** Map containing all the disabled skills. */
private volatile Map<Long, Long> _disabledSkills = null; private final Map<Long, Long> _disabledSkills = new ConcurrentHashMap<>();
private boolean _allSkillsDisabled; private boolean _allSkillsDisabled;
private final byte[] _zones = new byte[ZoneId.getZoneCount()]; private final byte[] _zones = new byte[ZoneId.getZoneCount()];
@@ -1342,16 +1342,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStampItem(ItemInstance item, long reuse, long systime) public void addTimeStampItem(ItemInstance item, long reuse, long systime)
{ {
if (_reuseTimeStampsItems == null)
{
synchronized (this)
{
if (_reuseTimeStampsItems == null)
{
_reuseTimeStampsItems = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime)); _reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime));
} }
@@ -1360,9 +1350,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param itemObjId the item object ID * @param itemObjId the item object ID
* @return if the item has a reuse time stamp, the remaining time, otherwise -1 * @return if the item has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getItemRemainingReuseTime(int itemObjId) public long getItemRemainingReuseTime(int itemObjId)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsItems != null) ? _reuseTimeStampsItems.get(itemObjId) : null; final TimeStamp reuseStamp = _reuseTimeStampsItems.get(itemObjId);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -1373,13 +1363,18 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public long getReuseDelayOnGroup(int group) public long getReuseDelayOnGroup(int group)
{ {
if ((group > 0) && (_reuseTimeStampsItems != null)) if ((group > 0) && !_reuseTimeStampsItems.isEmpty())
{ {
final long currentTime = System.currentTimeMillis();
for (TimeStamp ts : _reuseTimeStampsItems.values()) for (TimeStamp ts : _reuseTimeStampsItems.values())
{ {
if ((ts.getSharedReuseGroup() == group) && ts.hasNotPassed()) if (ts.getSharedReuseGroup() == group)
{ {
return ts.getRemaining(); final long stamp = ts.getStamp();
if (currentTime < stamp)
{
return Math.max(stamp - currentTime, 0);
}
} }
} }
} }
@@ -1414,16 +1409,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStamp(Skill skill, long reuse, long systime) public void addTimeStamp(Skill skill, long reuse, long systime)
{ {
if (_reuseTimeStampsSkills == null)
{
synchronized (this)
{
if (_reuseTimeStampsSkills == null)
{
_reuseTimeStampsSkills = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime)); _reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime));
} }
@@ -1431,33 +1416,27 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* Removes a skill reuse time stamp. * Removes a skill reuse time stamp.
* @param skill the skill to remove * @param skill the skill to remove
*/ */
public synchronized void removeTimeStamp(Skill skill) public void removeTimeStamp(Skill skill)
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.remove(skill.getReuseHashCode()); _reuseTimeStampsSkills.remove(skill.getReuseHashCode());
} }
}
/** /**
* Removes all skill reuse time stamps. * Removes all skill reuse time stamps.
*/ */
public synchronized void resetTimeStamps() public void resetTimeStamps()
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.clear(); _reuseTimeStampsSkills.clear();
} }
}
/** /**
* Gets the skill remaining reuse time for a given skill hash code. * Gets the skill remaining reuse time for a given skill hash code.
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return if the skill has a reuse time stamp, the remaining time, otherwise -1 * @return if the skill has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getSkillRemainingReuseTime(long hashCode) public long getSkillRemainingReuseTime(long hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -1466,9 +1445,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return {@code true} if the skill is under reuse time, {@code false} otherwise * @return {@code true} if the skill is under reuse time, {@code false} otherwise
*/ */
public synchronized boolean hasSkillReuse(long hashCode) public boolean hasSkillReuse(long hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return (reuseStamp != null) && reuseStamp.hasNotPassed(); return (reuseStamp != null) && reuseStamp.hasNotPassed();
} }
@@ -1479,7 +1458,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public synchronized TimeStamp getSkillReuseTimeStamp(long hashCode) public synchronized TimeStamp getSkillReuseTimeStamp(long hashCode)
{ {
return _reuseTimeStampsSkills != null ? _reuseTimeStampsSkills.get(hashCode) : null; return _reuseTimeStampsSkills.get(hashCode);
} }
/** /**
@@ -1497,7 +1476,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void enableSkill(Skill skill) public void enableSkill(Skill skill)
{ {
if ((skill == null) || (_disabledSkills == null)) if (skill == null)
{ {
return; return;
} }
@@ -1516,31 +1495,16 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
{ {
return; return;
} }
if (_disabledSkills == null)
{
synchronized (this)
{
if (_disabledSkills == null)
{
_disabledSkills = new ConcurrentHashMap<>();
}
}
}
_disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE); _disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE);
} }
/** /**
* Removes all the disabled skills. * Removes all the disabled skills.
*/ */
public synchronized void resetDisabledSkills() public void resetDisabledSkills()
{
if (_disabledSkills != null)
{ {
_disabledSkills.clear(); _disabledSkills.clear();
} }
}
/** /**
* Verifies if the skill is disabled. * Verifies if the skill is disabled.
@@ -1570,7 +1534,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
return true; return true;
} }
if (_disabledSkills == null) if (_disabledSkills.isEmpty())
{ {
return false; return false;
} }
@@ -7244,6 +7244,7 @@ public class PlayerInstance extends Playable
int buff_index = 0; int buff_index = 0;
final List<Long> storedSkills = new ArrayList<>(); final List<Long> storedSkills = new ArrayList<>();
final long currentTime = System.currentTimeMillis();
// Store all effect data along with calulated remaining // Store all effect data along with calulated remaining
// reuse delays for matching skills. 'restore_type'= 0. // reuse delays for matching skills. 'restore_type'= 0.
@@ -7303,8 +7304,8 @@ public class PlayerInstance extends Playable
statement.setInt(5, info.getTime()); statement.setInt(5, info.getTime());
final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode()); final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode());
statement.setLong(6, (t != null) && t.hasNotPassed() ? t.getReuse() : 0); statement.setLong(6, (t != null) && (currentTime < t.getStamp()) ? t.getReuse() : 0);
statement.setDouble(7, (t != null) && t.hasNotPassed() ? t.getStamp() : 0); statement.setDouble(7, (t != null) && (currentTime < t.getStamp()) ? t.getStamp() : 0);
statement.setInt(8, 0); // Store type 0, active buffs/debuffs. statement.setInt(8, 0); // Store type 0, active buffs/debuffs.
statement.setInt(9, _classIndex); statement.setInt(9, _classIndex);
@@ -7314,10 +7315,7 @@ public class PlayerInstance extends Playable
} }
// Skills under reuse. // Skills under reuse.
final Map<Long, TimeStamp> reuseTimeStamps = getSkillReuseTimeStamps(); for (Entry<Long, TimeStamp> ts : getSkillReuseTimeStamps().entrySet())
if (reuseTimeStamps != null)
{
for (Entry<Long, TimeStamp> ts : reuseTimeStamps.entrySet())
{ {
final long hash = ts.getKey(); final long hash = ts.getKey();
if (storedSkills.contains(hash)) if (storedSkills.contains(hash))
@@ -7326,7 +7324,7 @@ public class PlayerInstance extends Playable
} }
final TimeStamp t = ts.getValue(); final TimeStamp t = ts.getValue();
if ((t != null) && t.hasNotPassed()) if ((t != null) && (currentTime < t.getStamp()))
{ {
storedSkills.add(hash); storedSkills.add(hash);
@@ -7343,7 +7341,6 @@ public class PlayerInstance extends Playable
statement.addBatch(); statement.addBatch();
} }
} }
}
statement.executeBatch(); statement.executeBatch();
} }
@@ -7363,12 +7360,10 @@ public class PlayerInstance extends Playable
ps1.setInt(1, getObjectId()); ps1.setInt(1, getObjectId());
ps1.execute(); ps1.execute();
final Map<Integer, TimeStamp> itemReuseTimeStamps = getItemReuseTimeStamps(); final long currentTime = System.currentTimeMillis();
if (itemReuseTimeStamps != null) for (TimeStamp ts : getItemReuseTimeStamps().values())
{ {
for (TimeStamp ts : itemReuseTimeStamps.values()) if ((ts != null) && (currentTime < ts.getStamp()))
{
if ((ts != null) && ts.hasNotPassed())
{ {
ps2.setInt(1, getObjectId()); ps2.setInt(1, getObjectId());
ps2.setInt(2, ts.getItemId()); ps2.setInt(2, ts.getItemId());
@@ -7380,7 +7375,6 @@ public class PlayerInstance extends Playable
} }
ps2.executeBatch(); ps2.executeBatch();
} }
}
catch (Exception e) catch (Exception e)
{ {
LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e); LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e);
@@ -18,38 +18,33 @@ package org.l2jmobius.gameserver.network.serverpackets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import org.l2jmobius.commons.network.PacketWriter; import org.l2jmobius.commons.network.PacketWriter;
import org.l2jmobius.gameserver.data.xml.impl.SkillData; import org.l2jmobius.gameserver.data.xml.impl.SkillData;
import org.l2jmobius.gameserver.model.TimeStamp; import org.l2jmobius.gameserver.model.TimeStamp;
import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
import org.l2jmobius.gameserver.model.skills.Skill;
import org.l2jmobius.gameserver.network.OutgoingPackets; import org.l2jmobius.gameserver.network.OutgoingPackets;
/** /**
* Skill Cool Time server packet implementation. * Skill Cool Time server packet implementation.
* @author KenM, Zoey76 * @author KenM, Zoey76, Mobius
*/ */
public class SkillCoolTime implements IClientOutgoingPacket public class SkillCoolTime implements IClientOutgoingPacket
{ {
private final long _currentTime;
private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>(); private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>();
public SkillCoolTime(PlayerInstance player) public SkillCoolTime(PlayerInstance player)
{ {
final Map<Long, TimeStamp> skillReuseTimeStamps = player.getSkillReuseTimeStamps(); _currentTime = System.currentTimeMillis();
if (skillReuseTimeStamps != null) for (TimeStamp ts : player.getSkillReuseTimeStamps().values())
{ {
for (TimeStamp ts : skillReuseTimeStamps.values()) if ((_currentTime < ts.getStamp()) && !SkillData.getInstance().getSkill(ts.getSkillId(), ts.getSkillLvl(), ts.getSkillSubLvl()).isNotBroadcastable())
{
final Skill skill = SkillData.getInstance().getSkill(ts.getSkillId(), ts.getSkillLvl(), ts.getSkillSubLvl());
if (ts.hasNotPassed() && !skill.isNotBroadcastable())
{ {
_skillReuseTimeStamps.add(ts); _skillReuseTimeStamps.add(ts);
} }
} }
} }
}
@Override @Override
public boolean write(PacketWriter packet) public boolean write(PacketWriter packet)
@@ -62,7 +57,7 @@ public class SkillCoolTime implements IClientOutgoingPacket
packet.writeD(ts.getSkillId()); packet.writeD(ts.getSkillId());
packet.writeD(0x00); // ? packet.writeD(0x00); // ?
packet.writeD((int) ts.getReuse() / 1000); packet.writeD((int) ts.getReuse() / 1000);
packet.writeD((int) ts.getRemaining() / 1000); packet.writeD((int) Math.max(ts.getStamp() - _currentTime, 0) / 1000);
} }
return true; return true;
} }
@@ -23,7 +23,7 @@ import org.l2jmobius.gameserver.model.skills.Skill;
* Simple class containing all necessary information to maintain<br> * Simple class containing all necessary information to maintain<br>
* valid time stamps and reuse for skills and items reuse upon re-login.<br> * valid time stamps and reuse for skills and items reuse upon re-login.<br>
* <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b> * <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b>
* @author Yesod, Zoey76 * @author Yesod, Zoey76, Mobius
*/ */
public class TimeStamp public class TimeStamp
{ {
@@ -36,7 +36,7 @@ public class TimeStamp
/** Item or skill reuse time. */ /** Item or skill reuse time. */
private final long _reuse; private final long _reuse;
/** Time stamp. */ /** Time stamp. */
private final long _stamp; private volatile long _stamp;
/** Shared reuse group. */ /** Shared reuse group. */
private final int _group; private final int _group;
@@ -52,7 +52,7 @@ public class TimeStamp
_id2 = skill.getLevel(); _id2 = skill.getLevel();
_id3 = skill.getSubLevel(); _id3 = skill.getSubLevel();
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = -1; _group = -1;
} }
@@ -68,7 +68,7 @@ public class TimeStamp
_id2 = item.getObjectId(); _id2 = item.getObjectId();
_id3 = 0; _id3 = 0;
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = item.getSharedReuseGroup(); _group = item.getSharedReuseGroup();
} }
@@ -151,7 +151,16 @@ public class TimeStamp
*/ */
public long getRemaining() public long getRemaining()
{ {
return Math.max(_stamp - System.currentTimeMillis(), 0); if (_stamp == 0)
{
return 0;
}
final long remainingTime = Math.max(_stamp - System.currentTimeMillis(), 0);
if (remainingTime == 0)
{
_stamp = 0;
}
return remainingTime;
} }
/** /**
@@ -160,6 +169,15 @@ public class TimeStamp
*/ */
public boolean hasNotPassed() public boolean hasNotPassed()
{ {
return System.currentTimeMillis() < _stamp; if (_stamp == 0)
{
return false;
}
final boolean hasNotPassed = System.currentTimeMillis() < _stamp;
if (!hasNotPassed)
{
_stamp = 0;
}
return hasNotPassed;
} }
} }
@@ -212,11 +212,11 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
/** Map containing all skills of this character. */ /** Map containing all skills of this character. */
private final Map<Integer, Skill> _skills = new ConcurrentSkipListMap<>(); private final Map<Integer, Skill> _skills = new ConcurrentSkipListMap<>();
/** Map containing the skill reuse time stamps. */ /** Map containing the skill reuse time stamps. */
private volatile Map<Long, TimeStamp> _reuseTimeStampsSkills = null; private final Map<Long, TimeStamp> _reuseTimeStampsSkills = new ConcurrentHashMap<>();
/** Map containing the item reuse time stamps. */ /** Map containing the item reuse time stamps. */
private volatile Map<Integer, TimeStamp> _reuseTimeStampsItems = null; private final Map<Integer, TimeStamp> _reuseTimeStampsItems = new ConcurrentHashMap<>();
/** Map containing all the disabled skills. */ /** Map containing all the disabled skills. */
private volatile Map<Long, Long> _disabledSkills = null; private final Map<Long, Long> _disabledSkills = new ConcurrentHashMap<>();
private boolean _allSkillsDisabled; private boolean _allSkillsDisabled;
private final byte[] _zones = new byte[ZoneId.getZoneCount()]; private final byte[] _zones = new byte[ZoneId.getZoneCount()];
@@ -1342,16 +1342,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStampItem(ItemInstance item, long reuse, long systime) public void addTimeStampItem(ItemInstance item, long reuse, long systime)
{ {
if (_reuseTimeStampsItems == null)
{
synchronized (this)
{
if (_reuseTimeStampsItems == null)
{
_reuseTimeStampsItems = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime)); _reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime));
} }
@@ -1360,9 +1350,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param itemObjId the item object ID * @param itemObjId the item object ID
* @return if the item has a reuse time stamp, the remaining time, otherwise -1 * @return if the item has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getItemRemainingReuseTime(int itemObjId) public long getItemRemainingReuseTime(int itemObjId)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsItems != null) ? _reuseTimeStampsItems.get(itemObjId) : null; final TimeStamp reuseStamp = _reuseTimeStampsItems.get(itemObjId);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -1373,13 +1363,18 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public long getReuseDelayOnGroup(int group) public long getReuseDelayOnGroup(int group)
{ {
if ((group > 0) && (_reuseTimeStampsItems != null)) if ((group > 0) && !_reuseTimeStampsItems.isEmpty())
{ {
final long currentTime = System.currentTimeMillis();
for (TimeStamp ts : _reuseTimeStampsItems.values()) for (TimeStamp ts : _reuseTimeStampsItems.values())
{ {
if ((ts.getSharedReuseGroup() == group) && ts.hasNotPassed()) if (ts.getSharedReuseGroup() == group)
{ {
return ts.getRemaining(); final long stamp = ts.getStamp();
if (currentTime < stamp)
{
return Math.max(stamp - currentTime, 0);
}
} }
} }
} }
@@ -1414,16 +1409,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStamp(Skill skill, long reuse, long systime) public void addTimeStamp(Skill skill, long reuse, long systime)
{ {
if (_reuseTimeStampsSkills == null)
{
synchronized (this)
{
if (_reuseTimeStampsSkills == null)
{
_reuseTimeStampsSkills = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime)); _reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime));
} }
@@ -1431,33 +1416,27 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* Removes a skill reuse time stamp. * Removes a skill reuse time stamp.
* @param skill the skill to remove * @param skill the skill to remove
*/ */
public synchronized void removeTimeStamp(Skill skill) public void removeTimeStamp(Skill skill)
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.remove(skill.getReuseHashCode()); _reuseTimeStampsSkills.remove(skill.getReuseHashCode());
} }
}
/** /**
* Removes all skill reuse time stamps. * Removes all skill reuse time stamps.
*/ */
public synchronized void resetTimeStamps() public void resetTimeStamps()
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.clear(); _reuseTimeStampsSkills.clear();
} }
}
/** /**
* Gets the skill remaining reuse time for a given skill hash code. * Gets the skill remaining reuse time for a given skill hash code.
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return if the skill has a reuse time stamp, the remaining time, otherwise -1 * @return if the skill has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getSkillRemainingReuseTime(long hashCode) public long getSkillRemainingReuseTime(long hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -1466,9 +1445,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return {@code true} if the skill is under reuse time, {@code false} otherwise * @return {@code true} if the skill is under reuse time, {@code false} otherwise
*/ */
public synchronized boolean hasSkillReuse(long hashCode) public boolean hasSkillReuse(long hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return (reuseStamp != null) && reuseStamp.hasNotPassed(); return (reuseStamp != null) && reuseStamp.hasNotPassed();
} }
@@ -1479,7 +1458,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public synchronized TimeStamp getSkillReuseTimeStamp(long hashCode) public synchronized TimeStamp getSkillReuseTimeStamp(long hashCode)
{ {
return _reuseTimeStampsSkills != null ? _reuseTimeStampsSkills.get(hashCode) : null; return _reuseTimeStampsSkills.get(hashCode);
} }
/** /**
@@ -1497,7 +1476,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void enableSkill(Skill skill) public void enableSkill(Skill skill)
{ {
if ((skill == null) || (_disabledSkills == null)) if (skill == null)
{ {
return; return;
} }
@@ -1516,31 +1495,16 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
{ {
return; return;
} }
if (_disabledSkills == null)
{
synchronized (this)
{
if (_disabledSkills == null)
{
_disabledSkills = new ConcurrentHashMap<>();
}
}
}
_disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE); _disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE);
} }
/** /**
* Removes all the disabled skills. * Removes all the disabled skills.
*/ */
public synchronized void resetDisabledSkills() public void resetDisabledSkills()
{
if (_disabledSkills != null)
{ {
_disabledSkills.clear(); _disabledSkills.clear();
} }
}
/** /**
* Verifies if the skill is disabled. * Verifies if the skill is disabled.
@@ -1570,7 +1534,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
return true; return true;
} }
if (_disabledSkills == null) if (_disabledSkills.isEmpty())
{ {
return false; return false;
} }
@@ -7244,6 +7244,7 @@ public class PlayerInstance extends Playable
int buff_index = 0; int buff_index = 0;
final List<Long> storedSkills = new ArrayList<>(); final List<Long> storedSkills = new ArrayList<>();
final long currentTime = System.currentTimeMillis();
// Store all effect data along with calulated remaining // Store all effect data along with calulated remaining
// reuse delays for matching skills. 'restore_type'= 0. // reuse delays for matching skills. 'restore_type'= 0.
@@ -7303,8 +7304,8 @@ public class PlayerInstance extends Playable
statement.setInt(5, info.getTime()); statement.setInt(5, info.getTime());
final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode()); final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode());
statement.setLong(6, (t != null) && t.hasNotPassed() ? t.getReuse() : 0); statement.setLong(6, (t != null) && (currentTime < t.getStamp()) ? t.getReuse() : 0);
statement.setDouble(7, (t != null) && t.hasNotPassed() ? t.getStamp() : 0); statement.setDouble(7, (t != null) && (currentTime < t.getStamp()) ? t.getStamp() : 0);
statement.setInt(8, 0); // Store type 0, active buffs/debuffs. statement.setInt(8, 0); // Store type 0, active buffs/debuffs.
statement.setInt(9, _classIndex); statement.setInt(9, _classIndex);
@@ -7314,10 +7315,7 @@ public class PlayerInstance extends Playable
} }
// Skills under reuse. // Skills under reuse.
final Map<Long, TimeStamp> reuseTimeStamps = getSkillReuseTimeStamps(); for (Entry<Long, TimeStamp> ts : getSkillReuseTimeStamps().entrySet())
if (reuseTimeStamps != null)
{
for (Entry<Long, TimeStamp> ts : reuseTimeStamps.entrySet())
{ {
final long hash = ts.getKey(); final long hash = ts.getKey();
if (storedSkills.contains(hash)) if (storedSkills.contains(hash))
@@ -7326,7 +7324,7 @@ public class PlayerInstance extends Playable
} }
final TimeStamp t = ts.getValue(); final TimeStamp t = ts.getValue();
if ((t != null) && t.hasNotPassed()) if ((t != null) && (currentTime < t.getStamp()))
{ {
storedSkills.add(hash); storedSkills.add(hash);
@@ -7343,7 +7341,6 @@ public class PlayerInstance extends Playable
statement.addBatch(); statement.addBatch();
} }
} }
}
statement.executeBatch(); statement.executeBatch();
} }
@@ -7363,12 +7360,10 @@ public class PlayerInstance extends Playable
ps1.setInt(1, getObjectId()); ps1.setInt(1, getObjectId());
ps1.execute(); ps1.execute();
final Map<Integer, TimeStamp> itemReuseTimeStamps = getItemReuseTimeStamps(); final long currentTime = System.currentTimeMillis();
if (itemReuseTimeStamps != null) for (TimeStamp ts : getItemReuseTimeStamps().values())
{ {
for (TimeStamp ts : itemReuseTimeStamps.values()) if ((ts != null) && (currentTime < ts.getStamp()))
{
if ((ts != null) && ts.hasNotPassed())
{ {
ps2.setInt(1, getObjectId()); ps2.setInt(1, getObjectId());
ps2.setInt(2, ts.getItemId()); ps2.setInt(2, ts.getItemId());
@@ -7380,7 +7375,6 @@ public class PlayerInstance extends Playable
} }
ps2.executeBatch(); ps2.executeBatch();
} }
}
catch (Exception e) catch (Exception e)
{ {
LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e); LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e);
@@ -18,38 +18,33 @@ package org.l2jmobius.gameserver.network.serverpackets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import org.l2jmobius.commons.network.PacketWriter; import org.l2jmobius.commons.network.PacketWriter;
import org.l2jmobius.gameserver.data.xml.impl.SkillData; import org.l2jmobius.gameserver.data.xml.impl.SkillData;
import org.l2jmobius.gameserver.model.TimeStamp; import org.l2jmobius.gameserver.model.TimeStamp;
import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
import org.l2jmobius.gameserver.model.skills.Skill;
import org.l2jmobius.gameserver.network.OutgoingPackets; import org.l2jmobius.gameserver.network.OutgoingPackets;
/** /**
* Skill Cool Time server packet implementation. * Skill Cool Time server packet implementation.
* @author KenM, Zoey76 * @author KenM, Zoey76, Mobius
*/ */
public class SkillCoolTime implements IClientOutgoingPacket public class SkillCoolTime implements IClientOutgoingPacket
{ {
private final long _currentTime;
private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>(); private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>();
public SkillCoolTime(PlayerInstance player) public SkillCoolTime(PlayerInstance player)
{ {
final Map<Long, TimeStamp> skillReuseTimeStamps = player.getSkillReuseTimeStamps(); _currentTime = System.currentTimeMillis();
if (skillReuseTimeStamps != null) for (TimeStamp ts : player.getSkillReuseTimeStamps().values())
{ {
for (TimeStamp ts : skillReuseTimeStamps.values()) if ((_currentTime < ts.getStamp()) && !SkillData.getInstance().getSkill(ts.getSkillId(), ts.getSkillLvl(), ts.getSkillSubLvl()).isNotBroadcastable())
{
final Skill skill = SkillData.getInstance().getSkill(ts.getSkillId(), ts.getSkillLvl(), ts.getSkillSubLvl());
if (ts.hasNotPassed() && !skill.isNotBroadcastable())
{ {
_skillReuseTimeStamps.add(ts); _skillReuseTimeStamps.add(ts);
} }
} }
} }
}
@Override @Override
public boolean write(PacketWriter packet) public boolean write(PacketWriter packet)
@@ -62,7 +57,7 @@ public class SkillCoolTime implements IClientOutgoingPacket
packet.writeD(ts.getSkillId()); packet.writeD(ts.getSkillId());
packet.writeD(0x00); // ? packet.writeD(0x00); // ?
packet.writeD((int) ts.getReuse() / 1000); packet.writeD((int) ts.getReuse() / 1000);
packet.writeD((int) ts.getRemaining() / 1000); packet.writeD((int) Math.max(ts.getStamp() - _currentTime, 0) / 1000);
} }
return true; return true;
} }
@@ -23,7 +23,7 @@ import org.l2jmobius.gameserver.model.skills.Skill;
* Simple class containing all necessary information to maintain<br> * Simple class containing all necessary information to maintain<br>
* valid time stamps and reuse for skills and items reuse upon re-login.<br> * valid time stamps and reuse for skills and items reuse upon re-login.<br>
* <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b> * <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b>
* @author Yesod, Zoey76 * @author Yesod, Zoey76, Mobius
*/ */
public class TimeStamp public class TimeStamp
{ {
@@ -36,7 +36,7 @@ public class TimeStamp
/** Item or skill reuse time. */ /** Item or skill reuse time. */
private final long _reuse; private final long _reuse;
/** Time stamp. */ /** Time stamp. */
private final long _stamp; private volatile long _stamp;
/** Shared reuse group. */ /** Shared reuse group. */
private final int _group; private final int _group;
@@ -52,7 +52,7 @@ public class TimeStamp
_id2 = skill.getLevel(); _id2 = skill.getLevel();
_id3 = skill.getSubLevel(); _id3 = skill.getSubLevel();
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = -1; _group = -1;
} }
@@ -68,7 +68,7 @@ public class TimeStamp
_id2 = item.getObjectId(); _id2 = item.getObjectId();
_id3 = 0; _id3 = 0;
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = item.getSharedReuseGroup(); _group = item.getSharedReuseGroup();
} }
@@ -151,7 +151,16 @@ public class TimeStamp
*/ */
public long getRemaining() public long getRemaining()
{ {
return Math.max(_stamp - System.currentTimeMillis(), 0); if (_stamp == 0)
{
return 0;
}
final long remainingTime = Math.max(_stamp - System.currentTimeMillis(), 0);
if (remainingTime == 0)
{
_stamp = 0;
}
return remainingTime;
} }
/** /**
@@ -160,6 +169,15 @@ public class TimeStamp
*/ */
public boolean hasNotPassed() public boolean hasNotPassed()
{ {
return System.currentTimeMillis() < _stamp; if (_stamp == 0)
{
return false;
}
final boolean hasNotPassed = System.currentTimeMillis() < _stamp;
if (!hasNotPassed)
{
_stamp = 0;
}
return hasNotPassed;
} }
} }
@@ -212,11 +212,11 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
/** Map containing all skills of this character. */ /** Map containing all skills of this character. */
private final Map<Integer, Skill> _skills = new ConcurrentSkipListMap<>(); private final Map<Integer, Skill> _skills = new ConcurrentSkipListMap<>();
/** Map containing the skill reuse time stamps. */ /** Map containing the skill reuse time stamps. */
private volatile Map<Long, TimeStamp> _reuseTimeStampsSkills = null; private final Map<Long, TimeStamp> _reuseTimeStampsSkills = new ConcurrentHashMap<>();
/** Map containing the item reuse time stamps. */ /** Map containing the item reuse time stamps. */
private volatile Map<Integer, TimeStamp> _reuseTimeStampsItems = null; private final Map<Integer, TimeStamp> _reuseTimeStampsItems = new ConcurrentHashMap<>();
/** Map containing all the disabled skills. */ /** Map containing all the disabled skills. */
private volatile Map<Long, Long> _disabledSkills = null; private final Map<Long, Long> _disabledSkills = new ConcurrentHashMap<>();
private boolean _allSkillsDisabled; private boolean _allSkillsDisabled;
private final byte[] _zones = new byte[ZoneId.getZoneCount()]; private final byte[] _zones = new byte[ZoneId.getZoneCount()];
@@ -1342,16 +1342,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStampItem(ItemInstance item, long reuse, long systime) public void addTimeStampItem(ItemInstance item, long reuse, long systime)
{ {
if (_reuseTimeStampsItems == null)
{
synchronized (this)
{
if (_reuseTimeStampsItems == null)
{
_reuseTimeStampsItems = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime)); _reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime));
} }
@@ -1360,9 +1350,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param itemObjId the item object ID * @param itemObjId the item object ID
* @return if the item has a reuse time stamp, the remaining time, otherwise -1 * @return if the item has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getItemRemainingReuseTime(int itemObjId) public long getItemRemainingReuseTime(int itemObjId)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsItems != null) ? _reuseTimeStampsItems.get(itemObjId) : null; final TimeStamp reuseStamp = _reuseTimeStampsItems.get(itemObjId);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -1373,13 +1363,18 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public long getReuseDelayOnGroup(int group) public long getReuseDelayOnGroup(int group)
{ {
if ((group > 0) && (_reuseTimeStampsItems != null)) if ((group > 0) && !_reuseTimeStampsItems.isEmpty())
{ {
final long currentTime = System.currentTimeMillis();
for (TimeStamp ts : _reuseTimeStampsItems.values()) for (TimeStamp ts : _reuseTimeStampsItems.values())
{ {
if ((ts.getSharedReuseGroup() == group) && ts.hasNotPassed()) if (ts.getSharedReuseGroup() == group)
{ {
return ts.getRemaining(); final long stamp = ts.getStamp();
if (currentTime < stamp)
{
return Math.max(stamp - currentTime, 0);
}
} }
} }
} }
@@ -1414,16 +1409,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStamp(Skill skill, long reuse, long systime) public void addTimeStamp(Skill skill, long reuse, long systime)
{ {
if (_reuseTimeStampsSkills == null)
{
synchronized (this)
{
if (_reuseTimeStampsSkills == null)
{
_reuseTimeStampsSkills = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime)); _reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime));
} }
@@ -1431,33 +1416,27 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* Removes a skill reuse time stamp. * Removes a skill reuse time stamp.
* @param skill the skill to remove * @param skill the skill to remove
*/ */
public synchronized void removeTimeStamp(Skill skill) public void removeTimeStamp(Skill skill)
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.remove(skill.getReuseHashCode()); _reuseTimeStampsSkills.remove(skill.getReuseHashCode());
} }
}
/** /**
* Removes all skill reuse time stamps. * Removes all skill reuse time stamps.
*/ */
public synchronized void resetTimeStamps() public void resetTimeStamps()
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.clear(); _reuseTimeStampsSkills.clear();
} }
}
/** /**
* Gets the skill remaining reuse time for a given skill hash code. * Gets the skill remaining reuse time for a given skill hash code.
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return if the skill has a reuse time stamp, the remaining time, otherwise -1 * @return if the skill has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getSkillRemainingReuseTime(long hashCode) public long getSkillRemainingReuseTime(long hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -1466,9 +1445,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return {@code true} if the skill is under reuse time, {@code false} otherwise * @return {@code true} if the skill is under reuse time, {@code false} otherwise
*/ */
public synchronized boolean hasSkillReuse(long hashCode) public boolean hasSkillReuse(long hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return (reuseStamp != null) && reuseStamp.hasNotPassed(); return (reuseStamp != null) && reuseStamp.hasNotPassed();
} }
@@ -1479,7 +1458,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public synchronized TimeStamp getSkillReuseTimeStamp(long hashCode) public synchronized TimeStamp getSkillReuseTimeStamp(long hashCode)
{ {
return _reuseTimeStampsSkills != null ? _reuseTimeStampsSkills.get(hashCode) : null; return _reuseTimeStampsSkills.get(hashCode);
} }
/** /**
@@ -1497,7 +1476,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void enableSkill(Skill skill) public void enableSkill(Skill skill)
{ {
if ((skill == null) || (_disabledSkills == null)) if (skill == null)
{ {
return; return;
} }
@@ -1516,31 +1495,16 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
{ {
return; return;
} }
if (_disabledSkills == null)
{
synchronized (this)
{
if (_disabledSkills == null)
{
_disabledSkills = new ConcurrentHashMap<>();
}
}
}
_disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE); _disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE);
} }
/** /**
* Removes all the disabled skills. * Removes all the disabled skills.
*/ */
public synchronized void resetDisabledSkills() public void resetDisabledSkills()
{
if (_disabledSkills != null)
{ {
_disabledSkills.clear(); _disabledSkills.clear();
} }
}
/** /**
* Verifies if the skill is disabled. * Verifies if the skill is disabled.
@@ -1570,7 +1534,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
return true; return true;
} }
if (_disabledSkills == null) if (_disabledSkills.isEmpty())
{ {
return false; return false;
} }
@@ -7143,6 +7143,7 @@ public class PlayerInstance extends Playable
int buff_index = 0; int buff_index = 0;
final List<Long> storedSkills = new ArrayList<>(); final List<Long> storedSkills = new ArrayList<>();
final long currentTime = System.currentTimeMillis();
// Store all effect data along with calulated remaining // Store all effect data along with calulated remaining
// reuse delays for matching skills. 'restore_type'= 0. // reuse delays for matching skills. 'restore_type'= 0.
@@ -7202,8 +7203,8 @@ public class PlayerInstance extends Playable
statement.setInt(5, info.getTime()); statement.setInt(5, info.getTime());
final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode()); final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode());
statement.setLong(6, (t != null) && t.hasNotPassed() ? t.getReuse() : 0); statement.setLong(6, (t != null) && (currentTime < t.getStamp()) ? t.getReuse() : 0);
statement.setDouble(7, (t != null) && t.hasNotPassed() ? t.getStamp() : 0); statement.setDouble(7, (t != null) && (currentTime < t.getStamp()) ? t.getStamp() : 0);
statement.setInt(8, 0); // Store type 0, active buffs/debuffs. statement.setInt(8, 0); // Store type 0, active buffs/debuffs.
statement.setInt(9, _classIndex); statement.setInt(9, _classIndex);
@@ -7213,10 +7214,7 @@ public class PlayerInstance extends Playable
} }
// Skills under reuse. // Skills under reuse.
final Map<Long, TimeStamp> reuseTimeStamps = getSkillReuseTimeStamps(); for (Entry<Long, TimeStamp> ts : getSkillReuseTimeStamps().entrySet())
if (reuseTimeStamps != null)
{
for (Entry<Long, TimeStamp> ts : reuseTimeStamps.entrySet())
{ {
final long hash = ts.getKey(); final long hash = ts.getKey();
if (storedSkills.contains(hash)) if (storedSkills.contains(hash))
@@ -7225,7 +7223,7 @@ public class PlayerInstance extends Playable
} }
final TimeStamp t = ts.getValue(); final TimeStamp t = ts.getValue();
if ((t != null) && t.hasNotPassed()) if ((t != null) && (currentTime < t.getStamp()))
{ {
storedSkills.add(hash); storedSkills.add(hash);
@@ -7242,7 +7240,6 @@ public class PlayerInstance extends Playable
statement.addBatch(); statement.addBatch();
} }
} }
}
statement.executeBatch(); statement.executeBatch();
} }
@@ -7262,12 +7259,10 @@ public class PlayerInstance extends Playable
ps1.setInt(1, getObjectId()); ps1.setInt(1, getObjectId());
ps1.execute(); ps1.execute();
final Map<Integer, TimeStamp> itemReuseTimeStamps = getItemReuseTimeStamps(); final long currentTime = System.currentTimeMillis();
if (itemReuseTimeStamps != null) for (TimeStamp ts : getItemReuseTimeStamps().values())
{ {
for (TimeStamp ts : itemReuseTimeStamps.values()) if ((ts != null) && (currentTime < ts.getStamp()))
{
if ((ts != null) && ts.hasNotPassed())
{ {
ps2.setInt(1, getObjectId()); ps2.setInt(1, getObjectId());
ps2.setInt(2, ts.getItemId()); ps2.setInt(2, ts.getItemId());
@@ -7279,7 +7274,6 @@ public class PlayerInstance extends Playable
} }
ps2.executeBatch(); ps2.executeBatch();
} }
}
catch (Exception e) catch (Exception e)
{ {
LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e); LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e);
@@ -18,38 +18,33 @@ package org.l2jmobius.gameserver.network.serverpackets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import org.l2jmobius.commons.network.PacketWriter; import org.l2jmobius.commons.network.PacketWriter;
import org.l2jmobius.gameserver.data.xml.impl.SkillData; import org.l2jmobius.gameserver.data.xml.impl.SkillData;
import org.l2jmobius.gameserver.model.TimeStamp; import org.l2jmobius.gameserver.model.TimeStamp;
import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
import org.l2jmobius.gameserver.model.skills.Skill;
import org.l2jmobius.gameserver.network.OutgoingPackets; import org.l2jmobius.gameserver.network.OutgoingPackets;
/** /**
* Skill Cool Time server packet implementation. * Skill Cool Time server packet implementation.
* @author KenM, Zoey76 * @author KenM, Zoey76, Mobius
*/ */
public class SkillCoolTime implements IClientOutgoingPacket public class SkillCoolTime implements IClientOutgoingPacket
{ {
private final long _currentTime;
private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>(); private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>();
public SkillCoolTime(PlayerInstance player) public SkillCoolTime(PlayerInstance player)
{ {
final Map<Long, TimeStamp> skillReuseTimeStamps = player.getSkillReuseTimeStamps(); _currentTime = System.currentTimeMillis();
if (skillReuseTimeStamps != null) for (TimeStamp ts : player.getSkillReuseTimeStamps().values())
{ {
for (TimeStamp ts : skillReuseTimeStamps.values()) if ((_currentTime < ts.getStamp()) && !SkillData.getInstance().getSkill(ts.getSkillId(), ts.getSkillLvl(), ts.getSkillSubLvl()).isNotBroadcastable())
{
final Skill skill = SkillData.getInstance().getSkill(ts.getSkillId(), ts.getSkillLvl(), ts.getSkillSubLvl());
if (ts.hasNotPassed() && !skill.isNotBroadcastable())
{ {
_skillReuseTimeStamps.add(ts); _skillReuseTimeStamps.add(ts);
} }
} }
} }
}
@Override @Override
public boolean write(PacketWriter packet) public boolean write(PacketWriter packet)
@@ -62,7 +57,7 @@ public class SkillCoolTime implements IClientOutgoingPacket
packet.writeD(ts.getSkillId()); packet.writeD(ts.getSkillId());
packet.writeD(0x00); // ? packet.writeD(0x00); // ?
packet.writeD((int) ts.getReuse() / 1000); packet.writeD((int) ts.getReuse() / 1000);
packet.writeD((int) ts.getRemaining() / 1000); packet.writeD((int) Math.max(ts.getStamp() - _currentTime, 0) / 1000);
} }
return true; return true;
} }
@@ -23,7 +23,7 @@ import org.l2jmobius.gameserver.model.skills.Skill;
* Simple class containing all necessary information to maintain<br> * Simple class containing all necessary information to maintain<br>
* valid time stamps and reuse for skills and items reuse upon re-login.<br> * valid time stamps and reuse for skills and items reuse upon re-login.<br>
* <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b> * <b>Filter this carefully as it becomes redundant to store reuse for small delays.</b>
* @author Yesod, Zoey76 * @author Yesod, Zoey76, Mobius
*/ */
public class TimeStamp public class TimeStamp
{ {
@@ -36,7 +36,7 @@ public class TimeStamp
/** Item or skill reuse time. */ /** Item or skill reuse time. */
private final long _reuse; private final long _reuse;
/** Time stamp. */ /** Time stamp. */
private final long _stamp; private volatile long _stamp;
/** Shared reuse group. */ /** Shared reuse group. */
private final int _group; private final int _group;
@@ -52,7 +52,7 @@ public class TimeStamp
_id2 = skill.getLevel(); _id2 = skill.getLevel();
_id3 = skill.getSubLevel(); _id3 = skill.getSubLevel();
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = -1; _group = -1;
} }
@@ -68,7 +68,7 @@ public class TimeStamp
_id2 = item.getObjectId(); _id2 = item.getObjectId();
_id3 = 0; _id3 = 0;
_reuse = reuse; _reuse = reuse;
_stamp = systime > 0 ? systime : System.currentTimeMillis() + reuse; _stamp = systime > 0 ? systime : reuse != 0 ? System.currentTimeMillis() + reuse : 0;
_group = item.getSharedReuseGroup(); _group = item.getSharedReuseGroup();
} }
@@ -151,7 +151,16 @@ public class TimeStamp
*/ */
public long getRemaining() public long getRemaining()
{ {
return Math.max(_stamp - System.currentTimeMillis(), 0); if (_stamp == 0)
{
return 0;
}
final long remainingTime = Math.max(_stamp - System.currentTimeMillis(), 0);
if (remainingTime == 0)
{
_stamp = 0;
}
return remainingTime;
} }
/** /**
@@ -160,6 +169,15 @@ public class TimeStamp
*/ */
public boolean hasNotPassed() public boolean hasNotPassed()
{ {
return System.currentTimeMillis() < _stamp; if (_stamp == 0)
{
return false;
}
final boolean hasNotPassed = System.currentTimeMillis() < _stamp;
if (!hasNotPassed)
{
_stamp = 0;
}
return hasNotPassed;
} }
} }
@@ -211,11 +211,11 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
/** Map containing all skills of this character. */ /** Map containing all skills of this character. */
private final Map<Integer, Skill> _skills = new ConcurrentSkipListMap<>(); private final Map<Integer, Skill> _skills = new ConcurrentSkipListMap<>();
/** Map containing the skill reuse time stamps. */ /** Map containing the skill reuse time stamps. */
private volatile Map<Long, TimeStamp> _reuseTimeStampsSkills = null; private final Map<Long, TimeStamp> _reuseTimeStampsSkills = new ConcurrentHashMap<>();
/** Map containing the item reuse time stamps. */ /** Map containing the item reuse time stamps. */
private volatile Map<Integer, TimeStamp> _reuseTimeStampsItems = null; private final Map<Integer, TimeStamp> _reuseTimeStampsItems = new ConcurrentHashMap<>();
/** Map containing all the disabled skills. */ /** Map containing all the disabled skills. */
private volatile Map<Long, Long> _disabledSkills = null; private final Map<Long, Long> _disabledSkills = new ConcurrentHashMap<>();
private boolean _allSkillsDisabled; private boolean _allSkillsDisabled;
private final byte[] _zones = new byte[ZoneId.getZoneCount()]; private final byte[] _zones = new byte[ZoneId.getZoneCount()];
@@ -1341,16 +1341,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStampItem(ItemInstance item, long reuse, long systime) public void addTimeStampItem(ItemInstance item, long reuse, long systime)
{ {
if (_reuseTimeStampsItems == null)
{
synchronized (this)
{
if (_reuseTimeStampsItems == null)
{
_reuseTimeStampsItems = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime)); _reuseTimeStampsItems.put(item.getObjectId(), new TimeStamp(item, reuse, systime));
} }
@@ -1359,9 +1349,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param itemObjId the item object ID * @param itemObjId the item object ID
* @return if the item has a reuse time stamp, the remaining time, otherwise -1 * @return if the item has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getItemRemainingReuseTime(int itemObjId) public long getItemRemainingReuseTime(int itemObjId)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsItems != null) ? _reuseTimeStampsItems.get(itemObjId) : null; final TimeStamp reuseStamp = _reuseTimeStampsItems.get(itemObjId);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -1372,13 +1362,18 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public long getReuseDelayOnGroup(int group) public long getReuseDelayOnGroup(int group)
{ {
if ((group > 0) && (_reuseTimeStampsItems != null)) if ((group > 0) && !_reuseTimeStampsItems.isEmpty())
{ {
final long currentTime = System.currentTimeMillis();
for (TimeStamp ts : _reuseTimeStampsItems.values()) for (TimeStamp ts : _reuseTimeStampsItems.values())
{ {
if ((ts.getSharedReuseGroup() == group) && ts.hasNotPassed()) if (ts.getSharedReuseGroup() == group)
{ {
return ts.getRemaining(); final long stamp = ts.getStamp();
if (currentTime < stamp)
{
return Math.max(stamp - currentTime, 0);
}
} }
} }
} }
@@ -1413,16 +1408,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void addTimeStamp(Skill skill, long reuse, long systime) public void addTimeStamp(Skill skill, long reuse, long systime)
{ {
if (_reuseTimeStampsSkills == null)
{
synchronized (this)
{
if (_reuseTimeStampsSkills == null)
{
_reuseTimeStampsSkills = new ConcurrentHashMap<>();
}
}
}
_reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime)); _reuseTimeStampsSkills.put(skill.getReuseHashCode(), new TimeStamp(skill, reuse, systime));
} }
@@ -1430,33 +1415,27 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* Removes a skill reuse time stamp. * Removes a skill reuse time stamp.
* @param skill the skill to remove * @param skill the skill to remove
*/ */
public synchronized void removeTimeStamp(Skill skill) public void removeTimeStamp(Skill skill)
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.remove(skill.getReuseHashCode()); _reuseTimeStampsSkills.remove(skill.getReuseHashCode());
} }
}
/** /**
* Removes all skill reuse time stamps. * Removes all skill reuse time stamps.
*/ */
public synchronized void resetTimeStamps() public void resetTimeStamps()
{
if (_reuseTimeStampsSkills != null)
{ {
_reuseTimeStampsSkills.clear(); _reuseTimeStampsSkills.clear();
} }
}
/** /**
* Gets the skill remaining reuse time for a given skill hash code. * Gets the skill remaining reuse time for a given skill hash code.
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return if the skill has a reuse time stamp, the remaining time, otherwise -1 * @return if the skill has a reuse time stamp, the remaining time, otherwise -1
*/ */
public synchronized long getSkillRemainingReuseTime(long hashCode) public long getSkillRemainingReuseTime(long hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return reuseStamp != null ? reuseStamp.getRemaining() : -1; return reuseStamp != null ? reuseStamp.getRemaining() : -1;
} }
@@ -1465,9 +1444,9 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
* @param hashCode the skill hash code * @param hashCode the skill hash code
* @return {@code true} if the skill is under reuse time, {@code false} otherwise * @return {@code true} if the skill is under reuse time, {@code false} otherwise
*/ */
public synchronized boolean hasSkillReuse(long hashCode) public boolean hasSkillReuse(long hashCode)
{ {
final TimeStamp reuseStamp = (_reuseTimeStampsSkills != null) ? _reuseTimeStampsSkills.get(hashCode) : null; final TimeStamp reuseStamp = _reuseTimeStampsSkills.get(hashCode);
return (reuseStamp != null) && reuseStamp.hasNotPassed(); return (reuseStamp != null) && reuseStamp.hasNotPassed();
} }
@@ -1478,7 +1457,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public synchronized TimeStamp getSkillReuseTimeStamp(long hashCode) public synchronized TimeStamp getSkillReuseTimeStamp(long hashCode)
{ {
return _reuseTimeStampsSkills != null ? _reuseTimeStampsSkills.get(hashCode) : null; return _reuseTimeStampsSkills.get(hashCode);
} }
/** /**
@@ -1496,7 +1475,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
*/ */
public void enableSkill(Skill skill) public void enableSkill(Skill skill)
{ {
if ((skill == null) || (_disabledSkills == null)) if (skill == null)
{ {
return; return;
} }
@@ -1515,31 +1494,16 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
{ {
return; return;
} }
if (_disabledSkills == null)
{
synchronized (this)
{
if (_disabledSkills == null)
{
_disabledSkills = new ConcurrentHashMap<>();
}
}
}
_disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE); _disabledSkills.put(skill.getReuseHashCode(), delay > 0 ? System.currentTimeMillis() + delay : Long.MAX_VALUE);
} }
/** /**
* Removes all the disabled skills. * Removes all the disabled skills.
*/ */
public synchronized void resetDisabledSkills() public void resetDisabledSkills()
{
if (_disabledSkills != null)
{ {
_disabledSkills.clear(); _disabledSkills.clear();
} }
}
/** /**
* Verifies if the skill is disabled. * Verifies if the skill is disabled.
@@ -1569,7 +1533,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
return true; return true;
} }
if (_disabledSkills == null) if (_disabledSkills.isEmpty())
{ {
return false; return false;
} }
@@ -7232,6 +7232,7 @@ public class PlayerInstance extends Playable
int buff_index = 0; int buff_index = 0;
final List<Long> storedSkills = new ArrayList<>(); final List<Long> storedSkills = new ArrayList<>();
final long currentTime = System.currentTimeMillis();
// Store all effect data along with calulated remaining // Store all effect data along with calulated remaining
// reuse delays for matching skills. 'restore_type'= 0. // reuse delays for matching skills. 'restore_type'= 0.
@@ -7291,8 +7292,8 @@ public class PlayerInstance extends Playable
statement.setInt(5, info.getTime()); statement.setInt(5, info.getTime());
final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode()); final TimeStamp t = getSkillReuseTimeStamp(skill.getReuseHashCode());
statement.setLong(6, (t != null) && t.hasNotPassed() ? t.getReuse() : 0); statement.setLong(6, (t != null) && (currentTime < t.getStamp()) ? t.getReuse() : 0);
statement.setDouble(7, (t != null) && t.hasNotPassed() ? t.getStamp() : 0); statement.setDouble(7, (t != null) && (currentTime < t.getStamp()) ? t.getStamp() : 0);
statement.setInt(8, 0); // Store type 0, active buffs/debuffs. statement.setInt(8, 0); // Store type 0, active buffs/debuffs.
statement.setInt(9, _classIndex); statement.setInt(9, _classIndex);
@@ -7302,10 +7303,7 @@ public class PlayerInstance extends Playable
} }
// Skills under reuse. // Skills under reuse.
final Map<Long, TimeStamp> reuseTimeStamps = getSkillReuseTimeStamps(); for (Entry<Long, TimeStamp> ts : getSkillReuseTimeStamps().entrySet())
if (reuseTimeStamps != null)
{
for (Entry<Long, TimeStamp> ts : reuseTimeStamps.entrySet())
{ {
final long hash = ts.getKey(); final long hash = ts.getKey();
if (storedSkills.contains(hash)) if (storedSkills.contains(hash))
@@ -7314,7 +7312,7 @@ public class PlayerInstance extends Playable
} }
final TimeStamp t = ts.getValue(); final TimeStamp t = ts.getValue();
if ((t != null) && t.hasNotPassed()) if ((t != null) && (currentTime < t.getStamp()))
{ {
storedSkills.add(hash); storedSkills.add(hash);
@@ -7331,7 +7329,6 @@ public class PlayerInstance extends Playable
statement.addBatch(); statement.addBatch();
} }
} }
}
statement.executeBatch(); statement.executeBatch();
} }
@@ -7351,12 +7348,10 @@ public class PlayerInstance extends Playable
ps1.setInt(1, getObjectId()); ps1.setInt(1, getObjectId());
ps1.execute(); ps1.execute();
final Map<Integer, TimeStamp> itemReuseTimeStamps = getItemReuseTimeStamps(); final long currentTime = System.currentTimeMillis();
if (itemReuseTimeStamps != null) for (TimeStamp ts : getItemReuseTimeStamps().values())
{ {
for (TimeStamp ts : itemReuseTimeStamps.values()) if ((ts != null) && (currentTime < ts.getStamp()))
{
if ((ts != null) && ts.hasNotPassed())
{ {
ps2.setInt(1, getObjectId()); ps2.setInt(1, getObjectId());
ps2.setInt(2, ts.getItemId()); ps2.setInt(2, ts.getItemId());
@@ -7368,7 +7363,6 @@ public class PlayerInstance extends Playable
} }
ps2.executeBatch(); ps2.executeBatch();
} }
}
catch (Exception e) catch (Exception e)
{ {
LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e); LOGGER.log(Level.WARNING, "Could not store char item reuse data: ", e);
@@ -18,38 +18,33 @@ package org.l2jmobius.gameserver.network.serverpackets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import org.l2jmobius.commons.network.PacketWriter; import org.l2jmobius.commons.network.PacketWriter;
import org.l2jmobius.gameserver.data.xml.impl.SkillData; import org.l2jmobius.gameserver.data.xml.impl.SkillData;
import org.l2jmobius.gameserver.model.TimeStamp; import org.l2jmobius.gameserver.model.TimeStamp;
import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance;
import org.l2jmobius.gameserver.model.skills.Skill;
import org.l2jmobius.gameserver.network.OutgoingPackets; import org.l2jmobius.gameserver.network.OutgoingPackets;
/** /**
* Skill Cool Time server packet implementation. * Skill Cool Time server packet implementation.
* @author KenM, Zoey76 * @author KenM, Zoey76, Mobius
*/ */
public class SkillCoolTime implements IClientOutgoingPacket public class SkillCoolTime implements IClientOutgoingPacket
{ {
private final long _currentTime;
private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>(); private final List<TimeStamp> _skillReuseTimeStamps = new ArrayList<>();
public SkillCoolTime(PlayerInstance player) public SkillCoolTime(PlayerInstance player)
{ {
final Map<Long, TimeStamp> skillReuseTimeStamps = player.getSkillReuseTimeStamps(); _currentTime = System.currentTimeMillis();
if (skillReuseTimeStamps != null) for (TimeStamp ts : player.getSkillReuseTimeStamps().values())
{ {
for (TimeStamp ts : skillReuseTimeStamps.values()) if ((_currentTime < ts.getStamp()) && !SkillData.getInstance().getSkill(ts.getSkillId(), ts.getSkillLvl(), ts.getSkillSubLvl()).isNotBroadcastable())
{
final Skill skill = SkillData.getInstance().getSkill(ts.getSkillId(), ts.getSkillLvl(), ts.getSkillSubLvl());
if (ts.hasNotPassed() && !skill.isNotBroadcastable())
{ {
_skillReuseTimeStamps.add(ts); _skillReuseTimeStamps.add(ts);
} }
} }
} }
}
@Override @Override
public boolean write(PacketWriter packet) public boolean write(PacketWriter packet)
@@ -62,7 +57,7 @@ public class SkillCoolTime implements IClientOutgoingPacket
packet.writeD(ts.getSkillId()); packet.writeD(ts.getSkillId());
packet.writeD(0x00); // ? packet.writeD(0x00); // ?
packet.writeD((int) ts.getReuse() / 1000); packet.writeD((int) ts.getReuse() / 1000);
packet.writeD((int) ts.getRemaining() / 1000); packet.writeD((int) Math.max(ts.getStamp() - _currentTime, 0) / 1000);
} }
return true; return true;
} }