-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyour_code.js
More file actions
151 lines (126 loc) · 4.12 KB
/
Copy pathyour_code.js
File metadata and controls
151 lines (126 loc) · 4.12 KB
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
// Import all example functions from example.js
const {
setPosition,
followPlayer,
findBlocks,
mineBlock,
reportInventory,
craftItemByName,
placeBlockNearPlayer,
furnaceInfo,
useFurnace,
attackEntity,
bot,
minecraftData,
connectedToServer,
} = require("./example.js");
// ====== Example code below ======
// Main function to craft a wooden pickaxe
async function craftWoodenPickaxe() {
console.log("Crafting a wooden pickaxe");
bot.chat("I'm going to craft a wooden pickaxe!");
// Step 1: Check if we already have a wooden pickaxe
reportInventory();
let inventory = bot.inventory.items();
const hasPickaxe = inventory.some((item) => item.name === "wooden_pickaxe");
if (hasPickaxe) {
bot.chat("I already have a wooden pickaxe!");
return;
}
// Step 2: Gather logs (we need at least 1)
bot.chat("Looking for trees...");
// Find logs - try different types until one is found
const logTypes = [
"birch_log",
"oak_log",
"spruce_log",
"jungle_log",
"acacia_log",
"dark_oak_log",
];
let logsFound = false;
for (const logType of logTypes) {
const logs = findBlocks(logType, 64, 5); // Find up to 5 logs within 64 blocks
if (logs.length > 0) {
bot.chat(`Found ${logs.length} ${logType} nearby!`);
// Mine the logs
bot.chat("Mining logs...");
for (const log of logs) {
await mineBlock(log.x, log.y, log.z);
// Check if we have enough logs (need at least 1 for planks)
inventory = bot.inventory.items();
let logCount = inventory.filter((item) => item.name.includes("_log"))[0]
?.count;
if (logCount >= 3) break; // Need 4 planks for crafting table, 2 for sticks, 3 for pickaxe = 9 total planks = 3 logs
}
logsFound = true;
break;
}
}
if (!logsFound) {
bot.chat("I couldn't find any logs nearby!");
return;
}
// Step 3: Craft wooden planks (need at least 3 for sticks + pickaxe)
bot.chat("Crafting wooden planks...");
// Craft 3 planks of the type we found
inventory = bot.inventory.items();
const logType = inventory
.filter((item) => item.name.includes("_log"))[0]
.name.split("_")[0];
console.log(logType);
await craftItemByName(`${logType}_planks`, 3); // 1 log = 4 planks
console.log(bot.inventory.items());
// Step 4: Craft sticks (need 2 for a pickaxe)
bot.chat("Crafting sticks...");
await craftItemByName("stick", 4); // 2 planks = 4 sticks
// Step 5: Find or place a crafting table if needed
bot.chat("Looking for a crafting table...");
const craftingTables = findBlocks("crafting_table", 32, 1);
if (craftingTables.length === 0) {
bot.chat("No crafting table found nearby. I'll make one!");
// Craft a crafting table
await craftItemByName("crafting_table", 1);
// Find a good place to put it (preferably near the player)
const players = Object.values(bot.players);
if (players.length > 1) {
// Find a non-bot player
const otherPlayer = players.find(
(player) => player.username !== bot.username,
);
if (otherPlayer && otherPlayer.entity) {
await placeBlockNearPlayer(otherPlayer.username, "crafting_table");
} else {
// Just place it near the bot
await placeBlockNearPlayer(bot.username, "crafting_table");
}
}
} else {
// Move to the existing crafting table
bot.chat("Found a crafting table!");
await setPosition(
craftingTables[0].x,
craftingTables[0].y,
craftingTables[0].z,
);
}
// Step 6: Finally, craft the wooden pickaxe
bot.chat("Crafting a wooden pickaxe...");
await craftItemByName("wooden_pickaxe", 1);
// Check if we successfully crafted it
const newInventory = bot.inventory.items();
const hasPickaxeNow = newInventory.some(
(item) => item.name === "wooden_pickaxe",
);
if (hasPickaxeNow) {
bot.chat("Successfully crafted a wooden pickaxe!");
} else {
bot.chat("Something went wrong, I couldn't craft a wooden pickaxe.");
}
}
bot.on("spawn", async () => {
console.log("Bot spawned");
await craftWoodenPickaxe();
});
// Handle errors
bot.on("error", console.error);