Addition of AutoPotionTaskManager.
This commit is contained in:
		| @@ -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<Integer, Future<?>> 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() | ||||
| 	{ | ||||
|   | ||||
| @@ -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 <http://www.gnu.org/licenses/>. | ||||
|  */ | ||||
| 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!"); | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| @@ -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 <http://www.gnu.org/licenses/>. | ||||
|  */ | ||||
| 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<PlayerInstance> 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(); | ||||
| 	} | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 MobiusDevelopment
					MobiusDevelopment