diff --git a/src/main/java/emu/grasscutter/game/inventory/Inventory.java b/src/main/java/emu/grasscutter/game/inventory/Inventory.java index 52a60abfd..328134f70 100644 --- a/src/main/java/emu/grasscutter/game/inventory/Inventory.java +++ b/src/main/java/emu/grasscutter/game/inventory/Inventory.java @@ -1,7 +1,5 @@ package emu.grasscutter.game.inventory; -import static emu.grasscutter.config.Configuration.INVENTORY_LIMITS; - import emu.grasscutter.Grasscutter; import emu.grasscutter.data.GameData; import emu.grasscutter.data.common.ItemParamData; @@ -22,11 +20,14 @@ import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import it.unimi.dsi.fastutil.longs.Long2ObjectMap; import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; + import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; +import static emu.grasscutter.config.Configuration.INVENTORY_LIMITS; + public class Inventory extends BasePlayerManager implements Iterable { private final Long2ObjectMap store; private final Int2ObjectMap inventoryTypes; @@ -437,7 +438,7 @@ public class Inventory extends BasePlayerManager implements Iterable { } public synchronized boolean removeItem(long guid, int count) { - GameItem item = this.getItemByGuid(guid); + var item = this.getItemByGuid(guid); if (item == null) { return false; @@ -446,6 +447,24 @@ public class Inventory extends BasePlayerManager implements Iterable { return removeItem(item, count); } + /** + * Removes an item by its item ID. + * + * @param itemId The ID of the item to remove. + * @param count The amount of items to remove. + * @return True if the item was removed, false otherwise. + */ + public synchronized boolean removeItemById(int itemId, int count) { + var item = this.getItems().values().stream() + .filter(i -> i.getItemId() == itemId) + .findFirst(); + + // Check if the item is in the player's inventory. + return item.filter( + gameItem -> this.removeItem(gameItem, count) + ).isPresent(); + } + public synchronized boolean removeItem(GameItem item) { return removeItem(item, item.getCount()); } diff --git a/src/main/java/emu/grasscutter/game/quest/exec/ExecDelPackItem.java b/src/main/java/emu/grasscutter/game/quest/exec/ExecDelPackItem.java index 19960fd2e..a4604eddb 100644 --- a/src/main/java/emu/grasscutter/game/quest/exec/ExecDelPackItem.java +++ b/src/main/java/emu/grasscutter/game/quest/exec/ExecDelPackItem.java @@ -12,6 +12,6 @@ public class ExecDelPackItem extends QuestExecHandler { public boolean execute(GameQuest quest, QuestData.QuestExecParam condition, String... paramStr) { int itemId = Integer.parseInt(paramStr[0]); int amount = Integer.parseInt(paramStr[1]); - return quest.getOwner().getInventory().removeItem(itemId, amount); + return quest.getOwner().getInventory().removeItemById(itemId, amount); } } diff --git a/src/main/java/emu/grasscutter/game/quest/exec/ExecDelPackItemBatch.java b/src/main/java/emu/grasscutter/game/quest/exec/ExecDelPackItemBatch.java index 03e39725b..e9dc737eb 100644 --- a/src/main/java/emu/grasscutter/game/quest/exec/ExecDelPackItemBatch.java +++ b/src/main/java/emu/grasscutter/game/quest/exec/ExecDelPackItemBatch.java @@ -17,7 +17,7 @@ public class ExecDelPackItemBatch extends QuestExecHandler { var itemFields = itemString.split(":"); var itemId = Integer.parseInt(itemFields[0]); var amount = Integer.parseInt(itemFields[1]); - if (!quest.getOwner().getInventory().removeItem(itemId, amount)) { + if (!quest.getOwner().getInventory().removeItemById(itemId, amount)) { success = false; } } diff --git a/src/main/java/emu/grasscutter/game/systems/InventorySystem.java b/src/main/java/emu/grasscutter/game/systems/InventorySystem.java index 001e4003e..4d3c5db1c 100644 --- a/src/main/java/emu/grasscutter/game/systems/InventorySystem.java +++ b/src/main/java/emu/grasscutter/game/systems/InventorySystem.java @@ -26,9 +26,10 @@ import emu.grasscutter.server.game.GameServer; import emu.grasscutter.server.packet.send.*; import emu.grasscutter.utils.Utils; import it.unimi.dsi.fastutil.ints.*; +import lombok.val; + import java.util.*; import java.util.stream.Stream; -import lombok.val; public class InventorySystem extends BaseGameSystem { private static final Int2IntMap weaponRefundMaterials = new Int2IntArrayMap(); @@ -906,7 +907,7 @@ public class InventorySystem extends BaseGameSystem { // Use var actions = itemData.getItemUseActions(); - Grasscutter.getLogger().debug("Using - actions - {}", actions); + Grasscutter.getLogger().trace("Using - actions - {}", actions); if (actions == null) return true; // Maybe returning false would be more appropriate? return actions.stream() .map(use -> use.useItem(params))