From db1cc788b4cffe92e2e23347b1bdc362af2f766b Mon Sep 17 00:00:00 2001 From: KingRainbow44 Date: Thu, 11 May 2023 21:11:10 -0400 Subject: [PATCH] Validate the NPC being talked to --- .../data/excels/TalkConfigData.java | 9 ++++++++- .../grasscutter/game/talk/TalkManager.java | 19 ++++++++++++++----- .../server/packet/recv/HandlerNpcTalkReq.java | 2 +- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/main/java/emu/grasscutter/data/excels/TalkConfigData.java b/src/main/java/emu/grasscutter/data/excels/TalkConfigData.java index b8b756d0a..66d27f56f 100644 --- a/src/main/java/emu/grasscutter/data/excels/TalkConfigData.java +++ b/src/main/java/emu/grasscutter/data/excels/TalkConfigData.java @@ -4,8 +4,10 @@ import com.google.gson.annotations.SerializedName; import emu.grasscutter.data.GameResource; import emu.grasscutter.data.ResourceType; import emu.grasscutter.game.talk.TalkExec; +import lombok.Data; +import lombok.EqualsAndHashCode; + import java.util.List; -import lombok.*; @ResourceType(name = "TalkExcelConfigData.json") @EqualsAndHashCode(callSuper = false) @@ -26,6 +28,11 @@ public final class TalkConfigData extends GameResource { alternate = {"_questId"}) private int questId; + @SerializedName( + value = "npcId", + alternate = {"_npcId"}) + private List npcId; + @Override public void onLoad() { this.finishExec = diff --git a/src/main/java/emu/grasscutter/game/talk/TalkManager.java b/src/main/java/emu/grasscutter/game/talk/TalkManager.java index e0bb112b8..8a613834c 100644 --- a/src/main/java/emu/grasscutter/game/talk/TalkManager.java +++ b/src/main/java/emu/grasscutter/game/talk/TalkManager.java @@ -1,15 +1,15 @@ package emu.grasscutter.game.talk; -import static emu.grasscutter.game.quest.enums.QuestCond.QUEST_COND_COMPLETE_TALK; -import static emu.grasscutter.game.quest.enums.QuestContent.QUEST_CONTENT_COMPLETE_ANY_TALK; -import static emu.grasscutter.game.quest.enums.QuestContent.QUEST_CONTENT_COMPLETE_TALK; - import emu.grasscutter.data.GameData; import emu.grasscutter.data.binout.MainQuestData.TalkData; import emu.grasscutter.game.player.BasePlayerManager; import emu.grasscutter.game.player.Player; import lombok.NonNull; +import static emu.grasscutter.game.quest.enums.QuestCond.QUEST_COND_COMPLETE_TALK; +import static emu.grasscutter.game.quest.enums.QuestContent.QUEST_CONTENT_COMPLETE_ANY_TALK; +import static emu.grasscutter.game.quest.enums.QuestContent.QUEST_CONTENT_COMPLETE_TALK; + public final class TalkManager extends BasePlayerManager { public TalkManager(@NonNull Player player) { super(player); @@ -19,12 +19,21 @@ public final class TalkManager extends BasePlayerManager { * Invoked when a talk is triggered. * * @param talkId The ID of the talk. + * @param npcEntityId The entity ID of the NPC being talked to. */ - public void triggerTalkAction(int talkId) { + public void triggerTalkAction(int talkId, int npcEntityId) { var talkData = GameData.getTalkConfigDataMap().get(talkId); if (talkData == null) return; var player = this.getPlayer(); + // Check if the NPC id is valid. + var entity = player.getScene().getEntityById(npcEntityId); + if (entity == null) return; + + // The config ID of the entity is the NPC's ID. + if (!talkData.getNpcId().contains(entity.getConfigId())) + return; + // Execute the talk action on associated handlers. talkData .getFinishExec() diff --git a/src/main/java/emu/grasscutter/server/packet/recv/HandlerNpcTalkReq.java b/src/main/java/emu/grasscutter/server/packet/recv/HandlerNpcTalkReq.java index 780a67a82..778098bd0 100644 --- a/src/main/java/emu/grasscutter/server/packet/recv/HandlerNpcTalkReq.java +++ b/src/main/java/emu/grasscutter/server/packet/recv/HandlerNpcTalkReq.java @@ -13,7 +13,7 @@ public class HandlerNpcTalkReq extends PacketHandler { public void handle(GameSession session, byte[] header, byte[] payload) throws Exception { var req = NpcTalkReq.parseFrom(payload); - session.getPlayer().getTalkManager().triggerTalkAction(req.getTalkId()); + session.getPlayer().getTalkManager().triggerTalkAction(req.getTalkId(), req.getEntityId()); session.send(new PacketNpcTalkRsp(req.getNpcEntityId(), req.getTalkId(), req.getEntityId())); } }