Archived
0

TeleportManager

This commit is contained in:
2018-06-23 16:15:09 +03:00
parent 9507914fd9
commit bdfa420830
5 changed files with 77 additions and 5 deletions

View File

@@ -12,6 +12,14 @@ import lombok.Data;
public class Location { public class Location {
private double x, y, z; private double x, y, z;
public static Location copyOf(Location location) {
return new Location(
location.x,
location.y,
location.z
);
}
public void set(Location location) { public void set(Location location) {
this.x = location.x; this.x = location.x;
this.y = location.y; this.y = location.y;

View File

@@ -0,0 +1,55 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2018-06-23
*/
package mc.core.network.proto_1_12_2;
import lombok.AllArgsConstructor;
import mc.core.Location;
import mc.core.player.Player;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class TeleportManager {
private static TeleportManager instance = new TeleportManager();
public static TeleportManager getInstance() {
return instance;
}
@AllArgsConstructor
private class TpData {
public Player player;
public Location newLocation;
// TODO необходимо добавить TimeStamp, что бы понимать, когда клиент отвергнул телепортацию
// т.е. идея такова: долгое молчание клиента знак отвержения телепортации.
}
private final Random RAND = new Random();
private final Map<Integer, TpData> teleportMap = new HashMap<>();
private TeleportManager() {}
public int append(Player player, Location location) {
int teleportId;
do {
teleportId = RAND.nextInt(9999);
} while (teleportMap.containsKey(teleportId));
teleportMap.put(teleportId, new TpData(player, Location.copyOf(location)));
return teleportId;
}
public void apply(int teleportId) {
if (teleportMap.containsKey(teleportId)) {
TpData data = teleportMap.remove(teleportId);
data.player.getLocation().set(data.newLocation);
}
}
public void removeDataPlayer(Player player) {
teleportMap.entrySet().removeIf(entry -> entry.getValue().player.equals(player));
}
}

View File

@@ -11,17 +11,16 @@ import mc.core.Location;
import mc.core.network.CSPacket; import mc.core.network.CSPacket;
import mc.core.network.NetStream; import mc.core.network.NetStream;
import mc.core.network.SCPacket; import mc.core.network.SCPacket;
import mc.core.network.proto_1_12_2.TeleportManager;
import mc.core.player.Look; import mc.core.player.Look;
import java.util.Random;
@NoArgsConstructor @NoArgsConstructor
@Getter @Getter
@Setter @Setter
public class PlayerPositionAndLookPacket implements SCPacket, CSPacket { public class PlayerPositionAndLookPacket implements SCPacket, CSPacket {
private static Random RANDOM = new Random();
private Location location; private Location location;
private Look look; private Look look;
private int teleportId;
private boolean onGround = false; private boolean onGround = false;
@Override @Override
@@ -38,8 +37,7 @@ public class PlayerPositionAndLookPacket implements SCPacket, CSPacket {
* Y_ROT - 0x08 * Y_ROT - 0x08
* X_ROT - 0x10 * X_ROT - 0x10
*/ */
//FIXME teleport id netStream.writeVarInt(teleportId); // Client should confirm this packet with Teleport Confirm containing the same Teleport ID
netStream.writeVarInt(RANDOM.nextInt()); // Client should confirm this packet with Teleport Confirm containing the same Teleport ID
} }
@Override @Override

View File

@@ -7,6 +7,7 @@ package mc.core.network.proto_1_12_2.netty.handlers;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelFutureListener;
import mc.core.network.proto_1_12_2.State; import mc.core.network.proto_1_12_2.State;
import mc.core.network.proto_1_12_2.TeleportManager;
import mc.core.network.proto_1_12_2.packets.*; import mc.core.network.proto_1_12_2.packets.*;
import mc.core.player.Look; import mc.core.player.Look;
import mc.core.player.Player; import mc.core.player.Player;
@@ -80,6 +81,7 @@ public class LoginHandler extends AbstractStateHandler implements LoginStateHand
PlayerPositionAndLookPacket pkt4 = new PlayerPositionAndLookPacket(); PlayerPositionAndLookPacket pkt4 = new PlayerPositionAndLookPacket();
pkt4.setLocation(player.getLocation()); pkt4.setLocation(player.getLocation());
pkt4.setLook(player.getLook()); pkt4.setLook(player.getLook());
pkt4.setTeleportId(TeleportManager.getInstance().append(player, player.getLocation()));
channel.writeAndFlush(pkt4); channel.writeAndFlush(pkt4);
} }
} }

View File

@@ -5,7 +5,9 @@
package mc.core.network.proto_1_12_2.netty.handlers; package mc.core.network.proto_1_12_2.netty.handlers;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import mc.core.network.proto_1_12_2.TeleportManager;
import mc.core.network.proto_1_12_2.packets.ClientSettingsPacket; import mc.core.network.proto_1_12_2.packets.ClientSettingsPacket;
import mc.core.network.proto_1_12_2.packets.TeleportConfirmPacket;
import mc.core.player.Player; import mc.core.player.Player;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@@ -13,6 +15,8 @@ import static mc.core.network.proto_1_12_2.netty.NettyServer.ATTR_PLAYER;
@Component @Component
public class PlayHandler extends AbstractStateHandler implements PlayStateHandler { public class PlayHandler extends AbstractStateHandler implements PlayStateHandler {
private TeleportManager teleport = TeleportManager.getInstance();
@Handler @Handler
public void onClientSettings(Channel channel, ClientSettingsPacket packet) { public void onClientSettings(Channel channel, ClientSettingsPacket packet) {
Player player = channel.attr(ATTR_PLAYER).get(); Player player = channel.attr(ATTR_PLAYER).get();
@@ -32,4 +36,9 @@ public class PlayHandler extends AbstractStateHandler implements PlayStateHandle
player.getSettings().setMainHand(packet.getMainHand()); player.getSettings().setMainHand(packet.getMainHand());
} }
@Handler
public void onTeleportConfirm(Channel channel, TeleportConfirmPacket packet) {
this.teleport.apply(packet.getTeleportId());
}
} }