From 37ae98c3953721fb9da3fe7ff6e542a9c678cd15 Mon Sep 17 00:00:00 2001 From: MobiusDevelopment <8391001+MobiusDevelopment@users.noreply.github.com> Date: Tue, 29 Sep 2020 22:50:28 +0000 Subject: [PATCH] Addition of AutoPotionTaskManager. --- .../voicedcommandhandlers/AutoPotion.java | 42 +----- .../actor/tasks/player/AutoPotionTask.java | 105 ------------- .../taskmanager/AutoPotionTaskManager.java | 142 ++++++++++++++++++ .../voicedcommandhandlers/AutoPotion.java | 42 +----- .../actor/tasks/player/AutoPotionTask.java | 105 ------------- .../taskmanager/AutoPotionTaskManager.java | 142 ++++++++++++++++++ .../voicedcommandhandlers/AutoPotion.java | 42 +----- .../actor/tasks/player/AutoPotionTask.java | 105 ------------- .../taskmanager/AutoPotionTaskManager.java | 142 ++++++++++++++++++ .../voicedcommandhandlers/AutoPotion.java | 42 +----- .../actor/tasks/player/AutoPotionTask.java | 105 ------------- .../taskmanager/AutoPotionTaskManager.java | 142 ++++++++++++++++++ .../voicedcommandhandlers/AutoPotion.java | 42 +----- .../actor/tasks/player/AutoPotionTask.java | 105 ------------- .../taskmanager/AutoPotionTaskManager.java | 142 ++++++++++++++++++ .../voicedcommandhandlers/AutoPotion.java | 42 +----- .../actor/tasks/player/AutoPotionTask.java | 105 ------------- .../taskmanager/AutoPotionTaskManager.java | 142 ++++++++++++++++++ .../voicedcommandhandlers/AutoPotion.java | 42 +----- .../actor/tasks/player/AutoPotionTask.java | 105 ------------- .../taskmanager/AutoPotionTaskManager.java | 142 ++++++++++++++++++ .../voicedcommandhandlers/AutoPotion.java | 42 +----- .../actor/tasks/player/AutoPotionTask.java | 105 ------------- .../taskmanager/AutoPotionTaskManager.java | 142 ++++++++++++++++++ .../voicedcommandhandlers/AutoPotion.java | 42 +----- .../actor/tasks/player/AutoPotionTask.java | 105 ------------- .../taskmanager/AutoPotionTaskManager.java | 142 ++++++++++++++++++ .../voicedcommandhandlers/AutoPotion.java | 42 +----- .../actor/tasks/player/AutoPotionTask.java | 105 ------------- .../taskmanager/AutoPotionTaskManager.java | 142 ++++++++++++++++++ .../voicedcommandhandlers/AutoPotion.java | 42 +----- .../actor/tasks/player/AutoPotionTask.java | 105 ------------- .../taskmanager/AutoPotionTaskManager.java | 142 ++++++++++++++++++ .../voicedcommandhandlers/AutoPotion.java | 42 +----- .../actor/tasks/player/AutoPotionTask.java | 105 ------------- .../taskmanager/AutoPotionTaskManager.java | 142 ++++++++++++++++++ .../voicedcommandhandlers/AutoPotion.java | 42 +----- .../actor/tasks/player/AutoPotionTask.java | 105 ------------- .../taskmanager/AutoPotionTaskManager.java | 142 ++++++++++++++++++ .../voicedcommandhandlers/AutoPotion.java | 42 +----- .../actor/tasks/player/AutoPotionTask.java | 105 ------------- .../taskmanager/AutoPotionTaskManager.java | 142 ++++++++++++++++++ .../voicedcommandhandlers/AutoPotion.java | 42 +----- .../actor/tasks/player/AutoPotionTask.java | 105 ------------- .../taskmanager/AutoPotionTaskManager.java | 142 ++++++++++++++++++ .../voicedcommandhandlers/AutoPotion.java | 42 +----- .../actor/tasks/player/AutoPotionTask.java | 105 ------------- .../taskmanager/AutoPotionTaskManager.java | 142 ++++++++++++++++++ .../voicedcommandhandlers/AutoPotion.java | 42 +----- .../actor/tasks/player/AutoPotionTask.java | 105 ------------- .../taskmanager/AutoPotionTaskManager.java | 142 ++++++++++++++++++ 51 files changed, 2482 insertions(+), 2431 deletions(-) delete mode 100644 L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java create mode 100644 L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java delete mode 100644 L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java create mode 100644 L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java delete mode 100644 L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java create mode 100644 L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java delete mode 100644 L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java create mode 100644 L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java delete mode 100644 L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java create mode 100644 L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java delete mode 100644 L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java create mode 100644 L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java delete mode 100644 L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java create mode 100644 L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java delete mode 100644 L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java create mode 100644 L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java delete mode 100644 L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java create mode 100644 L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java delete mode 100644 L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java create mode 100644 L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java delete mode 100644 L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java create mode 100644 L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java delete mode 100644 L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java create mode 100644 L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java delete mode 100644 L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java create mode 100644 L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java delete mode 100644 L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java create mode 100644 L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java delete mode 100644 L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java create mode 100644 L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java delete mode 100644 L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java create mode 100644 L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java delete mode 100644 L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java create mode 100644 L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java diff --git a/L2J_Mobius_1.0_Ertheia/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java b/L2J_Mobius_1.0_Ertheia/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java index 722da268b9..5e40a92ff0 100644 --- a/L2J_Mobius_1.0_Ertheia/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java +++ b/L2J_Mobius_1.0_Ertheia/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java @@ -16,28 +16,16 @@ */ package handlers.voicedcommandhandlers; -import java.util.HashMap; -import java.util.concurrent.Future; - import org.l2jmobius.Config; -import org.l2jmobius.commons.concurrent.ThreadPool; import org.l2jmobius.gameserver.handler.IVoicedCommandHandler; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.actor.tasks.player.AutoPotionTask; -import org.l2jmobius.gameserver.model.events.EventType; -import org.l2jmobius.gameserver.model.events.ListenerRegisterType; -import org.l2jmobius.gameserver.model.events.annotations.RegisterEvent; -import org.l2jmobius.gameserver.model.events.annotations.RegisterType; -import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerLogout; +import org.l2jmobius.gameserver.taskmanager.AutoPotionTaskManager; /** - * @author Gigi, Mobius + * @author Mobius, Gigi */ public class AutoPotion implements IVoicedCommandHandler { - private static final HashMap> AUTO_POTION_TASKS = new HashMap<>(); - private static final int POTION_TASK_DELAY = 1000; // 1 second - private static final String[] VOICED_COMMANDS = { "apon", @@ -57,42 +45,20 @@ public class AutoPotion implements IVoicedCommandHandler return false; } - final int playerOID = activeChar.getObjectId(); if (command.equals("apon")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - AUTO_POTION_TASKS.put(activeChar.getObjectId(), ThreadPool.scheduleAtFixedRate(new AutoPotionTask(activeChar), POTION_TASK_DELAY, POTION_TASK_DELAY)); + AutoPotionTaskManager.getInstance().add(activeChar); activeChar.sendMessage("Auto potions is enabled."); return true; } else if (command.equals("apoff")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } + AutoPotionTaskManager.getInstance().remove(activeChar); activeChar.sendMessage("Auto potions is disabled."); } return false; } - @RegisterEvent(EventType.ON_PLAYER_LOGOUT) - @RegisterType(ListenerRegisterType.GLOBAL) - public void OnPlayerLogout(OnPlayerLogout event) - { - final int playerOID = event.getPlayer().getObjectId(); - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - } - @Override public String[] getVoicedCommandList() { diff --git a/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java b/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java deleted file mode 100644 index 3366932e4e..0000000000 --- a/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.l2jmobius.gameserver.model.actor.tasks.player; - -import org.l2jmobius.Config; -import org.l2jmobius.gameserver.handler.ItemHandler; -import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.items.instance.ItemInstance; - -/** - * @author Mobius - */ -public class AutoPotionTask implements Runnable -{ - private final PlayerInstance _player; - - public AutoPotionTask(PlayerInstance player) - { - _player = player; - } - - @Override - public void run() - { - if ((_player == null) || (_player.isOnlineInt() != 1) || _player.isAlikeDead() || (!Config.AUTO_POTIONS_IN_OLYMPIAD && _player.isInOlympiadMode())) - { - return; - } - - boolean success = false; - if (Config.AUTO_HP_ENABLED) - { - final boolean restoreHP = ((_player.getStatus().getCurrentHp() / _player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; - for (int itemId : Config.AUTO_HP_ITEM_IDS) - { - final ItemInstance hpPotion = _player.getInventory().getItemByItemId(itemId); - if ((hpPotion != null) && (hpPotion.getCount() > 0)) - { - success = true; - if (restoreHP) - { - ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(_player, hpPotion, false); - _player.sendMessage("Auto potion: Restored HP."); - break; - } - } - } - } - if (Config.AUTO_CP_ENABLED) - { - final boolean restoreCP = ((_player.getStatus().getCurrentCp() / _player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; - for (int itemId : Config.AUTO_CP_ITEM_IDS) - { - final ItemInstance cpPotion = _player.getInventory().getItemByItemId(itemId); - if ((cpPotion != null) && (cpPotion.getCount() > 0)) - { - success = true; - if (restoreCP) - { - ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(_player, cpPotion, false); - _player.sendMessage("Auto potion: Restored CP."); - break; - } - } - } - } - if (Config.AUTO_MP_ENABLED) - { - final boolean restoreMP = ((_player.getStatus().getCurrentMp() / _player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; - for (int itemId : Config.AUTO_MP_ITEM_IDS) - { - final ItemInstance mpPotion = _player.getInventory().getItemByItemId(itemId); - if ((mpPotion != null) && (mpPotion.getCount() > 0)) - { - success = true; - if (restoreMP) - { - ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(_player, mpPotion, false); - _player.sendMessage("Auto potion: Restored MP."); - break; - } - } - } - } - - if (!success) - { - _player.sendMessage("Auto potion: You are out of potions!"); - } - } -} diff --git a/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java b/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java new file mode 100644 index 0000000000..46f97cf7d9 --- /dev/null +++ b/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java @@ -0,0 +1,142 @@ +/* + * This file is part of the L2J Mobius project. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.l2jmobius.gameserver.taskmanager; + +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import org.l2jmobius.Config; +import org.l2jmobius.commons.concurrent.ThreadPool; +import org.l2jmobius.gameserver.handler.ItemHandler; +import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; +import org.l2jmobius.gameserver.model.items.instance.ItemInstance; + +/** + * @author Mobius, Gigi + */ +public class AutoPotionTaskManager +{ + private static final Set PLAYERS = ConcurrentHashMap.newKeySet(); + private static boolean _working = false; + + public AutoPotionTaskManager() + { + ThreadPool.scheduleAtFixedRate(() -> + { + if (_working) + { + return; + } + _working = true; + + PLAYER: for (PlayerInstance player : PLAYERS) + { + if ((player == null) || player.isAlikeDead() || (player.isOnlineInt() != 1) || (!Config.AUTO_POTIONS_IN_OLYMPIAD && player.isInOlympiadMode())) + { + remove(player); + continue PLAYER; + } + + boolean success = false; + if (Config.AUTO_HP_ENABLED) + { + final boolean restoreHP = ((player.getStatus().getCurrentHp() / player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; + HP: for (int itemId : Config.AUTO_HP_ITEM_IDS) + { + final ItemInstance hpPotion = player.getInventory().getItemByItemId(itemId); + if ((hpPotion != null) && (hpPotion.getCount() > 0)) + { + success = true; + if (restoreHP) + { + ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(player, hpPotion, false); + player.sendMessage("Auto potion: Restored HP."); + break HP; + } + } + } + } + if (Config.AUTO_CP_ENABLED) + { + final boolean restoreCP = ((player.getStatus().getCurrentCp() / player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; + CP: for (int itemId : Config.AUTO_CP_ITEM_IDS) + { + final ItemInstance cpPotion = player.getInventory().getItemByItemId(itemId); + if ((cpPotion != null) && (cpPotion.getCount() > 0)) + { + success = true; + if (restoreCP) + { + ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(player, cpPotion, false); + player.sendMessage("Auto potion: Restored CP."); + break CP; + } + } + } + } + if (Config.AUTO_MP_ENABLED) + { + final boolean restoreMP = ((player.getStatus().getCurrentMp() / player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; + MP: for (int itemId : Config.AUTO_MP_ITEM_IDS) + { + final ItemInstance mpPotion = player.getInventory().getItemByItemId(itemId); + if ((mpPotion != null) && (mpPotion.getCount() > 0)) + { + success = true; + if (restoreMP) + { + ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(player, mpPotion, false); + player.sendMessage("Auto potion: Restored MP."); + break MP; + } + } + } + } + + if (!success) + { + player.sendMessage("Auto potion: You are out of potions!"); + } + } + + _working = false; + }, 0, 1000); + } + + public void add(PlayerInstance player) + { + if (!PLAYERS.contains(player)) + { + PLAYERS.add(player); + } + } + + public void remove(PlayerInstance player) + { + PLAYERS.remove(player); + } + + public static AutoPotionTaskManager getInstance() + { + return SingletonHolder.INSTANCE; + } + + private static class SingletonHolder + { + protected static final AutoPotionTaskManager INSTANCE = new AutoPotionTaskManager(); + } +} diff --git a/L2J_Mobius_2.5_Underground/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java b/L2J_Mobius_2.5_Underground/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java index 722da268b9..5e40a92ff0 100644 --- a/L2J_Mobius_2.5_Underground/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java +++ b/L2J_Mobius_2.5_Underground/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java @@ -16,28 +16,16 @@ */ package handlers.voicedcommandhandlers; -import java.util.HashMap; -import java.util.concurrent.Future; - import org.l2jmobius.Config; -import org.l2jmobius.commons.concurrent.ThreadPool; import org.l2jmobius.gameserver.handler.IVoicedCommandHandler; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.actor.tasks.player.AutoPotionTask; -import org.l2jmobius.gameserver.model.events.EventType; -import org.l2jmobius.gameserver.model.events.ListenerRegisterType; -import org.l2jmobius.gameserver.model.events.annotations.RegisterEvent; -import org.l2jmobius.gameserver.model.events.annotations.RegisterType; -import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerLogout; +import org.l2jmobius.gameserver.taskmanager.AutoPotionTaskManager; /** - * @author Gigi, Mobius + * @author Mobius, Gigi */ public class AutoPotion implements IVoicedCommandHandler { - private static final HashMap> AUTO_POTION_TASKS = new HashMap<>(); - private static final int POTION_TASK_DELAY = 1000; // 1 second - private static final String[] VOICED_COMMANDS = { "apon", @@ -57,42 +45,20 @@ public class AutoPotion implements IVoicedCommandHandler return false; } - final int playerOID = activeChar.getObjectId(); if (command.equals("apon")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - AUTO_POTION_TASKS.put(activeChar.getObjectId(), ThreadPool.scheduleAtFixedRate(new AutoPotionTask(activeChar), POTION_TASK_DELAY, POTION_TASK_DELAY)); + AutoPotionTaskManager.getInstance().add(activeChar); activeChar.sendMessage("Auto potions is enabled."); return true; } else if (command.equals("apoff")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } + AutoPotionTaskManager.getInstance().remove(activeChar); activeChar.sendMessage("Auto potions is disabled."); } return false; } - @RegisterEvent(EventType.ON_PLAYER_LOGOUT) - @RegisterType(ListenerRegisterType.GLOBAL) - public void OnPlayerLogout(OnPlayerLogout event) - { - final int playerOID = event.getPlayer().getObjectId(); - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - } - @Override public String[] getVoicedCommandList() { diff --git a/L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java b/L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java deleted file mode 100644 index 3366932e4e..0000000000 --- a/L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.l2jmobius.gameserver.model.actor.tasks.player; - -import org.l2jmobius.Config; -import org.l2jmobius.gameserver.handler.ItemHandler; -import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.items.instance.ItemInstance; - -/** - * @author Mobius - */ -public class AutoPotionTask implements Runnable -{ - private final PlayerInstance _player; - - public AutoPotionTask(PlayerInstance player) - { - _player = player; - } - - @Override - public void run() - { - if ((_player == null) || (_player.isOnlineInt() != 1) || _player.isAlikeDead() || (!Config.AUTO_POTIONS_IN_OLYMPIAD && _player.isInOlympiadMode())) - { - return; - } - - boolean success = false; - if (Config.AUTO_HP_ENABLED) - { - final boolean restoreHP = ((_player.getStatus().getCurrentHp() / _player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; - for (int itemId : Config.AUTO_HP_ITEM_IDS) - { - final ItemInstance hpPotion = _player.getInventory().getItemByItemId(itemId); - if ((hpPotion != null) && (hpPotion.getCount() > 0)) - { - success = true; - if (restoreHP) - { - ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(_player, hpPotion, false); - _player.sendMessage("Auto potion: Restored HP."); - break; - } - } - } - } - if (Config.AUTO_CP_ENABLED) - { - final boolean restoreCP = ((_player.getStatus().getCurrentCp() / _player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; - for (int itemId : Config.AUTO_CP_ITEM_IDS) - { - final ItemInstance cpPotion = _player.getInventory().getItemByItemId(itemId); - if ((cpPotion != null) && (cpPotion.getCount() > 0)) - { - success = true; - if (restoreCP) - { - ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(_player, cpPotion, false); - _player.sendMessage("Auto potion: Restored CP."); - break; - } - } - } - } - if (Config.AUTO_MP_ENABLED) - { - final boolean restoreMP = ((_player.getStatus().getCurrentMp() / _player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; - for (int itemId : Config.AUTO_MP_ITEM_IDS) - { - final ItemInstance mpPotion = _player.getInventory().getItemByItemId(itemId); - if ((mpPotion != null) && (mpPotion.getCount() > 0)) - { - success = true; - if (restoreMP) - { - ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(_player, mpPotion, false); - _player.sendMessage("Auto potion: Restored MP."); - break; - } - } - } - } - - if (!success) - { - _player.sendMessage("Auto potion: You are out of potions!"); - } - } -} diff --git a/L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java b/L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java new file mode 100644 index 0000000000..46f97cf7d9 --- /dev/null +++ b/L2J_Mobius_2.5_Underground/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java @@ -0,0 +1,142 @@ +/* + * This file is part of the L2J Mobius project. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.l2jmobius.gameserver.taskmanager; + +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import org.l2jmobius.Config; +import org.l2jmobius.commons.concurrent.ThreadPool; +import org.l2jmobius.gameserver.handler.ItemHandler; +import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; +import org.l2jmobius.gameserver.model.items.instance.ItemInstance; + +/** + * @author Mobius, Gigi + */ +public class AutoPotionTaskManager +{ + private static final Set PLAYERS = ConcurrentHashMap.newKeySet(); + private static boolean _working = false; + + public AutoPotionTaskManager() + { + ThreadPool.scheduleAtFixedRate(() -> + { + if (_working) + { + return; + } + _working = true; + + PLAYER: for (PlayerInstance player : PLAYERS) + { + if ((player == null) || player.isAlikeDead() || (player.isOnlineInt() != 1) || (!Config.AUTO_POTIONS_IN_OLYMPIAD && player.isInOlympiadMode())) + { + remove(player); + continue PLAYER; + } + + boolean success = false; + if (Config.AUTO_HP_ENABLED) + { + final boolean restoreHP = ((player.getStatus().getCurrentHp() / player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; + HP: for (int itemId : Config.AUTO_HP_ITEM_IDS) + { + final ItemInstance hpPotion = player.getInventory().getItemByItemId(itemId); + if ((hpPotion != null) && (hpPotion.getCount() > 0)) + { + success = true; + if (restoreHP) + { + ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(player, hpPotion, false); + player.sendMessage("Auto potion: Restored HP."); + break HP; + } + } + } + } + if (Config.AUTO_CP_ENABLED) + { + final boolean restoreCP = ((player.getStatus().getCurrentCp() / player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; + CP: for (int itemId : Config.AUTO_CP_ITEM_IDS) + { + final ItemInstance cpPotion = player.getInventory().getItemByItemId(itemId); + if ((cpPotion != null) && (cpPotion.getCount() > 0)) + { + success = true; + if (restoreCP) + { + ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(player, cpPotion, false); + player.sendMessage("Auto potion: Restored CP."); + break CP; + } + } + } + } + if (Config.AUTO_MP_ENABLED) + { + final boolean restoreMP = ((player.getStatus().getCurrentMp() / player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; + MP: for (int itemId : Config.AUTO_MP_ITEM_IDS) + { + final ItemInstance mpPotion = player.getInventory().getItemByItemId(itemId); + if ((mpPotion != null) && (mpPotion.getCount() > 0)) + { + success = true; + if (restoreMP) + { + ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(player, mpPotion, false); + player.sendMessage("Auto potion: Restored MP."); + break MP; + } + } + } + } + + if (!success) + { + player.sendMessage("Auto potion: You are out of potions!"); + } + } + + _working = false; + }, 0, 1000); + } + + public void add(PlayerInstance player) + { + if (!PLAYERS.contains(player)) + { + PLAYERS.add(player); + } + } + + public void remove(PlayerInstance player) + { + PLAYERS.remove(player); + } + + public static AutoPotionTaskManager getInstance() + { + return SingletonHolder.INSTANCE; + } + + private static class SingletonHolder + { + protected static final AutoPotionTaskManager INSTANCE = new AutoPotionTaskManager(); + } +} diff --git a/L2J_Mobius_3.0_Helios/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java b/L2J_Mobius_3.0_Helios/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java index 722da268b9..5e40a92ff0 100644 --- a/L2J_Mobius_3.0_Helios/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java +++ b/L2J_Mobius_3.0_Helios/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java @@ -16,28 +16,16 @@ */ package handlers.voicedcommandhandlers; -import java.util.HashMap; -import java.util.concurrent.Future; - import org.l2jmobius.Config; -import org.l2jmobius.commons.concurrent.ThreadPool; import org.l2jmobius.gameserver.handler.IVoicedCommandHandler; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.actor.tasks.player.AutoPotionTask; -import org.l2jmobius.gameserver.model.events.EventType; -import org.l2jmobius.gameserver.model.events.ListenerRegisterType; -import org.l2jmobius.gameserver.model.events.annotations.RegisterEvent; -import org.l2jmobius.gameserver.model.events.annotations.RegisterType; -import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerLogout; +import org.l2jmobius.gameserver.taskmanager.AutoPotionTaskManager; /** - * @author Gigi, Mobius + * @author Mobius, Gigi */ public class AutoPotion implements IVoicedCommandHandler { - private static final HashMap> AUTO_POTION_TASKS = new HashMap<>(); - private static final int POTION_TASK_DELAY = 1000; // 1 second - private static final String[] VOICED_COMMANDS = { "apon", @@ -57,42 +45,20 @@ public class AutoPotion implements IVoicedCommandHandler return false; } - final int playerOID = activeChar.getObjectId(); if (command.equals("apon")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - AUTO_POTION_TASKS.put(activeChar.getObjectId(), ThreadPool.scheduleAtFixedRate(new AutoPotionTask(activeChar), POTION_TASK_DELAY, POTION_TASK_DELAY)); + AutoPotionTaskManager.getInstance().add(activeChar); activeChar.sendMessage("Auto potions is enabled."); return true; } else if (command.equals("apoff")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } + AutoPotionTaskManager.getInstance().remove(activeChar); activeChar.sendMessage("Auto potions is disabled."); } return false; } - @RegisterEvent(EventType.ON_PLAYER_LOGOUT) - @RegisterType(ListenerRegisterType.GLOBAL) - public void OnPlayerLogout(OnPlayerLogout event) - { - final int playerOID = event.getPlayer().getObjectId(); - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - } - @Override public String[] getVoicedCommandList() { diff --git a/L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java b/L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java deleted file mode 100644 index 3366932e4e..0000000000 --- a/L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.l2jmobius.gameserver.model.actor.tasks.player; - -import org.l2jmobius.Config; -import org.l2jmobius.gameserver.handler.ItemHandler; -import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.items.instance.ItemInstance; - -/** - * @author Mobius - */ -public class AutoPotionTask implements Runnable -{ - private final PlayerInstance _player; - - public AutoPotionTask(PlayerInstance player) - { - _player = player; - } - - @Override - public void run() - { - if ((_player == null) || (_player.isOnlineInt() != 1) || _player.isAlikeDead() || (!Config.AUTO_POTIONS_IN_OLYMPIAD && _player.isInOlympiadMode())) - { - return; - } - - boolean success = false; - if (Config.AUTO_HP_ENABLED) - { - final boolean restoreHP = ((_player.getStatus().getCurrentHp() / _player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; - for (int itemId : Config.AUTO_HP_ITEM_IDS) - { - final ItemInstance hpPotion = _player.getInventory().getItemByItemId(itemId); - if ((hpPotion != null) && (hpPotion.getCount() > 0)) - { - success = true; - if (restoreHP) - { - ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(_player, hpPotion, false); - _player.sendMessage("Auto potion: Restored HP."); - break; - } - } - } - } - if (Config.AUTO_CP_ENABLED) - { - final boolean restoreCP = ((_player.getStatus().getCurrentCp() / _player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; - for (int itemId : Config.AUTO_CP_ITEM_IDS) - { - final ItemInstance cpPotion = _player.getInventory().getItemByItemId(itemId); - if ((cpPotion != null) && (cpPotion.getCount() > 0)) - { - success = true; - if (restoreCP) - { - ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(_player, cpPotion, false); - _player.sendMessage("Auto potion: Restored CP."); - break; - } - } - } - } - if (Config.AUTO_MP_ENABLED) - { - final boolean restoreMP = ((_player.getStatus().getCurrentMp() / _player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; - for (int itemId : Config.AUTO_MP_ITEM_IDS) - { - final ItemInstance mpPotion = _player.getInventory().getItemByItemId(itemId); - if ((mpPotion != null) && (mpPotion.getCount() > 0)) - { - success = true; - if (restoreMP) - { - ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(_player, mpPotion, false); - _player.sendMessage("Auto potion: Restored MP."); - break; - } - } - } - } - - if (!success) - { - _player.sendMessage("Auto potion: You are out of potions!"); - } - } -} diff --git a/L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java b/L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java new file mode 100644 index 0000000000..46f97cf7d9 --- /dev/null +++ b/L2J_Mobius_3.0_Helios/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java @@ -0,0 +1,142 @@ +/* + * This file is part of the L2J Mobius project. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.l2jmobius.gameserver.taskmanager; + +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import org.l2jmobius.Config; +import org.l2jmobius.commons.concurrent.ThreadPool; +import org.l2jmobius.gameserver.handler.ItemHandler; +import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; +import org.l2jmobius.gameserver.model.items.instance.ItemInstance; + +/** + * @author Mobius, Gigi + */ +public class AutoPotionTaskManager +{ + private static final Set PLAYERS = ConcurrentHashMap.newKeySet(); + private static boolean _working = false; + + public AutoPotionTaskManager() + { + ThreadPool.scheduleAtFixedRate(() -> + { + if (_working) + { + return; + } + _working = true; + + PLAYER: for (PlayerInstance player : PLAYERS) + { + if ((player == null) || player.isAlikeDead() || (player.isOnlineInt() != 1) || (!Config.AUTO_POTIONS_IN_OLYMPIAD && player.isInOlympiadMode())) + { + remove(player); + continue PLAYER; + } + + boolean success = false; + if (Config.AUTO_HP_ENABLED) + { + final boolean restoreHP = ((player.getStatus().getCurrentHp() / player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; + HP: for (int itemId : Config.AUTO_HP_ITEM_IDS) + { + final ItemInstance hpPotion = player.getInventory().getItemByItemId(itemId); + if ((hpPotion != null) && (hpPotion.getCount() > 0)) + { + success = true; + if (restoreHP) + { + ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(player, hpPotion, false); + player.sendMessage("Auto potion: Restored HP."); + break HP; + } + } + } + } + if (Config.AUTO_CP_ENABLED) + { + final boolean restoreCP = ((player.getStatus().getCurrentCp() / player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; + CP: for (int itemId : Config.AUTO_CP_ITEM_IDS) + { + final ItemInstance cpPotion = player.getInventory().getItemByItemId(itemId); + if ((cpPotion != null) && (cpPotion.getCount() > 0)) + { + success = true; + if (restoreCP) + { + ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(player, cpPotion, false); + player.sendMessage("Auto potion: Restored CP."); + break CP; + } + } + } + } + if (Config.AUTO_MP_ENABLED) + { + final boolean restoreMP = ((player.getStatus().getCurrentMp() / player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; + MP: for (int itemId : Config.AUTO_MP_ITEM_IDS) + { + final ItemInstance mpPotion = player.getInventory().getItemByItemId(itemId); + if ((mpPotion != null) && (mpPotion.getCount() > 0)) + { + success = true; + if (restoreMP) + { + ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(player, mpPotion, false); + player.sendMessage("Auto potion: Restored MP."); + break MP; + } + } + } + } + + if (!success) + { + player.sendMessage("Auto potion: You are out of potions!"); + } + } + + _working = false; + }, 0, 1000); + } + + public void add(PlayerInstance player) + { + if (!PLAYERS.contains(player)) + { + PLAYERS.add(player); + } + } + + public void remove(PlayerInstance player) + { + PLAYERS.remove(player); + } + + public static AutoPotionTaskManager getInstance() + { + return SingletonHolder.INSTANCE; + } + + private static class SingletonHolder + { + protected static final AutoPotionTaskManager INSTANCE = new AutoPotionTaskManager(); + } +} diff --git a/L2J_Mobius_4.0_GrandCrusade/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java b/L2J_Mobius_4.0_GrandCrusade/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java index 722da268b9..5e40a92ff0 100644 --- a/L2J_Mobius_4.0_GrandCrusade/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java +++ b/L2J_Mobius_4.0_GrandCrusade/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java @@ -16,28 +16,16 @@ */ package handlers.voicedcommandhandlers; -import java.util.HashMap; -import java.util.concurrent.Future; - import org.l2jmobius.Config; -import org.l2jmobius.commons.concurrent.ThreadPool; import org.l2jmobius.gameserver.handler.IVoicedCommandHandler; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.actor.tasks.player.AutoPotionTask; -import org.l2jmobius.gameserver.model.events.EventType; -import org.l2jmobius.gameserver.model.events.ListenerRegisterType; -import org.l2jmobius.gameserver.model.events.annotations.RegisterEvent; -import org.l2jmobius.gameserver.model.events.annotations.RegisterType; -import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerLogout; +import org.l2jmobius.gameserver.taskmanager.AutoPotionTaskManager; /** - * @author Gigi, Mobius + * @author Mobius, Gigi */ public class AutoPotion implements IVoicedCommandHandler { - private static final HashMap> AUTO_POTION_TASKS = new HashMap<>(); - private static final int POTION_TASK_DELAY = 1000; // 1 second - private static final String[] VOICED_COMMANDS = { "apon", @@ -57,42 +45,20 @@ public class AutoPotion implements IVoicedCommandHandler return false; } - final int playerOID = activeChar.getObjectId(); if (command.equals("apon")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - AUTO_POTION_TASKS.put(activeChar.getObjectId(), ThreadPool.scheduleAtFixedRate(new AutoPotionTask(activeChar), POTION_TASK_DELAY, POTION_TASK_DELAY)); + AutoPotionTaskManager.getInstance().add(activeChar); activeChar.sendMessage("Auto potions is enabled."); return true; } else if (command.equals("apoff")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } + AutoPotionTaskManager.getInstance().remove(activeChar); activeChar.sendMessage("Auto potions is disabled."); } return false; } - @RegisterEvent(EventType.ON_PLAYER_LOGOUT) - @RegisterType(ListenerRegisterType.GLOBAL) - public void OnPlayerLogout(OnPlayerLogout event) - { - final int playerOID = event.getPlayer().getObjectId(); - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - } - @Override public String[] getVoicedCommandList() { diff --git a/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java b/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java deleted file mode 100644 index 3366932e4e..0000000000 --- a/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.l2jmobius.gameserver.model.actor.tasks.player; - -import org.l2jmobius.Config; -import org.l2jmobius.gameserver.handler.ItemHandler; -import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.items.instance.ItemInstance; - -/** - * @author Mobius - */ -public class AutoPotionTask implements Runnable -{ - private final PlayerInstance _player; - - public AutoPotionTask(PlayerInstance player) - { - _player = player; - } - - @Override - public void run() - { - if ((_player == null) || (_player.isOnlineInt() != 1) || _player.isAlikeDead() || (!Config.AUTO_POTIONS_IN_OLYMPIAD && _player.isInOlympiadMode())) - { - return; - } - - boolean success = false; - if (Config.AUTO_HP_ENABLED) - { - final boolean restoreHP = ((_player.getStatus().getCurrentHp() / _player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; - for (int itemId : Config.AUTO_HP_ITEM_IDS) - { - final ItemInstance hpPotion = _player.getInventory().getItemByItemId(itemId); - if ((hpPotion != null) && (hpPotion.getCount() > 0)) - { - success = true; - if (restoreHP) - { - ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(_player, hpPotion, false); - _player.sendMessage("Auto potion: Restored HP."); - break; - } - } - } - } - if (Config.AUTO_CP_ENABLED) - { - final boolean restoreCP = ((_player.getStatus().getCurrentCp() / _player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; - for (int itemId : Config.AUTO_CP_ITEM_IDS) - { - final ItemInstance cpPotion = _player.getInventory().getItemByItemId(itemId); - if ((cpPotion != null) && (cpPotion.getCount() > 0)) - { - success = true; - if (restoreCP) - { - ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(_player, cpPotion, false); - _player.sendMessage("Auto potion: Restored CP."); - break; - } - } - } - } - if (Config.AUTO_MP_ENABLED) - { - final boolean restoreMP = ((_player.getStatus().getCurrentMp() / _player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; - for (int itemId : Config.AUTO_MP_ITEM_IDS) - { - final ItemInstance mpPotion = _player.getInventory().getItemByItemId(itemId); - if ((mpPotion != null) && (mpPotion.getCount() > 0)) - { - success = true; - if (restoreMP) - { - ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(_player, mpPotion, false); - _player.sendMessage("Auto potion: Restored MP."); - break; - } - } - } - } - - if (!success) - { - _player.sendMessage("Auto potion: You are out of potions!"); - } - } -} diff --git a/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java b/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java new file mode 100644 index 0000000000..46f97cf7d9 --- /dev/null +++ b/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java @@ -0,0 +1,142 @@ +/* + * This file is part of the L2J Mobius project. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.l2jmobius.gameserver.taskmanager; + +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import org.l2jmobius.Config; +import org.l2jmobius.commons.concurrent.ThreadPool; +import org.l2jmobius.gameserver.handler.ItemHandler; +import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; +import org.l2jmobius.gameserver.model.items.instance.ItemInstance; + +/** + * @author Mobius, Gigi + */ +public class AutoPotionTaskManager +{ + private static final Set PLAYERS = ConcurrentHashMap.newKeySet(); + private static boolean _working = false; + + public AutoPotionTaskManager() + { + ThreadPool.scheduleAtFixedRate(() -> + { + if (_working) + { + return; + } + _working = true; + + PLAYER: for (PlayerInstance player : PLAYERS) + { + if ((player == null) || player.isAlikeDead() || (player.isOnlineInt() != 1) || (!Config.AUTO_POTIONS_IN_OLYMPIAD && player.isInOlympiadMode())) + { + remove(player); + continue PLAYER; + } + + boolean success = false; + if (Config.AUTO_HP_ENABLED) + { + final boolean restoreHP = ((player.getStatus().getCurrentHp() / player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; + HP: for (int itemId : Config.AUTO_HP_ITEM_IDS) + { + final ItemInstance hpPotion = player.getInventory().getItemByItemId(itemId); + if ((hpPotion != null) && (hpPotion.getCount() > 0)) + { + success = true; + if (restoreHP) + { + ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(player, hpPotion, false); + player.sendMessage("Auto potion: Restored HP."); + break HP; + } + } + } + } + if (Config.AUTO_CP_ENABLED) + { + final boolean restoreCP = ((player.getStatus().getCurrentCp() / player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; + CP: for (int itemId : Config.AUTO_CP_ITEM_IDS) + { + final ItemInstance cpPotion = player.getInventory().getItemByItemId(itemId); + if ((cpPotion != null) && (cpPotion.getCount() > 0)) + { + success = true; + if (restoreCP) + { + ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(player, cpPotion, false); + player.sendMessage("Auto potion: Restored CP."); + break CP; + } + } + } + } + if (Config.AUTO_MP_ENABLED) + { + final boolean restoreMP = ((player.getStatus().getCurrentMp() / player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; + MP: for (int itemId : Config.AUTO_MP_ITEM_IDS) + { + final ItemInstance mpPotion = player.getInventory().getItemByItemId(itemId); + if ((mpPotion != null) && (mpPotion.getCount() > 0)) + { + success = true; + if (restoreMP) + { + ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(player, mpPotion, false); + player.sendMessage("Auto potion: Restored MP."); + break MP; + } + } + } + } + + if (!success) + { + player.sendMessage("Auto potion: You are out of potions!"); + } + } + + _working = false; + }, 0, 1000); + } + + public void add(PlayerInstance player) + { + if (!PLAYERS.contains(player)) + { + PLAYERS.add(player); + } + } + + public void remove(PlayerInstance player) + { + PLAYERS.remove(player); + } + + public static AutoPotionTaskManager getInstance() + { + return SingletonHolder.INSTANCE; + } + + private static class SingletonHolder + { + protected static final AutoPotionTaskManager INSTANCE = new AutoPotionTaskManager(); + } +} diff --git a/L2J_Mobius_5.0_Salvation/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java b/L2J_Mobius_5.0_Salvation/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java index 722da268b9..5e40a92ff0 100644 --- a/L2J_Mobius_5.0_Salvation/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java +++ b/L2J_Mobius_5.0_Salvation/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java @@ -16,28 +16,16 @@ */ package handlers.voicedcommandhandlers; -import java.util.HashMap; -import java.util.concurrent.Future; - import org.l2jmobius.Config; -import org.l2jmobius.commons.concurrent.ThreadPool; import org.l2jmobius.gameserver.handler.IVoicedCommandHandler; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.actor.tasks.player.AutoPotionTask; -import org.l2jmobius.gameserver.model.events.EventType; -import org.l2jmobius.gameserver.model.events.ListenerRegisterType; -import org.l2jmobius.gameserver.model.events.annotations.RegisterEvent; -import org.l2jmobius.gameserver.model.events.annotations.RegisterType; -import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerLogout; +import org.l2jmobius.gameserver.taskmanager.AutoPotionTaskManager; /** - * @author Gigi, Mobius + * @author Mobius, Gigi */ public class AutoPotion implements IVoicedCommandHandler { - private static final HashMap> AUTO_POTION_TASKS = new HashMap<>(); - private static final int POTION_TASK_DELAY = 1000; // 1 second - private static final String[] VOICED_COMMANDS = { "apon", @@ -57,42 +45,20 @@ public class AutoPotion implements IVoicedCommandHandler return false; } - final int playerOID = activeChar.getObjectId(); if (command.equals("apon")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - AUTO_POTION_TASKS.put(activeChar.getObjectId(), ThreadPool.scheduleAtFixedRate(new AutoPotionTask(activeChar), POTION_TASK_DELAY, POTION_TASK_DELAY)); + AutoPotionTaskManager.getInstance().add(activeChar); activeChar.sendMessage("Auto potions is enabled."); return true; } else if (command.equals("apoff")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } + AutoPotionTaskManager.getInstance().remove(activeChar); activeChar.sendMessage("Auto potions is disabled."); } return false; } - @RegisterEvent(EventType.ON_PLAYER_LOGOUT) - @RegisterType(ListenerRegisterType.GLOBAL) - public void OnPlayerLogout(OnPlayerLogout event) - { - final int playerOID = event.getPlayer().getObjectId(); - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - } - @Override public String[] getVoicedCommandList() { diff --git a/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java b/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java deleted file mode 100644 index 3366932e4e..0000000000 --- a/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.l2jmobius.gameserver.model.actor.tasks.player; - -import org.l2jmobius.Config; -import org.l2jmobius.gameserver.handler.ItemHandler; -import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.items.instance.ItemInstance; - -/** - * @author Mobius - */ -public class AutoPotionTask implements Runnable -{ - private final PlayerInstance _player; - - public AutoPotionTask(PlayerInstance player) - { - _player = player; - } - - @Override - public void run() - { - if ((_player == null) || (_player.isOnlineInt() != 1) || _player.isAlikeDead() || (!Config.AUTO_POTIONS_IN_OLYMPIAD && _player.isInOlympiadMode())) - { - return; - } - - boolean success = false; - if (Config.AUTO_HP_ENABLED) - { - final boolean restoreHP = ((_player.getStatus().getCurrentHp() / _player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; - for (int itemId : Config.AUTO_HP_ITEM_IDS) - { - final ItemInstance hpPotion = _player.getInventory().getItemByItemId(itemId); - if ((hpPotion != null) && (hpPotion.getCount() > 0)) - { - success = true; - if (restoreHP) - { - ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(_player, hpPotion, false); - _player.sendMessage("Auto potion: Restored HP."); - break; - } - } - } - } - if (Config.AUTO_CP_ENABLED) - { - final boolean restoreCP = ((_player.getStatus().getCurrentCp() / _player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; - for (int itemId : Config.AUTO_CP_ITEM_IDS) - { - final ItemInstance cpPotion = _player.getInventory().getItemByItemId(itemId); - if ((cpPotion != null) && (cpPotion.getCount() > 0)) - { - success = true; - if (restoreCP) - { - ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(_player, cpPotion, false); - _player.sendMessage("Auto potion: Restored CP."); - break; - } - } - } - } - if (Config.AUTO_MP_ENABLED) - { - final boolean restoreMP = ((_player.getStatus().getCurrentMp() / _player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; - for (int itemId : Config.AUTO_MP_ITEM_IDS) - { - final ItemInstance mpPotion = _player.getInventory().getItemByItemId(itemId); - if ((mpPotion != null) && (mpPotion.getCount() > 0)) - { - success = true; - if (restoreMP) - { - ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(_player, mpPotion, false); - _player.sendMessage("Auto potion: Restored MP."); - break; - } - } - } - } - - if (!success) - { - _player.sendMessage("Auto potion: You are out of potions!"); - } - } -} diff --git a/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java b/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java new file mode 100644 index 0000000000..46f97cf7d9 --- /dev/null +++ b/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java @@ -0,0 +1,142 @@ +/* + * This file is part of the L2J Mobius project. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.l2jmobius.gameserver.taskmanager; + +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import org.l2jmobius.Config; +import org.l2jmobius.commons.concurrent.ThreadPool; +import org.l2jmobius.gameserver.handler.ItemHandler; +import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; +import org.l2jmobius.gameserver.model.items.instance.ItemInstance; + +/** + * @author Mobius, Gigi + */ +public class AutoPotionTaskManager +{ + private static final Set PLAYERS = ConcurrentHashMap.newKeySet(); + private static boolean _working = false; + + public AutoPotionTaskManager() + { + ThreadPool.scheduleAtFixedRate(() -> + { + if (_working) + { + return; + } + _working = true; + + PLAYER: for (PlayerInstance player : PLAYERS) + { + if ((player == null) || player.isAlikeDead() || (player.isOnlineInt() != 1) || (!Config.AUTO_POTIONS_IN_OLYMPIAD && player.isInOlympiadMode())) + { + remove(player); + continue PLAYER; + } + + boolean success = false; + if (Config.AUTO_HP_ENABLED) + { + final boolean restoreHP = ((player.getStatus().getCurrentHp() / player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; + HP: for (int itemId : Config.AUTO_HP_ITEM_IDS) + { + final ItemInstance hpPotion = player.getInventory().getItemByItemId(itemId); + if ((hpPotion != null) && (hpPotion.getCount() > 0)) + { + success = true; + if (restoreHP) + { + ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(player, hpPotion, false); + player.sendMessage("Auto potion: Restored HP."); + break HP; + } + } + } + } + if (Config.AUTO_CP_ENABLED) + { + final boolean restoreCP = ((player.getStatus().getCurrentCp() / player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; + CP: for (int itemId : Config.AUTO_CP_ITEM_IDS) + { + final ItemInstance cpPotion = player.getInventory().getItemByItemId(itemId); + if ((cpPotion != null) && (cpPotion.getCount() > 0)) + { + success = true; + if (restoreCP) + { + ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(player, cpPotion, false); + player.sendMessage("Auto potion: Restored CP."); + break CP; + } + } + } + } + if (Config.AUTO_MP_ENABLED) + { + final boolean restoreMP = ((player.getStatus().getCurrentMp() / player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; + MP: for (int itemId : Config.AUTO_MP_ITEM_IDS) + { + final ItemInstance mpPotion = player.getInventory().getItemByItemId(itemId); + if ((mpPotion != null) && (mpPotion.getCount() > 0)) + { + success = true; + if (restoreMP) + { + ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(player, mpPotion, false); + player.sendMessage("Auto potion: Restored MP."); + break MP; + } + } + } + } + + if (!success) + { + player.sendMessage("Auto potion: You are out of potions!"); + } + } + + _working = false; + }, 0, 1000); + } + + public void add(PlayerInstance player) + { + if (!PLAYERS.contains(player)) + { + PLAYERS.add(player); + } + } + + public void remove(PlayerInstance player) + { + PLAYERS.remove(player); + } + + public static AutoPotionTaskManager getInstance() + { + return SingletonHolder.INSTANCE; + } + + private static class SingletonHolder + { + protected static final AutoPotionTaskManager INSTANCE = new AutoPotionTaskManager(); + } +} diff --git a/L2J_Mobius_5.5_EtinasFate/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java b/L2J_Mobius_5.5_EtinasFate/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java index 722da268b9..5e40a92ff0 100644 --- a/L2J_Mobius_5.5_EtinasFate/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java +++ b/L2J_Mobius_5.5_EtinasFate/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java @@ -16,28 +16,16 @@ */ package handlers.voicedcommandhandlers; -import java.util.HashMap; -import java.util.concurrent.Future; - import org.l2jmobius.Config; -import org.l2jmobius.commons.concurrent.ThreadPool; import org.l2jmobius.gameserver.handler.IVoicedCommandHandler; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.actor.tasks.player.AutoPotionTask; -import org.l2jmobius.gameserver.model.events.EventType; -import org.l2jmobius.gameserver.model.events.ListenerRegisterType; -import org.l2jmobius.gameserver.model.events.annotations.RegisterEvent; -import org.l2jmobius.gameserver.model.events.annotations.RegisterType; -import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerLogout; +import org.l2jmobius.gameserver.taskmanager.AutoPotionTaskManager; /** - * @author Gigi, Mobius + * @author Mobius, Gigi */ public class AutoPotion implements IVoicedCommandHandler { - private static final HashMap> AUTO_POTION_TASKS = new HashMap<>(); - private static final int POTION_TASK_DELAY = 1000; // 1 second - private static final String[] VOICED_COMMANDS = { "apon", @@ -57,42 +45,20 @@ public class AutoPotion implements IVoicedCommandHandler return false; } - final int playerOID = activeChar.getObjectId(); if (command.equals("apon")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - AUTO_POTION_TASKS.put(activeChar.getObjectId(), ThreadPool.scheduleAtFixedRate(new AutoPotionTask(activeChar), POTION_TASK_DELAY, POTION_TASK_DELAY)); + AutoPotionTaskManager.getInstance().add(activeChar); activeChar.sendMessage("Auto potions is enabled."); return true; } else if (command.equals("apoff")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } + AutoPotionTaskManager.getInstance().remove(activeChar); activeChar.sendMessage("Auto potions is disabled."); } return false; } - @RegisterEvent(EventType.ON_PLAYER_LOGOUT) - @RegisterType(ListenerRegisterType.GLOBAL) - public void OnPlayerLogout(OnPlayerLogout event) - { - final int playerOID = event.getPlayer().getObjectId(); - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - } - @Override public String[] getVoicedCommandList() { diff --git a/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java b/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java deleted file mode 100644 index 3366932e4e..0000000000 --- a/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.l2jmobius.gameserver.model.actor.tasks.player; - -import org.l2jmobius.Config; -import org.l2jmobius.gameserver.handler.ItemHandler; -import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.items.instance.ItemInstance; - -/** - * @author Mobius - */ -public class AutoPotionTask implements Runnable -{ - private final PlayerInstance _player; - - public AutoPotionTask(PlayerInstance player) - { - _player = player; - } - - @Override - public void run() - { - if ((_player == null) || (_player.isOnlineInt() != 1) || _player.isAlikeDead() || (!Config.AUTO_POTIONS_IN_OLYMPIAD && _player.isInOlympiadMode())) - { - return; - } - - boolean success = false; - if (Config.AUTO_HP_ENABLED) - { - final boolean restoreHP = ((_player.getStatus().getCurrentHp() / _player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; - for (int itemId : Config.AUTO_HP_ITEM_IDS) - { - final ItemInstance hpPotion = _player.getInventory().getItemByItemId(itemId); - if ((hpPotion != null) && (hpPotion.getCount() > 0)) - { - success = true; - if (restoreHP) - { - ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(_player, hpPotion, false); - _player.sendMessage("Auto potion: Restored HP."); - break; - } - } - } - } - if (Config.AUTO_CP_ENABLED) - { - final boolean restoreCP = ((_player.getStatus().getCurrentCp() / _player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; - for (int itemId : Config.AUTO_CP_ITEM_IDS) - { - final ItemInstance cpPotion = _player.getInventory().getItemByItemId(itemId); - if ((cpPotion != null) && (cpPotion.getCount() > 0)) - { - success = true; - if (restoreCP) - { - ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(_player, cpPotion, false); - _player.sendMessage("Auto potion: Restored CP."); - break; - } - } - } - } - if (Config.AUTO_MP_ENABLED) - { - final boolean restoreMP = ((_player.getStatus().getCurrentMp() / _player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; - for (int itemId : Config.AUTO_MP_ITEM_IDS) - { - final ItemInstance mpPotion = _player.getInventory().getItemByItemId(itemId); - if ((mpPotion != null) && (mpPotion.getCount() > 0)) - { - success = true; - if (restoreMP) - { - ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(_player, mpPotion, false); - _player.sendMessage("Auto potion: Restored MP."); - break; - } - } - } - } - - if (!success) - { - _player.sendMessage("Auto potion: You are out of potions!"); - } - } -} diff --git a/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java b/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java new file mode 100644 index 0000000000..46f97cf7d9 --- /dev/null +++ b/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java @@ -0,0 +1,142 @@ +/* + * This file is part of the L2J Mobius project. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.l2jmobius.gameserver.taskmanager; + +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import org.l2jmobius.Config; +import org.l2jmobius.commons.concurrent.ThreadPool; +import org.l2jmobius.gameserver.handler.ItemHandler; +import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; +import org.l2jmobius.gameserver.model.items.instance.ItemInstance; + +/** + * @author Mobius, Gigi + */ +public class AutoPotionTaskManager +{ + private static final Set PLAYERS = ConcurrentHashMap.newKeySet(); + private static boolean _working = false; + + public AutoPotionTaskManager() + { + ThreadPool.scheduleAtFixedRate(() -> + { + if (_working) + { + return; + } + _working = true; + + PLAYER: for (PlayerInstance player : PLAYERS) + { + if ((player == null) || player.isAlikeDead() || (player.isOnlineInt() != 1) || (!Config.AUTO_POTIONS_IN_OLYMPIAD && player.isInOlympiadMode())) + { + remove(player); + continue PLAYER; + } + + boolean success = false; + if (Config.AUTO_HP_ENABLED) + { + final boolean restoreHP = ((player.getStatus().getCurrentHp() / player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; + HP: for (int itemId : Config.AUTO_HP_ITEM_IDS) + { + final ItemInstance hpPotion = player.getInventory().getItemByItemId(itemId); + if ((hpPotion != null) && (hpPotion.getCount() > 0)) + { + success = true; + if (restoreHP) + { + ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(player, hpPotion, false); + player.sendMessage("Auto potion: Restored HP."); + break HP; + } + } + } + } + if (Config.AUTO_CP_ENABLED) + { + final boolean restoreCP = ((player.getStatus().getCurrentCp() / player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; + CP: for (int itemId : Config.AUTO_CP_ITEM_IDS) + { + final ItemInstance cpPotion = player.getInventory().getItemByItemId(itemId); + if ((cpPotion != null) && (cpPotion.getCount() > 0)) + { + success = true; + if (restoreCP) + { + ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(player, cpPotion, false); + player.sendMessage("Auto potion: Restored CP."); + break CP; + } + } + } + } + if (Config.AUTO_MP_ENABLED) + { + final boolean restoreMP = ((player.getStatus().getCurrentMp() / player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; + MP: for (int itemId : Config.AUTO_MP_ITEM_IDS) + { + final ItemInstance mpPotion = player.getInventory().getItemByItemId(itemId); + if ((mpPotion != null) && (mpPotion.getCount() > 0)) + { + success = true; + if (restoreMP) + { + ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(player, mpPotion, false); + player.sendMessage("Auto potion: Restored MP."); + break MP; + } + } + } + } + + if (!success) + { + player.sendMessage("Auto potion: You are out of potions!"); + } + } + + _working = false; + }, 0, 1000); + } + + public void add(PlayerInstance player) + { + if (!PLAYERS.contains(player)) + { + PLAYERS.add(player); + } + } + + public void remove(PlayerInstance player) + { + PLAYERS.remove(player); + } + + public static AutoPotionTaskManager getInstance() + { + return SingletonHolder.INSTANCE; + } + + private static class SingletonHolder + { + protected static final AutoPotionTaskManager INSTANCE = new AutoPotionTaskManager(); + } +} diff --git a/L2J_Mobius_6.0_Fafurion/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java b/L2J_Mobius_6.0_Fafurion/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java index 722da268b9..5e40a92ff0 100644 --- a/L2J_Mobius_6.0_Fafurion/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java +++ b/L2J_Mobius_6.0_Fafurion/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java @@ -16,28 +16,16 @@ */ package handlers.voicedcommandhandlers; -import java.util.HashMap; -import java.util.concurrent.Future; - import org.l2jmobius.Config; -import org.l2jmobius.commons.concurrent.ThreadPool; import org.l2jmobius.gameserver.handler.IVoicedCommandHandler; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.actor.tasks.player.AutoPotionTask; -import org.l2jmobius.gameserver.model.events.EventType; -import org.l2jmobius.gameserver.model.events.ListenerRegisterType; -import org.l2jmobius.gameserver.model.events.annotations.RegisterEvent; -import org.l2jmobius.gameserver.model.events.annotations.RegisterType; -import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerLogout; +import org.l2jmobius.gameserver.taskmanager.AutoPotionTaskManager; /** - * @author Gigi, Mobius + * @author Mobius, Gigi */ public class AutoPotion implements IVoicedCommandHandler { - private static final HashMap> AUTO_POTION_TASKS = new HashMap<>(); - private static final int POTION_TASK_DELAY = 1000; // 1 second - private static final String[] VOICED_COMMANDS = { "apon", @@ -57,42 +45,20 @@ public class AutoPotion implements IVoicedCommandHandler return false; } - final int playerOID = activeChar.getObjectId(); if (command.equals("apon")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - AUTO_POTION_TASKS.put(activeChar.getObjectId(), ThreadPool.scheduleAtFixedRate(new AutoPotionTask(activeChar), POTION_TASK_DELAY, POTION_TASK_DELAY)); + AutoPotionTaskManager.getInstance().add(activeChar); activeChar.sendMessage("Auto potions is enabled."); return true; } else if (command.equals("apoff")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } + AutoPotionTaskManager.getInstance().remove(activeChar); activeChar.sendMessage("Auto potions is disabled."); } return false; } - @RegisterEvent(EventType.ON_PLAYER_LOGOUT) - @RegisterType(ListenerRegisterType.GLOBAL) - public void OnPlayerLogout(OnPlayerLogout event) - { - final int playerOID = event.getPlayer().getObjectId(); - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - } - @Override public String[] getVoicedCommandList() { diff --git a/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java b/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java deleted file mode 100644 index 3366932e4e..0000000000 --- a/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.l2jmobius.gameserver.model.actor.tasks.player; - -import org.l2jmobius.Config; -import org.l2jmobius.gameserver.handler.ItemHandler; -import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.items.instance.ItemInstance; - -/** - * @author Mobius - */ -public class AutoPotionTask implements Runnable -{ - private final PlayerInstance _player; - - public AutoPotionTask(PlayerInstance player) - { - _player = player; - } - - @Override - public void run() - { - if ((_player == null) || (_player.isOnlineInt() != 1) || _player.isAlikeDead() || (!Config.AUTO_POTIONS_IN_OLYMPIAD && _player.isInOlympiadMode())) - { - return; - } - - boolean success = false; - if (Config.AUTO_HP_ENABLED) - { - final boolean restoreHP = ((_player.getStatus().getCurrentHp() / _player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; - for (int itemId : Config.AUTO_HP_ITEM_IDS) - { - final ItemInstance hpPotion = _player.getInventory().getItemByItemId(itemId); - if ((hpPotion != null) && (hpPotion.getCount() > 0)) - { - success = true; - if (restoreHP) - { - ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(_player, hpPotion, false); - _player.sendMessage("Auto potion: Restored HP."); - break; - } - } - } - } - if (Config.AUTO_CP_ENABLED) - { - final boolean restoreCP = ((_player.getStatus().getCurrentCp() / _player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; - for (int itemId : Config.AUTO_CP_ITEM_IDS) - { - final ItemInstance cpPotion = _player.getInventory().getItemByItemId(itemId); - if ((cpPotion != null) && (cpPotion.getCount() > 0)) - { - success = true; - if (restoreCP) - { - ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(_player, cpPotion, false); - _player.sendMessage("Auto potion: Restored CP."); - break; - } - } - } - } - if (Config.AUTO_MP_ENABLED) - { - final boolean restoreMP = ((_player.getStatus().getCurrentMp() / _player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; - for (int itemId : Config.AUTO_MP_ITEM_IDS) - { - final ItemInstance mpPotion = _player.getInventory().getItemByItemId(itemId); - if ((mpPotion != null) && (mpPotion.getCount() > 0)) - { - success = true; - if (restoreMP) - { - ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(_player, mpPotion, false); - _player.sendMessage("Auto potion: Restored MP."); - break; - } - } - } - } - - if (!success) - { - _player.sendMessage("Auto potion: You are out of potions!"); - } - } -} diff --git a/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java b/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java new file mode 100644 index 0000000000..46f97cf7d9 --- /dev/null +++ b/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java @@ -0,0 +1,142 @@ +/* + * This file is part of the L2J Mobius project. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.l2jmobius.gameserver.taskmanager; + +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import org.l2jmobius.Config; +import org.l2jmobius.commons.concurrent.ThreadPool; +import org.l2jmobius.gameserver.handler.ItemHandler; +import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; +import org.l2jmobius.gameserver.model.items.instance.ItemInstance; + +/** + * @author Mobius, Gigi + */ +public class AutoPotionTaskManager +{ + private static final Set PLAYERS = ConcurrentHashMap.newKeySet(); + private static boolean _working = false; + + public AutoPotionTaskManager() + { + ThreadPool.scheduleAtFixedRate(() -> + { + if (_working) + { + return; + } + _working = true; + + PLAYER: for (PlayerInstance player : PLAYERS) + { + if ((player == null) || player.isAlikeDead() || (player.isOnlineInt() != 1) || (!Config.AUTO_POTIONS_IN_OLYMPIAD && player.isInOlympiadMode())) + { + remove(player); + continue PLAYER; + } + + boolean success = false; + if (Config.AUTO_HP_ENABLED) + { + final boolean restoreHP = ((player.getStatus().getCurrentHp() / player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; + HP: for (int itemId : Config.AUTO_HP_ITEM_IDS) + { + final ItemInstance hpPotion = player.getInventory().getItemByItemId(itemId); + if ((hpPotion != null) && (hpPotion.getCount() > 0)) + { + success = true; + if (restoreHP) + { + ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(player, hpPotion, false); + player.sendMessage("Auto potion: Restored HP."); + break HP; + } + } + } + } + if (Config.AUTO_CP_ENABLED) + { + final boolean restoreCP = ((player.getStatus().getCurrentCp() / player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; + CP: for (int itemId : Config.AUTO_CP_ITEM_IDS) + { + final ItemInstance cpPotion = player.getInventory().getItemByItemId(itemId); + if ((cpPotion != null) && (cpPotion.getCount() > 0)) + { + success = true; + if (restoreCP) + { + ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(player, cpPotion, false); + player.sendMessage("Auto potion: Restored CP."); + break CP; + } + } + } + } + if (Config.AUTO_MP_ENABLED) + { + final boolean restoreMP = ((player.getStatus().getCurrentMp() / player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; + MP: for (int itemId : Config.AUTO_MP_ITEM_IDS) + { + final ItemInstance mpPotion = player.getInventory().getItemByItemId(itemId); + if ((mpPotion != null) && (mpPotion.getCount() > 0)) + { + success = true; + if (restoreMP) + { + ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(player, mpPotion, false); + player.sendMessage("Auto potion: Restored MP."); + break MP; + } + } + } + } + + if (!success) + { + player.sendMessage("Auto potion: You are out of potions!"); + } + } + + _working = false; + }, 0, 1000); + } + + public void add(PlayerInstance player) + { + if (!PLAYERS.contains(player)) + { + PLAYERS.add(player); + } + } + + public void remove(PlayerInstance player) + { + PLAYERS.remove(player); + } + + public static AutoPotionTaskManager getInstance() + { + return SingletonHolder.INSTANCE; + } + + private static class SingletonHolder + { + protected static final AutoPotionTaskManager INSTANCE = new AutoPotionTaskManager(); + } +} diff --git a/L2J_Mobius_7.0_PreludeOfWar/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java b/L2J_Mobius_7.0_PreludeOfWar/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java index 722da268b9..5e40a92ff0 100644 --- a/L2J_Mobius_7.0_PreludeOfWar/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java +++ b/L2J_Mobius_7.0_PreludeOfWar/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java @@ -16,28 +16,16 @@ */ package handlers.voicedcommandhandlers; -import java.util.HashMap; -import java.util.concurrent.Future; - import org.l2jmobius.Config; -import org.l2jmobius.commons.concurrent.ThreadPool; import org.l2jmobius.gameserver.handler.IVoicedCommandHandler; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.actor.tasks.player.AutoPotionTask; -import org.l2jmobius.gameserver.model.events.EventType; -import org.l2jmobius.gameserver.model.events.ListenerRegisterType; -import org.l2jmobius.gameserver.model.events.annotations.RegisterEvent; -import org.l2jmobius.gameserver.model.events.annotations.RegisterType; -import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerLogout; +import org.l2jmobius.gameserver.taskmanager.AutoPotionTaskManager; /** - * @author Gigi, Mobius + * @author Mobius, Gigi */ public class AutoPotion implements IVoicedCommandHandler { - private static final HashMap> AUTO_POTION_TASKS = new HashMap<>(); - private static final int POTION_TASK_DELAY = 1000; // 1 second - private static final String[] VOICED_COMMANDS = { "apon", @@ -57,42 +45,20 @@ public class AutoPotion implements IVoicedCommandHandler return false; } - final int playerOID = activeChar.getObjectId(); if (command.equals("apon")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - AUTO_POTION_TASKS.put(activeChar.getObjectId(), ThreadPool.scheduleAtFixedRate(new AutoPotionTask(activeChar), POTION_TASK_DELAY, POTION_TASK_DELAY)); + AutoPotionTaskManager.getInstance().add(activeChar); activeChar.sendMessage("Auto potions is enabled."); return true; } else if (command.equals("apoff")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } + AutoPotionTaskManager.getInstance().remove(activeChar); activeChar.sendMessage("Auto potions is disabled."); } return false; } - @RegisterEvent(EventType.ON_PLAYER_LOGOUT) - @RegisterType(ListenerRegisterType.GLOBAL) - public void OnPlayerLogout(OnPlayerLogout event) - { - final int playerOID = event.getPlayer().getObjectId(); - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - } - @Override public String[] getVoicedCommandList() { diff --git a/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java b/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java deleted file mode 100644 index 3366932e4e..0000000000 --- a/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.l2jmobius.gameserver.model.actor.tasks.player; - -import org.l2jmobius.Config; -import org.l2jmobius.gameserver.handler.ItemHandler; -import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.items.instance.ItemInstance; - -/** - * @author Mobius - */ -public class AutoPotionTask implements Runnable -{ - private final PlayerInstance _player; - - public AutoPotionTask(PlayerInstance player) - { - _player = player; - } - - @Override - public void run() - { - if ((_player == null) || (_player.isOnlineInt() != 1) || _player.isAlikeDead() || (!Config.AUTO_POTIONS_IN_OLYMPIAD && _player.isInOlympiadMode())) - { - return; - } - - boolean success = false; - if (Config.AUTO_HP_ENABLED) - { - final boolean restoreHP = ((_player.getStatus().getCurrentHp() / _player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; - for (int itemId : Config.AUTO_HP_ITEM_IDS) - { - final ItemInstance hpPotion = _player.getInventory().getItemByItemId(itemId); - if ((hpPotion != null) && (hpPotion.getCount() > 0)) - { - success = true; - if (restoreHP) - { - ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(_player, hpPotion, false); - _player.sendMessage("Auto potion: Restored HP."); - break; - } - } - } - } - if (Config.AUTO_CP_ENABLED) - { - final boolean restoreCP = ((_player.getStatus().getCurrentCp() / _player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; - for (int itemId : Config.AUTO_CP_ITEM_IDS) - { - final ItemInstance cpPotion = _player.getInventory().getItemByItemId(itemId); - if ((cpPotion != null) && (cpPotion.getCount() > 0)) - { - success = true; - if (restoreCP) - { - ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(_player, cpPotion, false); - _player.sendMessage("Auto potion: Restored CP."); - break; - } - } - } - } - if (Config.AUTO_MP_ENABLED) - { - final boolean restoreMP = ((_player.getStatus().getCurrentMp() / _player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; - for (int itemId : Config.AUTO_MP_ITEM_IDS) - { - final ItemInstance mpPotion = _player.getInventory().getItemByItemId(itemId); - if ((mpPotion != null) && (mpPotion.getCount() > 0)) - { - success = true; - if (restoreMP) - { - ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(_player, mpPotion, false); - _player.sendMessage("Auto potion: Restored MP."); - break; - } - } - } - } - - if (!success) - { - _player.sendMessage("Auto potion: You are out of potions!"); - } - } -} diff --git a/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java b/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java new file mode 100644 index 0000000000..46f97cf7d9 --- /dev/null +++ b/L2J_Mobius_7.0_PreludeOfWar/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java @@ -0,0 +1,142 @@ +/* + * This file is part of the L2J Mobius project. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.l2jmobius.gameserver.taskmanager; + +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import org.l2jmobius.Config; +import org.l2jmobius.commons.concurrent.ThreadPool; +import org.l2jmobius.gameserver.handler.ItemHandler; +import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; +import org.l2jmobius.gameserver.model.items.instance.ItemInstance; + +/** + * @author Mobius, Gigi + */ +public class AutoPotionTaskManager +{ + private static final Set PLAYERS = ConcurrentHashMap.newKeySet(); + private static boolean _working = false; + + public AutoPotionTaskManager() + { + ThreadPool.scheduleAtFixedRate(() -> + { + if (_working) + { + return; + } + _working = true; + + PLAYER: for (PlayerInstance player : PLAYERS) + { + if ((player == null) || player.isAlikeDead() || (player.isOnlineInt() != 1) || (!Config.AUTO_POTIONS_IN_OLYMPIAD && player.isInOlympiadMode())) + { + remove(player); + continue PLAYER; + } + + boolean success = false; + if (Config.AUTO_HP_ENABLED) + { + final boolean restoreHP = ((player.getStatus().getCurrentHp() / player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; + HP: for (int itemId : Config.AUTO_HP_ITEM_IDS) + { + final ItemInstance hpPotion = player.getInventory().getItemByItemId(itemId); + if ((hpPotion != null) && (hpPotion.getCount() > 0)) + { + success = true; + if (restoreHP) + { + ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(player, hpPotion, false); + player.sendMessage("Auto potion: Restored HP."); + break HP; + } + } + } + } + if (Config.AUTO_CP_ENABLED) + { + final boolean restoreCP = ((player.getStatus().getCurrentCp() / player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; + CP: for (int itemId : Config.AUTO_CP_ITEM_IDS) + { + final ItemInstance cpPotion = player.getInventory().getItemByItemId(itemId); + if ((cpPotion != null) && (cpPotion.getCount() > 0)) + { + success = true; + if (restoreCP) + { + ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(player, cpPotion, false); + player.sendMessage("Auto potion: Restored CP."); + break CP; + } + } + } + } + if (Config.AUTO_MP_ENABLED) + { + final boolean restoreMP = ((player.getStatus().getCurrentMp() / player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; + MP: for (int itemId : Config.AUTO_MP_ITEM_IDS) + { + final ItemInstance mpPotion = player.getInventory().getItemByItemId(itemId); + if ((mpPotion != null) && (mpPotion.getCount() > 0)) + { + success = true; + if (restoreMP) + { + ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(player, mpPotion, false); + player.sendMessage("Auto potion: Restored MP."); + break MP; + } + } + } + } + + if (!success) + { + player.sendMessage("Auto potion: You are out of potions!"); + } + } + + _working = false; + }, 0, 1000); + } + + public void add(PlayerInstance player) + { + if (!PLAYERS.contains(player)) + { + PLAYERS.add(player); + } + } + + public void remove(PlayerInstance player) + { + PLAYERS.remove(player); + } + + public static AutoPotionTaskManager getInstance() + { + return SingletonHolder.INSTANCE; + } + + private static class SingletonHolder + { + protected static final AutoPotionTaskManager INSTANCE = new AutoPotionTaskManager(); + } +} diff --git a/L2J_Mobius_CT_2.4_Epilogue/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java b/L2J_Mobius_CT_2.4_Epilogue/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java index 722da268b9..5e40a92ff0 100644 --- a/L2J_Mobius_CT_2.4_Epilogue/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java +++ b/L2J_Mobius_CT_2.4_Epilogue/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java @@ -16,28 +16,16 @@ */ package handlers.voicedcommandhandlers; -import java.util.HashMap; -import java.util.concurrent.Future; - import org.l2jmobius.Config; -import org.l2jmobius.commons.concurrent.ThreadPool; import org.l2jmobius.gameserver.handler.IVoicedCommandHandler; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.actor.tasks.player.AutoPotionTask; -import org.l2jmobius.gameserver.model.events.EventType; -import org.l2jmobius.gameserver.model.events.ListenerRegisterType; -import org.l2jmobius.gameserver.model.events.annotations.RegisterEvent; -import org.l2jmobius.gameserver.model.events.annotations.RegisterType; -import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerLogout; +import org.l2jmobius.gameserver.taskmanager.AutoPotionTaskManager; /** - * @author Gigi, Mobius + * @author Mobius, Gigi */ public class AutoPotion implements IVoicedCommandHandler { - private static final HashMap> AUTO_POTION_TASKS = new HashMap<>(); - private static final int POTION_TASK_DELAY = 1000; // 1 second - private static final String[] VOICED_COMMANDS = { "apon", @@ -57,42 +45,20 @@ public class AutoPotion implements IVoicedCommandHandler return false; } - final int playerOID = activeChar.getObjectId(); if (command.equals("apon")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - AUTO_POTION_TASKS.put(activeChar.getObjectId(), ThreadPool.scheduleAtFixedRate(new AutoPotionTask(activeChar), POTION_TASK_DELAY, POTION_TASK_DELAY)); + AutoPotionTaskManager.getInstance().add(activeChar); activeChar.sendMessage("Auto potions is enabled."); return true; } else if (command.equals("apoff")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } + AutoPotionTaskManager.getInstance().remove(activeChar); activeChar.sendMessage("Auto potions is disabled."); } return false; } - @RegisterEvent(EventType.ON_PLAYER_LOGOUT) - @RegisterType(ListenerRegisterType.GLOBAL) - public void OnPlayerLogout(OnPlayerLogout event) - { - final int playerOID = event.getPlayer().getObjectId(); - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - } - @Override public String[] getVoicedCommandList() { diff --git a/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java b/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java deleted file mode 100644 index 3366932e4e..0000000000 --- a/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.l2jmobius.gameserver.model.actor.tasks.player; - -import org.l2jmobius.Config; -import org.l2jmobius.gameserver.handler.ItemHandler; -import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.items.instance.ItemInstance; - -/** - * @author Mobius - */ -public class AutoPotionTask implements Runnable -{ - private final PlayerInstance _player; - - public AutoPotionTask(PlayerInstance player) - { - _player = player; - } - - @Override - public void run() - { - if ((_player == null) || (_player.isOnlineInt() != 1) || _player.isAlikeDead() || (!Config.AUTO_POTIONS_IN_OLYMPIAD && _player.isInOlympiadMode())) - { - return; - } - - boolean success = false; - if (Config.AUTO_HP_ENABLED) - { - final boolean restoreHP = ((_player.getStatus().getCurrentHp() / _player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; - for (int itemId : Config.AUTO_HP_ITEM_IDS) - { - final ItemInstance hpPotion = _player.getInventory().getItemByItemId(itemId); - if ((hpPotion != null) && (hpPotion.getCount() > 0)) - { - success = true; - if (restoreHP) - { - ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(_player, hpPotion, false); - _player.sendMessage("Auto potion: Restored HP."); - break; - } - } - } - } - if (Config.AUTO_CP_ENABLED) - { - final boolean restoreCP = ((_player.getStatus().getCurrentCp() / _player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; - for (int itemId : Config.AUTO_CP_ITEM_IDS) - { - final ItemInstance cpPotion = _player.getInventory().getItemByItemId(itemId); - if ((cpPotion != null) && (cpPotion.getCount() > 0)) - { - success = true; - if (restoreCP) - { - ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(_player, cpPotion, false); - _player.sendMessage("Auto potion: Restored CP."); - break; - } - } - } - } - if (Config.AUTO_MP_ENABLED) - { - final boolean restoreMP = ((_player.getStatus().getCurrentMp() / _player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; - for (int itemId : Config.AUTO_MP_ITEM_IDS) - { - final ItemInstance mpPotion = _player.getInventory().getItemByItemId(itemId); - if ((mpPotion != null) && (mpPotion.getCount() > 0)) - { - success = true; - if (restoreMP) - { - ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(_player, mpPotion, false); - _player.sendMessage("Auto potion: Restored MP."); - break; - } - } - } - } - - if (!success) - { - _player.sendMessage("Auto potion: You are out of potions!"); - } - } -} diff --git a/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java b/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java new file mode 100644 index 0000000000..46f97cf7d9 --- /dev/null +++ b/L2J_Mobius_CT_2.4_Epilogue/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java @@ -0,0 +1,142 @@ +/* + * This file is part of the L2J Mobius project. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.l2jmobius.gameserver.taskmanager; + +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import org.l2jmobius.Config; +import org.l2jmobius.commons.concurrent.ThreadPool; +import org.l2jmobius.gameserver.handler.ItemHandler; +import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; +import org.l2jmobius.gameserver.model.items.instance.ItemInstance; + +/** + * @author Mobius, Gigi + */ +public class AutoPotionTaskManager +{ + private static final Set PLAYERS = ConcurrentHashMap.newKeySet(); + private static boolean _working = false; + + public AutoPotionTaskManager() + { + ThreadPool.scheduleAtFixedRate(() -> + { + if (_working) + { + return; + } + _working = true; + + PLAYER: for (PlayerInstance player : PLAYERS) + { + if ((player == null) || player.isAlikeDead() || (player.isOnlineInt() != 1) || (!Config.AUTO_POTIONS_IN_OLYMPIAD && player.isInOlympiadMode())) + { + remove(player); + continue PLAYER; + } + + boolean success = false; + if (Config.AUTO_HP_ENABLED) + { + final boolean restoreHP = ((player.getStatus().getCurrentHp() / player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; + HP: for (int itemId : Config.AUTO_HP_ITEM_IDS) + { + final ItemInstance hpPotion = player.getInventory().getItemByItemId(itemId); + if ((hpPotion != null) && (hpPotion.getCount() > 0)) + { + success = true; + if (restoreHP) + { + ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(player, hpPotion, false); + player.sendMessage("Auto potion: Restored HP."); + break HP; + } + } + } + } + if (Config.AUTO_CP_ENABLED) + { + final boolean restoreCP = ((player.getStatus().getCurrentCp() / player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; + CP: for (int itemId : Config.AUTO_CP_ITEM_IDS) + { + final ItemInstance cpPotion = player.getInventory().getItemByItemId(itemId); + if ((cpPotion != null) && (cpPotion.getCount() > 0)) + { + success = true; + if (restoreCP) + { + ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(player, cpPotion, false); + player.sendMessage("Auto potion: Restored CP."); + break CP; + } + } + } + } + if (Config.AUTO_MP_ENABLED) + { + final boolean restoreMP = ((player.getStatus().getCurrentMp() / player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; + MP: for (int itemId : Config.AUTO_MP_ITEM_IDS) + { + final ItemInstance mpPotion = player.getInventory().getItemByItemId(itemId); + if ((mpPotion != null) && (mpPotion.getCount() > 0)) + { + success = true; + if (restoreMP) + { + ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(player, mpPotion, false); + player.sendMessage("Auto potion: Restored MP."); + break MP; + } + } + } + } + + if (!success) + { + player.sendMessage("Auto potion: You are out of potions!"); + } + } + + _working = false; + }, 0, 1000); + } + + public void add(PlayerInstance player) + { + if (!PLAYERS.contains(player)) + { + PLAYERS.add(player); + } + } + + public void remove(PlayerInstance player) + { + PLAYERS.remove(player); + } + + public static AutoPotionTaskManager getInstance() + { + return SingletonHolder.INSTANCE; + } + + private static class SingletonHolder + { + protected static final AutoPotionTaskManager INSTANCE = new AutoPotionTaskManager(); + } +} diff --git a/L2J_Mobius_CT_2.6_HighFive/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java b/L2J_Mobius_CT_2.6_HighFive/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java index 722da268b9..5e40a92ff0 100644 --- a/L2J_Mobius_CT_2.6_HighFive/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java +++ b/L2J_Mobius_CT_2.6_HighFive/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java @@ -16,28 +16,16 @@ */ package handlers.voicedcommandhandlers; -import java.util.HashMap; -import java.util.concurrent.Future; - import org.l2jmobius.Config; -import org.l2jmobius.commons.concurrent.ThreadPool; import org.l2jmobius.gameserver.handler.IVoicedCommandHandler; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.actor.tasks.player.AutoPotionTask; -import org.l2jmobius.gameserver.model.events.EventType; -import org.l2jmobius.gameserver.model.events.ListenerRegisterType; -import org.l2jmobius.gameserver.model.events.annotations.RegisterEvent; -import org.l2jmobius.gameserver.model.events.annotations.RegisterType; -import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerLogout; +import org.l2jmobius.gameserver.taskmanager.AutoPotionTaskManager; /** - * @author Gigi, Mobius + * @author Mobius, Gigi */ public class AutoPotion implements IVoicedCommandHandler { - private static final HashMap> AUTO_POTION_TASKS = new HashMap<>(); - private static final int POTION_TASK_DELAY = 1000; // 1 second - private static final String[] VOICED_COMMANDS = { "apon", @@ -57,42 +45,20 @@ public class AutoPotion implements IVoicedCommandHandler return false; } - final int playerOID = activeChar.getObjectId(); if (command.equals("apon")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - AUTO_POTION_TASKS.put(activeChar.getObjectId(), ThreadPool.scheduleAtFixedRate(new AutoPotionTask(activeChar), POTION_TASK_DELAY, POTION_TASK_DELAY)); + AutoPotionTaskManager.getInstance().add(activeChar); activeChar.sendMessage("Auto potions is enabled."); return true; } else if (command.equals("apoff")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } + AutoPotionTaskManager.getInstance().remove(activeChar); activeChar.sendMessage("Auto potions is disabled."); } return false; } - @RegisterEvent(EventType.ON_PLAYER_LOGOUT) - @RegisterType(ListenerRegisterType.GLOBAL) - public void OnPlayerLogout(OnPlayerLogout event) - { - final int playerOID = event.getPlayer().getObjectId(); - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - } - @Override public String[] getVoicedCommandList() { diff --git a/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java b/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java deleted file mode 100644 index 3366932e4e..0000000000 --- a/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.l2jmobius.gameserver.model.actor.tasks.player; - -import org.l2jmobius.Config; -import org.l2jmobius.gameserver.handler.ItemHandler; -import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.items.instance.ItemInstance; - -/** - * @author Mobius - */ -public class AutoPotionTask implements Runnable -{ - private final PlayerInstance _player; - - public AutoPotionTask(PlayerInstance player) - { - _player = player; - } - - @Override - public void run() - { - if ((_player == null) || (_player.isOnlineInt() != 1) || _player.isAlikeDead() || (!Config.AUTO_POTIONS_IN_OLYMPIAD && _player.isInOlympiadMode())) - { - return; - } - - boolean success = false; - if (Config.AUTO_HP_ENABLED) - { - final boolean restoreHP = ((_player.getStatus().getCurrentHp() / _player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; - for (int itemId : Config.AUTO_HP_ITEM_IDS) - { - final ItemInstance hpPotion = _player.getInventory().getItemByItemId(itemId); - if ((hpPotion != null) && (hpPotion.getCount() > 0)) - { - success = true; - if (restoreHP) - { - ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(_player, hpPotion, false); - _player.sendMessage("Auto potion: Restored HP."); - break; - } - } - } - } - if (Config.AUTO_CP_ENABLED) - { - final boolean restoreCP = ((_player.getStatus().getCurrentCp() / _player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; - for (int itemId : Config.AUTO_CP_ITEM_IDS) - { - final ItemInstance cpPotion = _player.getInventory().getItemByItemId(itemId); - if ((cpPotion != null) && (cpPotion.getCount() > 0)) - { - success = true; - if (restoreCP) - { - ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(_player, cpPotion, false); - _player.sendMessage("Auto potion: Restored CP."); - break; - } - } - } - } - if (Config.AUTO_MP_ENABLED) - { - final boolean restoreMP = ((_player.getStatus().getCurrentMp() / _player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; - for (int itemId : Config.AUTO_MP_ITEM_IDS) - { - final ItemInstance mpPotion = _player.getInventory().getItemByItemId(itemId); - if ((mpPotion != null) && (mpPotion.getCount() > 0)) - { - success = true; - if (restoreMP) - { - ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(_player, mpPotion, false); - _player.sendMessage("Auto potion: Restored MP."); - break; - } - } - } - } - - if (!success) - { - _player.sendMessage("Auto potion: You are out of potions!"); - } - } -} diff --git a/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java b/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java new file mode 100644 index 0000000000..46f97cf7d9 --- /dev/null +++ b/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java @@ -0,0 +1,142 @@ +/* + * This file is part of the L2J Mobius project. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.l2jmobius.gameserver.taskmanager; + +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import org.l2jmobius.Config; +import org.l2jmobius.commons.concurrent.ThreadPool; +import org.l2jmobius.gameserver.handler.ItemHandler; +import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; +import org.l2jmobius.gameserver.model.items.instance.ItemInstance; + +/** + * @author Mobius, Gigi + */ +public class AutoPotionTaskManager +{ + private static final Set PLAYERS = ConcurrentHashMap.newKeySet(); + private static boolean _working = false; + + public AutoPotionTaskManager() + { + ThreadPool.scheduleAtFixedRate(() -> + { + if (_working) + { + return; + } + _working = true; + + PLAYER: for (PlayerInstance player : PLAYERS) + { + if ((player == null) || player.isAlikeDead() || (player.isOnlineInt() != 1) || (!Config.AUTO_POTIONS_IN_OLYMPIAD && player.isInOlympiadMode())) + { + remove(player); + continue PLAYER; + } + + boolean success = false; + if (Config.AUTO_HP_ENABLED) + { + final boolean restoreHP = ((player.getStatus().getCurrentHp() / player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; + HP: for (int itemId : Config.AUTO_HP_ITEM_IDS) + { + final ItemInstance hpPotion = player.getInventory().getItemByItemId(itemId); + if ((hpPotion != null) && (hpPotion.getCount() > 0)) + { + success = true; + if (restoreHP) + { + ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(player, hpPotion, false); + player.sendMessage("Auto potion: Restored HP."); + break HP; + } + } + } + } + if (Config.AUTO_CP_ENABLED) + { + final boolean restoreCP = ((player.getStatus().getCurrentCp() / player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; + CP: for (int itemId : Config.AUTO_CP_ITEM_IDS) + { + final ItemInstance cpPotion = player.getInventory().getItemByItemId(itemId); + if ((cpPotion != null) && (cpPotion.getCount() > 0)) + { + success = true; + if (restoreCP) + { + ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(player, cpPotion, false); + player.sendMessage("Auto potion: Restored CP."); + break CP; + } + } + } + } + if (Config.AUTO_MP_ENABLED) + { + final boolean restoreMP = ((player.getStatus().getCurrentMp() / player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; + MP: for (int itemId : Config.AUTO_MP_ITEM_IDS) + { + final ItemInstance mpPotion = player.getInventory().getItemByItemId(itemId); + if ((mpPotion != null) && (mpPotion.getCount() > 0)) + { + success = true; + if (restoreMP) + { + ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(player, mpPotion, false); + player.sendMessage("Auto potion: Restored MP."); + break MP; + } + } + } + } + + if (!success) + { + player.sendMessage("Auto potion: You are out of potions!"); + } + } + + _working = false; + }, 0, 1000); + } + + public void add(PlayerInstance player) + { + if (!PLAYERS.contains(player)) + { + PLAYERS.add(player); + } + } + + public void remove(PlayerInstance player) + { + PLAYERS.remove(player); + } + + public static AutoPotionTaskManager getInstance() + { + return SingletonHolder.INSTANCE; + } + + private static class SingletonHolder + { + protected static final AutoPotionTaskManager INSTANCE = new AutoPotionTaskManager(); + } +} diff --git a/L2J_Mobius_Classic_2.0_Saviors/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java b/L2J_Mobius_Classic_2.0_Saviors/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java index 722da268b9..5e40a92ff0 100644 --- a/L2J_Mobius_Classic_2.0_Saviors/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java +++ b/L2J_Mobius_Classic_2.0_Saviors/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java @@ -16,28 +16,16 @@ */ package handlers.voicedcommandhandlers; -import java.util.HashMap; -import java.util.concurrent.Future; - import org.l2jmobius.Config; -import org.l2jmobius.commons.concurrent.ThreadPool; import org.l2jmobius.gameserver.handler.IVoicedCommandHandler; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.actor.tasks.player.AutoPotionTask; -import org.l2jmobius.gameserver.model.events.EventType; -import org.l2jmobius.gameserver.model.events.ListenerRegisterType; -import org.l2jmobius.gameserver.model.events.annotations.RegisterEvent; -import org.l2jmobius.gameserver.model.events.annotations.RegisterType; -import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerLogout; +import org.l2jmobius.gameserver.taskmanager.AutoPotionTaskManager; /** - * @author Gigi, Mobius + * @author Mobius, Gigi */ public class AutoPotion implements IVoicedCommandHandler { - private static final HashMap> AUTO_POTION_TASKS = new HashMap<>(); - private static final int POTION_TASK_DELAY = 1000; // 1 second - private static final String[] VOICED_COMMANDS = { "apon", @@ -57,42 +45,20 @@ public class AutoPotion implements IVoicedCommandHandler return false; } - final int playerOID = activeChar.getObjectId(); if (command.equals("apon")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - AUTO_POTION_TASKS.put(activeChar.getObjectId(), ThreadPool.scheduleAtFixedRate(new AutoPotionTask(activeChar), POTION_TASK_DELAY, POTION_TASK_DELAY)); + AutoPotionTaskManager.getInstance().add(activeChar); activeChar.sendMessage("Auto potions is enabled."); return true; } else if (command.equals("apoff")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } + AutoPotionTaskManager.getInstance().remove(activeChar); activeChar.sendMessage("Auto potions is disabled."); } return false; } - @RegisterEvent(EventType.ON_PLAYER_LOGOUT) - @RegisterType(ListenerRegisterType.GLOBAL) - public void OnPlayerLogout(OnPlayerLogout event) - { - final int playerOID = event.getPlayer().getObjectId(); - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - } - @Override public String[] getVoicedCommandList() { diff --git a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java b/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java deleted file mode 100644 index 3366932e4e..0000000000 --- a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.l2jmobius.gameserver.model.actor.tasks.player; - -import org.l2jmobius.Config; -import org.l2jmobius.gameserver.handler.ItemHandler; -import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.items.instance.ItemInstance; - -/** - * @author Mobius - */ -public class AutoPotionTask implements Runnable -{ - private final PlayerInstance _player; - - public AutoPotionTask(PlayerInstance player) - { - _player = player; - } - - @Override - public void run() - { - if ((_player == null) || (_player.isOnlineInt() != 1) || _player.isAlikeDead() || (!Config.AUTO_POTIONS_IN_OLYMPIAD && _player.isInOlympiadMode())) - { - return; - } - - boolean success = false; - if (Config.AUTO_HP_ENABLED) - { - final boolean restoreHP = ((_player.getStatus().getCurrentHp() / _player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; - for (int itemId : Config.AUTO_HP_ITEM_IDS) - { - final ItemInstance hpPotion = _player.getInventory().getItemByItemId(itemId); - if ((hpPotion != null) && (hpPotion.getCount() > 0)) - { - success = true; - if (restoreHP) - { - ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(_player, hpPotion, false); - _player.sendMessage("Auto potion: Restored HP."); - break; - } - } - } - } - if (Config.AUTO_CP_ENABLED) - { - final boolean restoreCP = ((_player.getStatus().getCurrentCp() / _player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; - for (int itemId : Config.AUTO_CP_ITEM_IDS) - { - final ItemInstance cpPotion = _player.getInventory().getItemByItemId(itemId); - if ((cpPotion != null) && (cpPotion.getCount() > 0)) - { - success = true; - if (restoreCP) - { - ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(_player, cpPotion, false); - _player.sendMessage("Auto potion: Restored CP."); - break; - } - } - } - } - if (Config.AUTO_MP_ENABLED) - { - final boolean restoreMP = ((_player.getStatus().getCurrentMp() / _player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; - for (int itemId : Config.AUTO_MP_ITEM_IDS) - { - final ItemInstance mpPotion = _player.getInventory().getItemByItemId(itemId); - if ((mpPotion != null) && (mpPotion.getCount() > 0)) - { - success = true; - if (restoreMP) - { - ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(_player, mpPotion, false); - _player.sendMessage("Auto potion: Restored MP."); - break; - } - } - } - } - - if (!success) - { - _player.sendMessage("Auto potion: You are out of potions!"); - } - } -} diff --git a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java b/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java new file mode 100644 index 0000000000..46f97cf7d9 --- /dev/null +++ b/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java @@ -0,0 +1,142 @@ +/* + * This file is part of the L2J Mobius project. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.l2jmobius.gameserver.taskmanager; + +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import org.l2jmobius.Config; +import org.l2jmobius.commons.concurrent.ThreadPool; +import org.l2jmobius.gameserver.handler.ItemHandler; +import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; +import org.l2jmobius.gameserver.model.items.instance.ItemInstance; + +/** + * @author Mobius, Gigi + */ +public class AutoPotionTaskManager +{ + private static final Set PLAYERS = ConcurrentHashMap.newKeySet(); + private static boolean _working = false; + + public AutoPotionTaskManager() + { + ThreadPool.scheduleAtFixedRate(() -> + { + if (_working) + { + return; + } + _working = true; + + PLAYER: for (PlayerInstance player : PLAYERS) + { + if ((player == null) || player.isAlikeDead() || (player.isOnlineInt() != 1) || (!Config.AUTO_POTIONS_IN_OLYMPIAD && player.isInOlympiadMode())) + { + remove(player); + continue PLAYER; + } + + boolean success = false; + if (Config.AUTO_HP_ENABLED) + { + final boolean restoreHP = ((player.getStatus().getCurrentHp() / player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; + HP: for (int itemId : Config.AUTO_HP_ITEM_IDS) + { + final ItemInstance hpPotion = player.getInventory().getItemByItemId(itemId); + if ((hpPotion != null) && (hpPotion.getCount() > 0)) + { + success = true; + if (restoreHP) + { + ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(player, hpPotion, false); + player.sendMessage("Auto potion: Restored HP."); + break HP; + } + } + } + } + if (Config.AUTO_CP_ENABLED) + { + final boolean restoreCP = ((player.getStatus().getCurrentCp() / player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; + CP: for (int itemId : Config.AUTO_CP_ITEM_IDS) + { + final ItemInstance cpPotion = player.getInventory().getItemByItemId(itemId); + if ((cpPotion != null) && (cpPotion.getCount() > 0)) + { + success = true; + if (restoreCP) + { + ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(player, cpPotion, false); + player.sendMessage("Auto potion: Restored CP."); + break CP; + } + } + } + } + if (Config.AUTO_MP_ENABLED) + { + final boolean restoreMP = ((player.getStatus().getCurrentMp() / player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; + MP: for (int itemId : Config.AUTO_MP_ITEM_IDS) + { + final ItemInstance mpPotion = player.getInventory().getItemByItemId(itemId); + if ((mpPotion != null) && (mpPotion.getCount() > 0)) + { + success = true; + if (restoreMP) + { + ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(player, mpPotion, false); + player.sendMessage("Auto potion: Restored MP."); + break MP; + } + } + } + } + + if (!success) + { + player.sendMessage("Auto potion: You are out of potions!"); + } + } + + _working = false; + }, 0, 1000); + } + + public void add(PlayerInstance player) + { + if (!PLAYERS.contains(player)) + { + PLAYERS.add(player); + } + } + + public void remove(PlayerInstance player) + { + PLAYERS.remove(player); + } + + public static AutoPotionTaskManager getInstance() + { + return SingletonHolder.INSTANCE; + } + + private static class SingletonHolder + { + protected static final AutoPotionTaskManager INSTANCE = new AutoPotionTaskManager(); + } +} diff --git a/L2J_Mobius_Classic_2.1_Zaken/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java b/L2J_Mobius_Classic_2.1_Zaken/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java index 722da268b9..5e40a92ff0 100644 --- a/L2J_Mobius_Classic_2.1_Zaken/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java +++ b/L2J_Mobius_Classic_2.1_Zaken/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java @@ -16,28 +16,16 @@ */ package handlers.voicedcommandhandlers; -import java.util.HashMap; -import java.util.concurrent.Future; - import org.l2jmobius.Config; -import org.l2jmobius.commons.concurrent.ThreadPool; import org.l2jmobius.gameserver.handler.IVoicedCommandHandler; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.actor.tasks.player.AutoPotionTask; -import org.l2jmobius.gameserver.model.events.EventType; -import org.l2jmobius.gameserver.model.events.ListenerRegisterType; -import org.l2jmobius.gameserver.model.events.annotations.RegisterEvent; -import org.l2jmobius.gameserver.model.events.annotations.RegisterType; -import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerLogout; +import org.l2jmobius.gameserver.taskmanager.AutoPotionTaskManager; /** - * @author Gigi, Mobius + * @author Mobius, Gigi */ public class AutoPotion implements IVoicedCommandHandler { - private static final HashMap> AUTO_POTION_TASKS = new HashMap<>(); - private static final int POTION_TASK_DELAY = 1000; // 1 second - private static final String[] VOICED_COMMANDS = { "apon", @@ -57,42 +45,20 @@ public class AutoPotion implements IVoicedCommandHandler return false; } - final int playerOID = activeChar.getObjectId(); if (command.equals("apon")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - AUTO_POTION_TASKS.put(activeChar.getObjectId(), ThreadPool.scheduleAtFixedRate(new AutoPotionTask(activeChar), POTION_TASK_DELAY, POTION_TASK_DELAY)); + AutoPotionTaskManager.getInstance().add(activeChar); activeChar.sendMessage("Auto potions is enabled."); return true; } else if (command.equals("apoff")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } + AutoPotionTaskManager.getInstance().remove(activeChar); activeChar.sendMessage("Auto potions is disabled."); } return false; } - @RegisterEvent(EventType.ON_PLAYER_LOGOUT) - @RegisterType(ListenerRegisterType.GLOBAL) - public void OnPlayerLogout(OnPlayerLogout event) - { - final int playerOID = event.getPlayer().getObjectId(); - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - } - @Override public String[] getVoicedCommandList() { diff --git a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java b/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java deleted file mode 100644 index 3366932e4e..0000000000 --- a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.l2jmobius.gameserver.model.actor.tasks.player; - -import org.l2jmobius.Config; -import org.l2jmobius.gameserver.handler.ItemHandler; -import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.items.instance.ItemInstance; - -/** - * @author Mobius - */ -public class AutoPotionTask implements Runnable -{ - private final PlayerInstance _player; - - public AutoPotionTask(PlayerInstance player) - { - _player = player; - } - - @Override - public void run() - { - if ((_player == null) || (_player.isOnlineInt() != 1) || _player.isAlikeDead() || (!Config.AUTO_POTIONS_IN_OLYMPIAD && _player.isInOlympiadMode())) - { - return; - } - - boolean success = false; - if (Config.AUTO_HP_ENABLED) - { - final boolean restoreHP = ((_player.getStatus().getCurrentHp() / _player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; - for (int itemId : Config.AUTO_HP_ITEM_IDS) - { - final ItemInstance hpPotion = _player.getInventory().getItemByItemId(itemId); - if ((hpPotion != null) && (hpPotion.getCount() > 0)) - { - success = true; - if (restoreHP) - { - ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(_player, hpPotion, false); - _player.sendMessage("Auto potion: Restored HP."); - break; - } - } - } - } - if (Config.AUTO_CP_ENABLED) - { - final boolean restoreCP = ((_player.getStatus().getCurrentCp() / _player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; - for (int itemId : Config.AUTO_CP_ITEM_IDS) - { - final ItemInstance cpPotion = _player.getInventory().getItemByItemId(itemId); - if ((cpPotion != null) && (cpPotion.getCount() > 0)) - { - success = true; - if (restoreCP) - { - ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(_player, cpPotion, false); - _player.sendMessage("Auto potion: Restored CP."); - break; - } - } - } - } - if (Config.AUTO_MP_ENABLED) - { - final boolean restoreMP = ((_player.getStatus().getCurrentMp() / _player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; - for (int itemId : Config.AUTO_MP_ITEM_IDS) - { - final ItemInstance mpPotion = _player.getInventory().getItemByItemId(itemId); - if ((mpPotion != null) && (mpPotion.getCount() > 0)) - { - success = true; - if (restoreMP) - { - ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(_player, mpPotion, false); - _player.sendMessage("Auto potion: Restored MP."); - break; - } - } - } - } - - if (!success) - { - _player.sendMessage("Auto potion: You are out of potions!"); - } - } -} diff --git a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java b/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java new file mode 100644 index 0000000000..46f97cf7d9 --- /dev/null +++ b/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java @@ -0,0 +1,142 @@ +/* + * This file is part of the L2J Mobius project. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.l2jmobius.gameserver.taskmanager; + +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import org.l2jmobius.Config; +import org.l2jmobius.commons.concurrent.ThreadPool; +import org.l2jmobius.gameserver.handler.ItemHandler; +import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; +import org.l2jmobius.gameserver.model.items.instance.ItemInstance; + +/** + * @author Mobius, Gigi + */ +public class AutoPotionTaskManager +{ + private static final Set PLAYERS = ConcurrentHashMap.newKeySet(); + private static boolean _working = false; + + public AutoPotionTaskManager() + { + ThreadPool.scheduleAtFixedRate(() -> + { + if (_working) + { + return; + } + _working = true; + + PLAYER: for (PlayerInstance player : PLAYERS) + { + if ((player == null) || player.isAlikeDead() || (player.isOnlineInt() != 1) || (!Config.AUTO_POTIONS_IN_OLYMPIAD && player.isInOlympiadMode())) + { + remove(player); + continue PLAYER; + } + + boolean success = false; + if (Config.AUTO_HP_ENABLED) + { + final boolean restoreHP = ((player.getStatus().getCurrentHp() / player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; + HP: for (int itemId : Config.AUTO_HP_ITEM_IDS) + { + final ItemInstance hpPotion = player.getInventory().getItemByItemId(itemId); + if ((hpPotion != null) && (hpPotion.getCount() > 0)) + { + success = true; + if (restoreHP) + { + ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(player, hpPotion, false); + player.sendMessage("Auto potion: Restored HP."); + break HP; + } + } + } + } + if (Config.AUTO_CP_ENABLED) + { + final boolean restoreCP = ((player.getStatus().getCurrentCp() / player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; + CP: for (int itemId : Config.AUTO_CP_ITEM_IDS) + { + final ItemInstance cpPotion = player.getInventory().getItemByItemId(itemId); + if ((cpPotion != null) && (cpPotion.getCount() > 0)) + { + success = true; + if (restoreCP) + { + ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(player, cpPotion, false); + player.sendMessage("Auto potion: Restored CP."); + break CP; + } + } + } + } + if (Config.AUTO_MP_ENABLED) + { + final boolean restoreMP = ((player.getStatus().getCurrentMp() / player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; + MP: for (int itemId : Config.AUTO_MP_ITEM_IDS) + { + final ItemInstance mpPotion = player.getInventory().getItemByItemId(itemId); + if ((mpPotion != null) && (mpPotion.getCount() > 0)) + { + success = true; + if (restoreMP) + { + ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(player, mpPotion, false); + player.sendMessage("Auto potion: Restored MP."); + break MP; + } + } + } + } + + if (!success) + { + player.sendMessage("Auto potion: You are out of potions!"); + } + } + + _working = false; + }, 0, 1000); + } + + public void add(PlayerInstance player) + { + if (!PLAYERS.contains(player)) + { + PLAYERS.add(player); + } + } + + public void remove(PlayerInstance player) + { + PLAYERS.remove(player); + } + + public static AutoPotionTaskManager getInstance() + { + return SingletonHolder.INSTANCE; + } + + private static class SingletonHolder + { + protected static final AutoPotionTaskManager INSTANCE = new AutoPotionTaskManager(); + } +} diff --git a/L2J_Mobius_Classic_2.2_Antharas/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java b/L2J_Mobius_Classic_2.2_Antharas/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java index 722da268b9..5e40a92ff0 100644 --- a/L2J_Mobius_Classic_2.2_Antharas/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java +++ b/L2J_Mobius_Classic_2.2_Antharas/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java @@ -16,28 +16,16 @@ */ package handlers.voicedcommandhandlers; -import java.util.HashMap; -import java.util.concurrent.Future; - import org.l2jmobius.Config; -import org.l2jmobius.commons.concurrent.ThreadPool; import org.l2jmobius.gameserver.handler.IVoicedCommandHandler; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.actor.tasks.player.AutoPotionTask; -import org.l2jmobius.gameserver.model.events.EventType; -import org.l2jmobius.gameserver.model.events.ListenerRegisterType; -import org.l2jmobius.gameserver.model.events.annotations.RegisterEvent; -import org.l2jmobius.gameserver.model.events.annotations.RegisterType; -import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerLogout; +import org.l2jmobius.gameserver.taskmanager.AutoPotionTaskManager; /** - * @author Gigi, Mobius + * @author Mobius, Gigi */ public class AutoPotion implements IVoicedCommandHandler { - private static final HashMap> AUTO_POTION_TASKS = new HashMap<>(); - private static final int POTION_TASK_DELAY = 1000; // 1 second - private static final String[] VOICED_COMMANDS = { "apon", @@ -57,42 +45,20 @@ public class AutoPotion implements IVoicedCommandHandler return false; } - final int playerOID = activeChar.getObjectId(); if (command.equals("apon")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - AUTO_POTION_TASKS.put(activeChar.getObjectId(), ThreadPool.scheduleAtFixedRate(new AutoPotionTask(activeChar), POTION_TASK_DELAY, POTION_TASK_DELAY)); + AutoPotionTaskManager.getInstance().add(activeChar); activeChar.sendMessage("Auto potions is enabled."); return true; } else if (command.equals("apoff")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } + AutoPotionTaskManager.getInstance().remove(activeChar); activeChar.sendMessage("Auto potions is disabled."); } return false; } - @RegisterEvent(EventType.ON_PLAYER_LOGOUT) - @RegisterType(ListenerRegisterType.GLOBAL) - public void OnPlayerLogout(OnPlayerLogout event) - { - final int playerOID = event.getPlayer().getObjectId(); - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - } - @Override public String[] getVoicedCommandList() { diff --git a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java b/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java deleted file mode 100644 index 3366932e4e..0000000000 --- a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.l2jmobius.gameserver.model.actor.tasks.player; - -import org.l2jmobius.Config; -import org.l2jmobius.gameserver.handler.ItemHandler; -import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.items.instance.ItemInstance; - -/** - * @author Mobius - */ -public class AutoPotionTask implements Runnable -{ - private final PlayerInstance _player; - - public AutoPotionTask(PlayerInstance player) - { - _player = player; - } - - @Override - public void run() - { - if ((_player == null) || (_player.isOnlineInt() != 1) || _player.isAlikeDead() || (!Config.AUTO_POTIONS_IN_OLYMPIAD && _player.isInOlympiadMode())) - { - return; - } - - boolean success = false; - if (Config.AUTO_HP_ENABLED) - { - final boolean restoreHP = ((_player.getStatus().getCurrentHp() / _player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; - for (int itemId : Config.AUTO_HP_ITEM_IDS) - { - final ItemInstance hpPotion = _player.getInventory().getItemByItemId(itemId); - if ((hpPotion != null) && (hpPotion.getCount() > 0)) - { - success = true; - if (restoreHP) - { - ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(_player, hpPotion, false); - _player.sendMessage("Auto potion: Restored HP."); - break; - } - } - } - } - if (Config.AUTO_CP_ENABLED) - { - final boolean restoreCP = ((_player.getStatus().getCurrentCp() / _player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; - for (int itemId : Config.AUTO_CP_ITEM_IDS) - { - final ItemInstance cpPotion = _player.getInventory().getItemByItemId(itemId); - if ((cpPotion != null) && (cpPotion.getCount() > 0)) - { - success = true; - if (restoreCP) - { - ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(_player, cpPotion, false); - _player.sendMessage("Auto potion: Restored CP."); - break; - } - } - } - } - if (Config.AUTO_MP_ENABLED) - { - final boolean restoreMP = ((_player.getStatus().getCurrentMp() / _player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; - for (int itemId : Config.AUTO_MP_ITEM_IDS) - { - final ItemInstance mpPotion = _player.getInventory().getItemByItemId(itemId); - if ((mpPotion != null) && (mpPotion.getCount() > 0)) - { - success = true; - if (restoreMP) - { - ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(_player, mpPotion, false); - _player.sendMessage("Auto potion: Restored MP."); - break; - } - } - } - } - - if (!success) - { - _player.sendMessage("Auto potion: You are out of potions!"); - } - } -} diff --git a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java b/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java new file mode 100644 index 0000000000..46f97cf7d9 --- /dev/null +++ b/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java @@ -0,0 +1,142 @@ +/* + * This file is part of the L2J Mobius project. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.l2jmobius.gameserver.taskmanager; + +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import org.l2jmobius.Config; +import org.l2jmobius.commons.concurrent.ThreadPool; +import org.l2jmobius.gameserver.handler.ItemHandler; +import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; +import org.l2jmobius.gameserver.model.items.instance.ItemInstance; + +/** + * @author Mobius, Gigi + */ +public class AutoPotionTaskManager +{ + private static final Set PLAYERS = ConcurrentHashMap.newKeySet(); + private static boolean _working = false; + + public AutoPotionTaskManager() + { + ThreadPool.scheduleAtFixedRate(() -> + { + if (_working) + { + return; + } + _working = true; + + PLAYER: for (PlayerInstance player : PLAYERS) + { + if ((player == null) || player.isAlikeDead() || (player.isOnlineInt() != 1) || (!Config.AUTO_POTIONS_IN_OLYMPIAD && player.isInOlympiadMode())) + { + remove(player); + continue PLAYER; + } + + boolean success = false; + if (Config.AUTO_HP_ENABLED) + { + final boolean restoreHP = ((player.getStatus().getCurrentHp() / player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; + HP: for (int itemId : Config.AUTO_HP_ITEM_IDS) + { + final ItemInstance hpPotion = player.getInventory().getItemByItemId(itemId); + if ((hpPotion != null) && (hpPotion.getCount() > 0)) + { + success = true; + if (restoreHP) + { + ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(player, hpPotion, false); + player.sendMessage("Auto potion: Restored HP."); + break HP; + } + } + } + } + if (Config.AUTO_CP_ENABLED) + { + final boolean restoreCP = ((player.getStatus().getCurrentCp() / player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; + CP: for (int itemId : Config.AUTO_CP_ITEM_IDS) + { + final ItemInstance cpPotion = player.getInventory().getItemByItemId(itemId); + if ((cpPotion != null) && (cpPotion.getCount() > 0)) + { + success = true; + if (restoreCP) + { + ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(player, cpPotion, false); + player.sendMessage("Auto potion: Restored CP."); + break CP; + } + } + } + } + if (Config.AUTO_MP_ENABLED) + { + final boolean restoreMP = ((player.getStatus().getCurrentMp() / player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; + MP: for (int itemId : Config.AUTO_MP_ITEM_IDS) + { + final ItemInstance mpPotion = player.getInventory().getItemByItemId(itemId); + if ((mpPotion != null) && (mpPotion.getCount() > 0)) + { + success = true; + if (restoreMP) + { + ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(player, mpPotion, false); + player.sendMessage("Auto potion: Restored MP."); + break MP; + } + } + } + } + + if (!success) + { + player.sendMessage("Auto potion: You are out of potions!"); + } + } + + _working = false; + }, 0, 1000); + } + + public void add(PlayerInstance player) + { + if (!PLAYERS.contains(player)) + { + PLAYERS.add(player); + } + } + + public void remove(PlayerInstance player) + { + PLAYERS.remove(player); + } + + public static AutoPotionTaskManager getInstance() + { + return SingletonHolder.INSTANCE; + } + + private static class SingletonHolder + { + protected static final AutoPotionTaskManager INSTANCE = new AutoPotionTaskManager(); + } +} diff --git a/L2J_Mobius_Classic_2.3_SevenSigns/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java b/L2J_Mobius_Classic_2.3_SevenSigns/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java index 722da268b9..5e40a92ff0 100644 --- a/L2J_Mobius_Classic_2.3_SevenSigns/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java +++ b/L2J_Mobius_Classic_2.3_SevenSigns/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java @@ -16,28 +16,16 @@ */ package handlers.voicedcommandhandlers; -import java.util.HashMap; -import java.util.concurrent.Future; - import org.l2jmobius.Config; -import org.l2jmobius.commons.concurrent.ThreadPool; import org.l2jmobius.gameserver.handler.IVoicedCommandHandler; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.actor.tasks.player.AutoPotionTask; -import org.l2jmobius.gameserver.model.events.EventType; -import org.l2jmobius.gameserver.model.events.ListenerRegisterType; -import org.l2jmobius.gameserver.model.events.annotations.RegisterEvent; -import org.l2jmobius.gameserver.model.events.annotations.RegisterType; -import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerLogout; +import org.l2jmobius.gameserver.taskmanager.AutoPotionTaskManager; /** - * @author Gigi, Mobius + * @author Mobius, Gigi */ public class AutoPotion implements IVoicedCommandHandler { - private static final HashMap> AUTO_POTION_TASKS = new HashMap<>(); - private static final int POTION_TASK_DELAY = 1000; // 1 second - private static final String[] VOICED_COMMANDS = { "apon", @@ -57,42 +45,20 @@ public class AutoPotion implements IVoicedCommandHandler return false; } - final int playerOID = activeChar.getObjectId(); if (command.equals("apon")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - AUTO_POTION_TASKS.put(activeChar.getObjectId(), ThreadPool.scheduleAtFixedRate(new AutoPotionTask(activeChar), POTION_TASK_DELAY, POTION_TASK_DELAY)); + AutoPotionTaskManager.getInstance().add(activeChar); activeChar.sendMessage("Auto potions is enabled."); return true; } else if (command.equals("apoff")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } + AutoPotionTaskManager.getInstance().remove(activeChar); activeChar.sendMessage("Auto potions is disabled."); } return false; } - @RegisterEvent(EventType.ON_PLAYER_LOGOUT) - @RegisterType(ListenerRegisterType.GLOBAL) - public void OnPlayerLogout(OnPlayerLogout event) - { - final int playerOID = event.getPlayer().getObjectId(); - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - } - @Override public String[] getVoicedCommandList() { diff --git a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java b/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java deleted file mode 100644 index 3366932e4e..0000000000 --- a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.l2jmobius.gameserver.model.actor.tasks.player; - -import org.l2jmobius.Config; -import org.l2jmobius.gameserver.handler.ItemHandler; -import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.items.instance.ItemInstance; - -/** - * @author Mobius - */ -public class AutoPotionTask implements Runnable -{ - private final PlayerInstance _player; - - public AutoPotionTask(PlayerInstance player) - { - _player = player; - } - - @Override - public void run() - { - if ((_player == null) || (_player.isOnlineInt() != 1) || _player.isAlikeDead() || (!Config.AUTO_POTIONS_IN_OLYMPIAD && _player.isInOlympiadMode())) - { - return; - } - - boolean success = false; - if (Config.AUTO_HP_ENABLED) - { - final boolean restoreHP = ((_player.getStatus().getCurrentHp() / _player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; - for (int itemId : Config.AUTO_HP_ITEM_IDS) - { - final ItemInstance hpPotion = _player.getInventory().getItemByItemId(itemId); - if ((hpPotion != null) && (hpPotion.getCount() > 0)) - { - success = true; - if (restoreHP) - { - ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(_player, hpPotion, false); - _player.sendMessage("Auto potion: Restored HP."); - break; - } - } - } - } - if (Config.AUTO_CP_ENABLED) - { - final boolean restoreCP = ((_player.getStatus().getCurrentCp() / _player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; - for (int itemId : Config.AUTO_CP_ITEM_IDS) - { - final ItemInstance cpPotion = _player.getInventory().getItemByItemId(itemId); - if ((cpPotion != null) && (cpPotion.getCount() > 0)) - { - success = true; - if (restoreCP) - { - ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(_player, cpPotion, false); - _player.sendMessage("Auto potion: Restored CP."); - break; - } - } - } - } - if (Config.AUTO_MP_ENABLED) - { - final boolean restoreMP = ((_player.getStatus().getCurrentMp() / _player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; - for (int itemId : Config.AUTO_MP_ITEM_IDS) - { - final ItemInstance mpPotion = _player.getInventory().getItemByItemId(itemId); - if ((mpPotion != null) && (mpPotion.getCount() > 0)) - { - success = true; - if (restoreMP) - { - ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(_player, mpPotion, false); - _player.sendMessage("Auto potion: Restored MP."); - break; - } - } - } - } - - if (!success) - { - _player.sendMessage("Auto potion: You are out of potions!"); - } - } -} diff --git a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java b/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java new file mode 100644 index 0000000000..46f97cf7d9 --- /dev/null +++ b/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java @@ -0,0 +1,142 @@ +/* + * This file is part of the L2J Mobius project. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.l2jmobius.gameserver.taskmanager; + +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import org.l2jmobius.Config; +import org.l2jmobius.commons.concurrent.ThreadPool; +import org.l2jmobius.gameserver.handler.ItemHandler; +import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; +import org.l2jmobius.gameserver.model.items.instance.ItemInstance; + +/** + * @author Mobius, Gigi + */ +public class AutoPotionTaskManager +{ + private static final Set PLAYERS = ConcurrentHashMap.newKeySet(); + private static boolean _working = false; + + public AutoPotionTaskManager() + { + ThreadPool.scheduleAtFixedRate(() -> + { + if (_working) + { + return; + } + _working = true; + + PLAYER: for (PlayerInstance player : PLAYERS) + { + if ((player == null) || player.isAlikeDead() || (player.isOnlineInt() != 1) || (!Config.AUTO_POTIONS_IN_OLYMPIAD && player.isInOlympiadMode())) + { + remove(player); + continue PLAYER; + } + + boolean success = false; + if (Config.AUTO_HP_ENABLED) + { + final boolean restoreHP = ((player.getStatus().getCurrentHp() / player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; + HP: for (int itemId : Config.AUTO_HP_ITEM_IDS) + { + final ItemInstance hpPotion = player.getInventory().getItemByItemId(itemId); + if ((hpPotion != null) && (hpPotion.getCount() > 0)) + { + success = true; + if (restoreHP) + { + ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(player, hpPotion, false); + player.sendMessage("Auto potion: Restored HP."); + break HP; + } + } + } + } + if (Config.AUTO_CP_ENABLED) + { + final boolean restoreCP = ((player.getStatus().getCurrentCp() / player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; + CP: for (int itemId : Config.AUTO_CP_ITEM_IDS) + { + final ItemInstance cpPotion = player.getInventory().getItemByItemId(itemId); + if ((cpPotion != null) && (cpPotion.getCount() > 0)) + { + success = true; + if (restoreCP) + { + ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(player, cpPotion, false); + player.sendMessage("Auto potion: Restored CP."); + break CP; + } + } + } + } + if (Config.AUTO_MP_ENABLED) + { + final boolean restoreMP = ((player.getStatus().getCurrentMp() / player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; + MP: for (int itemId : Config.AUTO_MP_ITEM_IDS) + { + final ItemInstance mpPotion = player.getInventory().getItemByItemId(itemId); + if ((mpPotion != null) && (mpPotion.getCount() > 0)) + { + success = true; + if (restoreMP) + { + ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(player, mpPotion, false); + player.sendMessage("Auto potion: Restored MP."); + break MP; + } + } + } + } + + if (!success) + { + player.sendMessage("Auto potion: You are out of potions!"); + } + } + + _working = false; + }, 0, 1000); + } + + public void add(PlayerInstance player) + { + if (!PLAYERS.contains(player)) + { + PLAYERS.add(player); + } + } + + public void remove(PlayerInstance player) + { + PLAYERS.remove(player); + } + + public static AutoPotionTaskManager getInstance() + { + return SingletonHolder.INSTANCE; + } + + private static class SingletonHolder + { + protected static final AutoPotionTaskManager INSTANCE = new AutoPotionTaskManager(); + } +} diff --git a/L2J_Mobius_Classic_2.4_SecretOfEmpire/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java b/L2J_Mobius_Classic_2.4_SecretOfEmpire/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java index 722da268b9..5e40a92ff0 100644 --- a/L2J_Mobius_Classic_2.4_SecretOfEmpire/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java +++ b/L2J_Mobius_Classic_2.4_SecretOfEmpire/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java @@ -16,28 +16,16 @@ */ package handlers.voicedcommandhandlers; -import java.util.HashMap; -import java.util.concurrent.Future; - import org.l2jmobius.Config; -import org.l2jmobius.commons.concurrent.ThreadPool; import org.l2jmobius.gameserver.handler.IVoicedCommandHandler; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.actor.tasks.player.AutoPotionTask; -import org.l2jmobius.gameserver.model.events.EventType; -import org.l2jmobius.gameserver.model.events.ListenerRegisterType; -import org.l2jmobius.gameserver.model.events.annotations.RegisterEvent; -import org.l2jmobius.gameserver.model.events.annotations.RegisterType; -import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerLogout; +import org.l2jmobius.gameserver.taskmanager.AutoPotionTaskManager; /** - * @author Gigi, Mobius + * @author Mobius, Gigi */ public class AutoPotion implements IVoicedCommandHandler { - private static final HashMap> AUTO_POTION_TASKS = new HashMap<>(); - private static final int POTION_TASK_DELAY = 1000; // 1 second - private static final String[] VOICED_COMMANDS = { "apon", @@ -57,42 +45,20 @@ public class AutoPotion implements IVoicedCommandHandler return false; } - final int playerOID = activeChar.getObjectId(); if (command.equals("apon")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - AUTO_POTION_TASKS.put(activeChar.getObjectId(), ThreadPool.scheduleAtFixedRate(new AutoPotionTask(activeChar), POTION_TASK_DELAY, POTION_TASK_DELAY)); + AutoPotionTaskManager.getInstance().add(activeChar); activeChar.sendMessage("Auto potions is enabled."); return true; } else if (command.equals("apoff")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } + AutoPotionTaskManager.getInstance().remove(activeChar); activeChar.sendMessage("Auto potions is disabled."); } return false; } - @RegisterEvent(EventType.ON_PLAYER_LOGOUT) - @RegisterType(ListenerRegisterType.GLOBAL) - public void OnPlayerLogout(OnPlayerLogout event) - { - final int playerOID = event.getPlayer().getObjectId(); - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - } - @Override public String[] getVoicedCommandList() { diff --git a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java b/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java deleted file mode 100644 index 3366932e4e..0000000000 --- a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.l2jmobius.gameserver.model.actor.tasks.player; - -import org.l2jmobius.Config; -import org.l2jmobius.gameserver.handler.ItemHandler; -import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.items.instance.ItemInstance; - -/** - * @author Mobius - */ -public class AutoPotionTask implements Runnable -{ - private final PlayerInstance _player; - - public AutoPotionTask(PlayerInstance player) - { - _player = player; - } - - @Override - public void run() - { - if ((_player == null) || (_player.isOnlineInt() != 1) || _player.isAlikeDead() || (!Config.AUTO_POTIONS_IN_OLYMPIAD && _player.isInOlympiadMode())) - { - return; - } - - boolean success = false; - if (Config.AUTO_HP_ENABLED) - { - final boolean restoreHP = ((_player.getStatus().getCurrentHp() / _player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; - for (int itemId : Config.AUTO_HP_ITEM_IDS) - { - final ItemInstance hpPotion = _player.getInventory().getItemByItemId(itemId); - if ((hpPotion != null) && (hpPotion.getCount() > 0)) - { - success = true; - if (restoreHP) - { - ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(_player, hpPotion, false); - _player.sendMessage("Auto potion: Restored HP."); - break; - } - } - } - } - if (Config.AUTO_CP_ENABLED) - { - final boolean restoreCP = ((_player.getStatus().getCurrentCp() / _player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; - for (int itemId : Config.AUTO_CP_ITEM_IDS) - { - final ItemInstance cpPotion = _player.getInventory().getItemByItemId(itemId); - if ((cpPotion != null) && (cpPotion.getCount() > 0)) - { - success = true; - if (restoreCP) - { - ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(_player, cpPotion, false); - _player.sendMessage("Auto potion: Restored CP."); - break; - } - } - } - } - if (Config.AUTO_MP_ENABLED) - { - final boolean restoreMP = ((_player.getStatus().getCurrentMp() / _player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; - for (int itemId : Config.AUTO_MP_ITEM_IDS) - { - final ItemInstance mpPotion = _player.getInventory().getItemByItemId(itemId); - if ((mpPotion != null) && (mpPotion.getCount() > 0)) - { - success = true; - if (restoreMP) - { - ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(_player, mpPotion, false); - _player.sendMessage("Auto potion: Restored MP."); - break; - } - } - } - } - - if (!success) - { - _player.sendMessage("Auto potion: You are out of potions!"); - } - } -} diff --git a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java b/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java new file mode 100644 index 0000000000..46f97cf7d9 --- /dev/null +++ b/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java @@ -0,0 +1,142 @@ +/* + * This file is part of the L2J Mobius project. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.l2jmobius.gameserver.taskmanager; + +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import org.l2jmobius.Config; +import org.l2jmobius.commons.concurrent.ThreadPool; +import org.l2jmobius.gameserver.handler.ItemHandler; +import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; +import org.l2jmobius.gameserver.model.items.instance.ItemInstance; + +/** + * @author Mobius, Gigi + */ +public class AutoPotionTaskManager +{ + private static final Set PLAYERS = ConcurrentHashMap.newKeySet(); + private static boolean _working = false; + + public AutoPotionTaskManager() + { + ThreadPool.scheduleAtFixedRate(() -> + { + if (_working) + { + return; + } + _working = true; + + PLAYER: for (PlayerInstance player : PLAYERS) + { + if ((player == null) || player.isAlikeDead() || (player.isOnlineInt() != 1) || (!Config.AUTO_POTIONS_IN_OLYMPIAD && player.isInOlympiadMode())) + { + remove(player); + continue PLAYER; + } + + boolean success = false; + if (Config.AUTO_HP_ENABLED) + { + final boolean restoreHP = ((player.getStatus().getCurrentHp() / player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; + HP: for (int itemId : Config.AUTO_HP_ITEM_IDS) + { + final ItemInstance hpPotion = player.getInventory().getItemByItemId(itemId); + if ((hpPotion != null) && (hpPotion.getCount() > 0)) + { + success = true; + if (restoreHP) + { + ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(player, hpPotion, false); + player.sendMessage("Auto potion: Restored HP."); + break HP; + } + } + } + } + if (Config.AUTO_CP_ENABLED) + { + final boolean restoreCP = ((player.getStatus().getCurrentCp() / player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; + CP: for (int itemId : Config.AUTO_CP_ITEM_IDS) + { + final ItemInstance cpPotion = player.getInventory().getItemByItemId(itemId); + if ((cpPotion != null) && (cpPotion.getCount() > 0)) + { + success = true; + if (restoreCP) + { + ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(player, cpPotion, false); + player.sendMessage("Auto potion: Restored CP."); + break CP; + } + } + } + } + if (Config.AUTO_MP_ENABLED) + { + final boolean restoreMP = ((player.getStatus().getCurrentMp() / player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; + MP: for (int itemId : Config.AUTO_MP_ITEM_IDS) + { + final ItemInstance mpPotion = player.getInventory().getItemByItemId(itemId); + if ((mpPotion != null) && (mpPotion.getCount() > 0)) + { + success = true; + if (restoreMP) + { + ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(player, mpPotion, false); + player.sendMessage("Auto potion: Restored MP."); + break MP; + } + } + } + } + + if (!success) + { + player.sendMessage("Auto potion: You are out of potions!"); + } + } + + _working = false; + }, 0, 1000); + } + + public void add(PlayerInstance player) + { + if (!PLAYERS.contains(player)) + { + PLAYERS.add(player); + } + } + + public void remove(PlayerInstance player) + { + PLAYERS.remove(player); + } + + public static AutoPotionTaskManager getInstance() + { + return SingletonHolder.INSTANCE; + } + + private static class SingletonHolder + { + protected static final AutoPotionTaskManager INSTANCE = new AutoPotionTaskManager(); + } +} diff --git a/L2J_Mobius_Classic_3.0_TheKamael/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java b/L2J_Mobius_Classic_3.0_TheKamael/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java index 722da268b9..5e40a92ff0 100644 --- a/L2J_Mobius_Classic_3.0_TheKamael/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java +++ b/L2J_Mobius_Classic_3.0_TheKamael/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java @@ -16,28 +16,16 @@ */ package handlers.voicedcommandhandlers; -import java.util.HashMap; -import java.util.concurrent.Future; - import org.l2jmobius.Config; -import org.l2jmobius.commons.concurrent.ThreadPool; import org.l2jmobius.gameserver.handler.IVoicedCommandHandler; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.actor.tasks.player.AutoPotionTask; -import org.l2jmobius.gameserver.model.events.EventType; -import org.l2jmobius.gameserver.model.events.ListenerRegisterType; -import org.l2jmobius.gameserver.model.events.annotations.RegisterEvent; -import org.l2jmobius.gameserver.model.events.annotations.RegisterType; -import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerLogout; +import org.l2jmobius.gameserver.taskmanager.AutoPotionTaskManager; /** - * @author Gigi, Mobius + * @author Mobius, Gigi */ public class AutoPotion implements IVoicedCommandHandler { - private static final HashMap> AUTO_POTION_TASKS = new HashMap<>(); - private static final int POTION_TASK_DELAY = 1000; // 1 second - private static final String[] VOICED_COMMANDS = { "apon", @@ -57,42 +45,20 @@ public class AutoPotion implements IVoicedCommandHandler return false; } - final int playerOID = activeChar.getObjectId(); if (command.equals("apon")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - AUTO_POTION_TASKS.put(activeChar.getObjectId(), ThreadPool.scheduleAtFixedRate(new AutoPotionTask(activeChar), POTION_TASK_DELAY, POTION_TASK_DELAY)); + AutoPotionTaskManager.getInstance().add(activeChar); activeChar.sendMessage("Auto potions is enabled."); return true; } else if (command.equals("apoff")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } + AutoPotionTaskManager.getInstance().remove(activeChar); activeChar.sendMessage("Auto potions is disabled."); } return false; } - @RegisterEvent(EventType.ON_PLAYER_LOGOUT) - @RegisterType(ListenerRegisterType.GLOBAL) - public void OnPlayerLogout(OnPlayerLogout event) - { - final int playerOID = event.getPlayer().getObjectId(); - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - } - @Override public String[] getVoicedCommandList() { diff --git a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java deleted file mode 100644 index 3366932e4e..0000000000 --- a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.l2jmobius.gameserver.model.actor.tasks.player; - -import org.l2jmobius.Config; -import org.l2jmobius.gameserver.handler.ItemHandler; -import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.items.instance.ItemInstance; - -/** - * @author Mobius - */ -public class AutoPotionTask implements Runnable -{ - private final PlayerInstance _player; - - public AutoPotionTask(PlayerInstance player) - { - _player = player; - } - - @Override - public void run() - { - if ((_player == null) || (_player.isOnlineInt() != 1) || _player.isAlikeDead() || (!Config.AUTO_POTIONS_IN_OLYMPIAD && _player.isInOlympiadMode())) - { - return; - } - - boolean success = false; - if (Config.AUTO_HP_ENABLED) - { - final boolean restoreHP = ((_player.getStatus().getCurrentHp() / _player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; - for (int itemId : Config.AUTO_HP_ITEM_IDS) - { - final ItemInstance hpPotion = _player.getInventory().getItemByItemId(itemId); - if ((hpPotion != null) && (hpPotion.getCount() > 0)) - { - success = true; - if (restoreHP) - { - ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(_player, hpPotion, false); - _player.sendMessage("Auto potion: Restored HP."); - break; - } - } - } - } - if (Config.AUTO_CP_ENABLED) - { - final boolean restoreCP = ((_player.getStatus().getCurrentCp() / _player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; - for (int itemId : Config.AUTO_CP_ITEM_IDS) - { - final ItemInstance cpPotion = _player.getInventory().getItemByItemId(itemId); - if ((cpPotion != null) && (cpPotion.getCount() > 0)) - { - success = true; - if (restoreCP) - { - ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(_player, cpPotion, false); - _player.sendMessage("Auto potion: Restored CP."); - break; - } - } - } - } - if (Config.AUTO_MP_ENABLED) - { - final boolean restoreMP = ((_player.getStatus().getCurrentMp() / _player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; - for (int itemId : Config.AUTO_MP_ITEM_IDS) - { - final ItemInstance mpPotion = _player.getInventory().getItemByItemId(itemId); - if ((mpPotion != null) && (mpPotion.getCount() > 0)) - { - success = true; - if (restoreMP) - { - ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(_player, mpPotion, false); - _player.sendMessage("Auto potion: Restored MP."); - break; - } - } - } - } - - if (!success) - { - _player.sendMessage("Auto potion: You are out of potions!"); - } - } -} diff --git a/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java new file mode 100644 index 0000000000..46f97cf7d9 --- /dev/null +++ b/L2J_Mobius_Classic_3.0_TheKamael/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java @@ -0,0 +1,142 @@ +/* + * This file is part of the L2J Mobius project. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.l2jmobius.gameserver.taskmanager; + +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import org.l2jmobius.Config; +import org.l2jmobius.commons.concurrent.ThreadPool; +import org.l2jmobius.gameserver.handler.ItemHandler; +import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; +import org.l2jmobius.gameserver.model.items.instance.ItemInstance; + +/** + * @author Mobius, Gigi + */ +public class AutoPotionTaskManager +{ + private static final Set PLAYERS = ConcurrentHashMap.newKeySet(); + private static boolean _working = false; + + public AutoPotionTaskManager() + { + ThreadPool.scheduleAtFixedRate(() -> + { + if (_working) + { + return; + } + _working = true; + + PLAYER: for (PlayerInstance player : PLAYERS) + { + if ((player == null) || player.isAlikeDead() || (player.isOnlineInt() != 1) || (!Config.AUTO_POTIONS_IN_OLYMPIAD && player.isInOlympiadMode())) + { + remove(player); + continue PLAYER; + } + + boolean success = false; + if (Config.AUTO_HP_ENABLED) + { + final boolean restoreHP = ((player.getStatus().getCurrentHp() / player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; + HP: for (int itemId : Config.AUTO_HP_ITEM_IDS) + { + final ItemInstance hpPotion = player.getInventory().getItemByItemId(itemId); + if ((hpPotion != null) && (hpPotion.getCount() > 0)) + { + success = true; + if (restoreHP) + { + ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(player, hpPotion, false); + player.sendMessage("Auto potion: Restored HP."); + break HP; + } + } + } + } + if (Config.AUTO_CP_ENABLED) + { + final boolean restoreCP = ((player.getStatus().getCurrentCp() / player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; + CP: for (int itemId : Config.AUTO_CP_ITEM_IDS) + { + final ItemInstance cpPotion = player.getInventory().getItemByItemId(itemId); + if ((cpPotion != null) && (cpPotion.getCount() > 0)) + { + success = true; + if (restoreCP) + { + ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(player, cpPotion, false); + player.sendMessage("Auto potion: Restored CP."); + break CP; + } + } + } + } + if (Config.AUTO_MP_ENABLED) + { + final boolean restoreMP = ((player.getStatus().getCurrentMp() / player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; + MP: for (int itemId : Config.AUTO_MP_ITEM_IDS) + { + final ItemInstance mpPotion = player.getInventory().getItemByItemId(itemId); + if ((mpPotion != null) && (mpPotion.getCount() > 0)) + { + success = true; + if (restoreMP) + { + ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(player, mpPotion, false); + player.sendMessage("Auto potion: Restored MP."); + break MP; + } + } + } + } + + if (!success) + { + player.sendMessage("Auto potion: You are out of potions!"); + } + } + + _working = false; + }, 0, 1000); + } + + public void add(PlayerInstance player) + { + if (!PLAYERS.contains(player)) + { + PLAYERS.add(player); + } + } + + public void remove(PlayerInstance player) + { + PLAYERS.remove(player); + } + + public static AutoPotionTaskManager getInstance() + { + return SingletonHolder.INSTANCE; + } + + private static class SingletonHolder + { + protected static final AutoPotionTaskManager INSTANCE = new AutoPotionTaskManager(); + } +} diff --git a/L2J_Mobius_Classic_Interlude/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java b/L2J_Mobius_Classic_Interlude/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java index 722da268b9..5e40a92ff0 100644 --- a/L2J_Mobius_Classic_Interlude/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java +++ b/L2J_Mobius_Classic_Interlude/dist/game/data/scripts/handlers/voicedcommandhandlers/AutoPotion.java @@ -16,28 +16,16 @@ */ package handlers.voicedcommandhandlers; -import java.util.HashMap; -import java.util.concurrent.Future; - import org.l2jmobius.Config; -import org.l2jmobius.commons.concurrent.ThreadPool; import org.l2jmobius.gameserver.handler.IVoicedCommandHandler; import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.actor.tasks.player.AutoPotionTask; -import org.l2jmobius.gameserver.model.events.EventType; -import org.l2jmobius.gameserver.model.events.ListenerRegisterType; -import org.l2jmobius.gameserver.model.events.annotations.RegisterEvent; -import org.l2jmobius.gameserver.model.events.annotations.RegisterType; -import org.l2jmobius.gameserver.model.events.impl.creature.player.OnPlayerLogout; +import org.l2jmobius.gameserver.taskmanager.AutoPotionTaskManager; /** - * @author Gigi, Mobius + * @author Mobius, Gigi */ public class AutoPotion implements IVoicedCommandHandler { - private static final HashMap> AUTO_POTION_TASKS = new HashMap<>(); - private static final int POTION_TASK_DELAY = 1000; // 1 second - private static final String[] VOICED_COMMANDS = { "apon", @@ -57,42 +45,20 @@ public class AutoPotion implements IVoicedCommandHandler return false; } - final int playerOID = activeChar.getObjectId(); if (command.equals("apon")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - AUTO_POTION_TASKS.put(activeChar.getObjectId(), ThreadPool.scheduleAtFixedRate(new AutoPotionTask(activeChar), POTION_TASK_DELAY, POTION_TASK_DELAY)); + AutoPotionTaskManager.getInstance().add(activeChar); activeChar.sendMessage("Auto potions is enabled."); return true; } else if (command.equals("apoff")) { - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } + AutoPotionTaskManager.getInstance().remove(activeChar); activeChar.sendMessage("Auto potions is disabled."); } return false; } - @RegisterEvent(EventType.ON_PLAYER_LOGOUT) - @RegisterType(ListenerRegisterType.GLOBAL) - public void OnPlayerLogout(OnPlayerLogout event) - { - final int playerOID = event.getPlayer().getObjectId(); - if (AUTO_POTION_TASKS.containsKey(playerOID)) - { - AUTO_POTION_TASKS.get(playerOID).cancel(true); - AUTO_POTION_TASKS.remove(playerOID); - } - } - @Override public String[] getVoicedCommandList() { diff --git a/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java b/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java deleted file mode 100644 index 3366932e4e..0000000000 --- a/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/model/actor/tasks/player/AutoPotionTask.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * This file is part of the L2J Mobius project. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.l2jmobius.gameserver.model.actor.tasks.player; - -import org.l2jmobius.Config; -import org.l2jmobius.gameserver.handler.ItemHandler; -import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; -import org.l2jmobius.gameserver.model.items.instance.ItemInstance; - -/** - * @author Mobius - */ -public class AutoPotionTask implements Runnable -{ - private final PlayerInstance _player; - - public AutoPotionTask(PlayerInstance player) - { - _player = player; - } - - @Override - public void run() - { - if ((_player == null) || (_player.isOnlineInt() != 1) || _player.isAlikeDead() || (!Config.AUTO_POTIONS_IN_OLYMPIAD && _player.isInOlympiadMode())) - { - return; - } - - boolean success = false; - if (Config.AUTO_HP_ENABLED) - { - final boolean restoreHP = ((_player.getStatus().getCurrentHp() / _player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; - for (int itemId : Config.AUTO_HP_ITEM_IDS) - { - final ItemInstance hpPotion = _player.getInventory().getItemByItemId(itemId); - if ((hpPotion != null) && (hpPotion.getCount() > 0)) - { - success = true; - if (restoreHP) - { - ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(_player, hpPotion, false); - _player.sendMessage("Auto potion: Restored HP."); - break; - } - } - } - } - if (Config.AUTO_CP_ENABLED) - { - final boolean restoreCP = ((_player.getStatus().getCurrentCp() / _player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; - for (int itemId : Config.AUTO_CP_ITEM_IDS) - { - final ItemInstance cpPotion = _player.getInventory().getItemByItemId(itemId); - if ((cpPotion != null) && (cpPotion.getCount() > 0)) - { - success = true; - if (restoreCP) - { - ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(_player, cpPotion, false); - _player.sendMessage("Auto potion: Restored CP."); - break; - } - } - } - } - if (Config.AUTO_MP_ENABLED) - { - final boolean restoreMP = ((_player.getStatus().getCurrentMp() / _player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; - for (int itemId : Config.AUTO_MP_ITEM_IDS) - { - final ItemInstance mpPotion = _player.getInventory().getItemByItemId(itemId); - if ((mpPotion != null) && (mpPotion.getCount() > 0)) - { - success = true; - if (restoreMP) - { - ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(_player, mpPotion, false); - _player.sendMessage("Auto potion: Restored MP."); - break; - } - } - } - } - - if (!success) - { - _player.sendMessage("Auto potion: You are out of potions!"); - } - } -} diff --git a/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java b/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java new file mode 100644 index 0000000000..46f97cf7d9 --- /dev/null +++ b/L2J_Mobius_Classic_Interlude/java/org/l2jmobius/gameserver/taskmanager/AutoPotionTaskManager.java @@ -0,0 +1,142 @@ +/* + * This file is part of the L2J Mobius project. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.l2jmobius.gameserver.taskmanager; + +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import org.l2jmobius.Config; +import org.l2jmobius.commons.concurrent.ThreadPool; +import org.l2jmobius.gameserver.handler.ItemHandler; +import org.l2jmobius.gameserver.model.actor.instance.PlayerInstance; +import org.l2jmobius.gameserver.model.items.instance.ItemInstance; + +/** + * @author Mobius, Gigi + */ +public class AutoPotionTaskManager +{ + private static final Set PLAYERS = ConcurrentHashMap.newKeySet(); + private static boolean _working = false; + + public AutoPotionTaskManager() + { + ThreadPool.scheduleAtFixedRate(() -> + { + if (_working) + { + return; + } + _working = true; + + PLAYER: for (PlayerInstance player : PLAYERS) + { + if ((player == null) || player.isAlikeDead() || (player.isOnlineInt() != 1) || (!Config.AUTO_POTIONS_IN_OLYMPIAD && player.isInOlympiadMode())) + { + remove(player); + continue PLAYER; + } + + boolean success = false; + if (Config.AUTO_HP_ENABLED) + { + final boolean restoreHP = ((player.getStatus().getCurrentHp() / player.getMaxHp()) * 100) < Config.AUTO_HP_PERCENTAGE; + HP: for (int itemId : Config.AUTO_HP_ITEM_IDS) + { + final ItemInstance hpPotion = player.getInventory().getItemByItemId(itemId); + if ((hpPotion != null) && (hpPotion.getCount() > 0)) + { + success = true; + if (restoreHP) + { + ItemHandler.getInstance().getHandler(hpPotion.getEtcItem()).useItem(player, hpPotion, false); + player.sendMessage("Auto potion: Restored HP."); + break HP; + } + } + } + } + if (Config.AUTO_CP_ENABLED) + { + final boolean restoreCP = ((player.getStatus().getCurrentCp() / player.getMaxCp()) * 100) < Config.AUTO_CP_PERCENTAGE; + CP: for (int itemId : Config.AUTO_CP_ITEM_IDS) + { + final ItemInstance cpPotion = player.getInventory().getItemByItemId(itemId); + if ((cpPotion != null) && (cpPotion.getCount() > 0)) + { + success = true; + if (restoreCP) + { + ItemHandler.getInstance().getHandler(cpPotion.getEtcItem()).useItem(player, cpPotion, false); + player.sendMessage("Auto potion: Restored CP."); + break CP; + } + } + } + } + if (Config.AUTO_MP_ENABLED) + { + final boolean restoreMP = ((player.getStatus().getCurrentMp() / player.getMaxMp()) * 100) < Config.AUTO_MP_PERCENTAGE; + MP: for (int itemId : Config.AUTO_MP_ITEM_IDS) + { + final ItemInstance mpPotion = player.getInventory().getItemByItemId(itemId); + if ((mpPotion != null) && (mpPotion.getCount() > 0)) + { + success = true; + if (restoreMP) + { + ItemHandler.getInstance().getHandler(mpPotion.getEtcItem()).useItem(player, mpPotion, false); + player.sendMessage("Auto potion: Restored MP."); + break MP; + } + } + } + } + + if (!success) + { + player.sendMessage("Auto potion: You are out of potions!"); + } + } + + _working = false; + }, 0, 1000); + } + + public void add(PlayerInstance player) + { + if (!PLAYERS.contains(player)) + { + PLAYERS.add(player); + } + } + + public void remove(PlayerInstance player) + { + PLAYERS.remove(player); + } + + public static AutoPotionTaskManager getInstance() + { + return SingletonHolder.INSTANCE; + } + + private static class SingletonHolder + { + protected static final AutoPotionTaskManager INSTANCE = new AutoPotionTaskManager(); + } +}