diff --git a/src/main/java/emu/grasscutter/game/managers/MapMarkManager/MapMark.java b/src/main/java/emu/grasscutter/game/managers/MapMarkManager/MapMark.java index 2dcbf7972..5e8a6b2dd 100644 --- a/src/main/java/emu/grasscutter/game/managers/MapMarkManager/MapMark.java +++ b/src/main/java/emu/grasscutter/game/managers/MapMarkManager/MapMark.java @@ -1,29 +1,29 @@ package emu.grasscutter.game.managers.MapMarkManager; import dev.morphia.annotations.Entity; -import emu.grasscutter.net.proto.MapMarkFromTypeOuterClass; -import emu.grasscutter.net.proto.MapMarkPointOuterClass; -import emu.grasscutter.net.proto.MapMarkPointTypeOuterClass; +import emu.grasscutter.net.proto.MapMarkFromTypeOuterClass.MapMarkFromType; +import emu.grasscutter.net.proto.MapMarkPointOuterClass.MapMarkPoint; +import emu.grasscutter.net.proto.MapMarkPointTypeOuterClass.MapMarkPointType; import emu.grasscutter.utils.Position; @Entity public class MapMark { - private int sceneId; - private String name; - private Position position; - private MapMarkPointTypeOuterClass.MapMarkPointType pointType; - private int monsterId = 0; - private MapMarkFromTypeOuterClass.MapMarkFromType fromType; - private int questId = 7; + private final int sceneId; + private final String name; + private final Position position; + private final MapMarkPointType pointType; + private final int monsterId; + private final MapMarkFromType fromType; + private final int questId; - public MapMark(Position position, MapMarkPointTypeOuterClass.MapMarkPointType type) { - this.position = position; - } - - public MapMark(MapMarkPointOuterClass.MapMarkPoint mapMarkPoint) { + public MapMark(MapMarkPoint mapMarkPoint) { this.sceneId = mapMarkPoint.getSceneId(); this.name = mapMarkPoint.getName(); - this.position = new Position(mapMarkPoint.getPos().getX(), mapMarkPoint.getPos().getY(), mapMarkPoint.getPos().getZ()); + this.position = new Position( + mapMarkPoint.getPos().getX(), + mapMarkPoint.getPos().getY(), + mapMarkPoint.getPos().getZ() + ); this.pointType = mapMarkPoint.getPointType(); this.monsterId = mapMarkPoint.getMonsterId(); this.fromType = mapMarkPoint.getFromType(); @@ -33,41 +33,22 @@ public class MapMark { public int getSceneId() { return this.sceneId; } - public String getName() { return this.name; } - public Position getPosition() { return this.position; } - - public MapMarkPointTypeOuterClass.MapMarkPointType getMapMarkPointType() { + public MapMarkPointType getMapMarkPointType() { return this.pointType; } - - public void setMapMarkPointType(MapMarkPointTypeOuterClass.MapMarkPointType pointType) { - this.pointType = pointType; - } - public int getMonsterId() { return this.monsterId; } - - public void setMonsterId(int monsterId) { - this.monsterId = monsterId; - } - - public MapMarkFromTypeOuterClass.MapMarkFromType getMapMarkFromType() { + public MapMarkFromType getMapMarkFromType() { return this.fromType; } - public int getQuestId() { return this.questId; } - - public void setQuestId(int questId) { - this.questId = questId; - } - -} +} \ No newline at end of file diff --git a/src/main/java/emu/grasscutter/game/managers/MapMarkManager/MapMarksManager.java b/src/main/java/emu/grasscutter/game/managers/MapMarkManager/MapMarksManager.java index d014ce204..2751adf9a 100644 --- a/src/main/java/emu/grasscutter/game/managers/MapMarkManager/MapMarksManager.java +++ b/src/main/java/emu/grasscutter/game/managers/MapMarkManager/MapMarksManager.java @@ -1,61 +1,90 @@ package emu.grasscutter.game.managers.MapMarkManager; -import dev.morphia.annotations.Entity; +import emu.grasscutter.game.player.Player; +import emu.grasscutter.net.proto.MapMarkPointTypeOuterClass.MapMarkPointType; +import emu.grasscutter.net.proto.MarkMapReqOuterClass.MarkMapReq; +import emu.grasscutter.net.proto.MarkMapReqOuterClass.MarkMapReq.Operation; +import emu.grasscutter.server.packet.send.PacketMarkMapRsp; +import emu.grasscutter.server.packet.send.PacketSceneEntityAppearNotify; import emu.grasscutter.utils.Position; + import java.util.HashMap; -@Entity public class MapMarksManager { - - static final int mapMarkMaxCount = 150; + public static final int mapMarkMaxCount = 150; private HashMap mapMarks; + private final Player player; - public MapMarksManager() { - mapMarks = new HashMap(); + public MapMarksManager(Player player) { + this.player = player; + this.mapMarks = player.getMapMarks(); + if (this.mapMarks == null) { this.mapMarks = new HashMap<>(); } } - public MapMarksManager(HashMap mapMarks) { - this.mapMarks = mapMarks; - } - - public HashMap getAllMapMarks() { - return mapMarks; - } - - public MapMark getMapMark(Position position) { - String key = getMapMarkKey(position); - if (mapMarks.containsKey(key)) { - return mapMarks.get(key); - } else { - return null; + public void handleMapMarkReq(MarkMapReq req) { + Operation op = req.getOp(); + switch (op) { + case ADD -> { + MapMark createMark = new MapMark(req.getMark()); + // keep teleporting functionality on fishhook mark. + if (createMark.getMapMarkPointType() == MapMarkPointType.MAP_MARK_POINT_TYPE_FISH_POOL) { + teleport(player, createMark); + return; + } + addMapMark(createMark); + } + case MOD -> { + MapMark oldMark = new MapMark(req.getOld()); + removeMapMark(oldMark.getPosition()); + MapMark newMark = new MapMark(req.getMark()); + addMapMark(newMark); + } + case DEL -> { + MapMark deleteMark = new MapMark(req.getMark()); + removeMapMark(deleteMark.getPosition()); + } } + if (op != Operation.GET) { + saveMapMarks(); + } + player.getSession().send(new PacketMarkMapRsp(getMapMarks())); + } + + public HashMap getMapMarks() { + return mapMarks; } public String getMapMarkKey(Position position) { return "x" + (int)position.getX()+ "z" + (int)position.getZ(); } - public boolean removeMapMark(Position position) { - String key = getMapMarkKey(position); - if (mapMarks.containsKey(key)) { - mapMarks.remove(key); - return true; - } - return false; + public void removeMapMark(Position position) { + mapMarks.remove(getMapMarkKey(position)); } - public boolean addMapMark(MapMark mapMark) { + public void addMapMark(MapMark mapMark) { if (mapMarks.size() < mapMarkMaxCount) { - if (!mapMarks.containsKey(getMapMarkKey(mapMark.getPosition()))) { - mapMarks.put(getMapMarkKey(mapMark.getPosition()), mapMark); - return true; - } + mapMarks.put(getMapMarkKey(mapMark.getPosition()), mapMark); } - return false; } - public void setMapMarks(HashMap mapMarks) { - this.mapMarks = mapMarks; + private void saveMapMarks() { + player.setMapMarks(mapMarks); + player.save(); } + private void teleport(Player player, MapMark mapMark) { + float y; + try { + y = (float)Integer.parseInt(mapMark.getName()); + } catch (Exception e) { + y = 300; + } + Position pos = mapMark.getPosition(); + player.getPos().set(pos.getX(), y, pos.getZ()); + if (mapMark.getSceneId() != player.getSceneId()) { + player.getWorld().transferPlayerToScene(player, mapMark.getSceneId(), player.getPos()); + } + player.getScene().broadcastPacket(new PacketSceneEntityAppearNotify(player)); + } } diff --git a/src/main/java/emu/grasscutter/game/player/Player.java b/src/main/java/emu/grasscutter/game/player/Player.java index 0be6c7a54..f27d68a29 100644 --- a/src/main/java/emu/grasscutter/game/player/Player.java +++ b/src/main/java/emu/grasscutter/game/player/Player.java @@ -134,10 +134,11 @@ public class Player { @Transient private final InvokeHandler abilityInvokeHandler; @Transient private final InvokeHandler clientAbilityInitFinishHandler; - private MapMarksManager mapMarksManager; + @Transient private MapMarksManager mapMarksManager; @Transient private StaminaManager staminaManager; private long springLastUsed; + private HashMap mapMarks; @Deprecated @@ -181,7 +182,7 @@ public class Player { this.shopLimit = new ArrayList<>(); this.expeditionInfo = new HashMap<>(); this.messageHandler = null; - this.mapMarksManager = new MapMarksManager(); + this.mapMarksManager = new MapMarksManager(this); this.staminaManager = new StaminaManager(this); this.sotsManager = new SotSManager(this); } @@ -209,7 +210,7 @@ public class Player { this.getPos().set(GameConstants.START_POSITION); this.getRotation().set(0, 307, 0); this.messageHandler = null; - this.mapMarksManager = new MapMarksManager(); + this.mapMarksManager = new MapMarksManager(this); this.staminaManager = new StaminaManager(this); this.sotsManager = new SotSManager(this); } @@ -1044,6 +1045,10 @@ public class Player { return abilityManager; } + public HashMap getMapMarks() { return mapMarks; } + + public void setMapMarks(HashMap newMarks) { mapMarks = newMarks; } + public synchronized void onTick() { // Check ping if (this.getLastPingTime() > System.currentTimeMillis() + 60000) { diff --git a/src/main/java/emu/grasscutter/server/packet/recv/HandlerMarkMapReq.java b/src/main/java/emu/grasscutter/server/packet/recv/HandlerMarkMapReq.java index fc5d0a602..58348a2e0 100644 --- a/src/main/java/emu/grasscutter/server/packet/recv/HandlerMarkMapReq.java +++ b/src/main/java/emu/grasscutter/server/packet/recv/HandlerMarkMapReq.java @@ -1,84 +1,17 @@ package emu.grasscutter.server.packet.recv; -import emu.grasscutter.game.managers.MapMarkManager.MapMark; -import emu.grasscutter.game.managers.MapMarkManager.MapMarksManager; -import emu.grasscutter.game.player.Player; import emu.grasscutter.net.packet.Opcodes; import emu.grasscutter.net.packet.PacketHandler; import emu.grasscutter.net.packet.PacketOpcodes; -import emu.grasscutter.net.proto.*; import emu.grasscutter.net.proto.MarkMapReqOuterClass.MarkMapReq; import emu.grasscutter.server.game.GameSession; -import emu.grasscutter.server.packet.send.PacketMarkMapRsp; -import emu.grasscutter.server.packet.send.PacketMarkNewNotify; -import emu.grasscutter.server.packet.send.PacketSceneEntityAppearNotify; -import emu.grasscutter.utils.Position; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; @Opcodes(PacketOpcodes.MarkMapReq) public class HandlerMarkMapReq extends PacketHandler { - private static boolean isInt(String str) { - - try { - @SuppressWarnings("unused") - int x = Integer.parseInt(str); - return true; // String is an Integer - } catch (NumberFormatException e) { - return false; // String is not an Integer - } - - } - @Override public void handle(GameSession session, byte[] header, byte[] payload) throws Exception { MarkMapReq req = MarkMapReq.parseFrom(payload); - MarkMapReq.Operation op = req.getOp(); - Player player = session.getPlayer(); - MapMarksManager mapMarksManager = player.getMapMarksManager(); - if (op == MarkMapReq.Operation.ADD) { - MapMark newMapMark = new MapMark(req.getMark()); - // keep teleporting functionality on fishhook mark. - if (newMapMark.getMapMarkPointType() == MapMarkPointTypeOuterClass.MapMarkPointType.MAP_MARK_POINT_TYPE_FISH_POOL) { - teleport(player, newMapMark); - return; - } - if (mapMarksManager.addMapMark(newMapMark)) { - player.save(); - } - } else if (op == MarkMapReq.Operation.MOD) { - MapMark newMapMark = new MapMark(req.getMark()); - if (mapMarksManager.removeMapMark(newMapMark.getPosition())) { - if (mapMarksManager.addMapMark(newMapMark)) { - player.save(); - } - } - } else if (op == MarkMapReq.Operation.DEL) { - MapMark newMapMark = new MapMark(req.getMark()); - if (mapMarksManager.removeMapMark(newMapMark.getPosition())) { - player.save(); - } - } else if (op == MarkMapReq.Operation.GET) { - // no-op - } - // send all marks to refresh client map view. - HashMap mapMarks = mapMarksManager.getAllMapMarks(); - session.send(new PacketMarkMapRsp(player, mapMarks)); - } - - private void teleport(Player player, MapMark mapMark) { - float y = isInt(mapMark.getName()) ? Integer.parseInt(mapMark.getName()) : 300; - float x = mapMark.getPosition().getX(); - float z = mapMark.getPosition().getZ(); - player.getPos().set(x, y, z); - if (mapMark.getSceneId() != player.getSceneId()) { - player.getWorld().transferPlayerToScene(player, mapMark.getSceneId(), - player.getPos()); - } else { - player.getScene().broadcastPacket(new PacketSceneEntityAppearNotify(player)); - } + session.getPlayer().getMapMarksManager().handleMapMarkReq(req); } } diff --git a/src/main/java/emu/grasscutter/server/packet/send/PacketMarkMapRsp.java b/src/main/java/emu/grasscutter/server/packet/send/PacketMarkMapRsp.java index d7ee20d61..21ccbe74e 100644 --- a/src/main/java/emu/grasscutter/server/packet/send/PacketMarkMapRsp.java +++ b/src/main/java/emu/grasscutter/server/packet/send/PacketMarkMapRsp.java @@ -10,7 +10,7 @@ import java.util.*; public class PacketMarkMapRsp extends BasePacket { - public PacketMarkMapRsp(Player player, HashMap mapMarks) { + public PacketMarkMapRsp(HashMap mapMarks) { super(PacketOpcodes.MarkMapRsp); MarkMapRspOuterClass.MarkMapRsp.Builder proto = MarkMapRspOuterClass.MarkMapRsp.newBuilder();