From 0c6d308b5edf1b2b0b714f803e44a522aebaf2d8 Mon Sep 17 00:00:00 2001 From: Melledy <121644117+Melledy@users.noreply.github.com> Date: Mon, 23 Oct 2023 10:05:50 -0700 Subject: [PATCH] Rework how item params are verified --- .../data/excel/AvatarPromotionExcel.java | 20 ------ .../data/excel/AvatarSkillTreeExcel.java | 20 ------ .../lunarcore/data/excel/ShopGoodsExcel.java | 7 -- .../lunarcore/game/inventory/Inventory.java | 50 ++++++++++++-- .../game/service/InventoryService.java | 68 ++++--------------- 5 files changed, 57 insertions(+), 108 deletions(-) diff --git a/src/main/java/emu/lunarcore/data/excel/AvatarPromotionExcel.java b/src/main/java/emu/lunarcore/data/excel/AvatarPromotionExcel.java index af80e2f..d41f68e 100644 --- a/src/main/java/emu/lunarcore/data/excel/AvatarPromotionExcel.java +++ b/src/main/java/emu/lunarcore/data/excel/AvatarPromotionExcel.java @@ -1,10 +1,7 @@ package emu.lunarcore.data.excel; -import java.util.ArrayList; -import java.util.Iterator; import java.util.List; -import emu.lunarcore.GameConstants; import emu.lunarcore.data.GameResource; import emu.lunarcore.data.ResourceType; import emu.lunarcore.data.ResourceType.LoadPriority; @@ -21,7 +18,6 @@ public class AvatarPromotionExcel extends GameResource { private int PlayerLevelRequire; private int WorldLevelRequire; private List PromotionCostList; - private transient int PromotionCostCoin; private double AttackBase; private double AttackAdd; @@ -38,20 +34,4 @@ public class AvatarPromotionExcel extends GameResource { public int getId() { return (AvatarID << 8) + Promotion; } - - @Override - public void onLoad() { - if (this.PromotionCostList == null) { - this.PromotionCostList = new ArrayList<>(); - } else { - Iterator it = this.PromotionCostList.iterator(); - while (it.hasNext()) { - ItemParam param = it.next(); - if (param.getId() == GameConstants.MATERIAL_COIN_ID) { - this.PromotionCostCoin = param.getCount(); - it.remove(); - } - } - } - } } diff --git a/src/main/java/emu/lunarcore/data/excel/AvatarSkillTreeExcel.java b/src/main/java/emu/lunarcore/data/excel/AvatarSkillTreeExcel.java index 27335e3..8ffae52 100644 --- a/src/main/java/emu/lunarcore/data/excel/AvatarSkillTreeExcel.java +++ b/src/main/java/emu/lunarcore/data/excel/AvatarSkillTreeExcel.java @@ -1,10 +1,7 @@ package emu.lunarcore.data.excel; -import java.util.ArrayList; -import java.util.Iterator; import java.util.List; -import emu.lunarcore.GameConstants; import emu.lunarcore.data.GameData; import emu.lunarcore.data.GameResource; import emu.lunarcore.data.ResourceType; @@ -12,7 +9,6 @@ import emu.lunarcore.data.ResourceType.LoadPriority; import emu.lunarcore.data.common.ItemParam; import it.unimi.dsi.fastutil.ints.IntArrayList; import lombok.Getter; -import net.bytebuddy.asm.Advice.This; @Getter @ResourceType(name = {"AvatarSkillTreeConfig.json"}, loadPriority = LoadPriority.LOW) @@ -30,8 +26,6 @@ public class AvatarSkillTreeExcel extends GameResource { private IntArrayList PrePoint; private IntArrayList LevelUpSkillID; - private transient int MaterialCostCoin; - @Override public int getId() { return (PointID << 4) + Level; @@ -39,20 +33,6 @@ public class AvatarSkillTreeExcel extends GameResource { @Override public void onLoad() { - // Parse material list - if (this.MaterialList == null) { - this.MaterialList = new ArrayList<>(); - } else { - Iterator it = this.MaterialList.iterator(); - while (it.hasNext()) { - ItemParam param = it.next(); - if (param.getId() == GameConstants.MATERIAL_COIN_ID) { - this.MaterialCostCoin = param.getCount(); - it.remove(); - } - } - } - // Load to excel AvatarExcel excel = GameData.getAvatarExcelMap().get(this.AvatarID); if (excel == null) return; diff --git a/src/main/java/emu/lunarcore/data/excel/ShopGoodsExcel.java b/src/main/java/emu/lunarcore/data/excel/ShopGoodsExcel.java index 98654eb..56bb792 100644 --- a/src/main/java/emu/lunarcore/data/excel/ShopGoodsExcel.java +++ b/src/main/java/emu/lunarcore/data/excel/ShopGoodsExcel.java @@ -3,7 +3,6 @@ package emu.lunarcore.data.excel; import java.util.ArrayList; import java.util.List; -import emu.lunarcore.GameConstants; import emu.lunarcore.data.GameData; import emu.lunarcore.data.GameResource; import emu.lunarcore.data.ResourceType; @@ -27,7 +26,6 @@ public class ShopGoodsExcel extends GameResource { private int[] CurrencyCostList; private transient List costList; - private transient int coinCost; @Override public int getId() { @@ -46,11 +44,6 @@ public class ShopGoodsExcel extends GameResource { this.costList = new ArrayList<>(CurrencyList.length); for (int i = 0; i < CurrencyList.length; i++) { - if (CurrencyList[i] == GameConstants.MATERIAL_COIN_ID) { - this.coinCost = CurrencyCostList[i]; - continue; - } - ItemParam param = new ItemParam(CurrencyList[i], CurrencyCostList[i]); this.costList.add(param); } diff --git a/src/main/java/emu/lunarcore/game/inventory/Inventory.java b/src/main/java/emu/lunarcore/game/inventory/Inventory.java index 78a1fe3..86458ba 100644 --- a/src/main/java/emu/lunarcore/game/inventory/Inventory.java +++ b/src/main/java/emu/lunarcore/game/inventory/Inventory.java @@ -5,6 +5,7 @@ import java.util.Collection; import java.util.List; import java.util.stream.Stream; +import emu.lunarcore.GameConstants; import emu.lunarcore.LunarRail; import emu.lunarcore.data.GameData; import emu.lunarcore.data.common.ItemParam; @@ -253,12 +254,19 @@ public class Inventory extends BasePlayerManager { List results = new ArrayList(items.size()); for (ItemParam param : items) { - GameItem item = this.getItemByParam(param); - if (item == null) continue; - - GameItem result = this.deleteItem(item, param.getCount() * multiplier); - if (result != null) { - results.add(result); + // Check param type + if (param.getId() == GameConstants.MATERIAL_COIN_ID) { + // Remove credits + getPlayer().addSCoin(-param.getCount() * multiplier); + } else { + // Remove param items + GameItem item = this.getItemByParam(param); + if (item == null) continue; + + GameItem result = this.deleteItem(item, param.getCount() * multiplier); + if (result != null) { + results.add(result); + } } } @@ -353,6 +361,36 @@ public class Inventory extends BasePlayerManager { // Returns true on success return item; } + + // Verifying items + + public boolean verifyItems(Collection params) { + return verifyItems(params, 1); + } + + public boolean verifyItems(Collection params, int multiplier) { + for (ItemParam param : params) { + // Check param type + if (param.getId() == GameConstants.MATERIAL_COIN_ID) { + // Check credits + if (!verifyScoin(param.getCount() * multiplier)) { + return false; + } + } else { + // Check param items + GameItem item = this.getItemByParam(param); + if (item == null || item.getCount() < param.getCount() * multiplier) { + return false; + } + } + } + + return true; + } + + public boolean verifyScoin(int cost) { + return this.getPlayer().getScoin() >= cost; + } // Equips diff --git a/src/main/java/emu/lunarcore/game/service/InventoryService.java b/src/main/java/emu/lunarcore/game/service/InventoryService.java index 7aa73f2..bf95bd5 100644 --- a/src/main/java/emu/lunarcore/game/service/InventoryService.java +++ b/src/main/java/emu/lunarcore/game/service/InventoryService.java @@ -103,21 +103,14 @@ public class InventoryService extends BaseGameService { return; } - // Verify items - for (ItemParam param : promotion.getPromotionCostList()) { - GameItem item = player.getInventory().getItemByParam(param); - if (item == null || item.getCount() < param.getCount()) return; - } - - // Verify credits - if (player.getScoin() < promotion.getPromotionCostCoin()) { + // Verify item params + if (!player.getInventory().verifyItems(promotion.getPromotionCostList())) { player.sendPacket(new BasePacket(CmdId.PromoteAvatarScRsp)); return; } // Pay items player.getInventory().removeItemsByParams(promotion.getPromotionCostList()); - player.addSCoin(-promotion.getPromotionCostCoin()); // Promote avatar.setPromotion(avatar.getPromotion() + 1); @@ -143,24 +136,14 @@ public class InventoryService extends BaseGameService { AvatarSkillTreeExcel skillTree = GameData.getAvatarSkillTreeExcel(pointId, nextLevel); if (skillTree == null || skillTree.getAvatarID() != avatar.getExcel().getAvatarID()) return; - // Verify items - for (ItemParam param : skillTree.getMaterialList()) { - GameItem item = player.getInventory().getItemByParam(param); - if (item == null || item.getCount() < param.getCount()) { - player.sendPacket(new PacketUnlockSkilltreeScRsp()); - return; - } - } - - // Verify credits - if (player.getScoin() < skillTree.getMaterialCostCoin()) { + // Verify item params + if (!player.getInventory().verifyItems(skillTree.getMaterialList())) { player.sendPacket(new PacketUnlockSkilltreeScRsp()); return; } // Pay items player.getInventory().removeItemsByParams(skillTree.getMaterialList()); - player.addSCoin(-skillTree.getMaterialCostCoin()); // Add skill avatar.getSkills().put(pointId, nextLevel); @@ -541,7 +524,7 @@ public class InventoryService extends BaseGameService { player.sendPacket(new PacketSellItemScRsp(returnItems)); } - public List buyShopGoods(Player player, int shopId, int goodsId, int goodsNum) { + public List buyShopGoods(Player player, int shopId, int goodsId, int count) { // Get shop and goods excels ShopExcel shop = GameData.getShopExcelMap().get(shopId); if (shop == null) return null; @@ -549,22 +532,13 @@ public class InventoryService extends BaseGameService { ShopGoodsExcel goods = shop.getGoods().get(goodsId); if (goods == null) return null; - // Verify items - for (ItemParam param : goods.getCostList()) { - GameItem item = player.getInventory().getItemByParam(param); - if (item == null || item.getCount() < param.getCount() * goodsNum) { - return null; - } - } - - // Verify credits - if (player.getScoin() < goods.getCoinCost() * goodsNum) { + // Verify item params + if (!player.getInventory().verifyItems(goods.getCostList(), count)) { return null; } // Pay items - player.getInventory().removeItemsByParams(goods.getCostList(), goodsNum); - player.addSCoin(goods.getCoinCost() * goodsNum); + player.getInventory().removeItemsByParams(goods.getCostList(), count); // Buy items List items = new ArrayList<>(); @@ -574,7 +548,7 @@ public class InventoryService extends BaseGameService { GameItem item = new GameItem(itemExcel, goods.getItemCount()); items.add(item); } else { - int num = goods.getItemCount() * goodsNum; + int num = goods.getItemCount() * count; for (int i = 0; i < num; i++) { GameItem item = new GameItem(itemExcel, 1); items.add(item); @@ -597,16 +571,8 @@ public class InventoryService extends BaseGameService { return null; } - // Verify items - for (ItemParam param : excel.getMaterialCost()) { - GameItem item = player.getInventory().getItemByParam(param); - if (item == null || item.getCount() < param.getCount() * count) { - return null; - } - } - - // Verify credits - if (player.getScoin() < excel.getCoinCost() * count) { + // Verify items + credits + if (!player.getInventory().verifyItems(excel.getMaterialCost(), count) || !player.getInventory().verifyScoin(excel.getCoinCost() * count)) { return null; } @@ -651,16 +617,8 @@ public class InventoryService extends BaseGameService { } } - // Verify items - for (ItemParam param : costItems) { - GameItem item = player.getInventory().getItemByParam(param); - if (item == null || item.getCount() < param.getCount() * count) { - return null; - } - } - - // Verify credits - if (player.getScoin() < excel.getCoinCost() * count) { + // Verify items + credits + if (!player.getInventory().verifyItems(costItems, count) || !player.getInventory().verifyScoin(excel.getCoinCost() * count)) { return null; }