Addition of item related task managers.

This commit is contained in:
MobiusDevelopment
2020-11-04 22:24:20 +00:00
parent d229e3a550
commit 946bcea559
116 changed files with 7250 additions and 2940 deletions

View File

@@ -25,7 +25,6 @@ import java.util.logging.LogRecord;
import java.util.logging.Logger;
import org.l2jmobius.Config;
import org.l2jmobius.commons.concurrent.ThreadPool;
import org.l2jmobius.commons.database.DatabaseFactory;
import org.l2jmobius.gameserver.ai.CtrlIntention;
import org.l2jmobius.gameserver.data.ItemTable;
@@ -48,6 +47,7 @@ import org.l2jmobius.gameserver.network.serverpackets.ActionFailed;
import org.l2jmobius.gameserver.network.serverpackets.InventoryUpdate;
import org.l2jmobius.gameserver.network.serverpackets.StatusUpdate;
import org.l2jmobius.gameserver.network.serverpackets.SystemMessage;
import org.l2jmobius.gameserver.taskmanager.ItemManaTaskManager;
import org.l2jmobius.gameserver.util.IllegalPlayerAction;
import org.l2jmobius.gameserver.util.Util;
@@ -92,7 +92,6 @@ public class ItemInstance extends WorldObject
private boolean _wear;
private int _mana = -1;
private boolean _consumingMana = false;
private static final int MANA_CONSUMPTION_RATE = 60000;
private int _type1;
private int _type2;
private long _dropTime;
@@ -653,39 +652,6 @@ public class ItemInstance extends WorldObject
return 0;
}
/**
* Used to decrease mana (mana means life time for shadow items).
*/
public class ScheduleConsumeManaTask implements Runnable
{
private final ItemInstance _shadowItem;
/**
* Instantiates a new schedule consume mana task.
* @param item the item
*/
public ScheduleConsumeManaTask(ItemInstance item)
{
_shadowItem = item;
}
@Override
public void run()
{
try
{
// decrease mana
if (_shadowItem != null)
{
_shadowItem.decreaseMana(true);
}
}
catch (Throwable t)
{
}
}
}
/**
* Returns true if this item is a shadow item Shadow items have a limited life-time.
* @return true, if is shadow item
@@ -834,7 +800,7 @@ public class ItemInstance extends WorldObject
private void scheduleConsumeManaTask()
{
_consumingMana = true;
ThreadPool.schedule(new ScheduleConsumeManaTask(this), MANA_CONSUMPTION_RATE);
ItemManaTaskManager.getInstance().add(this);
}
/**

View File

@@ -0,0 +1,77 @@
/*
* 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.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import org.l2jmobius.commons.concurrent.ThreadPool;
import org.l2jmobius.gameserver.model.items.instance.ItemInstance;
/**
* @author Mobius
*/
public class ItemManaTaskManager
{
private static final Map<ItemInstance, Long> ITEMS = new ConcurrentHashMap<>();
private static final int MANA_CONSUMPTION_RATE = 60000;
private static boolean _working = false;
public ItemManaTaskManager()
{
ThreadPool.scheduleAtFixedRate(() ->
{
if (_working)
{
return;
}
_working = true;
final long currentTime = System.currentTimeMillis();
for (Entry<ItemInstance, Long> entry : ITEMS.entrySet())
{
if (currentTime > entry.getValue().longValue())
{
final ItemInstance item = entry.getKey();
ITEMS.remove(item);
item.decreaseMana(true);
}
}
_working = false;
}, 1000, 1000);
}
public void add(ItemInstance item)
{
if (!ITEMS.containsKey(item))
{
ITEMS.put(item, System.currentTimeMillis() + MANA_CONSUMPTION_RATE);
}
}
public static ItemManaTaskManager getInstance()
{
return SingletonHolder.INSTANCE;
}
private static class SingletonHolder
{
protected static final ItemManaTaskManager INSTANCE = new ItemManaTaskManager();
}
}