MCPcopy Create free account
hub / github.com/cuberite/cuberite / OnUse

Method OnUse

src/Blocks/BlockBed.cpp:97–152  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

95
96
97void cBlockBedHandler::OnUse(cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface, cPlayer * a_Player, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ)
98{
99 if (a_WorldInterface.GetDimension() != dimOverworld)
100 {
101 Vector3i Coords(a_BlockX, a_BlockY, a_BlockZ);
102 a_WorldInterface.DoExplosionAt(5, a_BlockX, a_BlockY, a_BlockZ, true, esBed, &Coords);
103 }
104 else
105 {
106 if (a_WorldInterface.GetTimeOfDay() > 13000)
107 {
108 NIBBLETYPE Meta = a_ChunkInterface.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ);
109 if (Meta & 0x4)
110 {
111 a_Player->SendMessageFailure("This bed is occupied.");
112 }
113 else
114 {
115 Vector3i PillowDirection(0, 0, 0);
116
117 if (Meta & 0x8)
118 {
119 // Is pillow
120 a_WorldInterface.GetBroadcastManager().BroadcastUseBed(*a_Player, a_BlockX, a_BlockY, a_BlockZ);
121 }
122 else
123 {
124 // Is foot end
125 VERIFY((Meta & 0x4) != 0x4); // Occupied flag should never be set, else our compilator (intended) is broken
126
127 PillowDirection = MetaDataToDirection(Meta & 0x7);
128 if (a_ChunkInterface.GetBlock(a_BlockX + PillowDirection.x, a_BlockY, a_BlockZ + PillowDirection.z) == E_BLOCK_BED) // Must always use pillow location for sleeping
129 {
130 a_WorldInterface.GetBroadcastManager().BroadcastUseBed(*a_Player, a_BlockX + PillowDirection.x, a_BlockY, a_BlockZ + PillowDirection.z);
131 }
132 }
133
134 a_ChunkInterface.SetBlockMeta(a_BlockX, a_BlockY, a_BlockZ, Meta | 0x4); // Where 0x4 = occupied bit
135 a_Player->SetIsInBed(true);
136
137 cTimeFastForwardTester Tester;
138 if (a_WorldInterface.ForEachPlayer(Tester))
139 {
140 cPlayerBedStateUnsetter Unsetter(Vector3i(a_BlockX + PillowDirection.x, a_BlockY, a_BlockZ + PillowDirection.z), a_WorldInterface);
141 a_WorldInterface.ForEachPlayer(Unsetter);
142 a_WorldInterface.SetTimeOfDay(0);
143 a_ChunkInterface.SetBlockMeta(a_BlockX, a_BlockY, a_BlockZ, Meta & 0xB); // Where 0xB = 1011, and zero is to make sure 'occupied' bit is always unset
144 }
145 }
146 }
147 else
148 {
149 a_Player->SendMessageFailure("You can only sleep at night");
150 }
151 }
152}
153
154

Callers 1

HandleRightClickMethod · 0.45

Calls 11

GetTimeOfDayMethod · 0.80
SendMessageFailureMethod · 0.80
SetIsInBedMethod · 0.80
SetTimeOfDayMethod · 0.80
GetDimensionMethod · 0.45
DoExplosionAtMethod · 0.45
GetBlockMetaMethod · 0.45
BroadcastUseBedMethod · 0.45
GetBlockMethod · 0.45
SetBlockMetaMethod · 0.45
ForEachPlayerMethod · 0.45

Tested by

no test coverage detected