Use occupied bitflag and added distance check for beds

must be < 2 blocks from either half of the bed
This commit is contained in:
Dylan K. Taylor
2017-05-23 12:55:30 +01:00
parent 2d626d1d90
commit 8957dbf08e
2 changed files with 56 additions and 28 deletions

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine;
use pocketmine\block\Air;
use pocketmine\block\Bed;
use pocketmine\block\Block;
use pocketmine\command\Command;
use pocketmine\command\CommandSender;
@ -250,7 +251,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
protected $iusername;
protected $displayName;
protected $startAction = -1;
/** @var Vector3 */
/** @var Vector3|null */
protected $sleeping = null;
protected $clientID = null;
@ -1125,19 +1126,17 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
return false;
}
foreach($this->level->getNearbyEntities($this->boundingBox->grow(2, 1, 2), $this) as $p){
if($p instanceof Player){
if($p->sleeping !== null and $pos->distance($p->sleeping) <= 0.1){
return false;
}
}
}
$b = $this->level->getBlock($pos);
$this->server->getPluginManager()->callEvent($ev = new PlayerBedEnterEvent($this, $this->level->getBlock($pos)));
$this->server->getPluginManager()->callEvent($ev = new PlayerBedEnterEvent($this, $b));
if($ev->isCancelled()){
return false;
}
if($b instanceof Bed){
$b->setOccupied();
}
$this->sleeping = clone $pos;
$this->setDataProperty(self::DATA_PLAYER_BED_POSITION, self::DATA_TYPE_POS, [$pos->x, $pos->y, $pos->z]);
@ -1174,7 +1173,11 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
public function stopSleep(){
if($this->sleeping instanceof Vector3){
$this->server->getPluginManager()->callEvent($ev = new PlayerBedLeaveEvent($this, $this->level->getBlock($this->sleeping)));
$b = $this->level->getBlock($this->sleeping);
if($b instanceof Bed){
$b->setOccupied(false);
}
$this->server->getPluginManager()->callEvent($ev = new PlayerBedLeaveEvent($this, $b));
$this->sleeping = null;
$this->setDataProperty(self::DATA_PLAYER_BED_POSITION, self::DATA_TYPE_POS, [0, 0, 0]);
@ -3409,6 +3412,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
*/
final public function close($message = "", $reason = "generic reason", $notify = true){
if($this->connected and !$this->closed){
try{
if($notify and strlen((string) $reason) > 0){
$pk = new DisconnectPacket();
@ -3422,6 +3426,8 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
$this->server->getPluginManager()->unsubscribeFromPermission(Server::BROADCAST_CHANNEL_USERS, $this);
$this->server->getPluginManager()->unsubscribeFromPermission(Server::BROADCAST_CHANNEL_ADMINISTRATIVE, $this);
$this->stopSleep();
if($this->joined){
try{
$this->save();