Fixed a wide range of bugs with floating-point coordinates getting incorrectly int-casted

This causes lots of bugs in negative coordinates.

This fixes #1789 after world load & save.
This commit is contained in:
Dylan K. Taylor
2018-02-15 10:27:42 +00:00
parent 4e9e285e37
commit e7e4645c0b
7 changed files with 31 additions and 29 deletions

View File

@ -484,7 +484,7 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
/** @var float[] $pos */
$pos = $this->namedtag->getListTag("Pos")->getAllValues();
$this->chunk = $level->getChunk(((int) $pos[0]) >> 4, ((int) $pos[2]) >> 4, true);
$this->chunk = $level->getChunk(((int) floor($pos[0])) >> 4, ((int) floor($pos[2])) >> 4, true);
if($this->chunk === null){
throw new \InvalidStateException("Cannot create entities in unloaded chunks");
}
@ -1757,14 +1757,16 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
}
protected function checkChunks(){
if($this->chunk === null or ($this->chunk->getX() !== ($this->x >> 4) or $this->chunk->getZ() !== ($this->z >> 4))){
$chunkX = $this->getFloorX() >> 4;
$chunkZ = $this->getFloorZ() >> 4;
if($this->chunk === null or ($this->chunk->getX() !== $chunkX or $this->chunk->getZ() !== $chunkZ)){
if($this->chunk !== null){
$this->chunk->removeEntity($this);
}
$this->chunk = $this->level->getChunk($this->x >> 4, $this->z >> 4, true);
$this->chunk = $this->level->getChunk($chunkX, $chunkZ, true);
if(!$this->justCreated){
$newChunk = $this->level->getChunkPlayers($this->x >> 4, $this->z >> 4);
$newChunk = $this->level->getChunkPlayers($chunkX, $chunkZ);
foreach($this->hasSpawned as $player){
if(!isset($newChunk[$player->getLoaderId()])){
$this->despawnFrom($player);