Commit f6b4016f authored by KingRainbow44's avatar KingRainbow44
Browse files

Add `PlayerTeleportEvent`

parent 02ca5cf2
......@@ -4,6 +4,7 @@ import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.game.player.Player;
import emu.grasscutter.server.event.player.PlayerTeleportEvent;
import emu.grasscutter.utils.Position;
import java.util.List;
......@@ -23,9 +24,14 @@ public final class TeleportAllCommand implements CommandHandler {
for (Player player : targetPlayer.getWorld().getPlayers()) {
if (player.equals(targetPlayer))
continue;
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"));
......
......@@ -4,6 +4,7 @@ import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.game.player.Player;
import emu.grasscutter.server.event.player.PlayerTeleportEvent;
import emu.grasscutter.utils.Position;
import java.util.List;
......@@ -54,7 +55,11 @@ public final class TeleportCommand implements CommandHandler {
}
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) {
CommandHandler.sendMessage(sender, translate(sender, "commands.teleport.exists_error"));
} else {
......
......@@ -4,6 +4,7 @@ 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.event.player.PlayerTeleportEvent;
import emu.grasscutter.server.packet.send.PacketMarkMapRsp;
import emu.grasscutter.server.packet.send.PacketSceneEntityAppearNotify;
import emu.grasscutter.utils.Position;
......@@ -74,17 +75,21 @@ public class MapMarksManager {
}
private void teleport(Player player, MapMark mapMark) {
float y;
try {
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());
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()) {
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
......@@ -7,6 +7,7 @@ import emu.grasscutter.net.packet.PacketOpcodes;
import emu.grasscutter.net.proto.SceneTransToPointReqOuterClass.SceneTransToPointReq;
import emu.grasscutter.net.proto.SceneTransToPointRspOuterClass.SceneTransToPointRsp;
import emu.grasscutter.net.packet.PacketHandler;
import emu.grasscutter.server.event.player.PlayerTeleportEvent;
import emu.grasscutter.server.game.GameSession;
import emu.grasscutter.server.packet.send.PacketSceneTransToPointRsp;
import emu.grasscutter.utils.Position;
......@@ -26,8 +27,14 @@ public class HandlerSceneTransToPointReq extends PacketHandler {
float y = scenePointEntry.getPointData().getTranPos().getY();
float z = scenePointEntry.getPointData().getTranPos().getZ();
PlayerTeleportEvent event = new PlayerTeleportEvent(session.getPlayer(), PlayerTeleportEvent.TeleportType.WAYPOINT,
session.getPlayer().getPos(), new Position(x, y, z));
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;
import emu.grasscutter.net.packet.PacketOpcodes;
import emu.grasscutter.net.proto.TryEnterHomeReqOuterClass;
import emu.grasscutter.scripts.data.SceneConfig;
import emu.grasscutter.server.event.player.PlayerTeleportEvent;
import emu.grasscutter.server.game.GameSession;
import emu.grasscutter.server.packet.send.PacketTryEnterHomeRsp;
import emu.grasscutter.utils.Position;
......@@ -38,13 +39,17 @@ public class HandlerTryEnterHomeReq extends PacketHandler {
Scene scene = session.getPlayer().getWorld().getSceneById(realmId);
Position pos = scene.getScriptManager().getConfig().born_pos;
PlayerTeleportEvent event = new PlayerTeleportEvent(session.getPlayer(), PlayerTeleportEvent.TeleportType.WAYPOINT,
session.getPlayer().getPos(), pos);
event.call();
if(!event.isCanceled()) {
session.getPlayer().getWorld().transferPlayerToScene(
session.getPlayer(),
realmId,
pos
realmId, event.getDestination()
);
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