-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo.ts
168 lines (133 loc) · 5.02 KB
/
demo.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const mineflayer = require("mineflayer")
const {pathfinder, goals, Movements} = require("mineflayer-pathfinder")
const bot = mineflayer.createBot({
username: "test",
host: "pix3lpirat3.com",
})
const Vec3 = require('vec3').Vec3;
const minecraftData = require("minecraft-data")("1.18.2")
const ownerName = "EnderTheCoder"
bot.loadPlugin(pathfinder)
let botState = "resting"
let target
bot.once("spawn", async () => {
bot.waitForChunksToLoad().then(() => {
// containerToInventory(bot.blockAt(new Vec3(57, 56, 78)), minecraftData.itemsByName.birch_planks.id, 32)
// console.log(minecraftData.itemsByName.ladder.id, bot.inventory.slots)
})
})
bot.on("chat", async (username, message) => {
if (username == ownerName) {
switch (message) {
case "run": {
bot.chat("running")
botState = "running"
bot.waitForChunksToLoad().then(() => {
main()
})
break
}
case "follow": {
botState = "following"
bot.chat("following")
const followGoal = new goals.GoalFollow(bot.players[ownerName].entity)
const followMovement = new Movements(bot, minecraftData)
bot.pathfinder.setMovements(followMovement)
bot.pathfinder.setGoal(followGoal)
break
}
case "stop": {
try {
bot.chat("stopped")
botState = "resting"
bot.stopDigging()
bot.pathfinder.stop()
bot.pathfinder.setGoal(null)
} catch (e) {
console.warn(e)
}
// process.exit(0)
break
}
case "chunk": {
console.log(getChunkCorner(bot.entity.position))
break
}
case "state": {
bot.chat(botState)
break
}
}
}
})
async function main() {
let cornerPos = getChunkCorner(bot.entity.position)
target = cornerPos[1];
while (target != cornerPos[0]) {
await searchChunk(target, cornerPos[0], cornerPos[1])
await go()
await dig()
}
}
async function go() {
if (botState == "resting") bot.pathfinder.stop()
const digGoal = new goals.GoalGetToBlock(target.x, target.y, target.z)
const digMovement = new Movements(bot, minecraftData)
await bot.equip(bot.pathfinder.bestHarvestTool(bot.blockAt(target)), "hand")
await bot.pathfinder.setMovements(digMovement)
await bot.pathfinder.goto(digGoal)
}
async function dig() {
// console.log(target)
console.log(bot.world.getBlock(target))
try {
await bot.dig(bot.world.getBlock(target))
} catch (e) {
console.log(e)
}
}
/*WARNING: YOU ARE NOT EXPECTED TO UNDERSTAND THIS
* true is real to fake, false is fake to real
* */
function magicVec3Transfer(startPos, endPos, inputPos, type) {
return type ?
new Vec3(
Math.abs(inputPos.x) - Math.abs(startPos.x),
inputPos.y,
Math.abs(inputPos.z) - Math.abs(startPos.z)) :
new Vec3(
startPos.x > 0 ? 0 + (inputPos.x + Math.abs(startPos.x)) : 0 - (inputPos.x + Math.abs(startPos.x)),
inputPos.y,
startPos.z > 0 ? 0 + (inputPos.z + Math.abs(startPos.z)) : 0 - (inputPos.z + Math.abs(startPos.z)),)
}
async function searchChunk(nowPos, startPos, endPos) {
// console.log("start", startPos)
// console.log("end", endPos)
// console.log("now", nowPos)
let magicStartPos = magicVec3Transfer(startPos, endPos, startPos, true),
magicEndPos = magicVec3Transfer(startPos, endPos, endPos, true),
magicNowPos = magicVec3Transfer(startPos, endPos, nowPos, true)
for (; magicNowPos.y >= magicStartPos.y; magicNowPos.y--)
for (magicNowPos.x = magicEndPos.x; magicNowPos.x >= magicStartPos.x; magicNowPos.x--)
for (magicNowPos.z = magicEndPos.z; magicNowPos.z >= magicStartPos.z; magicNowPos.z--) {
nowPos = magicVec3Transfer(startPos, endPos, magicNowPos, false)
// console.log(nowPos)
// console.log(bot.world.getBlock(nowPos))
if (botState == "resting") return nowPos
if (
bot.blockAt(nowPos) != null &&
bot.blockAt(nowPos).name != "air"
// pathfinder.safeToBreak(bot.blockAt(nowPos))
) {
console.log("Target Found")
// console.log(nowPos)
target = nowPos
return nowPos
}
}
}
function getChunkCorner(pos) {
let startPos = new Vec3((pos.x) - pos.x % 16, -64, pos.z - pos.z % 16)
let endPos = new Vec3((startPos.x > 0) ? startPos.x + 15 : startPos.x - 15, 128, (startPos.z > 0) ? startPos.z + 15 : startPos.z - 15)
return [startPos, endPos]
}