Use removeItemById for deleting items via quests

This commit is contained in:
KingRainbow44 2023-05-18 02:55:28 -04:00
parent 427cb039cf
commit 13f055035f
No known key found for this signature in database
GPG Key ID: FC2CB64B00D257BE
4 changed files with 27 additions and 7 deletions

View File

@ -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<GameItem> {
private final Long2ObjectMap<GameItem> store;
private final Int2ObjectMap<InventoryTab> inventoryTypes;
@ -437,7 +438,7 @@ public class Inventory extends BasePlayerManager implements Iterable<GameItem> {
}
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<GameItem> {
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());
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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))