refactor: naming and import convention

This commit is contained in:
Neko Ayaka
2025-01-09 18:47:56 +08:00
parent eb7bfcc52e
commit 2715b38e31
8 changed files with 38 additions and 34 deletions
@@ -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
+10 -9
View File
@@ -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<boolean> {
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<boolean> {
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<Vec3 | null> {
]
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<boolean> {
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(
+8 -6
View File
@@ -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<boolean> {
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<boolean> {
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<boo
mineflayer.bot.pvp.attack(enemy)
attacked = true
await sleep(500)
enemy = world.getNearestEntityWhere(mineflayer, entity => mc.isHostile(entity), range)
enemy = getNearestEntityWhere(mineflayer, entity => isHostile(entity), range)
mineflayer.once('interrupt', () => {
mineflayer.bot.pvp.stop()
+11 -10
View File
@@ -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<boolean> {
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,
)}.`,
)
+4 -4
View File
@@ -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<boolean> {
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<boolean> {
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<boolean> {
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
+3 -3
View File
@@ -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<boolean> {
const entity = world.getNearestEntityWhere(
const entity = getNearestEntityWhere(
mineflayer,
entity => entity.name === entityType,
range,