Add Inventory.payItems() method

This commit is contained in:
AnimeGitB 2022-05-11 20:33:25 +09:30 committed by Melledy
parent 53797dfd3b
commit 010455548f

View File

@ -7,6 +7,7 @@ import java.util.List;
import emu.grasscutter.GameConstants; import emu.grasscutter.GameConstants;
import emu.grasscutter.data.GameData; import emu.grasscutter.data.GameData;
import emu.grasscutter.data.common.ItemParamData;
import emu.grasscutter.data.def.AvatarCostumeData; import emu.grasscutter.data.def.AvatarCostumeData;
import emu.grasscutter.data.def.AvatarData; import emu.grasscutter.data.def.AvatarData;
import emu.grasscutter.data.def.AvatarFlycloakData; import emu.grasscutter.data.def.AvatarFlycloakData;
@ -257,6 +258,52 @@ public class Inventory implements Iterable<GameItem> {
} }
} }
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<ItemParamData> items) {
return payItems(items, null);
}
public synchronized boolean payItems(Collection<ItemParamData> 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<GameItem> items) { public void removeItems(List<GameItem> items) {
// TODO Bulk delete // TODO Bulk delete
for (GameItem item : items) { for (GameItem item : items) {