From 7b591a2da0c20162a35a0fdc5ea424011221d2dd Mon Sep 17 00:00:00 2001 From: xtaodada Date: Mon, 2 May 2022 15:30:19 +0800 Subject: [PATCH] implement shopMail giftPackage function Co-authored-by: Kinesis --- .../java/emu/grasscutter/data/GameData.java | 5 ++++ .../data/common/RewardBoxItemData.java | 22 +++++++++++++++ .../grasscutter/data/def/RewardBoxData.java | 27 ++++++++++++++++++ .../grasscutter/data/def/ShopGoodsData.java | 5 ++++ .../grasscutter/game/inventory/GameItem.java | 13 ++++++++- .../game/managers/InventoryManager.java | 28 +++++++++++++++++++ .../emu/grasscutter/game/shop/ShopInfo.java | 10 +++++++ .../packet/recv/HandlerBuyGoodsReq.java | 5 ++-- 8 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 src/main/java/emu/grasscutter/data/common/RewardBoxItemData.java create mode 100644 src/main/java/emu/grasscutter/data/def/RewardBoxData.java diff --git a/src/main/java/emu/grasscutter/data/GameData.java b/src/main/java/emu/grasscutter/data/GameData.java index c187a1819..2ba251e71 100644 --- a/src/main/java/emu/grasscutter/data/GameData.java +++ b/src/main/java/emu/grasscutter/data/GameData.java @@ -62,6 +62,7 @@ public class GameData { private static final Int2ObjectMap fetterDataMap = new Int2ObjectOpenHashMap<>(); private static final Int2ObjectMap fetterCharacterCardDataMap = new Int2ObjectOpenHashMap<>(); private static final Int2ObjectMap rewardDataMap = new Int2ObjectOpenHashMap<>(); + private static final Int2ObjectMap rewardBoxDataMap = new Int2ObjectOpenHashMap<>(); private static final Int2ObjectMap worldLevelDataMap = new Int2ObjectOpenHashMap<>(); private static final Int2ObjectMap dailyDungeonDataMap = new Int2ObjectOpenHashMap<>(); private static final Int2ObjectMap dungeonDataMap = new Int2ObjectOpenHashMap<>(); @@ -263,6 +264,10 @@ public class GameData { return rewardDataMap; } + public static Int2ObjectMap getRewardBoxDataMap() { + return rewardBoxDataMap; + } + public static Map> getFetterDataEntries() { if (fetters.isEmpty()) { fetterDataMap.forEach((k, v) -> { diff --git a/src/main/java/emu/grasscutter/data/common/RewardBoxItemData.java b/src/main/java/emu/grasscutter/data/common/RewardBoxItemData.java new file mode 100644 index 000000000..b32e2c650 --- /dev/null +++ b/src/main/java/emu/grasscutter/data/common/RewardBoxItemData.java @@ -0,0 +1,22 @@ +package emu.grasscutter.data.common; + +public class RewardBoxItemData { + private int Id; + private String Count; + + public int getItemId() { + return Id; + } + + public void setItemId(int itemId) { + Id = itemId; + } + + public String getItemCount() { + return Count; + } + + public void setItemCount(String itemCount) { + Count = itemCount; + } +} diff --git a/src/main/java/emu/grasscutter/data/def/RewardBoxData.java b/src/main/java/emu/grasscutter/data/def/RewardBoxData.java new file mode 100644 index 000000000..c4c3ce527 --- /dev/null +++ b/src/main/java/emu/grasscutter/data/def/RewardBoxData.java @@ -0,0 +1,27 @@ +package emu.grasscutter.data.def; + +import java.util.List; + +import emu.grasscutter.data.GameResource; +import emu.grasscutter.data.ResourceType; +import emu.grasscutter.data.common.RewardBoxItemData; + +@ResourceType(name = "RewardPreviewExcelConfigData.json") +public class RewardBoxData extends GameResource { + public int Id; + public List PreviewItems; + + @Override + public int getId() { + return Id; + } + + public List getRewardBoxItemList() { + return PreviewItems; + } + + @Override + public void onLoad() { + + } +} diff --git a/src/main/java/emu/grasscutter/data/def/ShopGoodsData.java b/src/main/java/emu/grasscutter/data/def/ShopGoodsData.java index 1a4168637..a4d91e933 100644 --- a/src/main/java/emu/grasscutter/data/def/ShopGoodsData.java +++ b/src/main/java/emu/grasscutter/data/def/ShopGoodsData.java @@ -30,6 +30,7 @@ public class ShopGoodsData extends GameResource { private transient ShopInfo.ShopRefreshType RefreshTypeEnum; private int RefreshParam; + private int ShowId; @Override public void onLoad() { @@ -105,4 +106,8 @@ public class ShopGoodsData extends GameResource { public int getRefreshParam() { return RefreshParam; } + + public int getShowId() { + return ShowId; + } } diff --git a/src/main/java/emu/grasscutter/game/inventory/GameItem.java b/src/main/java/emu/grasscutter/game/inventory/GameItem.java index 7293b75c0..02ff8cd25 100644 --- a/src/main/java/emu/grasscutter/game/inventory/GameItem.java +++ b/src/main/java/emu/grasscutter/game/inventory/GameItem.java @@ -58,6 +58,9 @@ public class GameItem { // Relic private int mainPropId; private List appendPropIdList; + + // shopMailBox + private int rewardBoxId; private int equipCharacter; @Transient private int weaponEntityId; @@ -90,7 +93,7 @@ public class GameItem { // Equip data if (getItemType() == ItemType.ITEM_WEAPON) { - this.level = this.count > 1 ? this.count : 1; + this.level = Math.max(this.count, 1); this.affixes = new ArrayList<>(2); if (getItemData().getSkillAffix() != null) { for (int skillAffix : getItemData().getSkillAffix()) { @@ -248,6 +251,14 @@ public class GameItem { this.mainPropId = mainPropId; } + public int getRewardBoxId() { + return rewardBoxId; + } + + public void setRewardBoxId(int rewardBoxId) { + this.rewardBoxId = rewardBoxId; + } + public List getAppendPropIdList() { return appendPropIdList; } diff --git a/src/main/java/emu/grasscutter/game/managers/InventoryManager.java b/src/main/java/emu/grasscutter/game/managers/InventoryManager.java index 2a9629b73..6fef8afc4 100644 --- a/src/main/java/emu/grasscutter/game/managers/InventoryManager.java +++ b/src/main/java/emu/grasscutter/game/managers/InventoryManager.java @@ -7,11 +7,13 @@ import java.util.stream.Collectors; import emu.grasscutter.data.GameData; import emu.grasscutter.data.common.ItemParamData; +import emu.grasscutter.data.common.RewardBoxItemData; import emu.grasscutter.data.custom.OpenConfigEntry; import emu.grasscutter.data.custom.OpenConfigEntry.SkillPointModifier; import emu.grasscutter.data.def.AvatarPromoteData; import emu.grasscutter.data.def.AvatarSkillData; import emu.grasscutter.data.def.AvatarSkillDepotData; +import emu.grasscutter.data.def.RewardBoxData; import emu.grasscutter.data.def.WeaponPromoteData; import emu.grasscutter.data.def.AvatarSkillDepotData.InherentProudSkillOpens; import emu.grasscutter.data.def.AvatarTalentData; @@ -21,6 +23,7 @@ import emu.grasscutter.game.inventory.GameItem; import emu.grasscutter.game.inventory.ItemType; import emu.grasscutter.game.inventory.MaterialType; import emu.grasscutter.game.player.Player; +import emu.grasscutter.game.props.ActionReason; import emu.grasscutter.net.proto.ItemParamOuterClass.ItemParam; import emu.grasscutter.net.proto.MaterialInfoOuterClass.MaterialInfo; import emu.grasscutter.server.game.GameServer; @@ -897,6 +900,26 @@ public class InventoryManager { player.sendPacket(new PacketDestroyMaterialRsp(returnMaterialMap)); } + private boolean handleRewardBox(Player player, GameItem useItem) { + List rewardBoxDataList = GameData.getRewardBoxDataMap().values().stream().filter(x -> x.getId() == useItem.getRewardBoxId()).collect(Collectors.toList()); + if (rewardBoxDataList.isEmpty()) { + return false; + } + List rewardItemList = new ArrayList<>(); + for (RewardBoxItemData itemData : rewardBoxDataList.get(0).getRewardBoxItemList()) { + if (itemData.getItemId() == 0) { + continue; + } + String[] split = itemData.getItemCount().split(";"); + int itemCount = Integer.parseInt(split[(int) (Math.random()* split.length)]); + rewardItemList.add(new GameItem(itemData.getItemId(), itemCount)); + } + if (!rewardItemList.isEmpty()) { + player.getInventory().addItems(rewardItemList, ActionReason.Shop); + } + return true; + } + public GameItem useItem(Player player, long targetGuid, long itemGuid, int count) { Avatar target = player.getAvatars().getAvatarByGuid(targetGuid); GameItem useItem = player.getInventory().getItemByGuid(itemGuid); @@ -918,6 +941,11 @@ public class InventoryManager { used = player.getTeamManager().reviveAvatar(target) ? 1 : 0; } break; + case MATERIAL_CHEST: + if (useItem.getRewardBoxId() > 0) { + used = handleRewardBox(player, useItem) ? 1 : 0; + } + break; default: break; } diff --git a/src/main/java/emu/grasscutter/game/shop/ShopInfo.java b/src/main/java/emu/grasscutter/game/shop/ShopInfo.java index ebbeea2cd..ab5de27f6 100644 --- a/src/main/java/emu/grasscutter/game/shop/ShopInfo.java +++ b/src/main/java/emu/grasscutter/game/shop/ShopInfo.java @@ -13,6 +13,7 @@ public class ShopInfo { private List costItemList; private int boughtNum = 0; private int buyLimit = 0; + private int showId = 0; private int beginTime = 0; private int endTime = 1924992000; private int minLevel = 0; @@ -51,6 +52,7 @@ public class ShopInfo { this.mcoin = sgd.getCostMcoin(); this.hcoin = sgd.getCostHcoin(); this.buyLimit = sgd.getBuyLimit(); + this.showId = sgd.getShowId(); this.minLevel = sgd.getMinPlayerLevel(); this.maxLevel = sgd.getMaxPlayerLevel(); @@ -180,6 +182,14 @@ public class ShopInfo { this.maxLevel = maxLevel; } + public int getShowId() { + return showId; + } + + public void setShowId(int showId) { + this.showId = showId; + } + public ShopRefreshType getShopRefreshType() { if (refreshType == null) return ShopRefreshType.NONE; diff --git a/src/main/java/emu/grasscutter/server/packet/recv/HandlerBuyGoodsReq.java b/src/main/java/emu/grasscutter/server/packet/recv/HandlerBuyGoodsReq.java index 0088b7176..7b773995d 100644 --- a/src/main/java/emu/grasscutter/server/packet/recv/HandlerBuyGoodsReq.java +++ b/src/main/java/emu/grasscutter/server/packet/recv/HandlerBuyGoodsReq.java @@ -4,7 +4,6 @@ import emu.grasscutter.data.GameData; import emu.grasscutter.data.common.ItemParamData; import emu.grasscutter.game.inventory.GameItem; import emu.grasscutter.game.props.ActionReason; -import emu.grasscutter.game.props.PlayerProperty; import emu.grasscutter.game.shop.ShopInfo; import emu.grasscutter.game.shop.ShopLimit; import emu.grasscutter.game.shop.ShopManager; @@ -19,7 +18,6 @@ import emu.grasscutter.server.packet.send.PacketBuyGoodsRsp; import emu.grasscutter.server.packet.send.PacketStoreItemChangeNotify; import emu.grasscutter.utils.Utils; -import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Optional; @@ -92,6 +90,9 @@ public class HandlerBuyGoodsReq extends PacketHandler { session.getPlayer().addShopLimit(sg.getGoodsId(), buyGoodsReq.getBoughtNum(), ShopManager.getShopNextRefreshTime(sg)); GameItem item = new GameItem(GameData.getItemDataMap().get(sg.getGoodsItem().getId())); item.setCount(buyGoodsReq.getBoughtNum() * sg.getGoodsItem().getCount()); + if (sg.getShowId() > 0) { + item.setRewardBoxId(sg.getShowId()); + } session.getPlayer().getInventory().addItem(item, ActionReason.Shop, true); // fix: not notify when got virtual item from shop session.send(new PacketBuyGoodsRsp(buyGoodsReq.getShopType(), session.getPlayer().getGoodsLimit(sg.getGoodsId()).getHasBoughtInPeriod(), buyGoodsReq.getGoodsListList().stream().filter(x -> x.getGoodsId() == goodsId).findFirst().get())); }