From 010455548fd8b31b854cb61cfca6a69c50bd0d24 Mon Sep 17 00:00:00 2001 From: AnimeGitB Date: Wed, 11 May 2022 20:33:25 +0930 Subject: [PATCH] Add Inventory.payItems() method --- .../grasscutter/game/inventory/Inventory.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/main/java/emu/grasscutter/game/inventory/Inventory.java b/src/main/java/emu/grasscutter/game/inventory/Inventory.java index 4a217ba54..2c0478dda 100644 --- a/src/main/java/emu/grasscutter/game/inventory/Inventory.java +++ b/src/main/java/emu/grasscutter/game/inventory/Inventory.java @@ -7,6 +7,7 @@ import java.util.List; import emu.grasscutter.GameConstants; import emu.grasscutter.data.GameData; +import emu.grasscutter.data.common.ItemParamData; import emu.grasscutter.data.def.AvatarCostumeData; import emu.grasscutter.data.def.AvatarData; import emu.grasscutter.data.def.AvatarFlycloakData; @@ -256,6 +257,52 @@ public class Inventory implements Iterable { getPlayer().setCrystals(player.getCrystals() + count); } } + + private int getVirtualItemCount(int itemId) { + switch (itemId) { + case 201: // Primogem + return player.getPrimogems(); + case 202: // Mora + return player.getMora(); + case 203: // Genesis Crystals + return player.getCrystals(); + default: + GameItem item = getInventoryTab(ItemType.ITEM_MATERIAL).getItemById(itemId); // What if we ever want to operate on weapons/relics/furniture? :S + return (item == null) ? 0 : item.getCount(); + } + } + + public boolean payItems(Collection items) { + return payItems(items, null); + } + + public synchronized boolean payItems(Collection costItems, ActionReason reason) { + // Make sure player has requisite items + for (ItemParamData cost : costItems) { + if (getVirtualItemCount(cost.getId()) < cost.getCount()) { + return false; + } + } + // All costs are satisfied, now remove them all + for (ItemParamData cost : costItems) { + switch (cost.getId()) { + case 201 -> // Primogem + player.setPrimogems(player.getPrimogems() - cost.getCount()); + case 202 -> // Mora + player.setMora(player.getMora() - cost.getCount()); + case 203 -> // Genesis Crystals + player.setCrystals(player.getCrystals() - cost.getCount()); + default -> + removeItem(getInventoryTab(ItemType.ITEM_MATERIAL).getItemById(cost.getId()), cost.getCount()); + } + } + + if (reason != null) { // Do we need these? + // getPlayer().sendPacket(new PacketItemAddHintNotify(changedItems, reason)); + } + // getPlayer().sendPacket(new PacketStoreItemChangeNotify(changedItems)); + return true; + } public void removeItems(List items) { // TODO Bulk delete