forked from neonerz/hai2025-ts
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcustom.ts
More file actions
543 lines (480 loc) · 16.7 KB
/
custom.ts
File metadata and controls
543 lines (480 loc) · 16.7 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
// custom.ts
// Custom MakeCode extension file
class CraftPattern {
patternText: String
public constructor(patternText: String) {
this.patternText = patternText;
}
}
//% weight=200 color="#0096FF" icon="\uf126" block="Hour of AI"
namespace haiInputs {
/**
* This function takes
*/
//% block="craft with %n"
//% n.shadow="ghostBlock"
//% color="#0096FF"
export function craftWith(n: number): void {
player.execute(`scoreboard players add .outputsum global ${n}`);
}
}
//% weight=200 color="#0096FF" icon="\uf126" block="Hour of AI Python"
namespace ai {
const items: { [key: string]: string } = {
'oak_planks': '1',
'cobblestone': '3',
'oak_log': '1',
'birch_log': '2',
'acacia_log': '3',
'coal_ore': '4',
'iron_ore': '5',
'wool': '6',
'grass': '99',
'lava': '99',
'soul_sand': '99',
'coal': '21',
'iron_ingot': '22',
'stick': '2',
'wood_pickaxe': '1',
'wood_axe': '2',
'wood_shovel': '3',
'wood': '1',
'axe': '2',
'pickaxe': '1',
'shovel': '3',
}
const crafting_recipes: { [key: string]: string } = {
'BBBB': '4', // crafting bench
'OOOOBOOBO': '5', // stick or torch
'BBBOBOOBO': '6', // pickaxe
'BBOBBOOBO': '7', // axe
'OBOOBOOBO': '8', // shovel
'BBOBBOBBO': '9', // door
'BBBBOBBBB': '10', // furnace
'OOOBBBBBB': '11', // bed
'OOOOBOOOB': '12', // shears
'OOOB': '13' // plank
}
/**
* This function makes an API request.
*
* Usage Example:
* ai.make_api_request(
* api_url,
* api_key,
* api_endpoint,
* training_data)
*
* api_endpoint: Available endpoints are 'classify', 'crafting' or 'upgrade'.
*
* training_data should be a list of training_type:training_value key pairs.
* Example:
* training_data = {
* 'wood': 'oak_log'
* }
*/
export function make_api_request(api_url: string, api_key: string, api_endpoint: string, data: { [key: string]: string }): void {
if (api_url !== 'minecraft://agent.ai/') {
player.execute(`/title @p title §6404 Not Found:`);
player.execute(`/title @p subtitle §6The requested URL ${api_url} was not found on this server.`);
} else if (api_endpoint !== "classify" && api_endpoint !== "crafting" && api_endpoint !== "upgrade") {
player.execute(`/title @p title §6Invalid endpoint:`);
player.execute(`/title @p subtitle §6'${api_endpoint}'. Available endpoints are 'classify', 'crafting' or 'upgrade'.`);
} else {
if (api_endpoint == "classify") {
const keys = Object.keys(data);
for (const key of keys) {
// Trim the value to remove any accidental leading/trailing whitespace.
const value = data[key].trim();
// --- Validation Check ---
// Before using the value, check if it exists as a key in our 'items' list.
if (items[value] === undefined) {
// If it doesn't exist, show an error to the player with the original (untrimmed) value.
player.execute(`/title @p title §6Invalid Item Name:`);
player.execute(`/title @p subtitle §6'${data[key]}' is not a valid item for classification.`);
return; // Stop processing immediately
}
// This line will now only run if the value is valid.
player.execute(`scoreboard players add .outputsum global ${items[value]}`);
}
} else if (api_endpoint == "crafting") {
const keys = Object.keys(data);
for (const key of keys) {
// Trim the value to remove any accidental leading/trailing whitespace.
let value = data[key].trim();
value = value.split("\n").join("");
value = value.split(" ").join("");
value = value.split("\t").join("");
// --- Validation Check ---
// Before using the value, check if it exists as a key in our 'items' list.
if (crafting_recipes[value] === undefined) {
// If it doesn't exist, show an error to the player with the original (untrimmed) value.
player.execute(`/title @p title §6Invalid Crafting Recipe:`);
player.execute(`/title @p subtitle §6The passed pattern is not a valid crafting recipe.`);
return; // Stop processing immediately
}
// This line will now only run if the value is valid.
player.execute(`scoreboard players add .outputsum global ${crafting_recipes[value]}`);
}
} else if (api_endpoint == "upgrade") {
const keys = Object.keys(data);
for (const key of keys) {
// Trim the value to remove any accidental leading/trailing whitespace.
const value = data[key].trim();
// --- Validation Check ---
// Before using the key, check if it exists as a key in our 'items' list.
if (items[key] === undefined) {
// If it doesn't exist, show an error to the player with the original (untrimmed) value.
player.execute(`/title @p title §6Invalid Tool Type:`);
player.execute(`/title @p subtitle §6'${key}' is not a valid tool.`);
return; // Stop processing immediately
}
// Before using the value, check if it exists as a key in our 'items' list.
if (items[value] === undefined || items[value] !== '3') {
// If it doesn't exist, show an error to the player with the original (untrimmed) value.
player.execute(`/title @p title §6Invalid Upgrade Type:`);
player.execute(`/title @p subtitle §6'${value}' is not a valid upgrade.`);
return; // Stop processing immediately
}
// This line will now only run if the value is valid.
player.execute(`scoreboard players add .outputsum global ${items[key]}`);
player.execute(`scoreboard players add .outputsum global ${items[value]}`);
}
}
}
}
}
//% weight=200 color="#008106" icon="\uf126" block="Hour of AI"
namespace hai {
/**
* Training Module
*/
//% block="training module"
//% color="#0096FF"
//% blockId=on_training_start
export function trainingStart(handler: () => void) {
handler();
}
// INPUTS
// These functions set scoreboard values based on user selections.
/**
* Select block to craft with.
*/
//% block="craft with %n"
//% n.shadow="ghostBlock"
//% color="#0096FF"
export function craftWith(n: number): void {
player.execute(`scoreboard players add .outputsum global ${n}`);
}
/**
* Train agent crafting.
*/
//% block="crafting grid %pattern"
//% blockid="craft"
//% color="#0096FF"
export function crafting(pattern: CraftPattern): void {
// This simple logic removes space delimiters and trims extra newlines.
const normalizedPattern = pattern.patternText
.split(' ').join('')
.trim();
// This if/else if chain should now work perfectly.
if (
normalizedPattern === `##\n##` || // The original 2x2 pattern
normalizedPattern === `##.\n##.\n...` || // Top-left 2x2 in a 3x3
normalizedPattern === `.##\n.##\n...` || // Top-right 2x2 in a 3x3
normalizedPattern === `...\n##.\n##.` || // Bottom-left 2x2 in a 3x3
normalizedPattern === `...\n.##\n.##` // Bottom-right 2x2 in a 3x3
) {
// Crafted crafting bench
player.execute(`scoreboard players add .outputsum global 4`);
} else if (
normalizedPattern === `#.\n#.` || normalizedPattern === `.#\n.#` || // Stick patterns for 2x2
normalizedPattern === `#..\n#..\n...` || normalizedPattern === `.#.\n.#.\n...` ||
normalizedPattern === `..#\n..#\n...` || normalizedPattern === `...\n#..\n#..` ||
normalizedPattern === `...\n.#.\n.#.` || normalizedPattern === `...\n..#\n..#`
) {
// Crafted stick or torch
player.execute(`scoreboard players add .outputsum global 5`);
} else if (normalizedPattern === `###\n.#.\n.#.`) {
// Crafted pickaxe
player.execute(`scoreboard players add .outputsum global 6`);
} else if (normalizedPattern === `##.\n##.\n.#.` || normalizedPattern === `.##\n.##\n.#.`) {
// Crafted axe
player.execute(`scoreboard players add .outputsum global 7`);
} else if (
normalizedPattern === `.#.\n.#.\n.#.` || normalizedPattern === `#..\n#..\n#..` ||
normalizedPattern === `..#\n..#\n..#`
) {
// Crafted shovel
player.execute(`scoreboard players add .outputsum global 8`);
} else if (
normalizedPattern === `##.\n##.\n##.` || normalizedPattern === `.##\n.##\n.##`
) {
// Crafted door
player.execute(`scoreboard players add .outputsum global 9`);
} else if (
normalizedPattern === `###\n#.#\n###`
) {
// Crafted furnace
player.execute(`scoreboard players add .outputsum global 10`);
} else if (
normalizedPattern === `...\n###\n###` || normalizedPattern === `###\n###\n...`
) {
// Crafted bed
player.execute(`scoreboard players add .outputsum global 11`);
} else if (
// Shears Patterns
// 2x2 Grid
normalizedPattern === `#.\n.#` ||
normalizedPattern === `.#\n#.` ||
// 3x3 Grid (Top-Left to Bottom-Right Diagonal)
normalizedPattern === `#..\n.#.\n...` ||
normalizedPattern === `.#.\n..#\n...` ||
normalizedPattern === `...\n#..\n.#.` ||
normalizedPattern === `...\n.#.\n..#` ||
// 3x3 Grid (Top-Right to Bottom-Left Diagonal)
normalizedPattern === `.#.\n#..\n...` ||
normalizedPattern === `..#\n.#.\n...` ||
normalizedPattern === `...\n.#.\n#..` ||
normalizedPattern === `...\n..#\n.#.`
) {
// Crafted shears
player.execute(`scoreboard players add .outputsum global 12`);
} else if (
normalizedPattern === `#..\n...\n...` ||
normalizedPattern === `.#.\n...\n...` ||
normalizedPattern === `..#\n...\n...` ||
normalizedPattern === `...\n#..\n...` ||
normalizedPattern === `...\n.#.\n...` ||
normalizedPattern === `...\n..#\n...` ||
normalizedPattern === `...\n...\n#..` ||
normalizedPattern === `...\n...\n.#.` ||
normalizedPattern === `...\n...\n..#` ||
normalizedPattern === `#.\n..` ||
normalizedPattern === `.#\n..` ||
normalizedPattern === `..\n.#` ||
normalizedPattern === `..\n#.`
) {
// Crafted plank
player.execute(`scoreboard players add .outputsum global 13`);
} else {
// Pattern did not match any recipe
player.execute(`scoreboard players add .outputsum global 0`);
}
}
/**
* Classify a wood log.
*/
//% block="classify %n as wood"
//% n.shadow="ghostBlock"
//% color="#0096FF"
export function classifyWood(n: number): void {
player.execute(`scoreboard players add .outputsum global ${n}`);
}
/**
* Upgrade tools material.
*/
//% block="upgrade %t to %m"
//% t.shadow="ghostItem"
//% m.shadow="ghostBlock"
//% color="#0096FF"
export function upgradeTool(t: number, m: number): void {
let sub_sum = m + t;
player.execute(`scoreboard players add .outputsum global ${sub_sum}`);
}
/**
* House Materials
*/
//% block="building material %b"
//% b.shadow="ghostBlock"
//% color="#0096FF"
export function buildingMaterials(b: number): void {
player.execute(`scoreboard players add .outputsum global ${b}`);
}
/**
* Classify as ore
*/
//% block="classify %b as ore"
//% b.shadow="ghostBlock"
//% color="#0096FF"
export function classifyOre(b: number): void {
player.execute(`scoreboard players add .outputsum global ${b}`);
}
//VALUES
// These functions return constant values representing different items.
// BLOCKS
/**
* Select Wood
*/
//% block="`custom.PlanksOak` wood"
export function wood(): number {
return 1;
}
/**
* Select Cobblestone
*/
//% block="`custom.Cobblestone` cobblestone"
export function cobblestone(): number {
return 3;
}
/**
* Select Oak Log
*/
//% block="`custom.logOak` oak log"
//% blockId=oaklog
export function logOak(): number {
return 1;
}
/**
* Select Birch Log
*/
//% block="`custom.logBirch` birch log"
//% blockId=birchlog
export function logBirch(): number {
return 2;
}
/**
* Select Acacia Log
*/
//% block="`custom.logAcacia` acacia log"
//% blockId=acacialog
export function logAcacia(): number {
return 3;
}
/**
* Select Coal Ore
*/
//% block="`custom.CoalOre` coal ore"
//% blockId=coalore
export function coalOre(): number {
return 4;
}
/**
* Select Iron Ore
*/
//% block="`custom.IronOre` iron ore"
//% blockId=ironore
export function ironOre(): number {
return 5;
}
/**
* Select Wool
*/
//% block="`custom.Wool` wool"
//% blockId=wool
export function wool(): number {
return 6;
}
/**
* Select Grass
*/
//% block="`custom.Grass` grass"
//% blockId=grass
export function grass(): number {
return 99;
}
/**
* Select Lava
*/
//% block="`custom.Lava` lava"
//% blockId=lava
export function lava(): number {
return 99;
}
/**
* Select Soul Sand
*/
//% block="`custom.SoulSand` soul sand"
//% blockId=soul_sand
export function soulSand(): number {
return 99;
}
/**
* Ghost Block
*/
//% block="`custom.Ghost`"
//% color="#8E8E8E"
//% blockId=ghostBlock
export function ghostBlock(): number {
return 99;
}
// CRAFTING INGREDIENTS
/**
* Coal
*/
//% block="`custom.Coal` coal"
//% blockId=coal
export function coal(): number {
return 21;
}
/**
* Iron Ingot
*/
//% block="`custom.IronIngot` iron ingot"
//% blockId=ironIngot
export function ironIngot(): number {
return 22;
}
// ITEMS
/**
* Select Stick
*/
//% block="`custom.Stick` stick"
export function stick(): number {
return 2;
}
/**
* Select Pickaxe
*/
//% block="`custom.WoodenPickaxe` pickaxe"
//% blockId=pickaxe
export function pickaxe(): number {
return 1;
}
/**
* Select Axe
*/
//% block="`custom.WoodenAxe` axe"
//% blockId=axe
export function axe(): number {
return 2;
}
/**
* Select Pickaxe
*/
//% block="`custom.WoodenShovel` shovel"
//% blockId=shovel
export function shovel(): number {
return 3;
}
/**
* Ghost Item
*/
//% block="`custom.GhostItem`"
//% color="#8E8E8E"
//% blockId=ghostItem
export function ghostItem(): number {
return 99;
}
// CRAFTING GRIDS
/**
* 2x2 Crafting Grid.
*/
//% blockId=pocketcraftPattern block="2x2"
//% imageLiteralColumns=2
//% imageLiteralRows=2
//% gridLiteral=1
export function pocketcraftingPattern(pattern: string) {
return new CraftPattern(pattern);
}
/**
* 3x3 Crafting Grid.
*/
//% blockId=craftPattern block="3x3"
//% imageLiteralColumns=3
//% imageLiteralRows=3
//% gridLiteral=1
export function craftingPattern(pattern: string) {
return new CraftPattern(pattern);
}
}