forked from wmantly/mc-bot-town
Mostly works
This commit is contained in:
@@ -153,8 +153,17 @@ module.exports = {
|
||||
const storage = this.plunginsLoaded['Storage'];
|
||||
if (!storage) return;
|
||||
|
||||
storage._busy = true;
|
||||
// Interrupt any active task and acquire operation lock
|
||||
await this.interruptTask(from);
|
||||
try {
|
||||
await storage._acquireOperationLock(5000);
|
||||
} catch (e) {
|
||||
this.whisper(from, 'Storage is busy, try again in a moment.');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
storage._busy = true;
|
||||
const pending = storage.pendingWithdrawals.get(from);
|
||||
|
||||
await this.say('/trade accept');
|
||||
@@ -220,32 +229,43 @@ module.exports = {
|
||||
}
|
||||
clearTimeout(timeoutCheck);
|
||||
|
||||
if (pending) {
|
||||
// Withdrawal complete — clear pending
|
||||
if (pending.timeoutId) clearTimeout(pending.timeoutId);
|
||||
storage.pendingWithdrawals.delete(from);
|
||||
this.whisper(from, `Withdrawal complete! Enjoy your ${pending.itemName}.`);
|
||||
} else {
|
||||
// Deposit — collect items from bot inventory and sort into storage
|
||||
await sleep(500);
|
||||
let tradeResult = null;
|
||||
|
||||
const hotbarNames = new Set((storage.config.hotbarItems || []).map(h => h.name));
|
||||
const itemsReceived = [];
|
||||
for (const item of this.bot.inventory.items()) {
|
||||
if (hotbarNames.has(item.name)) continue;
|
||||
itemsReceived.push({ name: item.name, id: item.type, count: item.count, nbt: item.nbt });
|
||||
}
|
||||
if (pending) {
|
||||
// Withdrawal complete — clear pending
|
||||
if (pending.timeoutId) clearTimeout(pending.timeoutId);
|
||||
storage.pendingWithdrawals.delete(from);
|
||||
this.whisper(from, `Withdrawal complete! Enjoy your ${pending.itemName}.`);
|
||||
} else {
|
||||
// Deposit — collect items from bot inventory and sort into storage
|
||||
await sleep(500);
|
||||
|
||||
if (itemsReceived.length > 0) {
|
||||
this.whisper(from, `Received ${itemsReceived.length} item type(s). Sorting into storage.`);
|
||||
await storage.handleTrade(from, itemsReceived);
|
||||
} else {
|
||||
this.whisper(from, 'No items received.');
|
||||
}
|
||||
const hotbarNames = new Set((storage.config.hotbarItems || []).map(h => h.name));
|
||||
const itemsReceived = [];
|
||||
for (const item of this.bot.inventory.items()) {
|
||||
if (hotbarNames.has(item.name)) continue;
|
||||
itemsReceived.push({ name: item.name, id: item.type, count: item.count, nbt: item.nbt });
|
||||
}
|
||||
} finally {
|
||||
storage._busy = false;
|
||||
if (itemsReceived.length > 0) {
|
||||
this.whisper(from, `Received ${itemsReceived.length} item type(s). Sorting into storage.`);
|
||||
tradeResult = await storage.handleTrade(from, itemsReceived);
|
||||
} else {
|
||||
this.whisper(from, 'No items received.');
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
storage._busy = false;
|
||||
storage._releaseOperationLock();
|
||||
}
|
||||
|
||||
// Organize after lock is released (handleTrade runs under the lock)
|
||||
if (tradeResult && tradeResult.needsOrganize) {
|
||||
try {
|
||||
await storage.organizeLooseItems();
|
||||
} catch (error) {
|
||||
console.error('Storage: Post-trade organize failed:', error.message);
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user