Prevent from going on unloaded chunks

This commit is contained in:
TheMode 2019-08-11 09:33:27 +02:00
parent c8ee6858bc
commit 87345f70ab
5 changed files with 35 additions and 12 deletions

View File

@ -1,7 +1,9 @@
package fr.themode.minestom.entity;
import fr.themode.minestom.Main;
import fr.themode.minestom.instance.Chunk;
import fr.themode.minestom.net.packet.server.play.EntityTeleportPacket;
import fr.themode.minestom.net.packet.server.play.PlayerPositionAndLookPacket;
import fr.themode.minestom.net.packet.server.play.UpdateViewPositionPacket;
import fr.themode.minestom.net.player.PlayerConnection;
@ -42,6 +44,23 @@ public class Player extends LivingEntity {
playerConnection.sendPacket(new UpdateViewPositionPacket(Math.floorDiv((int) x, 16), Math.floorDiv((int) z, 16)));
}
public boolean chunkTest(double x, double z) {
Chunk newChunk = getInstance().getChunk((int) Math.floor(x / 16), (int) Math.floor(z / 16));
if (newChunk == null) {
PlayerPositionAndLookPacket positionAndLookPacket = new PlayerPositionAndLookPacket();
positionAndLookPacket.x = getX();
positionAndLookPacket.y = getY();
positionAndLookPacket.z = getZ();
positionAndLookPacket.yaw = getYaw();
positionAndLookPacket.pitch = getPitch();
positionAndLookPacket.flags = 0x00;
positionAndLookPacket.teleportId = 67;
getPlayerConnection().sendPacket(positionAndLookPacket);
return true;
}
return false;
}
public String getUsername() {
return username;
}

View File

@ -8,15 +8,16 @@ import fr.themode.minestom.utils.GroupedCollections;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
public class Instance {
private int id;
private UUID uniqueId;
private Set<Chunk> chunksSet = Collections.synchronizedSet(new HashSet<>());
public Instance(int id) {
this.id = id;
public Instance(UUID uniqueId) {
this.uniqueId = uniqueId;
}
// TODO BlockBatch with pool
@ -98,8 +99,8 @@ public class Instance {
return players;
}
public int getId() {
return id;
public UUID getUniqueId() {
return uniqueId;
}
private Chunk createChunk(int chunkX, int chunkZ) {

View File

@ -3,19 +3,14 @@ package fr.themode.minestom.instance;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
public class InstanceManager {
private static volatile int lastInstanceId;
private Set<Instance> instances = Collections.synchronizedSet(new HashSet<>());
private static int generateId() {
return ++lastInstanceId;
}
public Instance createInstance() {
Instance instance = new Instance(generateId());
Instance instance = new Instance(UUID.randomUUID());
this.instances.add(instance);
return instance;
}

View File

@ -12,6 +12,10 @@ public class ClientPlayerPositionAndLookPacket implements ClientPlayPacket {
@Override
public void process(Player player) {
boolean chunkTest = player.chunkTest(x, z);
if (chunkTest)
return;
player.refreshPosition(x, y, z);
player.refreshView(yaw, pitch);
player.refreshOnGround(onGround);

View File

@ -11,6 +11,10 @@ public class ClientPlayerPositionPacket implements ClientPlayPacket {
@Override
public void process(Player player) {
boolean chunkTest = player.chunkTest(x, z);
if (chunkTest)
return;
player.refreshPosition(x, y, z);
player.refreshOnGround(onGround);
}