From b4a8767b9a8a57a59078f4f415c2e5c29bfcc58d Mon Sep 17 00:00:00 2001 From: Timotej Zatko Date: Fri, 17 Jul 2026 12:08:16 +0200 Subject: [PATCH] fix: prevent duplicate inventory dialogs Patch ItemUse.doItOrigin when a toolkit wrapper is present instead of replacing ItemUse.doIt. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 15ca1b2b-92ae-4ac7-955b-16245cbe8edf --- src/components/chests/chests.spec.ts | 14 ++++++++++++++ src/components/chests/chests.ts | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/components/chests/chests.spec.ts b/src/components/chests/chests.spec.ts index 3582724..b6c644c 100644 --- a/src/components/chests/chests.spec.ts +++ b/src/components/chests/chests.spec.ts @@ -60,6 +60,20 @@ describe('Chests', () => { expect(errorTrackerMock.Object.track).not.toHaveBeenCalled(); }); + it('patches doItOrigin without replacing an existing toolkit wrapper', () => { + setup('function toolkitWrapper() {}'); + const wrapper = itemUse.doIt; + itemUse.doItOrigin = doItWithSource( + "function (itemId, itemCount) { EventHandler.signal('item_used', [itemId]); }", + ); + + chests.init(); + + expect(itemUse.doIt).toBe(wrapper); + expect(itemUse.doItOrigin.toString()).toContain('TW_Calc.trackChest(itemId,res);'); + expect(errorTrackerMock.Object.track).not.toHaveBeenCalled(); + }); + it('tracks an error and leaves the handler untouched when the anchor is missing', () => { setup("function (itemId, itemCount) { EventHandler.signal('something_else', [itemId]); }"); const original = itemUse.doIt; diff --git a/src/components/chests/chests.ts b/src/components/chests/chests.ts index 366561e..7f2ea2e 100644 --- a/src/components/chests/chests.ts +++ b/src/components/chests/chests.ts @@ -35,7 +35,7 @@ export class Chests implements Component { const pos = match.index; const body = str.substr(0, pos) + 'TW_Calc.trackChest(itemId,res);' + str.substr(pos); this.logger.log('patching the chest handler...', body); - newStr = 'ItemUse.doIt = ' + body; + newStr = `ItemUse.${toolkit} = ${body}`; eval(newStr); } catch (e: unknown) { const error = e as Error;