Commit b4d457d7 authored by AnimeGitB's avatar AnimeGitB Committed by Melledy
Browse files

Add some usage of Inventory.payItems()

And add quantity to it
parent 595234cc
...@@ -13,6 +13,7 @@ import com.google.gson.reflect.TypeToken; ...@@ -13,6 +13,7 @@ import com.google.gson.reflect.TypeToken;
import com.sun.nio.file.SensitivityWatchEventModifier; import com.sun.nio.file.SensitivityWatchEventModifier;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
import emu.grasscutter.data.GameData; import emu.grasscutter.data.GameData;
import emu.grasscutter.data.common.ItemParamData;
import emu.grasscutter.data.def.ItemData; import emu.grasscutter.data.def.ItemData;
import emu.grasscutter.database.DatabaseHelper; import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.game.avatar.Avatar; import emu.grasscutter.game.avatar.Avatar;
...@@ -127,15 +128,10 @@ public class GachaManager { ...@@ -127,15 +128,10 @@ public class GachaManager {
} }
// Spend currency // Spend currency
if (banner.getCostItem() > 0) { if (banner.getCostItem() > 0 && !player.getInventory().payItems(new ItemParamData[] {new ItemParamData(banner.getCostItem(), times)})) {
GameItem costItem = player.getInventory().getInventoryTab(ItemType.ITEM_MATERIAL).getItemById(banner.getCostItem());
if (costItem == null || costItem.getCount() < times) {
return; return;
} }
player.getInventory().removeItem(costItem, times);
}
// Roll // Roll
PlayerGachaBannerInfo gachaInfo = player.getGachaInfo().getBannerInfo(banner); PlayerGachaBannerInfo gachaInfo = player.getGachaInfo().getBannerInfo(banner);
IntList wonItems = new IntArrayList(times); IntList wonItems = new IntArrayList(times);
......
...@@ -272,14 +272,18 @@ public class Inventory implements Iterable<GameItem> { ...@@ -272,14 +272,18 @@ public class Inventory implements Iterable<GameItem> {
} }
} }
public boolean payItems(Collection<ItemParamData> items) { public boolean payItems(ItemParamData[] costItems) {
return payItems(items, null); return payItems(costItems, 1, null);
} }
public synchronized boolean payItems(Collection<ItemParamData> costItems, ActionReason reason) { public boolean payItems(ItemParamData[] costItems, int quantity) {
return payItems(costItems, quantity, null);
}
public synchronized boolean payItems(ItemParamData[] costItems, int quantity, ActionReason reason) {
// Make sure player has requisite items // Make sure player has requisite items
for (ItemParamData cost : costItems) { for (ItemParamData cost : costItems) {
if (getVirtualItemCount(cost.getId()) < cost.getCount()) { if (getVirtualItemCount(cost.getId()) < (cost.getCount() * quantity)) {
return false; return false;
} }
} }
...@@ -287,13 +291,13 @@ public class Inventory implements Iterable<GameItem> { ...@@ -287,13 +291,13 @@ public class Inventory implements Iterable<GameItem> {
for (ItemParamData cost : costItems) { for (ItemParamData cost : costItems) {
switch (cost.getId()) { switch (cost.getId()) {
case 201 -> // Primogem case 201 -> // Primogem
player.setPrimogems(player.getPrimogems() - cost.getCount()); player.setPrimogems(player.getPrimogems() - (cost.getCount() * quantity));
case 202 -> // Mora case 202 -> // Mora
player.setMora(player.getMora() - cost.getCount()); player.setMora(player.getMora() - (cost.getCount() * quantity));
case 203 -> // Genesis Crystals case 203 -> // Genesis Crystals
player.setCrystals(player.getCrystals() - cost.getCount()); player.setCrystals(player.getCrystals() - (cost.getCount() * quantity));
default -> default ->
removeItem(getInventoryTab(ItemType.ITEM_MATERIAL).getItemById(cost.getId()), cost.getCount()); removeItem(getInventoryTab(ItemType.ITEM_MATERIAL).getItemById(cost.getId()), cost.getCount() * quantity);
} }
} }
......
package emu.grasscutter.game.managers; package emu.grasscutter.game.managers;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -496,27 +497,16 @@ public class InventoryManager { ...@@ -496,27 +497,16 @@ public class InventoryManager {
return; return;
} }
// Make sure player has promote items // Pay materials and mora if possible
for (ItemParamData cost : nextPromoteData.getCostItems()) { ItemParamData[] costs = nextPromoteData.getCostItems(); // Can this be null?
GameItem feedItem = player.getInventory().getInventoryTab(ItemType.ITEM_MATERIAL).getItemById(cost.getId()); if (nextPromoteData.getCoinCost() > 0) {
if (feedItem == null || feedItem.getCount() < cost.getCount()) { costs = Arrays.copyOf(costs, costs.length + 1);
return; costs[costs.length-1] = new ItemParamData(202, nextPromoteData.getCoinCost());
}
} }
if (!player.getInventory().payItems(costs)) {
// Mora check
if (player.getMora() >= nextPromoteData.getCoinCost()) {
player.setMora(player.getMora() - nextPromoteData.getCoinCost());
} else {
return; return;
} }
// Consume promote filler items
for (ItemParamData cost : nextPromoteData.getCostItems()) {
GameItem feedItem = player.getInventory().getInventoryTab(ItemType.ITEM_MATERIAL).getItemById(cost.getId());
player.getInventory().removeItem(feedItem, cost.getCount());
}
int oldPromoteLevel = weapon.getPromoteLevel(); int oldPromoteLevel = weapon.getPromoteLevel();
weapon.setPromoteLevel(nextPromoteLevel); weapon.setPromoteLevel(nextPromoteLevel);
weapon.save(); weapon.save();
...@@ -552,27 +542,16 @@ public class InventoryManager { ...@@ -552,27 +542,16 @@ public class InventoryManager {
return; return;
} }
// Make sure player has cost items // Pay materials and mora if possible
for (ItemParamData cost : nextPromoteData.getCostItems()) { ItemParamData[] costs = nextPromoteData.getCostItems(); // Can this be null?
GameItem feedItem = player.getInventory().getInventoryTab(ItemType.ITEM_MATERIAL).getItemById(cost.getId()); if (nextPromoteData.getCoinCost() > 0) {
if (feedItem == null || feedItem.getCount() < cost.getCount()) { costs = Arrays.copyOf(costs, costs.length + 1);
return; costs[costs.length-1] = new ItemParamData(202, nextPromoteData.getCoinCost());
}
} }
if (!player.getInventory().payItems(costs)) {
// Mora check
if (player.getMora() >= nextPromoteData.getCoinCost()) {
player.setMora(player.getMora() - nextPromoteData.getCoinCost());
} else {
return; return;
} }
// Consume promote filler items
for (ItemParamData cost : nextPromoteData.getCostItems()) {
GameItem feedItem = player.getInventory().getInventoryTab(ItemType.ITEM_MATERIAL).getItemById(cost.getId());
player.getInventory().removeItem(feedItem, cost.getCount());
}
// Update promote level // Update promote level
avatar.setPromoteLevel(nextPromoteLevel); avatar.setPromoteLevel(nextPromoteLevel);
...@@ -764,33 +743,15 @@ public class InventoryManager { ...@@ -764,33 +743,15 @@ public class InventoryManager {
return; return;
} }
// Make sure player has cost items // Pay materials and mora if possible
for (ItemParamData cost : proudSkill.getCostItems()) { List<ItemParamData> costs = proudSkill.getCostItems(); // Can this be null?
if (cost.getId() == 0) { if (proudSkill.getCoinCost() > 0) {
continue; costs.add(new ItemParamData(202, proudSkill.getCoinCost()));
}
GameItem feedItem = player.getInventory().getInventoryTab(ItemType.ITEM_MATERIAL).getItemById(cost.getId());
if (feedItem == null || feedItem.getCount() < cost.getCount()) {
return;
}
} }
if (!player.getInventory().payItems(costs.toArray(new ItemParamData[0]))) {
// Mora check
if (player.getMora() >= proudSkill.getCoinCost()) {
player.setMora(player.getMora() - proudSkill.getCoinCost());
} else {
return; return;
} }
// Consume promote filler items
for (ItemParamData cost : proudSkill.getCostItems()) {
if (cost.getId() == 0) {
continue;
}
GameItem feedItem = player.getInventory().getInventoryTab(ItemType.ITEM_MATERIAL).getItemById(cost.getId());
player.getInventory().removeItem(feedItem, cost.getCount());
}
// Upgrade skill // Upgrade skill
avatar.getSkillLevelMap().put(skillId, nextLevel); avatar.getSkillLevelMap().put(skillId, nextLevel);
avatar.save(); avatar.save();
......
...@@ -18,7 +18,7 @@ import emu.grasscutter.server.packet.send.PacketBuyGoodsRsp; ...@@ -18,7 +18,7 @@ import emu.grasscutter.server.packet.send.PacketBuyGoodsRsp;
import emu.grasscutter.server.packet.send.PacketStoreItemChangeNotify; import emu.grasscutter.server.packet.send.PacketStoreItemChangeNotify;
import emu.grasscutter.utils.Utils; import emu.grasscutter.utils.Utils;
import java.util.HashMap; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
...@@ -56,36 +56,13 @@ public class HandlerBuyGoodsReq extends PacketHandler { ...@@ -56,36 +56,13 @@ public class HandlerBuyGoodsReq extends PacketHandler {
return; return;
} }
if (sg.getScoin() > 0 && session.getPlayer().getMora() < buyGoodsReq.getBoughtNum() * sg.getScoin()) { List<ItemParamData> costs = sg.getCostItemList(); // Can this even be null?
costs.add(new ItemParamData(202, sg.getScoin()));
costs.add(new ItemParamData(201, sg.getHcoin()));
costs.add(new ItemParamData(203, sg.getMcoin()));
if (!session.getPlayer().getInventory().payItems(costs.toArray(new ItemParamData[0]), buyGoodsReq.getBoughtNum())) {
return; return;
} }
if (sg.getHcoin() > 0 && session.getPlayer().getPrimogems() < buyGoodsReq.getBoughtNum() * sg.getHcoin()) {
return;
}
if (sg.getMcoin() > 0 && session.getPlayer().getCrystals() < buyGoodsReq.getBoughtNum() * sg.getMcoin()) {
return;
}
HashMap<GameItem, Integer> itemsCache = new HashMap<>();
if (sg.getCostItemList() != null) {
for (ItemParamData p : sg.getCostItemList()) {
Optional<GameItem> invItem = session.getPlayer().getInventory().getItems().values().stream().filter(x -> x.getItemId() == p.getId()).findFirst();
if (invItem.isEmpty() || invItem.get().getCount() < p.getCount())
return;
itemsCache.put(invItem.get(), p.getCount() * buyGoodsReq.getBoughtNum());
}
}
session.getPlayer().setMora(session.getPlayer().getMora() - buyGoodsReq.getBoughtNum() * sg.getScoin());
session.getPlayer().setPrimogems(session.getPlayer().getPrimogems() - buyGoodsReq.getBoughtNum() * sg.getHcoin());
session.getPlayer().setCrystals(session.getPlayer().getCrystals() - buyGoodsReq.getBoughtNum() * sg.getMcoin());
if (!itemsCache.isEmpty()) {
for (GameItem gi : itemsCache.keySet()) {
session.getPlayer().getInventory().removeItem(gi, itemsCache.get(gi));
}
itemsCache.clear();
}
session.getPlayer().addShopLimit(sg.getGoodsId(), buyGoodsReq.getBoughtNum(), ShopManager.getShopNextRefreshTime(sg)); session.getPlayer().addShopLimit(sg.getGoodsId(), buyGoodsReq.getBoughtNum(), ShopManager.getShopNextRefreshTime(sg));
GameItem item = new GameItem(GameData.getItemDataMap().get(sg.getGoodsItem().getId())); GameItem item = new GameItem(GameData.getItemDataMap().get(sg.getGoodsItem().getId()));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment