Commit 8c71af26 authored by gentlespoon's avatar gentlespoon Committed by Melledy
Browse files

Clean up map mark related code

1. Remove unnecessary manager from player collection.
2. Clean up MarkMapReq handler.
3. Remove redundant checks.
parent 8c32438b
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;
public MapMark(Position position, MapMarkPointTypeOuterClass.MapMarkPointType type) {
this.position = position;
}
public MapMark(MapMarkPointOuterClass.MapMarkPoint mapMarkPoint) {
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(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
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<String, MapMark> mapMarks;
private final Player player;
public MapMarksManager() {
mapMarks = new HashMap<String, MapMark>();
public MapMarksManager(Player player) {
this.player = player;
this.mapMarks = player.getMapMarks();
if (this.mapMarks == null) { this.mapMarks = new HashMap<>(); }
}
public MapMarksManager(HashMap<String, MapMark> mapMarks) {
this.mapMarks = mapMarks;
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;
}
public HashMap<String, MapMark> getAllMapMarks() {
return mapMarks;
addMapMark(createMark);
}
public MapMark getMapMark(Position position) {
String key = getMapMarkKey(position);
if (mapMarks.containsKey(key)) {
return mapMarks.get(key);
} else {
return null;
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<String, MapMark> 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;
}
}
return false;
}
public void setMapMarks(HashMap<String, MapMark> 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));
}
}
......@@ -134,10 +134,11 @@ public class Player {
@Transient private final InvokeHandler<AbilityInvokeEntry> abilityInvokeHandler;
@Transient private final InvokeHandler<AbilityInvokeEntry> clientAbilityInitFinishHandler;
private MapMarksManager mapMarksManager;
@Transient private MapMarksManager mapMarksManager;
@Transient private StaminaManager staminaManager;
private long springLastUsed;
private HashMap<String, MapMark> 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<String, MapMark> getMapMarks() { return mapMarks; }
public void setMapMarks(HashMap<String, MapMark> newMarks) { mapMarks = newMarks; }
public synchronized void onTick() {
// Check ping
if (this.getLastPingTime() > System.currentTimeMillis() + 60000) {
......
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<String, MapMark> 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);
}
}
......@@ -10,7 +10,7 @@ import java.util.*;
public class PacketMarkMapRsp extends BasePacket {
public PacketMarkMapRsp(Player player, HashMap<String, MapMark> mapMarks) {
public PacketMarkMapRsp(HashMap<String, MapMark> mapMarks) {
super(PacketOpcodes.MarkMapRsp);
MarkMapRspOuterClass.MarkMapRsp.Builder proto = MarkMapRspOuterClass.MarkMapRsp.newBuilder();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment