Skip to content

Commit 6dc76a1

Browse files
no good
1 parent a5a2c64 commit 6dc76a1

File tree

4 files changed

+94
-14
lines changed

4 files changed

+94
-14
lines changed

examples/staticTest.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const mcVersion = "1.21.4"
1+
const mcVersion = "1.8.9"
22

33
function stringifyItem(registry, item) {
44
const mdItem = registry.items[item.id]
@@ -32,7 +32,8 @@ async function main(mcVersion) {
3232
const wantedAmount = parseInt(process.argv[3]) || 1
3333

3434
const sticks = {id: mcData.itemsByName[wantedItemName].id, count: wantedAmount}
35-
const plan = crafter(sticks)
35+
const plan = crafter(sticks, {availableItems: [{id: mcData.itemsByName.log.id, count: 2}],multipleRecipes: true})
36+
// const plan = crafter(sticks)
3637

3738
console.log(beautifyPlan(mcData, plan))
3839
}

examples/staticTest.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import minecraftData from 'minecraft-data';
2+
3+
interface Item {
4+
id: number;
5+
count: number;
6+
}
7+
8+
interface Recipe {
9+
ingredients: Item[] | null;
10+
delta: Item[];
11+
result: Item;
12+
}
13+
14+
interface RecipeAndAmt {
15+
recipe: Recipe;
16+
recipeApplications: number;
17+
}
18+
19+
interface Plan {
20+
itemsRequired: Item[];
21+
recipesToDo: RecipeAndAmt[];
22+
}
23+
24+
interface Registry {
25+
items: { [key: number]: { name: string } };
26+
itemsByName: { [key: string]: { id: number } };
27+
}
28+
29+
function stringifyItem(registry: Registry, item: Item): string {
30+
const mdItem = registry.items[item.id];
31+
return `${mdItem.name} x ${item.count}`;
32+
}
33+
34+
function beautifyPlan(registry: Registry, plan: Plan): string {
35+
const itemsRequired = plan.itemsRequired.filter(i => i.count > 0).map(stringifyItem.bind(null, registry)).join(", ");
36+
const plans = plan.recipesToDo.map(recipeAndAmt => {
37+
const { recipeApplications, recipe } = recipeAndAmt;
38+
39+
if (recipe.ingredients != null) {
40+
const items = recipe.ingredients.map(stringifyItem.bind(null, registry)).join(", ");
41+
return `${items} => ${stringifyItem(registry, recipe.result)} (x${recipeApplications})`;
42+
} else {
43+
const items = recipe.delta.filter(i => i.count < 0).map(stringifyItem.bind(null, registry)).join(", ");
44+
return `${items} => ${stringifyItem(registry, recipe.result)} (x${recipeApplications})`;
45+
}
46+
}).join("\n\t");
47+
48+
return `Items required:\n\t${itemsRequired}\nPlans:\n\t${plans}`;
49+
}
50+
51+
async function main(mcVersion: string): Promise<void> {
52+
const mcData = minecraftData(mcVersion);
53+
const crafter = await (await import("../src")).buildStatic(mcData); // buildStatic is async
54+
55+
const wantedItemName = process.argv[2] || "wooden_pickaxe";
56+
const wantedAmount = parseInt(process.argv[3]) || 1;
57+
58+
const sticks: Item = { id: mcData.itemsByName[wantedItemName].id, count: wantedAmount };
59+
const plan = crafter(sticks, {availableItems: [{id: mcData.itemsByName.oak_log.id, count: 2}],multipleRecipes: true})
60+
// const plan = crafter(sticks);
61+
62+
console.log(beautifyPlan(mcData, plan));
63+
}
64+
65+
(async () => main("1.21.4"))();

examples/test.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const mineflayer = require("mineflayer")
22

3-
const crafter = require("mineflayer-crafting-util").plugin
3+
const crafter = require("../lib").plugin
44

55
const bot = mineflayer.createBot({
6-
host: "localhost", // optional
6+
host: process.argv[2], // optional
77
port: 25565, // optional
88
username: "bot"
99
})
@@ -142,11 +142,14 @@ bot.once("spawn", () => {
142142
}
143143

144144
}
145+
let idx = 0;
145146
console.log(plan2.itemsRequired.map(stringifyItem).join(", "))
147+
console.log(plan2.recipesToDo)
146148
for (const info of plan2.recipesToDo) {
147-
console.log(info.recipe.delta.map(stringifyItem).join(", "))
149+
console.log(idx, info.recipe.delta.map(stringifyItem).join(", "))
148150
await bot.chat(`Crafting ${bot.registry.items[info.recipe.result.id].name} x ${info.recipe.result.count}`)
149151
await bot.craft(info.recipe, info.recipeApplications, craftingTable)
152+
idx++;
150153
await bot.waitForTicks(10)
151154
}
152155

src/craftingInjection.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function _build(Recipe: typeof PRecipe): CraftingFunc {
1717
target = item.count
1818
): { success: boolean; itemsRequired: Item[]; recipesToDo: Array<{ recipeApplications: number; recipe: PRecipe }> } {
1919
const id = item.id;
20-
const recipes = Recipe.find(id, null);
20+
let recipes = Recipe.find(id, null);
2121

2222
const availableItems = opts.availableItems;
2323
const includeRecursion = opts.includeRecursion ?? false;
@@ -34,6 +34,10 @@ export function _build(Recipe: typeof PRecipe): CraftingFunc {
3434
recipe: PRecipe;
3535
}> = [];
3636

37+
// disregard recipes that combine the item itself back together, as that is pointless for our usecase.
38+
recipes = recipes.filter((r) => r.delta.slice(0, -1).some((e) => e.id !== id));
39+
40+
3741
if (availableItems !== undefined) {
3842
matchingItem = availableItems.find((e) => e.id === id && e.count >= target);
3943
if (matchingItem != null) {
@@ -53,13 +57,18 @@ export function _build(Recipe: typeof PRecipe): CraftingFunc {
5357
return { success: false, itemsRequired: [item], recipesToDo: [] };
5458
}
5559

56-
seen.set(id, item);
60+
seen.set(id, item);
5761

58-
recipeWanted = recipes.find((r) =>
59-
r.delta.slice(0, -1).every((e) => (availableItems.find((i) => i.id === e.id)?.count ?? 0) >= -e.count)
62+
recipeWanted = recipes.find((r) => {
63+
if (r.ingredients != null) return r.ingredients.every(e=>(availableItems.find(i=>i.id === e.id)?.count ?? 0) >= -e.count)
64+
else return r.delta.slice(0, -1).every((e) => (availableItems.find((i) => i.id === e.id)?.count ?? 0) >= -e.count)
65+
}
66+
6067
);
6168

69+
6270
if (recipeWanted != null) {
71+
6372
} else {
6473
// since no recipes exist with all items available, search for the recipe with the most amount of items available inline
6574

@@ -141,10 +150,10 @@ export function _build(Recipe: typeof PRecipe): CraftingFunc {
141150
for (const toDo of test.recipesToDo) {
142151
for (const ing of toDo.recipe.delta) {
143152
const index = currentItems.findIndex((e) => e.id === ing.id);
144-
const num = (currentItems[index]?.count ?? 0) + ing.count * toDo.recipeApplications;
145-
if (num < 0) { // this should never happen, but just in case.
146-
return { success: false, itemsRequired: [item], recipesToDo: [] };
147-
}
153+
// const num = (currentItems[index]?.count ?? 0) + ing.count * toDo.recipeApplications;
154+
// if (num < 0) { // this should never happen, but just in case.
155+
// return { success: false, itemsRequired: [item], recipesToDo: [] };
156+
// }
148157
if (index !== -1) {
149158
currentItems[index].count += ing.count * toDo.recipeApplications;
150159
} else {
@@ -188,7 +197,7 @@ export function _build(Recipe: typeof PRecipe): CraftingFunc {
188197
}
189198
} else {
190199
// TODO : should be replaced by smelting recipe data
191-
const found = recipes.find((r) => r.result.count >= 1);
200+
const found = recipes.find((r) => r.result.count > 1);
192201
recipeWanted = found ?? recipes[0];
193202

194203
if (recipes.length == 0 || gettableItems.includes(id)) {
@@ -257,6 +266,8 @@ export function _build(Recipe: typeof PRecipe): CraftingFunc {
257266

258267
const ret = _newCraft(item, opts, seen);
259268

269+
console.log('internal', ret)
270+
260271
const availableItems = opts.availableItems;
261272

262273
const ret1 = ret as CraftingPlan;

0 commit comments

Comments
 (0)