Add iterable payItems methods

Shame they could never be fully generic, but oh well
This commit is contained in:
AnimeGitB 2022-08-17 23:52:03 +09:30
parent efa69c007d
commit 0cb75aeb5f
7 changed files with 60 additions and 33 deletions

View File

@ -81,7 +81,7 @@ public class CombineManger extends BaseGameSystem {
List<ItemParamData> material = new ArrayList<>(combineData.getMaterialItems()); List<ItemParamData> material = new ArrayList<>(combineData.getMaterialItems());
material.add(new ItemParamData(202, combineData.getScoinCost())); 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 // abort if not enough material
if (!success) { if (!success) {

View File

@ -293,6 +293,29 @@ public class Inventory extends BasePlayerManager implements Iterable<GameItem> {
} }
} }
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) { private int getVirtualItemCount(int itemId) {
switch (itemId) { switch (itemId) {
case 201: // Primogem case 201: // Primogem
@ -313,47 +336,33 @@ public class Inventory extends BasePlayerManager implements Iterable<GameItem> {
} }
} }
public boolean payItem(int id, int count) { public synchronized boolean payItem(int id, int count) {
return payItem(new ItemParamData(id, count)); if (this.getVirtualItemCount(id) < count)
return false;
this.payVirtualItem(id, count);
return true;
} }
public boolean payItem(ItemParamData costItem) { public boolean payItem(ItemParamData costItem) {
return payItems(new ItemParamData[] {costItem}, 1, null); return this.payItem(costItem.getId(), costItem.getCount());
} }
public boolean payItems(ItemParamData[] costItems) { public boolean payItems(ItemParamData[] costItems) {
return payItems(costItems, 1, null); return this.payItems(costItems, 1, null);
} }
public boolean payItems(ItemParamData[] costItems, int quantity) { 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) { 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() * quantity)) { if (this.getVirtualItemCount(cost.getId()) < (cost.getCount() * quantity))
return false; return false;
}
}
// All costs are satisfied, now remove them all // All costs are satisfied, now remove them all
for (ItemParamData cost : costItems) { for (ItemParamData cost : costItems) {
switch (cost.getId()) { this.payVirtualItem(cost.getId(), cost.getCount() * quantity);
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);
}
} }
if (reason != null) { // Do we need these? if (reason != null) { // Do we need these?
@ -363,6 +372,24 @@ public class Inventory extends BasePlayerManager implements Iterable<GameItem> {
return true; 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) { public void removeItems(List<GameItem> items) {
// TODO Bulk delete // TODO Bulk delete
for (GameItem item : items) { for (GameItem item : items) {

View File

@ -97,7 +97,7 @@ public class CookingManager extends BasePlayerManager {
int proficiency = this.player.getUnlockedRecipies().getOrDefault(recipeId, 0); int proficiency = this.player.getUnlockedRecipies().getOrDefault(recipeId, 0);
// Try consuming materials. // 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) { if (!success) {
this.player.sendPacket(new PacketPlayerCookRsp(Retcode.RET_FAIL)); this.player.sendPacket(new PacketPlayerCookRsp(Retcode.RET_FAIL));
} }

View File

@ -71,7 +71,7 @@ public class FurnitureManager extends BasePlayerManager {
} }
// pay items first // 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)); player.getSession().send(new PacketFurnitureMakeStartRsp(Retcode.RET_HOME_FURNITURE_COUNT_NOT_ENOUGH_VALUE, null));
return; return;
} }

View File

@ -157,7 +157,7 @@ public class ForgingManager extends BasePlayerManager {
List<ItemParamData> material = new ArrayList<>(forgeData.getMaterialItems()); List<ItemParamData> material = new ArrayList<>(forgeData.getMaterialItems());
material.add(new ItemParamData(202, forgeData.getScoinCost())); 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) { if (!success) {
this.player.sendPacket(new PacketForgeStartRsp(Retcode.RET_FORGE_POINT_NOT_ENOUGH)); //ToDo: Probably the wrong return code. this.player.sendPacket(new PacketForgeStartRsp(Retcode.RET_FORGE_POINT_NOT_ENOUGH)); //ToDo: Probably the wrong return code.

View File

@ -122,7 +122,7 @@ public class InventorySystem extends BaseGameSystem {
// Confirm payment of materials and mora (assume food relics are payable afterwards) // Confirm payment of materials and mora (assume food relics are payable afterwards)
payList.add(new ItemParamData(202, moraCost)); payList.add(new ItemParamData(202, moraCost));
if (!player.getInventory().payItems(payList.toArray(new ItemParamData[0]))) { if (!player.getInventory().payItems(payList)) {
return; return;
} }
@ -297,7 +297,7 @@ public class InventorySystem extends BaseGameSystem {
// Confirm payment of materials and mora (assume food weapons are payable afterwards) // Confirm payment of materials and mora (assume food weapons are payable afterwards)
payList.add(new ItemParamData(202, moraCost)); payList.add(new ItemParamData(202, moraCost));
if (!player.getInventory().payItems(payList.toArray(new ItemParamData[0]))) { if (!player.getInventory().payItems(payList)) {
return; return;
} }
player.getInventory().removeItems(foodWeapons); player.getInventory().removeItems(foodWeapons);
@ -692,7 +692,7 @@ public class InventorySystem extends BaseGameSystem {
if (proudSkill.getCoinCost() > 0) { if (proudSkill.getCoinCost() > 0) {
costs.add(new ItemParamData(202, proudSkill.getCoinCost())); costs.add(new ItemParamData(202, proudSkill.getCoinCost()));
} }
if (!player.getInventory().payItems(costs.toArray(new ItemParamData[0]))) { if (!player.getInventory().payItems(costs)) {
return; return;
} }

View File

@ -61,7 +61,7 @@ public class HandlerBuyGoodsReq extends PacketHandler {
costs.add(new ItemParamData(202, sg.getScoin())); costs.add(new ItemParamData(202, sg.getScoin()));
costs.add(new ItemParamData(201, sg.getHcoin())); costs.add(new ItemParamData(201, sg.getHcoin()));
costs.add(new ItemParamData(203, sg.getMcoin())); 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; return;
} }