Removal of enhanced inventory methods.

This commit is contained in:
MobiusDevelopment
2021-04-20 22:20:41 +00:00
parent 210717c77f
commit 1b68632161
425 changed files with 4564 additions and 3177 deletions

View File

@@ -389,7 +389,7 @@ public class CastleManorManager implements IXmlReader, IStorable
final ItemContainer cwh = owner.getWarehouse();
for (CropProcure crop : _procureNext.get(castleId))
{
if ((crop.getStartAmount() > 0) && (cwh.getItemsByItemId(getSeedByCrop(crop.getId()).getMatureId()) == null))
if ((crop.getStartAmount() > 0) && (cwh.getAllItemsByItemId(getSeedByCrop(crop.getId()).getMatureId()) == null))
{
slots++;
}

View File

@@ -1856,7 +1856,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder, IDe
// reduce talisman mana on skill use
if ((skill.getReferenceItemId() > 0) && (ItemTable.getInstance().getTemplate(skill.getReferenceItemId()).getBodyPart() == Item.SLOT_DECO))
{
for (ItemInstance item : getInventory().getItemsByItemId(skill.getReferenceItemId()))
for (ItemInstance item : getInventory().getAllItemsByItemId(skill.getReferenceItemId()))
{
if (item.isEquipped())
{

View File

@@ -13802,7 +13802,7 @@ public class PlayerInstance extends Playable
*/
public boolean isInventoryUnder90(boolean includeQuestInv)
{
return (_inventory.getSize(item -> !item.isQuestItem() || includeQuestInv) <= (getInventoryLimit() * 0.9));
return (includeQuestInv ? _inventory.getSize() : _inventory.getNonQuestSize()) <= (getInventoryLimit() * 0.9);
}
/**
@@ -13812,7 +13812,7 @@ public class PlayerInstance extends Playable
*/
public boolean isInventoryUnder80(boolean includeQuestInv)
{
return (_inventory.getSize(item -> !item.isQuestItem() || includeQuestInv) <= (getInventoryLimit() * 0.8));
return (includeQuestInv ? _inventory.getSize() : _inventory.getNonQuestSize()) <= (getInventoryLimit() * 0.8);
}
public boolean havePetInvItems()

View File

@@ -42,7 +42,7 @@ public class ConditionPlayerInvSize extends Condition
{
if (effector.getActingPlayer() != null)
{
return effector.getActingPlayer().getInventory().getSize(i -> !i.isQuestItem()) <= (effector.getActingPlayer().getInventoryLimit() - _size);
return effector.getActingPlayer().getInventory().getNonQuestSize() <= (effector.getActingPlayer().getInventoryLimit() - _size);
}
return true;
}

View File

@@ -44,7 +44,7 @@ public class ConditionTargetInvSize extends Condition
if ((effected != null) && effected.isPlayer())
{
final PlayerInstance target = effected.getActingPlayer();
return target.getInventory().getSize(i -> !i.isQuestItem()) <= (target.getInventoryLimit() - _size);
return target.getInventory().getNonQuestSize() <= (target.getInventoryLimit() - _size);
}
return false;
}

View File

@@ -2537,7 +2537,7 @@ public abstract class AbstractScript extends ManagedScript
*/
public static boolean takeItems(PlayerInstance player, int itemId, long amount)
{
final List<ItemInstance> items = player.getInventory().getItemsByItemId(itemId);
final Collection<ItemInstance> items = player.getInventory().getAllItemsByItemId(itemId);
if (amount < 0)
{
items.forEach(i -> takeItem(player, i, i.getCount()));

View File

@@ -1736,7 +1736,7 @@ public abstract class Inventory extends ItemContainer
}
ItemInstance arrow = null;
for (ItemInstance item : getItems())
for (ItemInstance item : _items)
{
if (item.isEtcItem() && (item.getItem().getCrystalTypePlus() == bow.getCrystalTypePlus()) && (item.getEtcItem().getItemType() == EtcItemType.ARROW))
{
@@ -1757,7 +1757,7 @@ public abstract class Inventory extends ItemContainer
public ItemInstance findBoltForCrossBow(Item crossbow)
{
ItemInstance bolt = null;
for (ItemInstance item : getItems())
for (ItemInstance item : _items)
{
if (item.isEtcItem() && (item.getItem().getCrystalTypePlus() == crossbow.getCrystalTypePlus()) && (item.getEtcItem().getItemType() == EtcItemType.BOLT))
{

View File

@@ -19,11 +19,9 @@ package org.l2jmobius.gameserver.model.itemcontainer;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -44,7 +42,8 @@ public abstract class ItemContainer
{
protected static final Logger LOGGER = Logger.getLogger(ItemContainer.class.getName());
protected final List<ItemInstance> _items = new CopyOnWriteArrayList<>();
protected final List<ItemInstance> _items = new ArrayList<>(1);
private int _questItemSize = 0;
protected ItemContainer()
{
@@ -76,28 +75,19 @@ public abstract class ItemContainer
}
/**
* @param filterValue
* @param filters
* @return the quantity of quest items in the inventory
*/
public int getQuestSize()
{
return _questItemSize;
}
/**
* @return the quantity of items in the inventory
*/
@SafeVarargs
public final int getSize(Predicate<ItemInstance> filterValue, Predicate<ItemInstance>... filters)
public int getNonQuestSize()
{
Predicate<ItemInstance> filter = filterValue;
for (Predicate<ItemInstance> additionalFilter : filters)
{
filter = filter.and(additionalFilter);
}
int count = 0;
for (ItemInstance item : _items)
{
if (filter.test(item))
{
count++;
}
}
return count;
return _items.size() - _questItemSize;
}
/**
@@ -117,7 +107,7 @@ public abstract class ItemContainer
{
for (ItemInstance item : _items)
{
if ((item != null) && (item.getId() == itemId))
if (item.getId() == itemId)
{
return item;
}
@@ -144,17 +134,17 @@ public abstract class ItemContainer
* @param itemId the item Id
* @return the items list from inventory by using its itemId
*/
public List<ItemInstance> getItemsByItemId(int itemId)
public Collection<ItemInstance> getAllItemsByItemId(int itemId)
{
final List<ItemInstance> returnList = new LinkedList<>();
final List<ItemInstance> result = new ArrayList<>();
for (ItemInstance item : _items)
{
if ((item != null) && (item.getId() == itemId))
if (itemId == item.getId())
{
returnList.add(item);
result.add(item);
}
}
return returnList;
return result;
}
/**
@@ -166,7 +156,7 @@ public abstract class ItemContainer
{
for (ItemInstance item : _items)
{
if ((item != null) && (item.getId() == itemId) && !item.equals(itemToIgnore))
if ((item.getId() == itemId) && !item.equals(itemToIgnore))
{
return item;
}
@@ -182,7 +172,7 @@ public abstract class ItemContainer
{
for (ItemInstance item : _items)
{
if ((item != null) && (item.getObjectId() == objectId))
if (objectId == item.getObjectId())
{
return item;
}
@@ -217,21 +207,9 @@ public abstract class ItemContainer
{
if (item.isStackable())
{
// FIXME: Zoey76: if there are more than one stacks of the same item Id
// it will return the count of the last one, if is not possible to
// have more than one stacks of the same item Id,
// it will continue iterating over all items
// possible fixes:
// count += item.getCount();
// or
// count = item.getCount();
// break;
count = item.getCount();
}
else
{
count++;
return item.getCount();
}
count++;
}
}
return count;
@@ -367,7 +345,7 @@ public abstract class ItemContainer
}
// If possible, move entire item object
if ((sourceitem.getCount() == count) && (targetitem == null))
if ((sourceitem.getCount() == count) && (targetitem == null) && !sourceitem.isStackable())
{
removeItem(sourceitem);
target.addItem(process, sourceitem, actor, reference);
@@ -442,6 +420,7 @@ public abstract class ItemContainer
{
item.changeCount(process, -count, actor, reference);
item.setLastChange(ItemInstance.MODIFIED);
refreshWeight();
}
else
{
@@ -458,9 +437,10 @@ public abstract class ItemContainer
ItemTable.getInstance().destroyItem(process, item, actor, reference);
item.updateDatabase();
refreshWeight();
item.stopAllTasks();
}
refreshWeight();
item.stopAllTasks();
}
return item;
}
@@ -503,9 +483,9 @@ public abstract class ItemContainer
*/
public void destroyAllItems(String process, PlayerInstance actor, Object reference)
{
for (ItemInstance item : _items)
synchronized (_items)
{
if (item != null)
for (ItemInstance item : _items)
{
destroyItem(process, item, actor, reference);
}
@@ -517,16 +497,14 @@ public abstract class ItemContainer
*/
public long getAdena()
{
long count = 0;
for (ItemInstance item : _items)
{
if ((item != null) && (item.getId() == Inventory.ADENA_ID))
if (item.getId() == Inventory.ADENA_ID)
{
count = item.getCount();
return count;
return item.getCount();
}
}
return count;
return 0;
}
/**
@@ -535,7 +513,15 @@ public abstract class ItemContainer
*/
protected void addItem(ItemInstance item)
{
_items.add(item);
synchronized (_items)
{
if (item.isQuestItem())
{
_questItemSize++;
}
_items.add(item);
}
}
/**
@@ -545,7 +531,15 @@ public abstract class ItemContainer
*/
protected boolean removeItem(ItemInstance item)
{
return _items.remove(item);
synchronized (_items)
{
if (item.isQuestItem())
{
_questItemSize--;
}
return _items.remove(item);
}
}
/**
@@ -564,12 +558,9 @@ public abstract class ItemContainer
{
for (ItemInstance item : _items)
{
if (item != null)
{
item.updateDatabase(true);
item.stopAllTasks();
World.getInstance().removeObject(item);
}
item.updateDatabase(true);
item.stopAllTasks();
World.getInstance().removeObject(item);
}
}
_items.clear();
@@ -584,10 +575,7 @@ public abstract class ItemContainer
{
for (ItemInstance item : _items)
{
if (item != null)
{
item.updateDatabase(true);
}
item.updateDatabase(true);
}
}
}
@@ -602,12 +590,12 @@ public abstract class ItemContainer
{
ps.setInt(1, getOwnerId());
ps.setString(2, getBaseLocation().name());
try (ResultSet inv = ps.executeQuery())
try (ResultSet rs = ps.executeQuery())
{
ItemInstance item;
while (inv.next())
while (rs.next())
{
item = ItemInstance.restoreFromDb(getOwnerId(), inv);
item = ItemInstance.restoreFromDb(getOwnerId(), rs);
if (item == null)
{
continue;
@@ -615,10 +603,12 @@ public abstract class ItemContainer
World.getInstance().addObject(item);
final PlayerInstance owner = getOwner() != null ? getOwner().getActingPlayer() : null;
// If stackable item is found in inventory just add to current quantity
if (item.isStackable() && (getItemByItemId(item.getId()) != null))
{
addItem("Restore", item, getOwner() == null ? null : getOwner().getActingPlayer(), null);
addItem("Restore", item, owner, null);
}
else
{

View File

@@ -125,7 +125,11 @@ public class PlayerInventory extends Inventory
final List<ItemInstance> list = new ArrayList<>();
for (ItemInstance item : _items)
{
if ((item == null) || (!allowAdena && (item.getId() == ADENA_ID)) || (!allowAncientAdena && (item.getId() == ANCIENT_ADENA_ID)))
if (!allowAdena && (item.getId() == ADENA_ID))
{
continue;
}
if (!allowAncientAdena && (item.getId() == ANCIENT_ADENA_ID))
{
continue;
}
@@ -185,37 +189,23 @@ public class PlayerInventory extends Inventory
return list;
}
/**
* @param itemId
* @return
*/
public Collection<ItemInstance> getAllItemsByItemId(int itemId)
{
return getAllItemsByItemId(itemId, true);
}
/**
* Returns the list of all items in inventory that have a given item id.
* @param itemId : ID of item
* @param includeEquipped : include equipped items
* @return List<ItemInstance> : matching items from inventory
* @return Collection<ItemInstance> : matching items from inventory
*/
public List<ItemInstance> getAllItemsByItemId(int itemId, boolean includeEquipped)
{
final List<ItemInstance> list = new ArrayList<>();
final List<ItemInstance> result = new ArrayList<>();
for (ItemInstance item : _items)
{
if (item == null)
if ((itemId == item.getId()) && (includeEquipped || !item.isEquipped()))
{
continue;
}
if ((item.getId() == itemId) && (includeEquipped || !item.isEquipped()))
{
list.add(item);
result.add(item);
}
}
return list;
return result;
}
/**
@@ -223,7 +213,7 @@ public class PlayerInventory extends Inventory
* @param enchantment
* @return
*/
public Collection<ItemInstance> getAllItemsByItemId(int itemId, int enchantment)
public List<ItemInstance> getAllItemsByItemId(int itemId, int enchantment)
{
return getAllItemsByItemId(itemId, enchantment, true);
}
@@ -233,24 +223,19 @@ public class PlayerInventory extends Inventory
* @param itemId : ID of item
* @param enchantment : enchant level of item
* @param includeEquipped : include equipped items
* @return List<ItemInstance> : matching items from inventory
* @return Collection<ItemInstance> : matching items from inventory
*/
public List<ItemInstance> getAllItemsByItemId(int itemId, int enchantment, boolean includeEquipped)
{
final List<ItemInstance> list = new ArrayList<>();
final List<ItemInstance> result = new ArrayList<>();
for (ItemInstance item : _items)
{
if (item == null)
if ((itemId == item.getId()) && (item.getEnchantLevel() == enchantment) && (includeEquipped || !item.isEquipped()))
{
continue;
}
if ((item.getId() == itemId) && (item.getEnchantLevel() == enchantment) && (includeEquipped || !item.isEquipped()))
{
list.add(item);
result.add(item);
}
}
return list;
return result;
}
/**
@@ -261,10 +246,10 @@ public class PlayerInventory extends Inventory
*/
public Collection<ItemInstance> getAvailableItems(boolean allowAdena, boolean allowNonTradeable, boolean feightable)
{
final List<ItemInstance> list = new ArrayList<>();
final List<ItemInstance> result = new ArrayList<>();
for (ItemInstance item : _items)
{
if ((item == null) || !item.isAvailable(_owner, allowAdena, allowNonTradeable) || !canManipulateWithItemId(item.getId()))
if (!item.isAvailable(_owner, allowAdena, allowNonTradeable) || !canManipulateWithItemId(item.getId()))
{
continue;
}
@@ -272,15 +257,13 @@ public class PlayerInventory extends Inventory
{
if ((item.getItemLocation() == ItemLocation.INVENTORY) && item.isFreightable())
{
list.add(item);
result.add(item);
}
continue;
}
else
{
list.add(item);
}
result.add(item);
}
return list;
return result;
}
/**
@@ -289,15 +272,15 @@ public class PlayerInventory extends Inventory
*/
public Collection<ItemInstance> getAugmentedItems()
{
final List<ItemInstance> list = new ArrayList<>();
final List<ItemInstance> result = new ArrayList<>();
for (ItemInstance item : _items)
{
if ((item != null) && item.isAugmented())
if (item.isAugmented())
{
list.add(item);
result.add(item);
}
}
return list;
return result;
}
/**
@@ -306,15 +289,15 @@ public class PlayerInventory extends Inventory
*/
public Collection<ItemInstance> getElementItems()
{
final List<ItemInstance> list = new ArrayList<>();
final List<ItemInstance> result = new ArrayList<>();
for (ItemInstance item : _items)
{
if ((item != null) && (item.getElementals() != null))
if (item.getElementals() != null)
{
list.add(item);
result.add(item);
}
}
return list;
return result;
}
/**
@@ -324,7 +307,7 @@ public class PlayerInventory extends Inventory
*/
public Collection<TradeItem> getAvailableItems(TradeList tradeList)
{
final List<TradeItem> list = new ArrayList<>();
final List<TradeItem> result = new ArrayList<>();
for (ItemInstance item : _items)
{
if ((item != null) && item.isAvailable(_owner, false, false))
@@ -332,11 +315,11 @@ public class PlayerInventory extends Inventory
final TradeItem adjItem = tradeList.adjustAvailableItem(item);
if (adjItem != null)
{
list.add(adjItem);
result.add(adjItem);
}
}
}
return list;
return result;
}
/**
@@ -346,17 +329,20 @@ public class PlayerInventory extends Inventory
public void adjustAvailableItem(TradeItem item)
{
boolean notAllEquipped = false;
for (ItemInstance adjItem : getItemsByItemId(item.getItem().getId()))
for (ItemInstance adjItem : getAllItemsByItemId(item.getItem().getId()))
{
if (!adjItem.isEquipable())
if (adjItem.isEquipable())
{
if (!adjItem.isEquipped())
{
notAllEquipped |= true;
}
}
else
{
notAllEquipped |= true;
break;
}
if (!adjItem.isEquipped())
{
notAllEquipped |= true;
}
}
if (notAllEquipped)
{
@@ -391,7 +377,7 @@ public class PlayerInventory extends Inventory
}
/**
* Removes adena to PCInventory
* Removes adena to PcInventory
* @param process : String Identifier of process triggering this action
* @param count : int Quantity of adena to be removed
* @param actor : PlayerInstance Player requesting the item add
@@ -486,8 +472,7 @@ public class PlayerInventory extends Inventory
{
_adena = item;
}
if ((item.getId() == ANCIENT_ADENA_ID) && !item.equals(_ancientAdena))
else if ((item.getId() == ANCIENT_ADENA_ID) && !item.equals(_ancientAdena))
{
_ancientAdena = item;
}
@@ -809,7 +794,7 @@ public class PlayerInventory extends Inventory
public boolean validateCapacity(ItemInstance item)
{
int slots = 0;
if (!item.isStackable() || (getInventoryItemCount(item.getId(), -1) <= 0) || !item.getItem().hasExImmediateEffect())
if (!item.isStackable() || ((getInventoryItemCount(item.getId(), -1) <= 0) && !item.getItem().hasExImmediateEffect()))
{
slots++;
}
@@ -840,14 +825,18 @@ public class PlayerInventory extends Inventory
public boolean validateCapacity(long slots, boolean questItem)
{
return ((slots == 0) && !Config.AUTO_LOOT_SLOT_LIMIT) || questItem ? (getSize(item -> item.isQuestItem()) + slots) <= _owner.getQuestInventoryLimit() : (getSize(item -> !item.isQuestItem()) + slots) <= _owner.getInventoryLimit();
return ((slots == 0) && !Config.AUTO_LOOT_SLOT_LIMIT) || questItem ? (getQuestSize() + slots) <= _owner.getQuestInventoryLimit() : (getNonQuestSize() + slots) <= _owner.getInventoryLimit();
}
@Override
public boolean validateWeight(long weight)
{
// Disable weight check for GMs.
return (_owner.isGM() && _owner.getDietMode() && _owner.getAccessLevel().allowTransaction()) || ((_totalWeight + weight) <= _owner.getMaxLoad());
if (_owner.isGM() && _owner.getDietMode() && _owner.getAccessLevel().allowTransaction())
{
return true;
}
return ((_totalWeight + weight) <= _owner.getMaxLoad());
}
/**