MCPcopy
hub / github.com/PrismarineJS/mineflayer / sleep

Function sleep

lib/plugins/bed.js:79–154  ·  view source on GitHub ↗
(bedBlock)

Source from the content-addressed store, hash-verified

77 }
78
79 async function sleep (bedBlock) {
80 const thunderstorm = bot.isRaining && (bot.thunderState > 0)
81 if (!thunderstorm && !(bot.time.timeOfDay >= 12541 && bot.time.timeOfDay <= 23458)) {
82 throw new Error("it's not night and it's not a thunderstorm")
83 } else if (bot.isSleeping) {
84 throw new Error('already sleeping')
85 } else if (!isABed(bedBlock)) {
86 throw new Error('wrong block : not a bed block')
87 } else {
88 const botPos = bot.entity.position.floored()
89 const metadata = parseBedMetadata(bedBlock)
90 let headPoint = bedBlock.position
91
92 if (metadata.occupied) {
93 throw new Error('the bed is occupied')
94 }
95
96 if (!metadata.part) { // Is foot
97 const upperBlock = bot.blockAt(bedBlock.position.plus(metadata.headOffset))
98
99 if (isABed(upperBlock)) {
100 headPoint = upperBlock.position
101 } else {
102 const lowerBlock = bot.blockAt(bedBlock.position.plus(metadata.headOffset.scaled(-1)))
103
104 if (isABed(lowerBlock)) {
105 // If there are 2 foot parts, minecraft only lets you sleep if you click on the lower one
106 headPoint = bedBlock.position
107 bedBlock = lowerBlock
108 } else {
109 throw new Error("there's only half bed")
110 }
111 }
112 }
113
114 if (!bot.canDigBlock(bedBlock)) {
115 throw new Error('cant click the bed')
116 }
117
118 const clickRange = [2, -3, -3, 2] // [south, west, north, east]
119 const monsterRange = [7, -8, -8, 7]
120 const oppositeCardinal = (metadata.facing + 2) % CARDINAL_DIRECTIONS.length
121
122 if (clickRange[oppositeCardinal] < 0) {
123 clickRange[oppositeCardinal]--
124 } else {
125 clickRange[oppositeCardinal]++
126 }
127
128 const nwClickCorner = headPoint.offset(clickRange[1], -2, clickRange[2]) // North-West lower corner
129 const seClickCorner = headPoint.offset(clickRange[3], 2, clickRange[0]) // South-East upper corner
130 if (botPos.x > seClickCorner.x || botPos.x < nwClickCorner.x || botPos.y > seClickCorner.y || botPos.y < nwClickCorner.y || botPos.z > seClickCorner.z || botPos.z < nwClickCorner.z) {
131 throw new Error('the bed is too far')
132 }
133
134 if (bot.game.gameMode !== 'creative' || bot.supportFeature('creativeSleepNearMobs')) { // If in creative mode the bot should be able to sleep even if there are monster nearby (starting in 1.13)
135 const nwMonsterCorner = headPoint.offset(monsterRange[1], -6, monsterRange[2]) // North-West lower corner
136 const seMonsterCorner = headPoint.offset(monsterRange[3], 4, monsterRange[0]) // South-East upper corner

Callers 6

clickWindowFunction · 0.70
flyToFunction · 0.70
addCustomNameFunction · 0.70
internalTest.jsFile · 0.50
nether.jsFile · 0.50
detectChildJoinFunction · 0.50

Calls 3

isABedFunction · 0.85
parseBedMetadataFunction · 0.85
waitUntilSleepFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…