From 8105d2a7a9c0993372204890b86a04760b67c8f7 Mon Sep 17 00:00:00 2001 From: Melledy <52122272+Melledy@users.noreply.github.com> Date: Tue, 26 Apr 2022 19:45:22 -0700 Subject: [PATCH] Call PacketItemAddHintNotify from addItem --- .../command/commands/GiveCommand.java | 6 +-- .../emu/grasscutter/game/GenshinPlayer.java | 6 +-- .../grasscutter/game/inventory/Inventory.java | 38 +++++++++++++------ .../HandlerAvatarFetterLevelRewardReq.java | 3 +- .../packet/send/PacketGetMailItemRsp.java | 3 +- .../packet/send/PacketItemAddHintNotify.java | 3 +- 6 files changed, 34 insertions(+), 25 deletions(-) diff --git a/src/main/java/emu/grasscutter/command/commands/GiveCommand.java b/src/main/java/emu/grasscutter/command/commands/GiveCommand.java index 2ecdac1f5..bd5c025be 100644 --- a/src/main/java/emu/grasscutter/command/commands/GiveCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/GiveCommand.java @@ -144,13 +144,11 @@ public final class GiveCommand implements CommandHandler { } items.add(item); } - player.getInventory().addItems(items); - player.sendPacket(new PacketItemAddHintNotify(items, ActionReason.SubfieldDrop)); + player.getInventory().addItems(items, ActionReason.SubfieldDrop); } else { GenshinItem genshinItem = new GenshinItem(itemData); genshinItem.setCount(amount); - player.getInventory().addItem(genshinItem); - player.sendPacket(new PacketItemAddHintNotify(genshinItem, ActionReason.SubfieldDrop)); + player.getInventory().addItem(genshinItem, ActionReason.SubfieldDrop); } } } diff --git a/src/main/java/emu/grasscutter/game/GenshinPlayer.java b/src/main/java/emu/grasscutter/game/GenshinPlayer.java index ad0fe248d..4de9b837f 100644 --- a/src/main/java/emu/grasscutter/game/GenshinPlayer.java +++ b/src/main/java/emu/grasscutter/game/GenshinPlayer.java @@ -584,8 +584,7 @@ public class GenshinPlayer { moonCardGetTimes.add(now); addMoonCardDays(1); GenshinItem genshinItem = new GenshinItem(201, 90); - getInventory().addItem(genshinItem); - session.send(new PacketItemAddHintNotify(genshinItem, ActionReason.BlessingRedeemReward)); + getInventory().addItem(genshinItem, ActionReason.BlessingRedeemReward); session.send(new PacketCardProductRewardNotify(getMoonCardRemainDays())); } @@ -730,10 +729,9 @@ public class GenshinPlayer { EntityItem drop = (EntityItem) entity; GenshinItem item = new GenshinItem(drop.getItemData(), drop.getCount()); // Add to inventory - boolean success = getInventory().addItem(item); + boolean success = getInventory().addItem(item, ActionReason.SubfieldDrop); if (success) { this.sendPacket(new PacketGadgetInteractRsp(drop, InteractType.INTERACT_PICK_ITEM)); - this.sendPacket(new PacketItemAddHintNotify(item, ActionReason.SubfieldDrop)); } } diff --git a/src/main/java/emu/grasscutter/game/inventory/Inventory.java b/src/main/java/emu/grasscutter/game/inventory/Inventory.java index 8954b1031..a41386530 100644 --- a/src/main/java/emu/grasscutter/game/inventory/Inventory.java +++ b/src/main/java/emu/grasscutter/game/inventory/Inventory.java @@ -17,8 +17,10 @@ import emu.grasscutter.game.GenshinPlayer; import emu.grasscutter.game.avatar.AvatarStorage; import emu.grasscutter.game.avatar.GenshinAvatar; import emu.grasscutter.game.entity.EntityAvatar; +import emu.grasscutter.game.props.ActionReason; import emu.grasscutter.net.proto.ItemParamOuterClass.ItemParam; import emu.grasscutter.server.packet.send.PacketAvatarEquipChangeNotify; +import emu.grasscutter.server.packet.send.PacketItemAddHintNotify; import emu.grasscutter.server.packet.send.PacketStoreItemChangeNotify; import emu.grasscutter.server.packet.send.PacketStoreItemDelNotify; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; @@ -86,7 +88,7 @@ public class Inventory implements Iterable { return addItem(item); } - + public boolean addItem(GenshinItem item) { GenshinItem result = putItem(item); @@ -98,7 +100,21 @@ public class Inventory implements Iterable { return false; } + public boolean addItem(GenshinItem item, ActionReason reason) { + boolean result = addItem(item); + + if (result && reason != null) { + getPlayer().sendPacket(new PacketItemAddHintNotify(item, reason)); + } + + return result; + } + public void addItems(Collection items) { + this.addItems(items, null); + } + + public void addItems(Collection items, ActionReason reason) { List changedItems = new LinkedList<>(); for (GenshinItem item : items) { @@ -108,21 +124,19 @@ public class Inventory implements Iterable { } } + if (changedItems.size() == 0) { + return; + } + + if (reason != null) { + getPlayer().sendPacket(new PacketItemAddHintNotify(changedItems, reason)); + } + getPlayer().sendPacket(new PacketStoreItemChangeNotify(changedItems)); } public void addItemParams(Collection items) { - List changedItems = new LinkedList<>(); - - for (ItemParam itemParam : items) { - GenshinItem toAdd = new GenshinItem(itemParam.getItemId(), itemParam.getCount()); - GenshinItem result = putItem(toAdd); - if (result != null) { - changedItems.add(result); - } - } - - getPlayer().sendPacket(new PacketStoreItemChangeNotify(changedItems)); + addItems(items.stream().map(param -> new GenshinItem(param.getItemId(), param.getCount())).toList(), null); } private synchronized GenshinItem putItem(GenshinItem item) { diff --git a/src/main/java/emu/grasscutter/server/packet/recv/HandlerAvatarFetterLevelRewardReq.java b/src/main/java/emu/grasscutter/server/packet/recv/HandlerAvatarFetterLevelRewardReq.java index b8b4f5e57..b0d45027d 100644 --- a/src/main/java/emu/grasscutter/server/packet/recv/HandlerAvatarFetterLevelRewardReq.java +++ b/src/main/java/emu/grasscutter/server/packet/recv/HandlerAvatarFetterLevelRewardReq.java @@ -44,8 +44,7 @@ public class HandlerAvatarFetterLevelRewardReq extends PacketHandler { } GenshinItem item = new GenshinItem(cardId); - session.getPlayer().getInventory().addItem(item); - session.getPlayer().sendPacket(new PacketItemAddHintNotify(item, ActionReason.FetterLevelReward)); + session.getPlayer().getInventory().addItem(item, ActionReason.FetterLevelReward); session.getPlayer().sendPacket(new PacketUnlockNameCardNotify(cardId)); session.send(new PacketAvatarFetterDataNotify(avatar)); session.send(new PacketAvatarDataNotify(avatar.getPlayer())); diff --git a/src/main/java/emu/grasscutter/server/packet/send/PacketGetMailItemRsp.java b/src/main/java/emu/grasscutter/server/packet/send/PacketGetMailItemRsp.java index a2379545b..7874acbc4 100644 --- a/src/main/java/emu/grasscutter/server/packet/send/PacketGetMailItemRsp.java +++ b/src/main/java/emu/grasscutter/server/packet/send/PacketGetMailItemRsp.java @@ -54,8 +54,7 @@ public class PacketGetMailItemRsp extends GenshinPacket { genshinItem.setCount(mailItem.itemCount); genshinItem.setLevel(mailItem.itemLevel); genshinItem.setPromoteLevel(promoteLevel); - player.getInventory().addItem(genshinItem); - player.sendPacket(new PacketItemAddHintNotify(genshinItem, ActionReason.MailAttachment)); + player.getInventory().addItem(genshinItem, ActionReason.MailAttachment); } message.isAttachmentGot = true; diff --git a/src/main/java/emu/grasscutter/server/packet/send/PacketItemAddHintNotify.java b/src/main/java/emu/grasscutter/server/packet/send/PacketItemAddHintNotify.java index 598b9a0f6..577e09f69 100644 --- a/src/main/java/emu/grasscutter/server/packet/send/PacketItemAddHintNotify.java +++ b/src/main/java/emu/grasscutter/server/packet/send/PacketItemAddHintNotify.java @@ -1,5 +1,6 @@ package emu.grasscutter.server.packet.send; +import java.util.Collection; import java.util.List; import emu.grasscutter.game.inventory.GenshinItem; @@ -21,7 +22,7 @@ public class PacketItemAddHintNotify extends GenshinPacket { this.setData(proto); } - public PacketItemAddHintNotify(List items, ActionReason reason) { + public PacketItemAddHintNotify(Collection items, ActionReason reason) { super(PacketOpcodes.ItemAddHintNotify); ItemAddHintNotify.Builder proto = ItemAddHintNotify.newBuilder()