diff --git a/src/battle-scene.ts b/src/battle-scene.ts index e008863969e..807c3740953 100644 --- a/src/battle-scene.ts +++ b/src/battle-scene.ts @@ -2850,18 +2850,22 @@ export default class BattleScene extends SceneBase { } return false; } - + /** + * Attempt to discard one or more copies of a held item. + * @param itemModifier - The {@linkcode PokemonHeldItemModifier} being discarded + * @param discardQuantity - The number of copies to remove (up to the amount currently held); default `1` + * @returns Whether the item was successfully discarded. + * Removing fewer items than requested is still considered a success. + */ tryDiscardHeldItemModifier(itemModifier: PokemonHeldItemModifier, discardQuantity = 1): boolean { const countTaken = Math.min(discardQuantity, itemModifier.stackCount); itemModifier.stackCount -= countTaken; - const removeOld = itemModifier.stackCount === 0; - - if (removeOld) { - return this.removeModifier(itemModifier); + if (itemModifier.stackCount > 0) { + return true; } - return true; + return this.removeModifier(itemModifier); } canTransferHeldItemModifier(itemModifier: PokemonHeldItemModifier, target: Pokemon, transferQuantity = 1): boolean { diff --git a/src/ui/party-ui-handler.ts b/src/ui/party-ui-handler.ts index b344ae404fa..42efa06f013 100644 --- a/src/ui/party-ui-handler.ts +++ b/src/ui/party-ui-handler.ts @@ -102,7 +102,7 @@ export enum PartyUiMode { */ SELECT, /** - * Indicates that the party UI is open to select a party member from which to discard an item from + * Indicates that the party UI is open to select a party member from which items will be discarded. * This type of selection can be cancelled. */ DISCARD, @@ -159,7 +159,7 @@ export default class PartyUiHandler extends MessageUiHandler { private partySlotsContainer: Phaser.GameObjects.Container; private partySlots: PartySlot[]; private partyCancelButton: PartyCancelButton; - private PartyDiscardModeButton: PartyDiscardModeButton; + private partyDiscardModeButton: PartyDiscardModeButton; private partyMessageBox: Phaser.GameObjects.NineSlice; private moveInfoOverlay: MoveInfoOverlay; @@ -317,7 +317,7 @@ export default class PartyUiHandler extends MessageUiHandler { partyContainer.add(partyDiscardModeButton); - this.PartyDiscardModeButton = partyDiscardModeButton; + this.partyDiscardModeButton = partyDiscardModeButton; // prepare move overlay. in case it appears to be too big, set the overlayScale to .5 const overlayScale = 1; @@ -367,7 +367,7 @@ export default class PartyUiHandler extends MessageUiHandler { } this.populatePartySlots(); - this.PartyDiscardModeButton.toggleIcon(this.partyUiMode); + this.partyDiscardModeButton.toggleIcon(this.partyUiMode); this.setCursor(0); return true; @@ -1013,7 +1013,7 @@ export default class PartyUiHandler extends MessageUiHandler { ui.playError(); break; } - this.PartyDiscardModeButton.toggleIcon(this.partyUiMode); + this.partyDiscardModeButton.toggleIcon(this.partyUiMode); ui.playSelect(); } // Pressing return button @@ -1036,7 +1036,7 @@ export default class PartyUiHandler extends MessageUiHandler { this.clearTransfer(); ui.playSelect(); } else if (this.allowCancel()) { - this.PartyDiscardModeButton.clear(); + this.partyDiscardModeButton.clear(); if (this.selectCallback) { const selectCallback = this.selectCallback; this.selectCallback = null; @@ -1058,26 +1058,20 @@ export default class PartyUiHandler extends MessageUiHandler { let success = false; switch (button) { case Button.UP: - if (this.cursor === 1) { - if (this.isItemManageMode()) { + if (this.isItemManageMode()) { + if (this.cursor === 1) { success = this.setCursor(globalScene.currentBattle.double ? 0 : 7); break; } - } - if (this.cursor === 2) { - if (this.isItemManageMode()) { + if (this.cursor === 2) { success = this.setCursor(globalScene.currentBattle.double ? 7 : 1); break; } - } - if (this.cursor === 6) { - if (this.isItemManageMode()) { + if (this.cursor === 6) { success = this.setCursor(slotCount <= globalScene.currentBattle.getBattlerCount() ? 7 : slotCount - 1); break; } - } - if (this.cursor === 7) { - if (this.isItemManageMode()) { + if (this.cursor === 7) { success = this.setCursor(globalScene.currentBattle.double && slotCount > 1 ? 1 : 0); break; } @@ -1085,20 +1079,16 @@ export default class PartyUiHandler extends MessageUiHandler { success = this.setCursor(this.cursor ? (this.cursor < 6 ? this.cursor - 1 : slotCount - 1) : 6); break; case Button.DOWN: - if (this.cursor === 0) { - if (this.isItemManageMode()) { + if (this.isItemManageMode()) { + if (this.cursor === 0) { success = this.setCursor(globalScene.currentBattle.double && slotCount > 1 ? 1 : 7); break; } - } - if (this.cursor === 1) { - if (this.isItemManageMode()) { + if (this.cursor === 1) { success = this.setCursor(globalScene.currentBattle.double ? 7 : slotCount > 2 ? 2 : 6); break; } - } - if (this.cursor === 7) { - if (this.isItemManageMode()) { + if (this.cursor === 7) { success = this.setCursor( slotCount > globalScene.currentBattle.getBattlerCount() ? globalScene.currentBattle.getBattlerCount() : 6, ); @@ -1170,14 +1160,14 @@ export default class PartyUiHandler extends MessageUiHandler { } else if (this.lastCursor === 6) { this.partyCancelButton.deselect(); } else if (this.lastCursor === 7) { - this.PartyDiscardModeButton.deselect(); + this.partyDiscardModeButton.deselect(); } if (cursor < 6) { this.partySlots[cursor].select(); } else if (cursor === 6) { this.partyCancelButton.select(); } else if (cursor === 7) { - this.PartyDiscardModeButton.select(); + this.partyDiscardModeButton.select(); } } return changed; diff --git a/test/ui/item-manage-button.test.ts b/test/ui/item-manage-button.test.ts index 6d598729fb0..38f51c135e5 100644 --- a/test/ui/item-manage-button.test.ts +++ b/test/ui/item-manage-button.test.ts @@ -33,13 +33,13 @@ describe("UI - Transfer Items", () => { { name: "BERRY", count: 2, type: BerryType.APICOT }, { name: "BERRY", count: 2, type: BerryType.LUM }, ]); - game.override.moveset([MoveId.DRAGON_CLAW]); + game.override.moveset(MoveId.DRAGON_CLAW); game.override.enemySpecies(SpeciesId.MAGIKARP); - game.override.enemyMoveset([MoveId.SPLASH]); + game.override.enemyMoveset(MoveId.SPLASH); await game.classicMode.startBattle([SpeciesId.RAYQUAZA, SpeciesId.RAYQUAZA, SpeciesId.RAYQUAZA]); - game.move.select(MoveId.DRAGON_CLAW); + game.move.use(MoveId.DRAGON_CLAW); }); it("manage button exists in the proper screen", async () => { @@ -47,7 +47,7 @@ describe("UI - Transfer Items", () => { expect(game.scene.ui.getHandler()).toBeInstanceOf(ModifierSelectUiHandler); const handler = game.scene.ui.getHandler() as ModifierSelectUiHandler; - handler.setCursor(1); //manage items menu, manage button should exist + handler.setCursor(1); // Select/click manage items button handler.processInput(Button.ACTION); void game.scene.ui.setModeWithoutClear(UiMode.PARTY, PartyUiMode.MODIFIER_TRANSFER); @@ -55,19 +55,19 @@ describe("UI - Transfer Items", () => { await game.phaseInterceptor.to("BattleEndPhase"); - game.phaseInterceptor.addToNextPrompt("SelectModifierPhase", UiMode.PARTY, () => { + game.onNextPrompt("SelectModifierPhase", UiMode.PARTY, () => { expect(game.scene.ui.getHandler()).toBeInstanceOf(PartyUiHandler); const handler = game.scene.ui.getHandler() as PartyUiHandler; handler.processInput(Button.DOWN); handler.processInput(Button.ACTION); - expect(handler.optionsContainer.list.length).toBe(0); //should select manage button,which has no menu + expect(handler.optionsContainer.list).toHaveLength(0); // should select manage button, which has no menu game.phaseInterceptor.unlock(); }); await game.phaseInterceptor.to("SelectModifierPhase"); - }, 20000); + }); it("manage button doesn't exist in the other screens", async () => { game.onNextPrompt("SelectModifierPhase", UiMode.MODIFIER_SELECT, () => { @@ -88,11 +88,11 @@ describe("UI - Transfer Items", () => { handler.processInput(Button.DOWN); handler.processInput(Button.ACTION); - expect(handler.optionsContainer.list.length).toBeGreaterThan(0); //should select a pokemon, which has at least the cancel option + expect(handler.optionsContainer.list.length).toBeGreaterThan(0); // should select a pokemon, which has at least the cancel option game.phaseInterceptor.unlock(); }); await game.phaseInterceptor.to("SelectModifierPhase"); - }, 20000); + }); });