Skip to content

Commit

Permalink
Add iterable payItems methods
Browse files Browse the repository at this point in the history
Shame they could never be fully generic, but oh well
  • Loading branch information
Birdulon committed Aug 18, 2022
1 parent efa69c0 commit 0cb75ae
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public CombineResult combineItem(Player player, int cid, int count) {
List<ItemParamData> material = new ArrayList<>(combineData.getMaterialItems());
material.add(new ItemParamData(202, combineData.getScoinCost()));

boolean success = player.getInventory().payItems(material.toArray(new ItemParamData[0]), count, ActionReason.Combine);
boolean success = player.getInventory().payItems(material, count, ActionReason.Combine);

// abort if not enough material
if (!success) {
Expand Down
77 changes: 52 additions & 25 deletions src/main/java/emu/grasscutter/game/inventory/Inventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,29 @@ private void addVirtualItem(int itemId, int count) {
}
}

private GameItem payVirtualItem(int itemId, int count) {
switch (itemId) {
case 201 -> // Primogem
player.setPrimogems(player.getPrimogems() - count);
case 202 -> // Mora
player.setMora(player.getMora() - count);
case 203 -> // Genesis Crystals
player.setCrystals(player.getCrystals() - count);
case 106 -> // Resin
player.getResinManager().useResin(count);
case 107 -> // LegendaryKey
player.useLegendaryKey(count);
case 204 -> // Home Coin
player.setHomeCoin(player.getHomeCoin() - count);
default -> {
var gameItem = getInventoryTab(ItemType.ITEM_MATERIAL).getItemById(itemId);
removeItem(gameItem, count);
return gameItem;
}
}
return null;
}

private int getVirtualItemCount(int itemId) {
switch (itemId) {
case 201: // Primogem
Expand All @@ -313,47 +336,33 @@ private int getVirtualItemCount(int itemId) {
}
}

public boolean payItem(int id, int count) {
return payItem(new ItemParamData(id, count));
public synchronized boolean payItem(int id, int count) {
if (this.getVirtualItemCount(id) < count)
return false;
this.payVirtualItem(id, count);
return true;
}

public boolean payItem(ItemParamData costItem) {
return payItems(new ItemParamData[] {costItem}, 1, null);
return this.payItem(costItem.getId(), costItem.getCount());
}

public boolean payItems(ItemParamData[] costItems) {
return payItems(costItems, 1, null);
return this.payItems(costItems, 1, null);
}

public boolean payItems(ItemParamData[] costItems, int quantity) {
return payItems(costItems, quantity, null);
return this.payItems(costItems, quantity, null);
}

public synchronized boolean payItems(ItemParamData[] costItems, int quantity, ActionReason reason) {
// Make sure player has requisite items
for (ItemParamData cost : costItems) {
if (getVirtualItemCount(cost.getId()) < (cost.getCount() * quantity)) {
for (ItemParamData cost : costItems)
if (this.getVirtualItemCount(cost.getId()) < (cost.getCount() * quantity))
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() * quantity));
case 202 -> // Mora
player.setMora(player.getMora() - (cost.getCount() * quantity));
case 203 -> // Genesis Crystals
player.setCrystals(player.getCrystals() - (cost.getCount() * quantity));
case 106 -> // Resin
player.getResinManager().useResin(cost.getCount() * quantity);
case 107 -> // LegendaryKey
player.useLegendaryKey(cost.getCount() * quantity);
case 204 -> // Home Coin
player.setHomeCoin(player.getHomeCoin() - (cost.getCount() * quantity));
default ->
removeItem(getInventoryTab(ItemType.ITEM_MATERIAL).getItemById(cost.getId()), cost.getCount() * quantity);
}
this.payVirtualItem(cost.getId(), cost.getCount() * quantity);
}

if (reason != null) { // Do we need these?
Expand All @@ -363,6 +372,24 @@ public synchronized boolean payItems(ItemParamData[] costItems, int quantity, Ac
return true;
}

public boolean payItems(Iterable<ItemParamData> costItems) {
return this.payItems(costItems, 1, null);
}

public boolean payItems(Iterable<ItemParamData> costItems, int quantity) {
return this.payItems(costItems, quantity, null);
}

public synchronized boolean payItems(Iterable<ItemParamData> costItems, int quantity, ActionReason reason) {
// Make sure player has requisite items
for (ItemParamData cost : costItems)
if (getVirtualItemCount(cost.getId()) < (cost.getCount() * quantity))
return false;
// All costs are satisfied, now remove them all
costItems.forEach(cost -> this.payVirtualItem(cost.getId(), cost.getCount() * quantity));
return true;
}

public void removeItems(List<GameItem> items) {
// TODO Bulk delete
for (GameItem item : items) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void handlePlayerCookReq(PlayerCookReq req) {
int proficiency = this.player.getUnlockedRecipies().getOrDefault(recipeId, 0);

// Try consuming materials.
boolean success = player.getInventory().payItems(recipeData.getInputVec().toArray(new ItemParamData[0]), count, ActionReason.Cook);
boolean success = player.getInventory().payItems(recipeData.getInputVec(), count, ActionReason.Cook);
if (!success) {
this.player.sendPacket(new PacketPlayerCookRsp(Retcode.RET_FAIL));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void startMake(int makeId, int avatarId) {
}

// pay items first
if (!player.getInventory().payItems(makeData.getMaterialItems().toArray(new ItemParamData[0]))) {
if (!player.getInventory().payItems(makeData.getMaterialItems())) {
player.getSession().send(new PacketFurnitureMakeStartRsp(Retcode.RET_HOME_FURNITURE_COUNT_NOT_ENOUGH_VALUE, null));
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public synchronized void handleForgeStartReq(ForgeStartReq req) {
List<ItemParamData> material = new ArrayList<>(forgeData.getMaterialItems());
material.add(new ItemParamData(202, forgeData.getScoinCost()));

boolean success = player.getInventory().payItems(material.toArray(new ItemParamData[0]), req.getForgeCount(), ActionReason.ForgeCost);
boolean success = player.getInventory().payItems(material, req.getForgeCount(), ActionReason.ForgeCost);

if (!success) {
this.player.sendPacket(new PacketForgeStartRsp(Retcode.RET_FORGE_POINT_NOT_ENOUGH)); //ToDo: Probably the wrong return code.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public void upgradeRelic(Player player, long targetGuid, List<Long> foodRelicLis

// Confirm payment of materials and mora (assume food relics are payable afterwards)
payList.add(new ItemParamData(202, moraCost));
if (!player.getInventory().payItems(payList.toArray(new ItemParamData[0]))) {
if (!player.getInventory().payItems(payList)) {
return;
}

Expand Down Expand Up @@ -297,7 +297,7 @@ public void upgradeWeapon(Player player, long targetGuid, List<Long> foodWeaponG

// Confirm payment of materials and mora (assume food weapons are payable afterwards)
payList.add(new ItemParamData(202, moraCost));
if (!player.getInventory().payItems(payList.toArray(new ItemParamData[0]))) {
if (!player.getInventory().payItems(payList)) {
return;
}
player.getInventory().removeItems(foodWeapons);
Expand Down Expand Up @@ -692,7 +692,7 @@ public void upgradeAvatarSkill(Player player, long guid, int skillId) {
if (proudSkill.getCoinCost() > 0) {
costs.add(new ItemParamData(202, proudSkill.getCoinCost()));
}
if (!player.getInventory().payItems(costs.toArray(new ItemParamData[0]))) {
if (!player.getInventory().payItems(costs)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void handle(GameSession session, byte[] header, byte[] payload) throws Ex
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.getBuyCount())) {
if (!session.getPlayer().getInventory().payItems(costs, buyGoodsReq.getBuyCount())) {
return;
}

Expand Down

0 comments on commit 0cb75ae

Please sign in to comment.