From b99ca4e02f65ad019214dc8bf8fdcfdb75a2b869 Mon Sep 17 00:00:00 2001 From: Melledy <52122272+Melledy@users.noreply.github.com> Date: Wed, 20 Apr 2022 21:33:58 -0700 Subject: [PATCH] Refactor PacketSceneTransToPointRsp and add a null check when getting ScenePointEntries --- .../command/commands/ChangeSceneCommand.java | 10 +++++-- src/main/java/emu/grasscutter/game/World.java | 21 +------------- .../recv/HandlerSceneTransToPointReq.java | 19 +++++++++++- .../send/PacketSceneTransToPointRsp.java | 29 +++++++++---------- 4 files changed, 41 insertions(+), 38 deletions(-) diff --git a/src/main/java/emu/grasscutter/command/commands/ChangeSceneCommand.java b/src/main/java/emu/grasscutter/command/commands/ChangeSceneCommand.java index a5afe7bf7..eb2e998f2 100644 --- a/src/main/java/emu/grasscutter/command/commands/ChangeSceneCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/ChangeSceneCommand.java @@ -23,11 +23,17 @@ public final class ChangeSceneCommand implements CommandHandler { try { int sceneId = Integer.parseInt(args.get(0)); + + if (sceneId == sender.getSceneId()) { + CommandHandler.sendMessage(sender, "You are already in that scene"); + return; + } + boolean result = sender.getWorld().transferPlayerToScene(sender, sceneId, sender.getPos()); - CommandHandler.sendMessage(sender, "Changed to scene " + sceneId); + if (!result) { - CommandHandler.sendMessage(sender, "Scene does not exist or you are already in it"); + CommandHandler.sendMessage(sender, "Scene does not exist"); } } catch (Exception e) { CommandHandler.sendMessage(sender, "Usage: changescene "); diff --git a/src/main/java/emu/grasscutter/game/World.java b/src/main/java/emu/grasscutter/game/World.java index 4375d8e4f..f88d3bee4 100644 --- a/src/main/java/emu/grasscutter/game/World.java +++ b/src/main/java/emu/grasscutter/game/World.java @@ -206,28 +206,9 @@ public class World implements Iterable { public void deregisterScene(GenshinScene scene) { this.getScenes().remove(scene.getId()); } - - public boolean forceTransferPlayerToScene(GenshinPlayer player, int sceneId, Position pos) { - // Forces the client to reload the scene map to prevent the player from falling off the map. - if (GenshinData.getSceneDataMap().get(sceneId) == null) { - return false; - } - - if (player.getScene() != null) { - player.getScene().removePlayer(player); - } - - GenshinScene scene = this.getSceneById(sceneId); - scene.addPlayer(player); - player.getPos().set(pos); - - // Teleport packet - player.sendPacket(new PacketPlayerEnterSceneNotify(player, EnterType.EnterSelf, EnterReason.TransPoint, sceneId, pos)); - return true; - } public boolean transferPlayerToScene(GenshinPlayer player, int sceneId, Position pos) { - if (player.getScene().getId() == sceneId || GenshinData.getSceneDataMap().get(sceneId) == null) { + if (GenshinData.getSceneDataMap().get(sceneId) == null) { return false; } diff --git a/src/main/java/emu/grasscutter/server/packet/recv/HandlerSceneTransToPointReq.java b/src/main/java/emu/grasscutter/server/packet/recv/HandlerSceneTransToPointReq.java index 619a37b1e..f01f5980d 100644 --- a/src/main/java/emu/grasscutter/server/packet/recv/HandlerSceneTransToPointReq.java +++ b/src/main/java/emu/grasscutter/server/packet/recv/HandlerSceneTransToPointReq.java @@ -1,11 +1,15 @@ package emu.grasscutter.server.packet.recv; +import emu.grasscutter.data.GenshinData; +import emu.grasscutter.data.custom.ScenePointEntry; import emu.grasscutter.net.packet.Opcodes; import emu.grasscutter.net.packet.PacketOpcodes; import emu.grasscutter.net.proto.SceneTransToPointReqOuterClass.SceneTransToPointReq; +import emu.grasscutter.net.proto.SceneTransToPointRspOuterClass.SceneTransToPointRsp; import emu.grasscutter.net.packet.PacketHandler; import emu.grasscutter.server.game.GameSession; import emu.grasscutter.server.packet.send.PacketSceneTransToPointRsp; +import emu.grasscutter.utils.Position; @Opcodes(PacketOpcodes.SceneTransToPointReq) public class HandlerSceneTransToPointReq extends PacketHandler { @@ -13,7 +17,20 @@ public class HandlerSceneTransToPointReq extends PacketHandler { @Override public void handle(GameSession session, byte[] header, byte[] payload) throws Exception { SceneTransToPointReq req = SceneTransToPointReq.parseFrom(payload); - session.send(new PacketSceneTransToPointRsp(session.getPlayer(), req.getPointId(), req.getSceneId())); + + String code = req.getSceneId() + "_" + req.getPointId(); + ScenePointEntry scenePointEntry = GenshinData.getScenePointEntries().get(code); + + if (scenePointEntry != null) { + float x = scenePointEntry.getPointData().getTranPos().getX(); + float y = scenePointEntry.getPointData().getTranPos().getY(); + float z = scenePointEntry.getPointData().getTranPos().getZ(); + + session.getPlayer().getWorld().transferPlayerToScene(session.getPlayer(), req.getSceneId(), new Position(x, y, z)); + session.send(new PacketSceneTransToPointRsp(session.getPlayer(), req.getPointId(), req.getSceneId())); + } else { + session.send(new PacketSceneTransToPointRsp()); + } } } diff --git a/src/main/java/emu/grasscutter/server/packet/send/PacketSceneTransToPointRsp.java b/src/main/java/emu/grasscutter/server/packet/send/PacketSceneTransToPointRsp.java index 965c6aa6b..4795c5e9f 100644 --- a/src/main/java/emu/grasscutter/server/packet/send/PacketSceneTransToPointRsp.java +++ b/src/main/java/emu/grasscutter/server/packet/send/PacketSceneTransToPointRsp.java @@ -13,23 +13,22 @@ public class PacketSceneTransToPointRsp extends GenshinPacket { public PacketSceneTransToPointRsp(GenshinPlayer player, int pointId, int sceneId) { super(PacketOpcodes.SceneTransToPointRsp); - String code = sceneId + "_" + pointId; - ScenePointEntry scenePointEntry = GenshinData.getScenePointEntries().get(code); - - float x = scenePointEntry.getPointData().getTranPos().getX(); - float y = scenePointEntry.getPointData().getTranPos().getY(); - float z = scenePointEntry.getPointData().getTranPos().getZ(); - - player.getPos().set(new Position(x, y, z)); - - player.getWorld().forceTransferPlayerToScene(player, sceneId, player.getPos()); - SceneTransToPointRsp proto = SceneTransToPointRsp.newBuilder() - .setRetcode(0) - .setPointId(pointId) - .setSceneId(sceneId) - .build(); + .setRetcode(0) + .setPointId(pointId) + .setSceneId(sceneId) + .build(); + + this.setData(proto); + } + + public PacketSceneTransToPointRsp() { + super(PacketOpcodes.SceneTransToPointRsp); + SceneTransToPointRsp proto = SceneTransToPointRsp.newBuilder() + .setRetcode(1) // Internal server error + .build(); + this.setData(proto); } }