Commit f6b4016f authored by KingRainbow44's avatar KingRainbow44
Browse files

Add `PlayerTeleportEvent`

parent 02ca5cf2
...@@ -4,6 +4,7 @@ import emu.grasscutter.Grasscutter; ...@@ -4,6 +4,7 @@ import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.Command; import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler; import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.game.player.Player; import emu.grasscutter.game.player.Player;
import emu.grasscutter.server.event.player.PlayerTeleportEvent;
import emu.grasscutter.utils.Position; import emu.grasscutter.utils.Position;
import java.util.List; import java.util.List;
...@@ -19,15 +20,20 @@ public final class TeleportAllCommand implements CommandHandler { ...@@ -19,15 +20,20 @@ public final class TeleportAllCommand implements CommandHandler {
CommandHandler.sendMessage(sender, translate(sender, "commands.teleportAll.error")); CommandHandler.sendMessage(sender, translate(sender, "commands.teleportAll.error"));
return; return;
} }
for (Player player : targetPlayer.getWorld().getPlayers()) { for (Player player : targetPlayer.getWorld().getPlayers()) {
if (player.equals(targetPlayer)) if (player.equals(targetPlayer))
continue; continue;
Position pos = targetPlayer.getPos(); Position pos = targetPlayer.getPos();
PlayerTeleportEvent event = new PlayerTeleportEvent(targetPlayer, PlayerTeleportEvent.TeleportType.COMMAND,
targetPlayer.getPos(), pos);
event.call();
player.getWorld().transferPlayerToScene(player, targetPlayer.getSceneId(), pos); if(!event.isCanceled())
player.getWorld().transferPlayerToScene(player, targetPlayer.getSceneId(), event.getDestination());
} }
CommandHandler.sendMessage(sender, translate(sender, "commands.teleportAll.success")); CommandHandler.sendMessage(sender, translate(sender, "commands.teleportAll.success"));
} }
} }
...@@ -4,6 +4,7 @@ import emu.grasscutter.Grasscutter; ...@@ -4,6 +4,7 @@ import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.Command; import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler; import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.game.player.Player; import emu.grasscutter.game.player.Player;
import emu.grasscutter.server.event.player.PlayerTeleportEvent;
import emu.grasscutter.utils.Position; import emu.grasscutter.utils.Position;
import java.util.List; import java.util.List;
...@@ -54,12 +55,16 @@ public final class TeleportCommand implements CommandHandler { ...@@ -54,12 +55,16 @@ public final class TeleportCommand implements CommandHandler {
} }
Position target_pos = new Position(x, y, z); Position target_pos = new Position(x, y, z);
boolean result = targetPlayer.getWorld().transferPlayerToScene(targetPlayer, sceneId, target_pos); PlayerTeleportEvent event = new PlayerTeleportEvent(targetPlayer, PlayerTeleportEvent.TeleportType.COMMAND,
targetPlayer.getPos(), target_pos);
event.call();
boolean result = !event.isCanceled() || targetPlayer.getWorld().transferPlayerToScene(targetPlayer, sceneId, event.getDestination());
if (!result) { if (!result) {
CommandHandler.sendMessage(sender, translate(sender, "commands.teleport.exists_error")); CommandHandler.sendMessage(sender, translate(sender, "commands.teleport.exists_error"));
} else { } else {
CommandHandler.sendMessage(sender, translate(sender, "commands.teleport.success", CommandHandler.sendMessage(sender, translate(sender, "commands.teleport.success",
targetPlayer.getNickname(), Float.toString(x), Float.toString(y), targetPlayer.getNickname(), Float.toString(x), Float.toString(y),
Float.toString(z), Integer.toString(sceneId)) Float.toString(z), Integer.toString(sceneId))
); );
} }
......
...@@ -4,6 +4,7 @@ import emu.grasscutter.game.player.Player; ...@@ -4,6 +4,7 @@ import emu.grasscutter.game.player.Player;
import emu.grasscutter.net.proto.MapMarkPointTypeOuterClass.MapMarkPointType; import emu.grasscutter.net.proto.MapMarkPointTypeOuterClass.MapMarkPointType;
import emu.grasscutter.net.proto.MarkMapReqOuterClass.MarkMapReq; import emu.grasscutter.net.proto.MarkMapReqOuterClass.MarkMapReq;
import emu.grasscutter.net.proto.MarkMapReqOuterClass.MarkMapReq.Operation; import emu.grasscutter.net.proto.MarkMapReqOuterClass.MarkMapReq.Operation;
import emu.grasscutter.server.event.player.PlayerTeleportEvent;
import emu.grasscutter.server.packet.send.PacketMarkMapRsp; import emu.grasscutter.server.packet.send.PacketMarkMapRsp;
import emu.grasscutter.server.packet.send.PacketSceneEntityAppearNotify; import emu.grasscutter.server.packet.send.PacketSceneEntityAppearNotify;
import emu.grasscutter.utils.Position; import emu.grasscutter.utils.Position;
...@@ -74,17 +75,21 @@ public class MapMarksManager { ...@@ -74,17 +75,21 @@ public class MapMarksManager {
} }
private void teleport(Player player, MapMark mapMark) { private void teleport(Player player, MapMark mapMark) {
float y; float y; try {
try {
y = (float)Integer.parseInt(mapMark.getName()); y = (float)Integer.parseInt(mapMark.getName());
} catch (Exception e) { } catch (Exception e) {
y = 300; y = 300;
} }
Position pos = mapMark.getPosition(); Position pos = mapMark.getPosition();
player.getPos().set(pos.getX(), y, pos.getZ()); PlayerTeleportEvent event = new PlayerTeleportEvent(player, PlayerTeleportEvent.TeleportType.MAP,
player.getPos(), new Position(pos.getX(), y, pos.getZ()));
event.call(); if(event.isCanceled()) return;
player.getPos().set(event.getDestination());
if (mapMark.getSceneId() != player.getSceneId()) { if (mapMark.getSceneId() != player.getSceneId()) {
player.getWorld().transferPlayerToScene(player, mapMark.getSceneId(), player.getPos()); player.getWorld().transferPlayerToScene(player, mapMark.getSceneId(), player.getPos());
} } player.getScene().broadcastPacket(new PacketSceneEntityAppearNotify(player));
player.getScene().broadcastPacket(new PacketSceneEntityAppearNotify(player));
} }
} }
package emu.grasscutter.server.event.player;
import emu.grasscutter.game.player.Player;
import emu.grasscutter.server.event.Cancellable;
import emu.grasscutter.server.event.types.PlayerEvent;
import emu.grasscutter.utils.Position;
public final class PlayerTeleportEvent extends PlayerEvent implements Cancellable {
private final TeleportType type;
private final Position from;
private Position to;
public PlayerTeleportEvent(Player player, TeleportType type, Position from, Position to) {
super(player);
this.type = type;
this.from = from;
this.to = to;
}
public TeleportType getTeleportType() {
return this.type;
}
public Position getSource() {
return this.from;
}
public Position getDestination() {
return this.to;
}
public void setDestination(Position to) {
this.to = to;
}
public enum TeleportType {
/**
* The player has asked to teleport to a waypoint.
*/
WAYPOINT,
/**
* The player has asked to teleport using the map.
*/
MAP,
/**
* The player has asked to teleport using the command.
*/
COMMAND
}
}
\ No newline at end of file
package emu.grasscutter.server.packet.recv; package emu.grasscutter.server.packet.recv;
import emu.grasscutter.data.GameData; import emu.grasscutter.data.GameData;
import emu.grasscutter.data.binout.ScenePointEntry; import emu.grasscutter.data.binout.ScenePointEntry;
import emu.grasscutter.net.packet.Opcodes; import emu.grasscutter.net.packet.Opcodes;
import emu.grasscutter.net.packet.PacketOpcodes; import emu.grasscutter.net.packet.PacketOpcodes;
import emu.grasscutter.net.proto.SceneTransToPointReqOuterClass.SceneTransToPointReq; import emu.grasscutter.net.proto.SceneTransToPointReqOuterClass.SceneTransToPointReq;
import emu.grasscutter.net.proto.SceneTransToPointRspOuterClass.SceneTransToPointRsp; import emu.grasscutter.net.proto.SceneTransToPointRspOuterClass.SceneTransToPointRsp;
import emu.grasscutter.net.packet.PacketHandler; import emu.grasscutter.net.packet.PacketHandler;
import emu.grasscutter.server.game.GameSession; import emu.grasscutter.server.event.player.PlayerTeleportEvent;
import emu.grasscutter.server.packet.send.PacketSceneTransToPointRsp; import emu.grasscutter.server.game.GameSession;
import emu.grasscutter.utils.Position; import emu.grasscutter.server.packet.send.PacketSceneTransToPointRsp;
import emu.grasscutter.utils.Position;
@Opcodes(PacketOpcodes.SceneTransToPointReq)
public class HandlerSceneTransToPointReq extends PacketHandler { @Opcodes(PacketOpcodes.SceneTransToPointReq)
public class HandlerSceneTransToPointReq extends PacketHandler {
@Override
public void handle(GameSession session, byte[] header, byte[] payload) throws Exception { @Override
SceneTransToPointReq req = SceneTransToPointReq.parseFrom(payload); public void handle(GameSession session, byte[] header, byte[] payload) throws Exception {
SceneTransToPointReq req = SceneTransToPointReq.parseFrom(payload);
String code = req.getSceneId() + "_" + req.getPointId();
ScenePointEntry scenePointEntry = GameData.getScenePointEntries().get(code); String code = req.getSceneId() + "_" + req.getPointId();
ScenePointEntry scenePointEntry = GameData.getScenePointEntries().get(code);
if (scenePointEntry != null) {
float x = scenePointEntry.getPointData().getTranPos().getX(); if (scenePointEntry != null) {
float y = scenePointEntry.getPointData().getTranPos().getY(); float x = scenePointEntry.getPointData().getTranPos().getX();
float z = scenePointEntry.getPointData().getTranPos().getZ(); 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())); PlayerTeleportEvent event = new PlayerTeleportEvent(session.getPlayer(), PlayerTeleportEvent.TeleportType.WAYPOINT,
} else { session.getPlayer().getPos(), new Position(x, y, z));
session.send(new PacketSceneTransToPointRsp()); event.call();
}
} if(!event.isCanceled()) {
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());
}
}
}
...@@ -9,6 +9,7 @@ import emu.grasscutter.net.packet.PacketHandler; ...@@ -9,6 +9,7 @@ import emu.grasscutter.net.packet.PacketHandler;
import emu.grasscutter.net.packet.PacketOpcodes; import emu.grasscutter.net.packet.PacketOpcodes;
import emu.grasscutter.net.proto.TryEnterHomeReqOuterClass; import emu.grasscutter.net.proto.TryEnterHomeReqOuterClass;
import emu.grasscutter.scripts.data.SceneConfig; import emu.grasscutter.scripts.data.SceneConfig;
import emu.grasscutter.server.event.player.PlayerTeleportEvent;
import emu.grasscutter.server.game.GameSession; import emu.grasscutter.server.game.GameSession;
import emu.grasscutter.server.packet.send.PacketTryEnterHomeRsp; import emu.grasscutter.server.packet.send.PacketTryEnterHomeRsp;
import emu.grasscutter.utils.Position; import emu.grasscutter.utils.Position;
...@@ -38,13 +39,17 @@ public class HandlerTryEnterHomeReq extends PacketHandler { ...@@ -38,13 +39,17 @@ public class HandlerTryEnterHomeReq extends PacketHandler {
Scene scene = session.getPlayer().getWorld().getSceneById(realmId); Scene scene = session.getPlayer().getWorld().getSceneById(realmId);
Position pos = scene.getScriptManager().getConfig().born_pos; Position pos = scene.getScriptManager().getConfig().born_pos;
session.getPlayer().getWorld().transferPlayerToScene( PlayerTeleportEvent event = new PlayerTeleportEvent(session.getPlayer(), PlayerTeleportEvent.TeleportType.WAYPOINT,
session.getPlayer(), session.getPlayer().getPos(), pos);
realmId, event.call();
pos
);
if(!event.isCanceled()) {
session.getPlayer().getWorld().transferPlayerToScene(
session.getPlayer(),
realmId, event.getDestination()
);
session.send(new PacketTryEnterHomeRsp(req.getTargetUid())); session.send(new PacketTryEnterHomeRsp(req.getTargetUid()));
}
} }
} }
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