From 070cb0d21f74dda768770ba0b8d410abe381c67a Mon Sep 17 00:00:00 2001 From: Bond_009 Date: Sat, 1 Jul 2017 15:59:26 +0200 Subject: You can't use beds when there are hostile mobs nearby --- src/Blocks/BlockBed.cpp | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/src/Blocks/BlockBed.cpp b/src/Blocks/BlockBed.cpp index b980f80e0..adc01c158 100644 --- a/src/Blocks/BlockBed.cpp +++ b/src/Blocks/BlockBed.cpp @@ -4,6 +4,9 @@ #include "BroadcastInterface.h" #include "Entities/../World.h" #include "Entities/Player.h" +#include "../BoundingBox.h" +#include "../Mobs/Monster.h" +#include "../Entities/Entity.h" @@ -37,6 +40,22 @@ void cBlockBedHandler::OnDestroyed(cChunkInterface & a_ChunkInterface, cWorldInt +class cFindMobs : + public cEntityCallback +{ + virtual bool Item(cEntity * a_Entity) override + { + return ( + (a_Entity->GetEntityType() == cEntity::etMonster) && + (static_cast(a_Entity)->GetMobFamily() == cMonster::mfHostile) + ); + } +}; + + + + + class cTimeFastForwardTester : public cPlayerListCallback { @@ -88,14 +107,23 @@ bool cBlockBedHandler::OnUse(cChunkInterface & a_ChunkInterface, cWorldInterface Vector3i Coords(a_BlockX, a_BlockY, a_BlockZ); a_WorldInterface.DoExplosionAt(5, a_BlockX, a_BlockY, a_BlockZ, true, esBed, &Coords); } + else if (!((a_WorldInterface.GetTimeOfDay() > 12541) && (a_WorldInterface.GetTimeOfDay() < 23458))) // Source: http://minecraft.gamepedia.com/Bed#Sleeping + { + a_Player->SendMessageFailure("You can only sleep at night"); + } else { - if (a_WorldInterface.GetTimeOfDay() > 13000) + NIBBLETYPE Meta = a_ChunkInterface.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ); + if ((Meta & 0x4) == 0x4) + { + a_Player->SendMessageFailure("This bed is occupied"); + } + else { - NIBBLETYPE Meta = a_ChunkInterface.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ); - if ((Meta & 0x4) == 0x4) + cFindMobs FindMobs; + if (!a_Player->GetWorld()->ForEachEntityInBox(cBoundingBox(a_Player->GetPosition() - Vector3i(0, 5, 0), 8, 10), FindMobs)) { - a_Player->SendMessageFailure("This bed is occupied"); + a_Player->SendMessageFailure("You may not rest now, there are monsters nearby"); } else { @@ -133,10 +161,6 @@ bool cBlockBedHandler::OnUse(cChunkInterface & a_ChunkInterface, cWorldInterface } } } - else - { - a_Player->SendMessageFailure("You can only sleep at night"); - } } return true; } -- cgit v1.2.3