From 2715b38e31630f447bb45893f9f00baba6d35a4b Mon Sep 17 00:00:00 2001 From: Neko Ayaka Date: Thu, 9 Jan 2025 18:47:56 +0800 Subject: [PATCH] refactor: naming and import convention --- .../{collectBlock.ts => collect-block.ts} | 0 .../minecraft/src/skills/actions/ensure.ts | 4 ++-- .../actions/{gatherWood.ts => gather-wood.ts} | 0 services/minecraft/src/skills/blocks.ts | 19 +++++++++-------- services/minecraft/src/skills/combat.ts | 14 +++++++------ services/minecraft/src/skills/crafting.ts | 21 ++++++++++--------- services/minecraft/src/skills/inventory.ts | 8 +++---- services/minecraft/src/skills/movement.ts | 6 +++--- 8 files changed, 38 insertions(+), 34 deletions(-) rename services/minecraft/src/skills/actions/{collectBlock.ts => collect-block.ts} (100%) rename services/minecraft/src/skills/actions/{gatherWood.ts => gather-wood.ts} (100%) diff --git a/services/minecraft/src/skills/actions/collectBlock.ts b/services/minecraft/src/skills/actions/collect-block.ts similarity index 100% rename from services/minecraft/src/skills/actions/collectBlock.ts rename to services/minecraft/src/skills/actions/collect-block.ts diff --git a/services/minecraft/src/skills/actions/ensure.ts b/services/minecraft/src/skills/actions/ensure.ts index 6bcc21b7b..dfed7c73b 100644 --- a/services/minecraft/src/skills/actions/ensure.ts +++ b/services/minecraft/src/skills/actions/ensure.ts @@ -3,8 +3,8 @@ import { useLogg } from '@guiiai/logg' import { getItemId } from '../../utils/mcdata' import { craftRecipe } from '../crafting' import { moveAway } from '../movement' -import { collectBlock } from './collectBlock' -import { gatherWood } from './gatherWood' +import { collectBlock } from './collect-block' +import { gatherWood } from './gather-wood' import { getItemCount } from './inventory' // Constants for crafting and gathering diff --git a/services/minecraft/src/skills/actions/gatherWood.ts b/services/minecraft/src/skills/actions/gather-wood.ts similarity index 100% rename from services/minecraft/src/skills/actions/gatherWood.ts rename to services/minecraft/src/skills/actions/gather-wood.ts diff --git a/services/minecraft/src/skills/blocks.ts b/services/minecraft/src/skills/blocks.ts index eafd983e1..4d9f2b9df 100644 --- a/services/minecraft/src/skills/blocks.ts +++ b/services/minecraft/src/skills/blocks.ts @@ -1,9 +1,10 @@ import type { Mineflayer } from '../libs/mineflayer' import type { BlockFace } from './base' + import pathfinderModel, { type SafeBlock } from 'mineflayer-pathfinder' import { Vec3 } from 'vec3' -import * as world from '../composables/world' -import * as mc from '../utils/mcdata' +import { getNearestBlock, getNearestBlocks, getPosition, shouldPlaceTorch } from '../composables/world' +import { getBlockId, makeItem } from '../utils/mcdata' import { log } from './base' import { goToPosition } from './movement' @@ -13,9 +14,9 @@ const { goals, Movements } = pathfinderModel * Place a torch if needed */ async function autoLight(mineflayer: Mineflayer): Promise { - if (world.shouldPlaceTorch(mineflayer)) { + if (shouldPlaceTorch(mineflayer)) { try { - const pos = world.getPosition(mineflayer) + const pos = getPosition(mineflayer) return await placeBlock(mineflayer, 'torch', pos.x, pos.y, pos.z, 'bottom', true) } catch { @@ -112,7 +113,7 @@ export async function placeBlock( placeOn: BlockFace = 'bottom', dontCheat = false, ): Promise { - if (!mc.getBlockId(blockType)) { + if (!getBlockId(blockType)) { log(mineflayer, `Invalid block type: ${blockType}.`) return false } @@ -213,7 +214,7 @@ async function placeWithoutCheats( let block = mineflayer.bot.inventory.items().find(item => item.name === itemName) if (!block && mineflayer.isCreative) { - await mineflayer.bot.creative.setInventorySlot(36, mc.makeItem(itemName, 1)) + await mineflayer.bot.creative.setInventorySlot(36, makeItem(itemName, 1)) block = mineflayer.bot.inventory.items().find(item => item.name === itemName) } @@ -415,7 +416,7 @@ async function findNearestDoor(bot: any): Promise { ] for (const doorType of doorTypes) { - const block = world.getNearestBlock(bot, doorType, 16) + const block = getNearestBlock(bot, doorType, 16) if (block) { return block.position } @@ -544,7 +545,7 @@ function fixSeedName(seedType: string): string { } export async function activateNearestBlock(mineflayer: Mineflayer, type: string): Promise { - const block = world.getNearestBlock(mineflayer, type, 16) + const block = getNearestBlock(mineflayer, type, 16) if (!block) { log(mineflayer, `Could not find any ${type} to activate.`) return false @@ -621,7 +622,7 @@ function getBlockTypes(blockType: string): string[] { } function getValidBlocks(mineflayer: Mineflayer, blocktypes: string[], exclude: Vec3[] | null): any[] { - let blocks = world.getNearestBlocks(mineflayer, blocktypes, 64) + let blocks = getNearestBlocks(mineflayer, blocktypes, 64) if (exclude) { blocks = blocks.filter( diff --git a/services/minecraft/src/skills/combat.ts b/services/minecraft/src/skills/combat.ts index ad7b3ee81..eae3d10c9 100644 --- a/services/minecraft/src/skills/combat.ts +++ b/services/minecraft/src/skills/combat.ts @@ -1,10 +1,12 @@ import type { Entity } from 'prismarine-entity' import type { Item } from 'prismarine-item' import type { Mineflayer } from '../libs/mineflayer' + import pathfinderModel from 'mineflayer-pathfinder' -import * as world from '../composables/world' + +import { getNearbyEntities, getNearestEntityWhere } from '../composables/world' import { sleep } from '../utils/helper' -import * as mc from '../utils/mcdata' +import { isHostile } from '../utils/mcdata' import { log } from './base' const { goals } = pathfinderModel @@ -46,7 +48,7 @@ export async function attackNearest( mobType: string, kill = true, ): Promise { - const mob = world.getNearbyEntities(mineflayer, 24).find(entity => entity.name === mobType) + const mob = getNearbyEntities(mineflayer, 24).find(entity => entity.name === mobType) if (mob) { return await attackEntity(mineflayer, mob, kill) @@ -78,7 +80,7 @@ export async function attackEntity( }) mineflayer.bot.pvp.attack(entity) - while (world.getNearbyEntities(mineflayer, 24).includes(entity)) { + while (getNearbyEntities(mineflayer, 24).includes(entity)) { await new Promise(resolve => setTimeout(resolve, 1000)) } @@ -88,7 +90,7 @@ export async function attackEntity( export async function defendSelf(mineflayer: Mineflayer, range = 9): Promise { let attacked = false - let enemy = world.getNearestEntityWhere(mineflayer, entity => mc.isHostile(entity), range) + let enemy = getNearestEntityWhere(mineflayer, entity => isHostile(entity), range) while (enemy) { await equipHighestAttack(mineflayer) @@ -114,7 +116,7 @@ export async function defendSelf(mineflayer: Mineflayer, range = 9): Promise mc.isHostile(entity), range) + enemy = getNearestEntityWhere(mineflayer, entity => isHostile(entity), range) mineflayer.once('interrupt', () => { mineflayer.bot.pvp.stop() diff --git a/services/minecraft/src/skills/crafting.ts b/services/minecraft/src/skills/crafting.ts index 795d75eff..11375dfad 100644 --- a/services/minecraft/src/skills/crafting.ts +++ b/services/minecraft/src/skills/crafting.ts @@ -2,10 +2,11 @@ import type { Block } from 'prismarine-block' import type { Item } from 'prismarine-item' import type { Recipe } from 'prismarine-recipe' import type { Mineflayer } from '../libs/mineflayer' + import { useLogg } from '@guiiai/logg' -import * as world from '../composables/world' + import { getInventoryCounts, getNearestBlock, getNearestFreeSpace } from '../composables/world' -import * as mc from '../utils/mcdata' +import { getItemId, getItemName } from '../utils/mcdata' import { ensureCraftingTable } from './actions/ensure' import { collectBlock, placeBlock } from './blocks' import { goToNearestBlock, goToPosition, moveAway } from './movement' @@ -47,7 +48,7 @@ export async function craftRecipe( if (itemName.endsWith('plank')) itemName += 's' // Correct common mistakes - const itemId = mc.getItemId(itemName) + const itemId = getItemId(itemName) if (itemId === null) { logger.log(`Invalid item name: ${itemName}`) return false @@ -113,7 +114,7 @@ export async function craftRecipe( async function findAndUseCraftingTable( craftingTableRange: number, ): Promise { - let craftingTable = world.getNearestBlock(mineflayer, 'crafting_table', craftingTableRange) + let craftingTable = getNearestBlock(mineflayer, 'crafting_table', craftingTableRange) if (craftingTable) { return await moveToAndCraft(craftingTable) } @@ -214,11 +215,11 @@ export async function smeltItem(mineflayer: Mineflayer, itemName: string, num = const inputItem = furnace.inputItem() if ( inputItem - && inputItem.type !== mc.getItemId(itemName) + && inputItem.type !== getItemId(itemName) && inputItem.count > 0 ) { logger.log( - `The furnace is currently smelting ${mc.getItemName( + `The furnace is currently smelting ${getItemName( inputItem.type, )}.`, ) @@ -251,11 +252,11 @@ export async function smeltItem(mineflayer: Mineflayer, itemName: string, num = } await furnace.putFuel(fuel.type, null, putFuel) logger.log( - `Added ${putFuel} ${mc.getItemName(fuel.type)} to furnace fuel.`, + `Added ${putFuel} ${getItemName(fuel.type)} to furnace fuel.`, ) } // Put the items in the furnace - const itemId = mc.getItemId(itemName) + const itemId = getItemId(itemName) if (itemId === null) { logger.log(`Invalid item name: ${itemName}`) return false @@ -293,12 +294,12 @@ export async function smeltItem(mineflayer: Mineflayer, itemName: string, num = } if (total < num) { logger.log( - `Only smelted ${total} ${mc.getItemName(smeltedItem?.type || 0)}.`, + `Only smelted ${total} ${getItemName(smeltedItem?.type || 0)}.`, ) return false } logger.log( - `Successfully smelted ${itemName}, got ${total} ${mc.getItemName( + `Successfully smelted ${itemName}, got ${total} ${getItemName( smeltedItem?.type || 0, )}.`, ) diff --git a/services/minecraft/src/skills/inventory.ts b/services/minecraft/src/skills/inventory.ts index ddedfff6b..4d3623a2d 100644 --- a/services/minecraft/src/skills/inventory.ts +++ b/services/minecraft/src/skills/inventory.ts @@ -1,5 +1,5 @@ import type { Mineflayer } from '../libs/mineflayer' -import * as world from '../composables/world' +import { getNearestBlock } from '../composables/world' import { log } from './base' import { goToPlayer, goToPosition } from './movement' @@ -61,7 +61,7 @@ export async function discard(mineflayer: Mineflayer, itemName: string, num = -1 } export async function putInChest(mineflayer: Mineflayer, itemName: string, num = -1): Promise { - const chest = world.getNearestBlock(mineflayer, 'chest', 32) + const chest = getNearestBlock(mineflayer, 'chest', 32) if (!chest) { log(mineflayer, 'Could not find a chest nearby.') return false @@ -85,7 +85,7 @@ export async function putInChest(mineflayer: Mineflayer, itemName: string, num = } export async function takeFromChest(mineflayer: Mineflayer, itemName: string, num = -1): Promise { - const chest = world.getNearestBlock(mineflayer, 'chest', 32) + const chest = getNearestBlock(mineflayer, 'chest', 32) if (!chest) { log(mineflayer, 'Could not find a chest nearby.') return false @@ -110,7 +110,7 @@ export async function takeFromChest(mineflayer: Mineflayer, itemName: string, nu } export async function viewChest(mineflayer: Mineflayer): Promise { - const chest = world.getNearestBlock(mineflayer, 'chest', 32) + const chest = getNearestBlock(mineflayer, 'chest', 32) if (!chest) { log(mineflayer, 'Could not find a chest nearby.') return false diff --git a/services/minecraft/src/skills/movement.ts b/services/minecraft/src/skills/movement.ts index 2a1ab9586..22a3220b6 100644 --- a/services/minecraft/src/skills/movement.ts +++ b/services/minecraft/src/skills/movement.ts @@ -5,7 +5,7 @@ import { useLogg } from '@guiiai/logg' import { randomInt } from 'es-toolkit' import pathfinder from 'mineflayer-pathfinder' import { Vec3 } from 'vec3' -import * as world from '../composables/world' +import { getNearestBlock, getNearestEntityWhere } from '../composables/world' import { sleep } from '../utils/helper' import { log } from './base' @@ -47,7 +47,7 @@ export async function goToNearestBlock( range = MAX_RANGE } - const block = world.getNearestBlock(mineflayer, blockType, range) + const block = getNearestBlock(mineflayer, blockType, range) if (!block) { log(mineflayer, `Could not find any ${blockType} in ${range} blocks.`) return false @@ -64,7 +64,7 @@ export async function goToNearestEntity( minDistance = 2, range = 64, ): Promise { - const entity = world.getNearestEntityWhere( + const entity = getNearestEntityWhere( mineflayer, entity => entity.name === entityType, range,