45 lines
2.3 KiB
TypeScript
45 lines
2.3 KiB
TypeScript
import type {Bot} from '@wp2508/mineflayer';
|
|
import type {BaseFunction, FunctionArgument} from '../../../types/bot/function/function.ts';
|
|
|
|
export class init implements BaseFunction<number[]> {
|
|
readonly name = 'drop_slot_item_number';
|
|
readonly i18n = {
|
|
lang: 'zh-CN', name: '丢弃指定数量物品', description: '逐个丢弃指定槽位的物品,数量不超过槽位现有数量',
|
|
};
|
|
readonly args: FunctionArgument[] = [
|
|
{name: 'slot', type: 'number', i18n: {lang: 'zh-CN', name: '物品槽位', description: '当前窗口中的槽位编号'}},
|
|
{name: 'count', type: 'number', i18n: {lang: 'zh-CN', name: '丢弃数量', description: '大于零的整数,超过现有数量时丢弃全部'}},
|
|
];
|
|
|
|
async run(bot: Bot, args: number[]): Promise<boolean> {
|
|
if (!Array.isArray(args) || args.length !== 2 || !args.every(Number.isSafeInteger)) return false;
|
|
const [slot, count] = args;
|
|
const window = bot.currentWindow ?? bot.inventory;
|
|
if (!window || slot < 0 || slot >= window.slots.length || count <= 0 || window.selectedItem) return false;
|
|
const item = window.slots[slot];
|
|
if (!item) return false;
|
|
const drop_count = Math.min(count, item.count);
|
|
const initial_count = item.count;
|
|
const type = item.type;
|
|
const metadata = item.metadata;
|
|
try {
|
|
for (let index = 0; index < drop_count; index++) {
|
|
const current = window.slots[slot];
|
|
if ((bot.currentWindow ?? bot.inventory) !== window || window.selectedItem
|
|
|| !current || current.type !== type || current.metadata !== metadata
|
|
|| current.count !== initial_count - index) return false;
|
|
await bot.clickWindow(slot, 0, 4);
|
|
if ((bot.currentWindow ?? bot.inventory) !== window || window.selectedItem) return false;
|
|
const remaining = window.slots[slot];
|
|
const expected = initial_count - index - 1;
|
|
if (expected === 0 ? remaining !== null && remaining !== undefined
|
|
: !remaining || remaining.type !== type || remaining.metadata !== metadata
|
|
|| remaining.count !== expected) return false;
|
|
}
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
}
|