From 4acd1b5b173272d195cdd88c83c1287aeb08dd04 Mon Sep 17 00:00:00 2001 From: MobiusDevelopment <8391001+MobiusDevelopment@users.noreply.github.com> Date: Fri, 1 Nov 2019 15:25:09 +0000 Subject: [PATCH] Support for potion auto use. --- .../model/actor/instance/PlayerInstance.java | 130 +++++++++++++++++- .../autoplay/ExAutoPlaySetting.java | 2 + .../ExRequestActivateAutoShortcut.java | 71 +++++++--- .../model/actor/instance/PlayerInstance.java | 130 +++++++++++++++++- .../autoplay/ExAutoPlaySetting.java | 2 + .../ExRequestActivateAutoShortcut.java | 71 +++++++--- 6 files changed, 368 insertions(+), 38 deletions(-) diff --git a/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java b/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java index 75164c4ef4..910d203458 100644 --- a/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java +++ b/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java @@ -860,6 +860,11 @@ public class PlayerInstance extends Playable private final Set _whisperers = ConcurrentHashMap.newKeySet(); private ScheduledFuture _autoPlayTask = null; + private ScheduledFuture _autoUseTask = null; + private int _autoPotionPercent = 0; + private final Collection _autoSupplyItems = ConcurrentHashMap.newKeySet(); + private final Collection _autoPotionItems = ConcurrentHashMap.newKeySet(); + private final Collection _autoSkills = ConcurrentHashMap.newKeySet(); // Selling buffs system private boolean _isSellingBuffs = false; @@ -14071,8 +14076,8 @@ public class PlayerInstance extends Playable if ((_autoPlayTask != null) && !_autoPlayTask.isCancelled() && !_autoPlayTask.isDone()) { _autoPlayTask.cancel(true); + _autoPlayTask = null; } - _autoPlayTask = null; } public void startAutoPlayTask(boolean pickup, boolean longRange, boolean respectfulHunting) @@ -14157,4 +14162,127 @@ public class PlayerInstance extends Playable } }, 0, 1000); } + + private void stopAutoUseTask() + { + if ((_autoUseTask != null) && !_autoUseTask.isCancelled() && !_autoUseTask.isDone() && _autoSupplyItems.isEmpty() && _autoPotionItems.isEmpty() && _autoSkills.isEmpty()) + { + _autoUseTask.cancel(true); + _autoUseTask = null; + } + } + + private void startAutoUseTask() + { + if (_autoUseTask != null) + { + return; + } + + _autoUseTask = ThreadPool.scheduleAtFixedRate(() -> + { + if (hasBlockActions() || isControlBlocked() || isAlikeDead()) + { + return; + } + + for (int itemId : _autoSupplyItems) + { + final ItemInstance item = _inventory.getItemByItemId(itemId); + if (item == null) + { + removeAutoSupplyItem(itemId); + continue; + } + final int reuseDelay = item.getReuseDelay(); + if ((reuseDelay <= 0) || (getItemRemainingReuseTime(item.getObjectId()) <= 0)) + { + final EtcItem etcItem = item.getEtcItem(); + final IItemHandler handler = ItemHandler.getInstance().getHandler(etcItem); + if ((handler != null) && handler.useItem(this, item, false) && (reuseDelay > 0)) + { + addTimeStampItem(item, reuseDelay); + } + } + } + + if (getCurrentHpPercent() <= _autoPotionPercent) + { + for (int itemId : _autoPotionItems) + { + final ItemInstance item = _inventory.getItemByItemId(itemId); + if (item == null) + { + removeAutoPotionItem(itemId); + continue; + } + final int reuseDelay = item.getReuseDelay(); + if ((reuseDelay <= 0) || (getItemRemainingReuseTime(item.getObjectId()) <= 0)) + { + final EtcItem etcItem = item.getEtcItem(); + final IItemHandler handler = ItemHandler.getInstance().getHandler(etcItem); + if ((handler != null) && handler.useItem(this, item, false) && (reuseDelay > 0)) + { + addTimeStampItem(item, reuseDelay); + } + } + } + } + + for (int skillId : _autoSkills) + { + final Skill skill = getKnownSkill(skillId); + if (skill == null) + { + removeAutoSkill(skillId); + continue; + } + if (!hasSkillReuse(skill.getReuseHashCode())) + { + doCast(skill); + } + } + }, 0, 1000); + } + + public void setAutoPotionPercent(int value) + { + _autoPotionPercent = value; + } + + public void addAutoSupplyItem(int itemId) + { + _autoSupplyItems.add(itemId); + startAutoUseTask(); + } + + public void removeAutoSupplyItem(int itemId) + { + _autoSupplyItems.remove(itemId); + stopAutoUseTask(); + } + + public void addAutoPotionItem(int itemId) + { + _autoPotionItems.add(itemId); + startAutoUseTask(); + } + + public void removeAutoPotionItem(int itemId) + { + _autoPotionItems.remove(itemId); + stopAutoUseTask(); + } + + public void addAutoSkill(int skillId) + { + _autoSkills.add(skillId); + startAutoUseTask(); + } + + public void removeAutoSkill(int skillId) + { + _autoSkills.remove(skillId); + stopAutoUseTask(); + } } diff --git a/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/network/clientpackets/autoplay/ExAutoPlaySetting.java b/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/network/clientpackets/autoplay/ExAutoPlaySetting.java index 60ab62828e..146271146a 100644 --- a/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/network/clientpackets/autoplay/ExAutoPlaySetting.java +++ b/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/network/clientpackets/autoplay/ExAutoPlaySetting.java @@ -59,6 +59,8 @@ public class ExAutoPlaySetting implements IClientIncomingPacket player.sendPacket(new ExAutoPlaySettingSend(_options, _active, _pickUp, _nextTargetMode, _longRange, _potionPercent, _respectfulHunting)); + player.setAutoPotionPercent(_potionPercent); + if (_active) { player.startAutoPlayTask(_pickUp, _longRange, _respectfulHunting); diff --git a/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/network/clientpackets/autoplay/ExRequestActivateAutoShortcut.java b/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/network/clientpackets/autoplay/ExRequestActivateAutoShortcut.java index fbd43fbd71..dce6dcaa97 100644 --- a/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/network/clientpackets/autoplay/ExRequestActivateAutoShortcut.java +++ b/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/network/clientpackets/autoplay/ExRequestActivateAutoShortcut.java @@ -20,12 +20,13 @@ import org.l2jmobius.commons.network.PacketReader; import org.l2jmobius.gameserver.model.Shortcut; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; import org.l2jmobius.gameserver.model.items.instance.ItemInstance; +import org.l2jmobius.gameserver.model.skills.Skill; import org.l2jmobius.gameserver.network.GameClient; import org.l2jmobius.gameserver.network.clientpackets.IClientIncomingPacket; import org.l2jmobius.gameserver.network.serverpackets.autoplay.ExActivateAutoShortcut; /** - * @author JoeAlisson + * @author JoeAlisson, Mobius */ public class ExRequestActivateAutoShortcut implements IClientIncomingPacket { @@ -49,36 +50,70 @@ public class ExRequestActivateAutoShortcut implements IClientIncomingPacket return; } + final int slot = _room % 12; + final int page = _room / 12; + final Shortcut shortcut = player.getShortCut(slot, page); + if (shortcut == null) + { + return; + } + client.sendPacket(new ExActivateAutoShortcut(_room, _activate)); + + final ItemInstance item = player.getInventory().getItemByObjectId(shortcut.getId()); + Skill skill = null; + if (item == null) + { + skill = player.getKnownSkill(shortcut.getId()); + } + + // stop if (!_activate) { - client.sendPacket(new ExActivateAutoShortcut(_room, _activate)); + if (item != null) + { + // auto supply + if (_room == -1) + { + player.removeAutoSupplyItem(item.getId()); + } + else // auto potion + { + player.removeAutoPotionItem(item.getId()); + } + } + // TODO: auto skill + if (skill != null) + { + player.removeAutoSkill(skill.getId()); + } return; } + // start if (_room == -1) { - // TODO: auto supply - client.sendPacket(new ExActivateAutoShortcut(_room, _activate)); + // auto supply + if (item != null) + { + player.addAutoSupplyItem(item.getId()); + return; + } } else { - final int slot = _room % 12; - final int page = _room / 12; - final Shortcut shortcut = player.getShortCut(slot, page); - if (shortcut != null) + // auto potion + if ((page == 23) && (slot == 1)) { - if ((page == 23) && (slot == 1)) + if ((item != null) && item.isPotion()) { - // auto potion - final ItemInstance item = player.getInventory().getItemByObjectId(shortcut.getId()); - if ((item == null) || !item.isPotion()) - { - return; - } + player.addAutoPotionItem(item.getId()); + return; } - - // TODO: auto skill - client.sendPacket(new ExActivateAutoShortcut(_room, _activate)); + } + // TODO: auto skill + if (skill != null) + { + player.addAutoSkill(skill.getId()); } } } diff --git a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java index 6deec55984..8f4481b82a 100644 --- a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java +++ b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/actor/instance/PlayerInstance.java @@ -852,6 +852,11 @@ public class PlayerInstance extends Playable private ElementalType _activeElementalSpiritType; private ScheduledFuture _autoPlayTask = null; + private ScheduledFuture _autoUseTask = null; + private int _autoPotionPercent = 0; + private final Collection _autoSupplyItems = ConcurrentHashMap.newKeySet(); + private final Collection _autoPotionItems = ConcurrentHashMap.newKeySet(); + private final Collection _autoSkills = ConcurrentHashMap.newKeySet(); // Selling buffs system private boolean _isSellingBuffs = false; @@ -13998,8 +14003,8 @@ public class PlayerInstance extends Playable if ((_autoPlayTask != null) && !_autoPlayTask.isCancelled() && !_autoPlayTask.isDone()) { _autoPlayTask.cancel(true); + _autoPlayTask = null; } - _autoPlayTask = null; } public void startAutoPlayTask(boolean pickup, boolean longRange, boolean respectfulHunting) @@ -14084,4 +14089,127 @@ public class PlayerInstance extends Playable } }, 0, 1000); } + + private void stopAutoUseTask() + { + if ((_autoUseTask != null) && !_autoUseTask.isCancelled() && !_autoUseTask.isDone() && _autoSupplyItems.isEmpty() && _autoPotionItems.isEmpty() && _autoSkills.isEmpty()) + { + _autoUseTask.cancel(true); + _autoUseTask = null; + } + } + + private void startAutoUseTask() + { + if (_autoUseTask != null) + { + return; + } + + _autoUseTask = ThreadPool.scheduleAtFixedRate(() -> + { + if (hasBlockActions() || isControlBlocked() || isAlikeDead()) + { + return; + } + + for (int itemId : _autoSupplyItems) + { + final ItemInstance item = _inventory.getItemByItemId(itemId); + if (item == null) + { + removeAutoSupplyItem(itemId); + continue; + } + final int reuseDelay = item.getReuseDelay(); + if ((reuseDelay <= 0) || (getItemRemainingReuseTime(item.getObjectId()) <= 0)) + { + final EtcItem etcItem = item.getEtcItem(); + final IItemHandler handler = ItemHandler.getInstance().getHandler(etcItem); + if ((handler != null) && handler.useItem(this, item, false) && (reuseDelay > 0)) + { + addTimeStampItem(item, reuseDelay); + } + } + } + + if (getCurrentHpPercent() <= _autoPotionPercent) + { + for (int itemId : _autoPotionItems) + { + final ItemInstance item = _inventory.getItemByItemId(itemId); + if (item == null) + { + removeAutoPotionItem(itemId); + continue; + } + final int reuseDelay = item.getReuseDelay(); + if ((reuseDelay <= 0) || (getItemRemainingReuseTime(item.getObjectId()) <= 0)) + { + final EtcItem etcItem = item.getEtcItem(); + final IItemHandler handler = ItemHandler.getInstance().getHandler(etcItem); + if ((handler != null) && handler.useItem(this, item, false) && (reuseDelay > 0)) + { + addTimeStampItem(item, reuseDelay); + } + } + } + } + + for (int skillId : _autoSkills) + { + final Skill skill = getKnownSkill(skillId); + if (skill == null) + { + removeAutoSkill(skillId); + continue; + } + if (!hasSkillReuse(skill.getReuseHashCode())) + { + doCast(skill); + } + } + }, 0, 1000); + } + + public void setAutoPotionPercent(int value) + { + _autoPotionPercent = value; + } + + public void addAutoSupplyItem(int itemId) + { + _autoSupplyItems.add(itemId); + startAutoUseTask(); + } + + public void removeAutoSupplyItem(int itemId) + { + _autoSupplyItems.remove(itemId); + stopAutoUseTask(); + } + + public void addAutoPotionItem(int itemId) + { + _autoPotionItems.add(itemId); + startAutoUseTask(); + } + + public void removeAutoPotionItem(int itemId) + { + _autoPotionItems.remove(itemId); + stopAutoUseTask(); + } + + public void addAutoSkill(int skillId) + { + _autoSkills.add(skillId); + startAutoUseTask(); + } + + public void removeAutoSkill(int skillId) + { + _autoSkills.remove(skillId); + stopAutoUseTask(); + } } diff --git a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/network/clientpackets/autoplay/ExAutoPlaySetting.java b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/network/clientpackets/autoplay/ExAutoPlaySetting.java index 2792b838e0..4161f9a3af 100644 --- a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/network/clientpackets/autoplay/ExAutoPlaySetting.java +++ b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/network/clientpackets/autoplay/ExAutoPlaySetting.java @@ -59,6 +59,8 @@ public class ExAutoPlaySetting implements IClientIncomingPacket player.sendPacket(new ExAutoPlaySettingSend(_options, _active, _pickUp, _nextTargetMode, _longRange, _potionPercent, _respectfulHunting)); + player.setAutoPotionPercent(_potionPercent); + if (_active) { player.startAutoPlayTask(_pickUp, _longRange, _respectfulHunting); diff --git a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/network/clientpackets/autoplay/ExRequestActivateAutoShortcut.java b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/network/clientpackets/autoplay/ExRequestActivateAutoShortcut.java index fbd43fbd71..dce6dcaa97 100644 --- a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/network/clientpackets/autoplay/ExRequestActivateAutoShortcut.java +++ b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/network/clientpackets/autoplay/ExRequestActivateAutoShortcut.java @@ -20,12 +20,13 @@ import org.l2jmobius.commons.network.PacketReader; import org.l2jmobius.gameserver.model.Shortcut; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; import org.l2jmobius.gameserver.model.items.instance.ItemInstance; +import org.l2jmobius.gameserver.model.skills.Skill; import org.l2jmobius.gameserver.network.GameClient; import org.l2jmobius.gameserver.network.clientpackets.IClientIncomingPacket; import org.l2jmobius.gameserver.network.serverpackets.autoplay.ExActivateAutoShortcut; /** - * @author JoeAlisson + * @author JoeAlisson, Mobius */ public class ExRequestActivateAutoShortcut implements IClientIncomingPacket { @@ -49,36 +50,70 @@ public class ExRequestActivateAutoShortcut implements IClientIncomingPacket return; } + final int slot = _room % 12; + final int page = _room / 12; + final Shortcut shortcut = player.getShortCut(slot, page); + if (shortcut == null) + { + return; + } + client.sendPacket(new ExActivateAutoShortcut(_room, _activate)); + + final ItemInstance item = player.getInventory().getItemByObjectId(shortcut.getId()); + Skill skill = null; + if (item == null) + { + skill = player.getKnownSkill(shortcut.getId()); + } + + // stop if (!_activate) { - client.sendPacket(new ExActivateAutoShortcut(_room, _activate)); + if (item != null) + { + // auto supply + if (_room == -1) + { + player.removeAutoSupplyItem(item.getId()); + } + else // auto potion + { + player.removeAutoPotionItem(item.getId()); + } + } + // TODO: auto skill + if (skill != null) + { + player.removeAutoSkill(skill.getId()); + } return; } + // start if (_room == -1) { - // TODO: auto supply - client.sendPacket(new ExActivateAutoShortcut(_room, _activate)); + // auto supply + if (item != null) + { + player.addAutoSupplyItem(item.getId()); + return; + } } else { - final int slot = _room % 12; - final int page = _room / 12; - final Shortcut shortcut = player.getShortCut(slot, page); - if (shortcut != null) + // auto potion + if ((page == 23) && (slot == 1)) { - if ((page == 23) && (slot == 1)) + if ((item != null) && item.isPotion()) { - // auto potion - final ItemInstance item = player.getInventory().getItemByObjectId(shortcut.getId()); - if ((item == null) || !item.isPotion()) - { - return; - } + player.addAutoPotionItem(item.getId()); + return; } - - // TODO: auto skill - client.sendPacket(new ExActivateAutoShortcut(_room, _activate)); + } + // TODO: auto skill + if (skill != null) + { + player.addAutoSkill(skill.getId()); } } }