Rework how item params are verified

This commit is contained in:
Melledy
2023-10-23 10:05:50 -07:00
parent 4abb1f9470
commit 0c6d308b5e
5 changed files with 57 additions and 108 deletions

View File

@@ -1,10 +1,7 @@
package emu.lunarcore.data.excel; package emu.lunarcore.data.excel;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
import emu.lunarcore.GameConstants;
import emu.lunarcore.data.GameResource; import emu.lunarcore.data.GameResource;
import emu.lunarcore.data.ResourceType; import emu.lunarcore.data.ResourceType;
import emu.lunarcore.data.ResourceType.LoadPriority; import emu.lunarcore.data.ResourceType.LoadPriority;
@@ -21,7 +18,6 @@ public class AvatarPromotionExcel extends GameResource {
private int PlayerLevelRequire; private int PlayerLevelRequire;
private int WorldLevelRequire; private int WorldLevelRequire;
private List<ItemParam> PromotionCostList; private List<ItemParam> PromotionCostList;
private transient int PromotionCostCoin;
private double AttackBase; private double AttackBase;
private double AttackAdd; private double AttackAdd;
@@ -38,20 +34,4 @@ public class AvatarPromotionExcel extends GameResource {
public int getId() { public int getId() {
return (AvatarID << 8) + Promotion; return (AvatarID << 8) + Promotion;
} }
@Override
public void onLoad() {
if (this.PromotionCostList == null) {
this.PromotionCostList = new ArrayList<>();
} else {
Iterator<ItemParam> it = this.PromotionCostList.iterator();
while (it.hasNext()) {
ItemParam param = it.next();
if (param.getId() == GameConstants.MATERIAL_COIN_ID) {
this.PromotionCostCoin = param.getCount();
it.remove();
}
}
}
}
} }

View File

@@ -1,10 +1,7 @@
package emu.lunarcore.data.excel; package emu.lunarcore.data.excel;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
import emu.lunarcore.GameConstants;
import emu.lunarcore.data.GameData; import emu.lunarcore.data.GameData;
import emu.lunarcore.data.GameResource; import emu.lunarcore.data.GameResource;
import emu.lunarcore.data.ResourceType; import emu.lunarcore.data.ResourceType;
@@ -12,7 +9,6 @@ import emu.lunarcore.data.ResourceType.LoadPriority;
import emu.lunarcore.data.common.ItemParam; import emu.lunarcore.data.common.ItemParam;
import it.unimi.dsi.fastutil.ints.IntArrayList; import it.unimi.dsi.fastutil.ints.IntArrayList;
import lombok.Getter; import lombok.Getter;
import net.bytebuddy.asm.Advice.This;
@Getter @Getter
@ResourceType(name = {"AvatarSkillTreeConfig.json"}, loadPriority = LoadPriority.LOW) @ResourceType(name = {"AvatarSkillTreeConfig.json"}, loadPriority = LoadPriority.LOW)
@@ -30,8 +26,6 @@ public class AvatarSkillTreeExcel extends GameResource {
private IntArrayList PrePoint; private IntArrayList PrePoint;
private IntArrayList LevelUpSkillID; private IntArrayList LevelUpSkillID;
private transient int MaterialCostCoin;
@Override @Override
public int getId() { public int getId() {
return (PointID << 4) + Level; return (PointID << 4) + Level;
@@ -39,20 +33,6 @@ public class AvatarSkillTreeExcel extends GameResource {
@Override @Override
public void onLoad() { public void onLoad() {
// Parse material list
if (this.MaterialList == null) {
this.MaterialList = new ArrayList<>();
} else {
Iterator<ItemParam> 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 // Load to excel
AvatarExcel excel = GameData.getAvatarExcelMap().get(this.AvatarID); AvatarExcel excel = GameData.getAvatarExcelMap().get(this.AvatarID);
if (excel == null) return; if (excel == null) return;

View File

@@ -3,7 +3,6 @@ package emu.lunarcore.data.excel;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import emu.lunarcore.GameConstants;
import emu.lunarcore.data.GameData; import emu.lunarcore.data.GameData;
import emu.lunarcore.data.GameResource; import emu.lunarcore.data.GameResource;
import emu.lunarcore.data.ResourceType; import emu.lunarcore.data.ResourceType;
@@ -27,7 +26,6 @@ public class ShopGoodsExcel extends GameResource {
private int[] CurrencyCostList; private int[] CurrencyCostList;
private transient List<ItemParam> costList; private transient List<ItemParam> costList;
private transient int coinCost;
@Override @Override
public int getId() { public int getId() {
@@ -46,11 +44,6 @@ public class ShopGoodsExcel extends GameResource {
this.costList = new ArrayList<>(CurrencyList.length); this.costList = new ArrayList<>(CurrencyList.length);
for (int i = 0; i < CurrencyList.length; i++) { 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]); ItemParam param = new ItemParam(CurrencyList[i], CurrencyCostList[i]);
this.costList.add(param); this.costList.add(param);
} }

View File

@@ -5,6 +5,7 @@ import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.stream.Stream; import java.util.stream.Stream;
import emu.lunarcore.GameConstants;
import emu.lunarcore.LunarRail; import emu.lunarcore.LunarRail;
import emu.lunarcore.data.GameData; import emu.lunarcore.data.GameData;
import emu.lunarcore.data.common.ItemParam; import emu.lunarcore.data.common.ItemParam;
@@ -253,12 +254,19 @@ public class Inventory extends BasePlayerManager {
List<GameItem> results = new ArrayList<GameItem>(items.size()); List<GameItem> results = new ArrayList<GameItem>(items.size());
for (ItemParam param : items) { for (ItemParam param : items) {
GameItem item = this.getItemByParam(param); // Check param type
if (item == null) continue; if (param.getId() == GameConstants.MATERIAL_COIN_ID) {
// Remove credits
GameItem result = this.deleteItem(item, param.getCount() * multiplier); getPlayer().addSCoin(-param.getCount() * multiplier);
if (result != null) { } else {
results.add(result); // 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 // Returns true on success
return item; return item;
} }
// Verifying items
public boolean verifyItems(Collection<ItemParam> params) {
return verifyItems(params, 1);
}
public boolean verifyItems(Collection<ItemParam> 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 // Equips

View File

@@ -103,21 +103,14 @@ public class InventoryService extends BaseGameService {
return; return;
} }
// Verify items // Verify item params
for (ItemParam param : promotion.getPromotionCostList()) { if (!player.getInventory().verifyItems(promotion.getPromotionCostList())) {
GameItem item = player.getInventory().getItemByParam(param);
if (item == null || item.getCount() < param.getCount()) return;
}
// Verify credits
if (player.getScoin() < promotion.getPromotionCostCoin()) {
player.sendPacket(new BasePacket(CmdId.PromoteAvatarScRsp)); player.sendPacket(new BasePacket(CmdId.PromoteAvatarScRsp));
return; return;
} }
// Pay items // Pay items
player.getInventory().removeItemsByParams(promotion.getPromotionCostList()); player.getInventory().removeItemsByParams(promotion.getPromotionCostList());
player.addSCoin(-promotion.getPromotionCostCoin());
// Promote // Promote
avatar.setPromotion(avatar.getPromotion() + 1); avatar.setPromotion(avatar.getPromotion() + 1);
@@ -143,24 +136,14 @@ public class InventoryService extends BaseGameService {
AvatarSkillTreeExcel skillTree = GameData.getAvatarSkillTreeExcel(pointId, nextLevel); AvatarSkillTreeExcel skillTree = GameData.getAvatarSkillTreeExcel(pointId, nextLevel);
if (skillTree == null || skillTree.getAvatarID() != avatar.getExcel().getAvatarID()) return; if (skillTree == null || skillTree.getAvatarID() != avatar.getExcel().getAvatarID()) return;
// Verify items // Verify item params
for (ItemParam param : skillTree.getMaterialList()) { if (!player.getInventory().verifyItems(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()) {
player.sendPacket(new PacketUnlockSkilltreeScRsp()); player.sendPacket(new PacketUnlockSkilltreeScRsp());
return; return;
} }
// Pay items // Pay items
player.getInventory().removeItemsByParams(skillTree.getMaterialList()); player.getInventory().removeItemsByParams(skillTree.getMaterialList());
player.addSCoin(-skillTree.getMaterialCostCoin());
// Add skill // Add skill
avatar.getSkills().put(pointId, nextLevel); avatar.getSkills().put(pointId, nextLevel);
@@ -541,7 +524,7 @@ public class InventoryService extends BaseGameService {
player.sendPacket(new PacketSellItemScRsp(returnItems)); player.sendPacket(new PacketSellItemScRsp(returnItems));
} }
public List<GameItem> buyShopGoods(Player player, int shopId, int goodsId, int goodsNum) { public List<GameItem> buyShopGoods(Player player, int shopId, int goodsId, int count) {
// Get shop and goods excels // Get shop and goods excels
ShopExcel shop = GameData.getShopExcelMap().get(shopId); ShopExcel shop = GameData.getShopExcelMap().get(shopId);
if (shop == null) return null; if (shop == null) return null;
@@ -549,22 +532,13 @@ public class InventoryService extends BaseGameService {
ShopGoodsExcel goods = shop.getGoods().get(goodsId); ShopGoodsExcel goods = shop.getGoods().get(goodsId);
if (goods == null) return null; if (goods == null) return null;
// Verify items // Verify item params
for (ItemParam param : goods.getCostList()) { if (!player.getInventory().verifyItems(goods.getCostList(), count)) {
GameItem item = player.getInventory().getItemByParam(param);
if (item == null || item.getCount() < param.getCount() * goodsNum) {
return null;
}
}
// Verify credits
if (player.getScoin() < goods.getCoinCost() * goodsNum) {
return null; return null;
} }
// Pay items // Pay items
player.getInventory().removeItemsByParams(goods.getCostList(), goodsNum); player.getInventory().removeItemsByParams(goods.getCostList(), count);
player.addSCoin(goods.getCoinCost() * goodsNum);
// Buy items // Buy items
List<GameItem> items = new ArrayList<>(); List<GameItem> items = new ArrayList<>();
@@ -574,7 +548,7 @@ public class InventoryService extends BaseGameService {
GameItem item = new GameItem(itemExcel, goods.getItemCount()); GameItem item = new GameItem(itemExcel, goods.getItemCount());
items.add(item); items.add(item);
} else { } else {
int num = goods.getItemCount() * goodsNum; int num = goods.getItemCount() * count;
for (int i = 0; i < num; i++) { for (int i = 0; i < num; i++) {
GameItem item = new GameItem(itemExcel, 1); GameItem item = new GameItem(itemExcel, 1);
items.add(item); items.add(item);
@@ -597,16 +571,8 @@ public class InventoryService extends BaseGameService {
return null; return null;
} }
// Verify items // Verify items + credits
for (ItemParam param : excel.getMaterialCost()) { if (!player.getInventory().verifyItems(excel.getMaterialCost(), count) || !player.getInventory().verifyScoin(excel.getCoinCost() * count)) {
GameItem item = player.getInventory().getItemByParam(param);
if (item == null || item.getCount() < param.getCount() * count) {
return null;
}
}
// Verify credits
if (player.getScoin() < excel.getCoinCost() * count) {
return null; return null;
} }
@@ -651,16 +617,8 @@ public class InventoryService extends BaseGameService {
} }
} }
// Verify items // Verify items + credits
for (ItemParam param : costItems) { if (!player.getInventory().verifyItems(costItems, count) || !player.getInventory().verifyScoin(excel.getCoinCost() * count)) {
GameItem item = player.getInventory().getItemByParam(param);
if (item == null || item.getCount() < param.getCount() * count) {
return null;
}
}
// Verify credits
if (player.getScoin() < excel.getCoinCost() * count) {
return null; return null;
} }