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