From 6f43c0765fe0a8eb01aba30c26d707fe3b1a7a2c Mon Sep 17 00:00:00 2001 From: Yazawazi <47273265+Yazawazi@users.noreply.github.com> Date: Thu, 21 Apr 2022 04:45:38 +0800 Subject: [PATCH 1/3] teleports --- .../emu/grasscutter/data/GenshinData.java | 6 +++ .../emu/grasscutter/data/ResourceLoader.java | 44 +++++++++++++++++++ .../grasscutter/data/common/PointData.java | 43 ++++++++++++++++++ .../data/common/ScenePointConfig.java | 15 +++++++ .../data/custom/ScenePointEntry.java | 21 +++++++++ src/main/java/emu/grasscutter/game/World.java | 19 ++++++++ .../recv/HandlerSceneTransToPointReq.java | 18 ++++++++ .../send/PacketSceneTransToPointRsp.java | 35 +++++++++++++++ 8 files changed, 201 insertions(+) create mode 100644 src/main/java/emu/grasscutter/data/common/PointData.java create mode 100644 src/main/java/emu/grasscutter/data/common/ScenePointConfig.java create mode 100644 src/main/java/emu/grasscutter/data/custom/ScenePointEntry.java create mode 100644 src/main/java/emu/grasscutter/server/packet/recv/HandlerSceneTransToPointReq.java create mode 100644 src/main/java/emu/grasscutter/server/packet/send/PacketSceneTransToPointRsp.java diff --git a/src/main/java/emu/grasscutter/data/GenshinData.java b/src/main/java/emu/grasscutter/data/GenshinData.java index bb2709108..7dc5a5f7c 100644 --- a/src/main/java/emu/grasscutter/data/GenshinData.java +++ b/src/main/java/emu/grasscutter/data/GenshinData.java @@ -8,6 +8,7 @@ import emu.grasscutter.Grasscutter; import emu.grasscutter.utils.Utils; import emu.grasscutter.data.custom.AbilityEmbryoEntry; import emu.grasscutter.data.custom.OpenConfigEntry; +import emu.grasscutter.data.custom.ScenePointEntry; import emu.grasscutter.data.def.*; import it.unimi.dsi.fastutil.ints.Int2ObjectLinkedOpenHashMap; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; @@ -18,6 +19,7 @@ public class GenshinData { private static final Int2ObjectMap abilityHashes = new Int2ObjectOpenHashMap<>(); private static final Map abilityEmbryos = new HashMap<>(); private static final Map openConfigEntries = new HashMap<>(); + private static final Map scenePointEntries = new HashMap<>(); // ExcelConfigs private static final Int2ObjectMap playerLevelDataMap = new Int2ObjectOpenHashMap<>(); @@ -82,6 +84,10 @@ public class GenshinData { return openConfigEntries; } + public static Map getScenePointEntries() { + return scenePointEntries; + } + public static Int2ObjectMap getAvatarDataMap() { return avatarDataMap; } diff --git a/src/main/java/emu/grasscutter/data/ResourceLoader.java b/src/main/java/emu/grasscutter/data/ResourceLoader.java index f50067c65..e597257ce 100644 --- a/src/main/java/emu/grasscutter/data/ResourceLoader.java +++ b/src/main/java/emu/grasscutter/data/ResourceLoader.java @@ -10,11 +10,15 @@ import java.util.regex.Pattern; import emu.grasscutter.utils.Utils; import org.reflections.Reflections; +import com.google.gson.JsonElement; import com.google.gson.reflect.TypeToken; import emu.grasscutter.Grasscutter; +import emu.grasscutter.data.common.PointData; +import emu.grasscutter.data.common.ScenePointConfig; import emu.grasscutter.data.custom.AbilityEmbryoEntry; import emu.grasscutter.data.custom.OpenConfigEntry; +import emu.grasscutter.data.custom.ScenePointEntry; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; public class ResourceLoader { @@ -42,6 +46,7 @@ public class ResourceLoader { loadOpenConfig(); // Load resources loadResources(); + loadScenePoints(); // Process into depots GenshinDepot.load(); // Custom - TODO move this somewhere else @@ -121,6 +126,45 @@ public class ResourceLoader { } } + private static void loadScenePoints() { + Pattern pattern = Pattern.compile("(?<=scene)(.*?)(?=_point.json)"); + File folder = new File(Grasscutter.getConfig().RESOURCE_FOLDER + "BinOutPut/Scene/Point"); + List scenePointList = new ArrayList<>(); + for (File file : folder.listFiles()) { + ScenePointConfig config = null; + Integer sceneId = null; + + Matcher matcher = pattern.matcher(file.getName()); + if (matcher.find()) { + sceneId = Integer.parseInt(matcher.group(1)); + } else { + continue; + } + + try (FileReader fileReader = new FileReader(file)) { + config = Grasscutter.getGsonFactory().fromJson(fileReader, ScenePointConfig.class); + } catch (Exception e) { + e.printStackTrace(); + continue; + } + + if (config.points == null) { + continue; + } + + for (Map.Entry entry : config.points.entrySet()) { + PointData pointData = Grasscutter.getGsonFactory().fromJson(entry.getValue(), PointData.class); + + ScenePointEntry sl = new ScenePointEntry(sceneId + "_" + entry.getKey(), pointData); + scenePointList.add(sl); + } + + for (ScenePointEntry entry : scenePointList) { + GenshinData.getScenePointEntries().put(entry.getName(), entry); + } + } + } + private static void loadAbilityEmbryos() { // Read from cached file if exists File embryoCache = new File(Grasscutter.getConfig().DATA_FOLDER + "AbilityEmbryos.json"); diff --git a/src/main/java/emu/grasscutter/data/common/PointData.java b/src/main/java/emu/grasscutter/data/common/PointData.java new file mode 100644 index 000000000..7c31d2f06 --- /dev/null +++ b/src/main/java/emu/grasscutter/data/common/PointData.java @@ -0,0 +1,43 @@ +package emu.grasscutter.data.common; + +public class PointData { + private pos tranPos; + + public pos getTranPos() { + return tranPos; + } + + public void setTranPos(pos tranPos) { + this.tranPos = tranPos; + } + + public class pos { + private float x; + private float y; + private float z; + + public float getX() { + return x; + } + + public void setX(float x) { + this.x = x; + } + + public float getY() { + return y; + } + + public void setY(float y) { + this.y = y; + } + + public float getZ() { + return z; + } + + public void setZ(float z) { + this.z = z; + } + } +} diff --git a/src/main/java/emu/grasscutter/data/common/ScenePointConfig.java b/src/main/java/emu/grasscutter/data/common/ScenePointConfig.java new file mode 100644 index 000000000..340f7476f --- /dev/null +++ b/src/main/java/emu/grasscutter/data/common/ScenePointConfig.java @@ -0,0 +1,15 @@ +package emu.grasscutter.data.common; + +import com.google.gson.JsonObject; + +public class ScenePointConfig { + public JsonObject points; + + public JsonObject getPoints() { + return points; + } + + public void setPoints(JsonObject Points) { + points = Points; + } +} diff --git a/src/main/java/emu/grasscutter/data/custom/ScenePointEntry.java b/src/main/java/emu/grasscutter/data/custom/ScenePointEntry.java new file mode 100644 index 000000000..3e904f5c8 --- /dev/null +++ b/src/main/java/emu/grasscutter/data/custom/ScenePointEntry.java @@ -0,0 +1,21 @@ +package emu.grasscutter.data.custom; + +import emu.grasscutter.data.common.PointData; + +public class ScenePointEntry { + private String name; + private PointData pointData; + + public ScenePointEntry(String name, PointData pointData) { + this.name = name; + this.pointData = pointData; + } + + public String getName() { + return name; + } + + public PointData getPointData() { + return pointData; + } +} diff --git a/src/main/java/emu/grasscutter/game/World.java b/src/main/java/emu/grasscutter/game/World.java index 02c0df63a..4375d8e4f 100644 --- a/src/main/java/emu/grasscutter/game/World.java +++ b/src/main/java/emu/grasscutter/game/World.java @@ -206,6 +206,25 @@ 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) { diff --git a/src/main/java/emu/grasscutter/server/packet/recv/HandlerSceneTransToPointReq.java b/src/main/java/emu/grasscutter/server/packet/recv/HandlerSceneTransToPointReq.java new file mode 100644 index 000000000..6e38fb282 --- /dev/null +++ b/src/main/java/emu/grasscutter/server/packet/recv/HandlerSceneTransToPointReq.java @@ -0,0 +1,18 @@ +package emu.grasscutter.server.packet.recv; + +import emu.grasscutter.net.packet.Opcodes; +import emu.grasscutter.net.packet.PacketOpcodes; +import emu.grasscutter.net.proto.SceneTransToPointReqOuterClass.SceneTransToPointReq; +import emu.grasscutter.net.packet.PacketHandler; +import emu.grasscutter.server.game.GameSession; +import emu.grasscutter.server.packet.send.PacketSceneTransToPointRsp; + +@Opcodes(PacketOpcodes.SceneTransToPointReq) +public class HandlerSceneTransToPointReq extends PacketHandler { + + @Override + public void handle(GameSession session, byte[] header, byte[] payload) throws Exception { + SceneTransToPointReq req = SceneTransToPointReq.parseFrom(payload); + } + +} diff --git a/src/main/java/emu/grasscutter/server/packet/send/PacketSceneTransToPointRsp.java b/src/main/java/emu/grasscutter/server/packet/send/PacketSceneTransToPointRsp.java new file mode 100644 index 000000000..965c6aa6b --- /dev/null +++ b/src/main/java/emu/grasscutter/server/packet/send/PacketSceneTransToPointRsp.java @@ -0,0 +1,35 @@ +package emu.grasscutter.server.packet.send; + +import emu.grasscutter.data.GenshinData; +import emu.grasscutter.data.custom.ScenePointEntry; +import emu.grasscutter.game.GenshinPlayer; +import emu.grasscutter.net.packet.GenshinPacket; +import emu.grasscutter.net.packet.PacketOpcodes; +import emu.grasscutter.net.proto.SceneTransToPointRspOuterClass.SceneTransToPointRsp; +import emu.grasscutter.utils.Position; + +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(); + + this.setData(proto); + } +} From ec438dc16ed561b7af42d0a3c379734ed5860ccd Mon Sep 17 00:00:00 2001 From: Yazawazi <47273265+Yazawazi@users.noreply.github.com> Date: Thu, 21 Apr 2022 04:59:42 +0800 Subject: [PATCH 3/3] session send sorry --- .../server/packet/recv/HandlerSceneTransToPointReq.java | 1 + 1 file changed, 1 insertion(+) 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 6e38fb282..619a37b1e 100644 --- a/src/main/java/emu/grasscutter/server/packet/recv/HandlerSceneTransToPointReq.java +++ b/src/main/java/emu/grasscutter/server/packet/recv/HandlerSceneTransToPointReq.java @@ -13,6 +13,7 @@ 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())); } }