mirror of
https://github.com/Melledy/LunarCore.git
synced 2025-12-14 14:24:37 +01:00
Rework how item params are verified
This commit is contained in:
@@ -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<ItemParam> 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<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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<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
|
||||
AvatarExcel excel = GameData.getAvatarExcelMap().get(this.AvatarID);
|
||||
if (excel == null) return;
|
||||
|
||||
@@ -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<ItemParam> 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);
|
||||
}
|
||||
|
||||
@@ -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,6 +254,12 @@ public class Inventory extends BasePlayerManager {
|
||||
List<GameItem> results = new ArrayList<GameItem>(items.size());
|
||||
|
||||
for (ItemParam param : items) {
|
||||
// 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;
|
||||
|
||||
@@ -261,6 +268,7 @@ public class Inventory extends BasePlayerManager {
|
||||
results.add(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Send packet (update)
|
||||
if (results.size() > 0) {
|
||||
@@ -354,6 +362,36 @@ public class Inventory extends BasePlayerManager {
|
||||
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
|
||||
|
||||
public boolean equipItem(int avatarId, int equipId) {
|
||||
|
||||
@@ -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<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
|
||||
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<GameItem> 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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user