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; package emu.grasscutter.game.managers.MapMarkManager;
import dev.morphia.annotations.Entity; import dev.morphia.annotations.Entity;
import emu.grasscutter.net.proto.MapMarkFromTypeOuterClass; import emu.grasscutter.net.proto.MapMarkFromTypeOuterClass.MapMarkFromType;
import emu.grasscutter.net.proto.MapMarkPointOuterClass; import emu.grasscutter.net.proto.MapMarkPointOuterClass.MapMarkPoint;
import emu.grasscutter.net.proto.MapMarkPointTypeOuterClass; import emu.grasscutter.net.proto.MapMarkPointTypeOuterClass.MapMarkPointType;
import emu.grasscutter.utils.Position; import emu.grasscutter.utils.Position;
@Entity @Entity
public class MapMark { public class MapMark {
private int sceneId; private final int sceneId;
private String name; private final String name;
private Position position; private final Position position;
private MapMarkPointTypeOuterClass.MapMarkPointType pointType; private final MapMarkPointType pointType;
private int monsterId = 0; private final int monsterId;
private MapMarkFromTypeOuterClass.MapMarkFromType fromType; private final MapMarkFromType fromType;
private int questId = 7; private final int questId;
public MapMark(Position position, MapMarkPointTypeOuterClass.MapMarkPointType type) { public MapMark(MapMarkPoint mapMarkPoint) {
this.position = position;
}
public MapMark(MapMarkPointOuterClass.MapMarkPoint mapMarkPoint) {
this.sceneId = mapMarkPoint.getSceneId(); this.sceneId = mapMarkPoint.getSceneId();
this.name = mapMarkPoint.getName(); 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.pointType = mapMarkPoint.getPointType();
this.monsterId = mapMarkPoint.getMonsterId(); this.monsterId = mapMarkPoint.getMonsterId();
this.fromType = mapMarkPoint.getFromType(); this.fromType = mapMarkPoint.getFromType();
...@@ -33,41 +33,22 @@ public class MapMark { ...@@ -33,41 +33,22 @@ public class MapMark {
public int getSceneId() { public int getSceneId() {
return this.sceneId; return this.sceneId;
} }
public String getName() { public String getName() {
return this.name; return this.name;
} }
public Position getPosition() { public Position getPosition() {
return this.position; return this.position;
} }
public MapMarkPointType getMapMarkPointType() {
public MapMarkPointTypeOuterClass.MapMarkPointType getMapMarkPointType() {
return this.pointType; return this.pointType;
} }
public void setMapMarkPointType(MapMarkPointTypeOuterClass.MapMarkPointType pointType) {
this.pointType = pointType;
}
public int getMonsterId() { public int getMonsterId() {
return this.monsterId; return this.monsterId;
} }
public MapMarkFromType getMapMarkFromType() {
public void setMonsterId(int monsterId) {
this.monsterId = monsterId;
}
public MapMarkFromTypeOuterClass.MapMarkFromType getMapMarkFromType() {
return this.fromType; return this.fromType;
} }
public int getQuestId() { public int getQuestId() {
return this.questId; return this.questId;
} }
public void setQuestId(int questId) {
this.questId = questId;
}
} }
\ No newline at end of file
package emu.grasscutter.game.managers.MapMarkManager; 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 emu.grasscutter.utils.Position;
import java.util.HashMap; import java.util.HashMap;
@Entity
public class MapMarksManager { public class MapMarksManager {
public static final int mapMarkMaxCount = 150;
static final int mapMarkMaxCount = 150;
private HashMap<String, MapMark> mapMarks; private HashMap<String, MapMark> mapMarks;
private final Player player;
public MapMarksManager() { public MapMarksManager(Player player) {
mapMarks = new HashMap<String, MapMark>(); this.player = player;
this.mapMarks = player.getMapMarks();
if (this.mapMarks == null) { this.mapMarks = new HashMap<>(); }
} }
public MapMarksManager(HashMap<String, MapMark> mapMarks) { public void handleMapMarkReq(MarkMapReq req) {
this.mapMarks = mapMarks; 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);
public HashMap<String, MapMark> getAllMapMarks() {
return mapMarks;
} }
case MOD -> {
public MapMark getMapMark(Position position) { MapMark oldMark = new MapMark(req.getOld());
String key = getMapMarkKey(position); removeMapMark(oldMark.getPosition());
if (mapMarks.containsKey(key)) { MapMark newMark = new MapMark(req.getMark());
return mapMarks.get(key); addMapMark(newMark);
} else { }
return null; 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) { public String getMapMarkKey(Position position) {
return "x" + (int)position.getX()+ "z" + (int)position.getZ(); return "x" + (int)position.getX()+ "z" + (int)position.getZ();
} }
public boolean removeMapMark(Position position) { public void removeMapMark(Position position) {
String key = getMapMarkKey(position); mapMarks.remove(getMapMarkKey(position));
if (mapMarks.containsKey(key)) {
mapMarks.remove(key);
return true;
}
return false;
} }
public boolean addMapMark(MapMark mapMark) { public void addMapMark(MapMark mapMark) {
if (mapMarks.size() < mapMarkMaxCount) { if (mapMarks.size() < mapMarkMaxCount) {
if (!mapMarks.containsKey(getMapMarkKey(mapMark.getPosition()))) {
mapMarks.put(getMapMarkKey(mapMark.getPosition()), mapMark); mapMarks.put(getMapMarkKey(mapMark.getPosition()), mapMark);
return true;
} }
} }
return false;
}
public void setMapMarks(HashMap<String, MapMark> mapMarks) { private void saveMapMarks() {
this.mapMarks = mapMarks; 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 { ...@@ -134,10 +134,11 @@ public class Player {
@Transient private final InvokeHandler<AbilityInvokeEntry> abilityInvokeHandler; @Transient private final InvokeHandler<AbilityInvokeEntry> abilityInvokeHandler;
@Transient private final InvokeHandler<AbilityInvokeEntry> clientAbilityInitFinishHandler; @Transient private final InvokeHandler<AbilityInvokeEntry> clientAbilityInitFinishHandler;
private MapMarksManager mapMarksManager; @Transient private MapMarksManager mapMarksManager;
@Transient private StaminaManager staminaManager; @Transient private StaminaManager staminaManager;
private long springLastUsed; private long springLastUsed;
private HashMap<String, MapMark> mapMarks;
@Deprecated @Deprecated
...@@ -181,7 +182,7 @@ public class Player { ...@@ -181,7 +182,7 @@ public class Player {
this.shopLimit = new ArrayList<>(); this.shopLimit = new ArrayList<>();
this.expeditionInfo = new HashMap<>(); this.expeditionInfo = new HashMap<>();
this.messageHandler = null; this.messageHandler = null;
this.mapMarksManager = new MapMarksManager(); this.mapMarksManager = new MapMarksManager(this);
this.staminaManager = new StaminaManager(this); this.staminaManager = new StaminaManager(this);
this.sotsManager = new SotSManager(this); this.sotsManager = new SotSManager(this);
} }
...@@ -209,7 +210,7 @@ public class Player { ...@@ -209,7 +210,7 @@ public class Player {
this.getPos().set(GameConstants.START_POSITION); this.getPos().set(GameConstants.START_POSITION);
this.getRotation().set(0, 307, 0); this.getRotation().set(0, 307, 0);
this.messageHandler = null; this.messageHandler = null;
this.mapMarksManager = new MapMarksManager(); this.mapMarksManager = new MapMarksManager(this);
this.staminaManager = new StaminaManager(this); this.staminaManager = new StaminaManager(this);
this.sotsManager = new SotSManager(this); this.sotsManager = new SotSManager(this);
} }
...@@ -1044,6 +1045,10 @@ public class Player { ...@@ -1044,6 +1045,10 @@ public class Player {
return abilityManager; return abilityManager;
} }
public HashMap<String, MapMark> getMapMarks() { return mapMarks; }
public void setMapMarks(HashMap<String, MapMark> newMarks) { mapMarks = newMarks; }
public synchronized void onTick() { public synchronized void onTick() {
// Check ping // Check ping
if (this.getLastPingTime() > System.currentTimeMillis() + 60000) { if (this.getLastPingTime() > System.currentTimeMillis() + 60000) {
......
package emu.grasscutter.server.packet.recv; 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.Opcodes;
import emu.grasscutter.net.packet.PacketHandler; import emu.grasscutter.net.packet.PacketHandler;
import emu.grasscutter.net.packet.PacketOpcodes; import emu.grasscutter.net.packet.PacketOpcodes;
import emu.grasscutter.net.proto.*;
import emu.grasscutter.net.proto.MarkMapReqOuterClass.MarkMapReq; import emu.grasscutter.net.proto.MarkMapReqOuterClass.MarkMapReq;
import emu.grasscutter.server.game.GameSession; 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) @Opcodes(PacketOpcodes.MarkMapReq)
public class HandlerMarkMapReq extends PacketHandler { 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 @Override
public void handle(GameSession session, byte[] header, byte[] payload) throws Exception { public void handle(GameSession session, byte[] header, byte[] payload) throws Exception {
MarkMapReq req = MarkMapReq.parseFrom(payload); MarkMapReq req = MarkMapReq.parseFrom(payload);
MarkMapReq.Operation op = req.getOp(); session.getPlayer().getMapMarksManager().handleMapMarkReq(req);
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));
}
} }
} }
...@@ -10,7 +10,7 @@ import java.util.*; ...@@ -10,7 +10,7 @@ import java.util.*;
public class PacketMarkMapRsp extends BasePacket { public class PacketMarkMapRsp extends BasePacket {
public PacketMarkMapRsp(Player player, HashMap<String, MapMark> mapMarks) { public PacketMarkMapRsp(HashMap<String, MapMark> mapMarks) {
super(PacketOpcodes.MarkMapRsp); super(PacketOpcodes.MarkMapRsp);
MarkMapRspOuterClass.MarkMapRsp.Builder proto = MarkMapRspOuterClass.MarkMapRsp.newBuilder(); 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